Safe Haskell | None |
---|---|
Language | Haskell2010 |
AUTHOR
- Dr. Alistair Ward
DESCRIPTION
- Models the board as a sparse array, each element of which might contain a piece.
- N.B.: while this could be represented as
Data.Map.Map Coordinates Piece
, replacing!
withlookup
, it actually required more space (despite having at most half the elements) & runs slower (because ofcompare
). - cf. the piece-centric model of the board defined in BishBosh.State.CoordinatesByRankByLogicalColour.
Synopsis
- data MaybePieceByCoordinates
- inferMoveType :: MaybePieceByCoordinates -> Move -> Maybe Rank -> MoveType
- findBlockingPiece :: MaybePieceByCoordinates -> Coordinates -> Direction -> Maybe LocatedPiece
- findBlockingPieces :: MaybePieceByCoordinates -> Coordinates -> Maybe [Direction] -> [LocatedPiece]
- findAttackerInDirection :: MaybePieceByCoordinates -> LogicalColour -> Coordinates -> Direction -> Maybe (Coordinates, Rank)
- findAttackerInDirections :: MaybePieceByCoordinates -> LogicalColour -> Coordinates -> Maybe [Direction] -> [(Coordinates, Rank)]
- listDestinationsFor :: MaybePieceByCoordinates -> Coordinates -> Piece -> [(Coordinates, Maybe Rank)]
- show2D :: MaybePieceByCoordinates -> Column -> ColourScheme -> Bool -> (X, Y) -> String
- dereference :: MaybePieceByCoordinates -> Coordinates -> Maybe Piece
- isVacant :: MaybePieceByCoordinates -> Coordinates -> Bool
- isOccupied :: MaybePieceByCoordinates -> Coordinates -> Bool
- isClear :: MaybePieceByCoordinates -> Coordinates -> Coordinates -> Bool
- isObstructed :: MaybePieceByCoordinates -> Coordinates -> Coordinates -> Bool
- isEnPassantMove :: MaybePieceByCoordinates -> Move -> Bool
Types
Data-types
data MaybePieceByCoordinates Source #
Instances
Functions
:: MaybePieceByCoordinates | |
-> Move | |
-> Maybe Rank | The rank to which a |
-> MoveType |
Infer the type of the specified move.
:: MaybePieceByCoordinates | |
-> Coordinates | The starting point. |
-> Direction | The direction in which to search. |
-> Maybe LocatedPiece | Any blocking piece. |
- Find the first piece of either logical colour, encountered in the specified direction, from just after the specified coordinates.
- CAVEAT: this is a performance-hotspot.
:: MaybePieceByCoordinates | |
-> Coordinates | The starting point. |
-> Maybe [Direction] | The directions in which to search; |
-> [LocatedPiece] | Blocking pieces in non-specific directions. |
- Find the first piece of either logical colour, encountered in each of the specified directions, from just after the specified coordinates.
- N.B.: one could call
findBlockingPiece
for each direction, but this function exploits optimisations available when all directions are required.
findAttackerInDirection Source #
:: MaybePieceByCoordinates | |
-> LogicalColour | The defender's logical colour. |
-> Coordinates | The defender's square. |
-> Direction | The direction from the coordinates of concern; the opposite direction from which an attacker might strike. |
-> Maybe (Coordinates, Rank) | Any opposing piece which can attack the specified square from the specified direction. |
- Find the coordinates of any attacker who can strike the specified coordinates, from the specified direction (as seen by the target).
- N.B.: there's no requirement for there to actually be a piece to attack at the specified target.
findAttackerInDirections Source #
:: MaybePieceByCoordinates | |
-> LogicalColour | The defender's logical colour. |
-> Coordinates | The defender's square. |
-> Maybe [Direction] | The directions from the coordinates of concern; the opposite direction from which an attacker might strike; |
-> [(Coordinates, Rank)] | Any opposing pieces which can attack the specified square from the specified directions. |
- Find the coordinates of any attacker who can strike the specified coordinates, from the specified directions (as seen by the target).
- N.B.: one could call
findAttackerInDirection
for each direction, but this function exploits optimisations available when all directions are required.
:: MaybePieceByCoordinates | |
-> Coordinates | The source for which destinations are required. |
-> Piece | The piece at the specified source. |
-> [(Coordinates, Maybe Rank)] | The destination & the rank of any piece taken. |
- Lists the destination-coordinates to which the referenced piece can move, & the rank of any piece taken.
- N.B.: one can reference either player's piece, regardless of whose turn it is to move.
- CAVEAT: doesn't include either Castling or En-passant, because this function doesn't know the history of the game.
- CAVEAT: doesn't check whether any proposed move exposes one's
King
, because this function doesn't assume the existence of aKing
. - CAVEAT: the opponent's
King
may be one of the destinations returned, but only if it was actually their move next. - CAVEAT: doesn't typically check whether anything (let alone the specified piece) exists at the specified source-coordinates.
:: MaybePieceByCoordinates | |
-> Column | The column-magnification. |
-> ColourScheme | |
-> Bool | Whether to depict figurines. |
-> (X, Y) | The origin from which axes are labelled. |
-> String | The output suitable for display on a terminal. |
Show the board using a two-dimensional representation.
Accessors
dereference :: MaybePieceByCoordinates -> Coordinates -> Maybe Piece Source #
Dereference the array.
Predicates
isVacant :: MaybePieceByCoordinates -> Coordinates -> Bool Source #
Whether the specified coordinates are unoccupied.
isOccupied :: MaybePieceByCoordinates -> Coordinates -> Bool Source #
Whether the specified coordinates are occupied.
:: MaybePieceByCoordinates | |
-> Coordinates | Source. |
-> Coordinates | Destination. |
-> Bool |
- Whether the open interval (source, destination) is unobstructed.
- CAVEAT: the move must be straight, so that all intermediate points lie on squares of the board.
- N.B.: the specified end-points are uninspected.
:: MaybePieceByCoordinates | |
-> Coordinates | Source. |
-> Coordinates | Destination. |
-> Bool |
Whether there's a blockage between a piece presumed to exist at the specified source, & a piece presumed to exist @ the specified destination.
isEnPassantMove :: MaybePieceByCoordinates -> Move -> Bool Source #
- Whether the specified move matches the rules for en-passant.
- CAVEAT: assumes that the move is valid;
otherwise one would also need to confirm that the opponent's
Pawn
had just double-advanced into the appropriate position.