Safe Haskell | None |
---|---|
Language | Haskell2010 |
AUTHOR
- Dr. Alistair Ward
DESCRIPTION
- This data-type maintains the state of the board, but it doesn't know its history.
In consequence it knows neither whether Castling has already been performed nor which
Pawn
s have been promoted, nor whose turn it is. - It allows unvalidated access to the board, to place, move, or remove pieces.
In consequence;
it enforces neither a conventional layout for the pieces nor even that there is exactly one
King
per side; it permits one to move into check or to take aKing
. - For efficiency, two models of the board are maintained; square-centric (State.MaybePieceByCoordinates) & piece-centric (State.CoordinatesByRankByLogicalColour).
Synopsis
- type NBoards = Int
- data Board x y
- countDefendersByCoordinatesByLogicalColour :: (Enum x, Enum y, Ord x, Ord y) => Board x y -> NDefendersByCoordinatesByLogicalColour x y
- summariseNDefendersByLogicalColour :: Board x y -> ByLogicalColour NPieces
- findProximateKnights :: (Enum x, Enum y, Ord x, Ord y) => LogicalColour -> Coordinates x y -> Board x y -> [Coordinates x y]
- sumPieceSquareValueByLogicalColour :: (Enum x, Enum y, Num pieceSquareValue, Ord x, Ord y) => PieceSquareArray x y pieceSquareValue -> Board x y -> ByLogicalColour pieceSquareValue
- findAttackersOf :: (Enum x, Enum y, Ord x, Ord y) => LogicalColour -> Coordinates x y -> Board x y -> [(Coordinates x y, Rank)]
- findAttacksBy :: (Enum x, Enum y, Ord x, Ord y) => Piece -> Coordinates x y -> Board x y -> [Coordinates x y]
- movePiece :: (Enum x, Enum y, Ord x, Ord y, Show x, Show y) => Move x y -> Maybe MoveType -> Transformation x y
- defineCoordinates :: (Enum x, Enum y, Ord x, Ord y) => Maybe Piece -> Coordinates x y -> Transformation x y
- placePiece :: (Enum x, Enum y, Ord x, Ord y) => Piece -> Coordinates x y -> Transformation x y
- removePiece :: (Enum x, Enum y, Ord x, Ord y) => Coordinates x y -> Transformation x y
- isKingChecked :: (Enum x, Enum y, Ord x, Ord y) => LogicalColour -> Board x y -> Bool
- exposesKing :: (Enum x, Enum y, Ord x, Ord y) => LogicalColour -> Move x y -> Board x y -> Bool
Types
Type-synonyms
Data-types
- The board is modelled as two alternative structures representing the same data, but indexed by either coordinates or piece.
- For efficiency some ancillary structures are also maintained.
Instances
Functions
countDefendersByCoordinatesByLogicalColour :: (Enum x, Enum y, Ord x, Ord y) => Board x y -> NDefendersByCoordinatesByLogicalColour x y Source #
Count the number of defenders of each piece on the board.
summariseNDefendersByLogicalColour :: Board x y -> ByLogicalColour NPieces Source #
Collapses NDefendersByCoordinatesByLogicalColour
into the total number of defenders on either side.
:: (Enum x, Enum y, Ord x, Ord y) | |
=> LogicalColour | The logical colour of the |
-> Coordinates x y | The destination to which the |
-> Board x y | |
-> [Coordinates x y] |
Forward request.
sumPieceSquareValueByLogicalColour :: (Enum x, Enum y, Num pieceSquareValue, Ord x, Ord y) => PieceSquareArray x y pieceSquareValue -> Board x y -> ByLogicalColour pieceSquareValue Source #
Calculate the total value of the coordinates occupied by the pieces of either side, at a stage in the game's life-span defined by the total number of pieces remaining.
:: (Enum x, Enum y, Ord x, Ord y) | |
=> LogicalColour | The defender's logical colour. |
-> Coordinates x y | The defender's location. |
-> Board x y | |
-> [(Coordinates x y, Rank)] | The locations from which the specified square can be attacked by the opposite logical colour. |
- Lists the source-coordinates from which the referenced destination can be attacked.
- N.B.: the algorithm is independent of whose turn it actually is.
- CAVEAT: checks neither the logical colour of the defender, nor that their piece even exists.
- CAVEAT: may return the coordinates of a diagonally adjacent
Pawn
; which would be an illegal move if there's not actually any piece at the referenced destination. - CAVEAT: can't detect an en-passant attack, since this depends both on whether the previous move was a double advance & that the defender is a
Pawn
.
:: (Enum x, Enum y, Ord x, Ord y) | |
=> Piece | The type of attacker. |
-> Coordinates x y | The defender's location. |
-> Board x y | |
-> [Coordinates x y] | The sources from which the specified attacker could strike. |
- Lists the source-coordinates from which the referenced destination can be attacked by the specified type of piece.
- N.B.: similar to
findAttackersOf
, but can be more efficient since the attacking piece is known. - CAVEAT: can't detect an en-passant attack, since this depends both on whether the previous move was a double advance & that the defender is a
Pawn
.
Constructors
Mutators
:: (Enum x, Enum y, Ord x, Ord y, Show x, Show y) | |
=> Move x y | N.B.: illegal moves are acceptable. |
-> Maybe MoveType | N.B.: this may not be available to the caller, for example during the illegal moves required for rollback. |
-> Transformation x y |
- Moves the referenced piece.
- CAVEAT: no validation is performed.
- CAVEAT: castling must be implemented by making two calls.
:: (Enum x, Enum y, Ord x, Ord y) | |
=> Maybe Piece | The optional piece to place (or remove if |
-> Coordinates x y | The coordinates to define. |
-> Transformation x y |
- Define the specified coordinates, by either placing or removing a piece.
- CAVEAT: this function should only be used to construct custom scenarios, since pieces don't normally spring into existence.
- CAVEAT: doesn't validate the request, so
King
s can be placed in check &Pawn
s can be placed behind their starting rank or unpromoted on their last rank. - CAVEAT: simple but inefficient implementation, since this function isn't called during normal play.
placePiece :: (Enum x, Enum y, Ord x, Ord y) => Piece -> Coordinates x y -> Transformation x y Source #
Place a piece at the specified unoccupied coordinates.
removePiece :: (Enum x, Enum y, Ord x, Ord y) => Coordinates x y -> Transformation x y Source #
Remove a piece from the board.
Predicates
:: (Enum x, Enum y, Ord x, Ord y) | |
=> LogicalColour | The logical colour of the |
-> Board x y | |
-> Bool |
- Whether the
King
of the specified logical colour is currently checked. - N.B.: independent of whose turn it actually is.
- CAVEAT: assumes there's exactly one
King
of the specified logical colour.
:: (Enum x, Enum y, Ord x, Ord y) | |
=> LogicalColour | The logical colour of the player proposing to move. |
-> Move x y | The move. |
-> Board x y | The original board, i.e. prior to the move. |
-> Bool |
- Whether one's own
King
has become exposed in the proposed board. - CAVEAT: assumes that one's
King
wasn't already checked. - CAVEAT: this function is a performance-hotspot.