| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
BishBosh.State.Board
Description
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 occurred, nor which
Pawns 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
Kingper side; it permits one to move into check or to take aKing. - Two models of the board are simultaneously maintained; a square-centric model State.MaybePieceByCoordinates & a piece-centric model State.CoordinatesByRankByLogicalColour. Though maintenance of two models is a burden, the duality permits alternative implementations of the required searches, & often one is more efficient than the other.
Synopsis
- data Board
- countDefendersByCoordinatesByLogicalColour :: Board -> NDefendersByCoordinatesByLogicalColour
- summariseNDefendersByLogicalColour :: Board -> ArrayByLogicalColour NPieces
- sumPieceSquareValueByLogicalColour :: PieceSquareValueByCoordinatesByRank -> Board -> [Base]
- findAttackersOf :: Board -> LogicalColour -> Coordinates -> [(Coordinates, Rank)]
- findAttacksBy :: Board -> Piece -> Coordinates -> [Coordinates]
- movePiece :: Move -> Maybe MoveType -> Transformation
- passesThroughCheck :: Board -> LogicalColour -> Move -> Bool
- isKingChecked :: Board -> LogicalColour -> Bool
- exposesKing :: Board -> LogicalColour -> Move -> 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 :: Board -> NDefendersByCoordinatesByLogicalColour Source #
Count the number of defenders of each piece on the board.
summariseNDefendersByLogicalColour :: Board -> ArrayByLogicalColour NPieces Source #
Collapses NDefendersByCoordinatesByLogicalColour into the total number of defenders on either side.
sumPieceSquareValueByLogicalColour Source #
Arguments
| :: PieceSquareValueByCoordinatesByRank | |
| -> Board | |
| -> [Base] | Individual sums for each logical colours, of the PieceSquareValues. |
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.
Arguments
| :: Board | |
| -> LogicalColour | The defender's logical colour. |
| -> Coordinates | The defender's location. |
| -> [(Coordinates, 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.
Arguments
| :: Board | |
| -> Piece | The type of attacker. |
| -> Coordinates | The defender's location. |
| -> [Coordinates] | 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
Arguments
| :: Move | 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 |
- Moves the referenced piece.
- CAVEAT: no validation is performed.
- CAVEAT: castling must be implemented by making two calls.
Predicates
Arguments
| :: Board | |
| -> LogicalColour | The mover's logical colour. |
| -> Move | |
| -> Bool |
Confirm whether the specified move passes through check.
Arguments
| :: Board | |
| -> LogicalColour | The logical colour of the |
| -> Bool |
- Whether the
Kingof the specified logical colour is currently checked. - N.B.: independent of whose turn it actually is.
- CAVEAT: assumes there's exactly one
Kingof the specified logical colour.
Arguments
| :: Board | The original board, i.e. prior to the move. |
| -> LogicalColour | The logical colour of the player proposing to move. |
| -> Move | The move. |
| -> Bool |
- Whether one's own
Kinghas become exposed in the proposed board. - CAVEAT: assumes that one's
Kingwasn't already checked. - CAVEAT: this function is a performance-hotspot.