Protected Member Functions | List of all members
cwchess::ChessPosition Class Reference

A chess position. More...

#include <ChessPosition.h>

Inheritance diagram for cwchess::ChessPosition:
Inheritance graph
[legend]

Public Member Functions

Constructor
 ChessPosition (void)
 Construct an uninitialized position.
 
Position setup
void clear (void)
 Clear the board. More...
 
void initial_position (void)
 Set up the initial position.
 
bool skip_move (void)
 Skip a move. More...
 
void to_move (Color const& color)
 Explicitly set whose turn it is. More...
 
void set_half_move_clock (int count)
 Explicitly set the number of half moves since the last pawn move or capture. More...
 
void set_full_move_number (int move)
 Explicitly set the move number. More...
 
bool set_en_passant (Index const& index)
 Explicity set the en passant information. More...
 
void swap_colors (void)
 Swap colors. More...
 
bool place (Code const& code, Index const& index)
 Place a piece on the board. More...
 
void set_has_moved (Index const& index)
 Specifically specify that a king or rook has already moved. More...
 
void clear_has_moved (Index const& index)
 Specifically specify that a king or rook didn't move yet. More...
 
bool load_FEN (std::string const& FEN)
 Read a FEN code. More...
 
Accessors
Piece piece_at (Index const& index) const
 Return the Piece on the square index.
 
Piece piece_at (int col, int row) const
 Return the Piece on the square col, row.
 
Color to_move (void) const
 Return whose turn it is.
 
unsigned int half_move_clock (void) const
 Return the half move clock.
 
unsigned int full_move_number (void) const
 Return the full move number.
 
CastleFlags const&  castle_flags (void) const
 Return the castle flag object.
 
EnPassant const&  en_passant (void) const
 Return the en passant object.
 
BitBoard const&  all (Code const& code) const
 Return a BitBoard with bits set for all code, where code may not be 'nothing'.
 
BitBoard const&  all (Color const& color) const
 Return a BitBoard with bits set for all pieces of color color.
 
Visitors
std::string FEN (void) const
 Return the FEN code for this position.
 
int candidates_table_offset (Type const& type) const
 Return the offset into the candidates_table for type type. More...
 
BitBoard candidates (Index const& index) const
 Return a BitBoard with bits set for all squares that are candidates to move to. More...
 
BitBoard reachables (Index const& index, bool attacked_squares=false) const
 Return a BitBoard with bits set for each square that a piece can reach in one move. More...
 
BitBoard defendables (Code const& code, Index const& index, bool& battery) const
 Return a BitBoard with bits set for each square that a piece defends, or would defend if an exchange was going on there. More...
 
Index index_of_king (Color const& color) const
 Return the index of the king with color color.
 
bool check (void) const
 Return true if the king is in check.
 
bool check (Color const& color) const
 Return true if the king of color color is in check.
 
bool double_check (Color const& color) const
 Return true if the king of color color is in double check.
 
bool has_moved (Index const& index)
 Return true if the king or rook on index has moved or not. More...
 
BitBoard moves (Index const& index) const
 Return a BitBoard with bits set for each square the piece at index can legally go to. More...
 
bool legal (Move const& move) const
 Return true if the move is a legal move.
 
Iterators
PieceIterator piece_begin (Color const& color) const
 Return an iterator to the first piece of color color. More...
 
PieceIterator piece_end (void) const
 Return an iterator one beyond the last piece.
 
PieceIterator piece_begin (Code const& code) const
 Return an iterator to the first piece with code code. More...
 
MoveIterator move_begin (Index const& index) const
 Return an iterator to the first move of the piece at index index.
 
MoveIterator move_end (void) const
 Return an iterator one beyond the last move.
 
Game play
bool execute (Move const& move)
 Execute move move.
 

Protected Member Functions

void reset_en_passant (void)
 

Detailed Description

A chess position.

This class represents a chess position. It has all information regarding a single position, including the full move count (because that is needed for the FEN code), the half move clock (to determine whether a derived position becomes a draw due to the 50 moves rule), en passant and castling information.

Definition at line 50 of file ChessPosition.h.

Member Function Documentation

BitBoard cwchess::ChessPosition::candidates ( Index const&   index) const
inline

Return a BitBoard with bits set for all squares that are candidates to move to.

In the case of the pawn, the returned BitBoard is exact. In the case of the king, the castling squares are not included. In all other cases the returned squares assume an empty board.

This function may not be called for an empty square.

Definition at line 299 of file ChessPosition.h.

References cwchess::black_pawn, candidates_table_offset(), defendables(), cwchess::Piece::flags(), cwchess::BitBoardData::M_bitmask, reachables(), cwchess::Piece::type(), and cwchess::white_pawn.

int cwchess::ChessPosition::candidates_table_offset ( Type const&   type) const
inline

Return the offset into the candidates_table for type type.

The type may not be a pawn (there is no candidates_table entry for a pawn).

Definition at line 283 of file ChessPosition.h.

Referenced by candidates().

void cwchess::ChessPosition::clear ( void  )

Clear the board.

This does not change whose move it is. The half move clock and full move number are reset (to zero and one respectively).

Definition at line 59 of file ChessPosition.cc.

References cwchess::black, cwchess::EnPassant::index(), cwchess::index_begin, cwchess::index_end, cwchess::nothing, and cwchess::white.

Referenced by ChessPosition(), and swap_colors().

void cwchess::ChessPosition::clear_has_moved ( Index const&   index)
inline

Specifically specify that a king or rook didn't move yet.

This function only has effect if the king or rook is on its initial square.

Parameters
index: The (initial position of the) rook or king.

Definition at line 203 of file ChessPosition.h.

References FEN(), load_FEN(), and piece_at().

BitBoard cwchess::ChessPosition::defendables ( Code const&   code,
Index const&   index,
bool&   battery 
) const

Return a BitBoard with bits set for each square that a piece defends, or would defend if an exchange was going on there.

This function returns the squares that the enemy king is not allowed to go to as a result of this piece. However, the attacks are considered to go through slider pieces of the same type. For example, if you ask about the defendables of a white rook on e1, and there is already a white rook (or queen) on e2, then the square e3 (and beyond) is returned nevertheless.

It is not necessary for the piece to actually stand on the board yet.

Parameters
code: The piece.
index: The place that the piece is considered to be standing on.
battery: Internal output variable (ignore it).

Definition at line 1228 of file ChessPosition.cc.

References cwchess::bishop, cwchess::bishop_bits, cwchess::black, cwchess::black_bishop, cwchess::black_pawn, cwchess::black_queen, cwchess::black_rook, cwchess::Index::col(), cwchess::Code::color(), cwchess::file_a, cwchess::file_h, cwchess::Direction::from(), cwchess::EnPassant::index(), cwchess::index2mask(), cwchess::index_begin, cwchess::index_end, cwchess::index_pre_begin, cwchess::Code::is_a_bishopmover(), cwchess::Code::is_a_rookmover(), cwchess::Code::is_a_slider(), cwchess::king, cwchess::king_bits, cwchess::knight, cwchess::knight_bits, cwchess::BitBoardData::M_bitmask, cwchess::mask2index(), cwchess::Index::next_bit_in(), cwchess::nothing_bits, cwchess::Color::opposite(), cwchess::pawn_bits, cwchess::Index::prev_bit_in(), cwchess::queen, cwchess::queen_bits, cwchess::BitBoard::reset(), cwchess::rook, cwchess::rook_bits, cwchess::BitBoard::test(), cwchess::Code::type(), cwchess::white, cwchess::white_bishop, cwchess::white_pawn, cwchess::white_queen, and cwchess::white_rook.

Referenced by candidates().

bool cwchess::ChessPosition::has_moved ( Index const&   index)
inline

Return true if the king or rook on index has moved or not.

Calling this function for another piece than a king or rook returns false.

Definition at line 371 of file ChessPosition.h.

References cwchess::CastleFlags::has_moved(), legal(), and moves().

bool cwchess::ChessPosition::load_FEN ( std::string const&   FEN)

Read a FEN code.

Parameters
FEN: A Forsyth-Edwards Notation.

If the FEN code is non-parsable, the function return FALSE and the position is in an undefined state. Therefore you might want to use the function as follows:

ChessPosition tmp(current_chess_position);
if (tmp.load_FEN(fen))
  current_chess_position = tmp;
else
{
  // report error
}
Returns
TRUE if loading was successful.

Definition at line 779 of file ChessPosition.cc.

References cwchess::bishop, cwchess::black, cwchess::black_pawn, cwchess::king, cwchess::knight, cwchess::nothing, cwchess::pawn, cwchess::queen, cwchess::rook, cwchess::white, and cwchess::white_pawn.

Referenced by clear_has_moved(), and cwmm::ChessPositionWidget::load_FEN().

BitBoard cwchess::ChessPosition::moves ( Index const&   index) const
PieceIterator cwchess::ChessPosition::piece_begin ( Color const&   color) const
inline

Return an iterator to the first piece of color color.

The iterator will iterate over all pieces with color color.

Definition at line 391 of file ChessPosition.h.

PieceIterator cwchess::ChessPosition::piece_begin ( Code const&   code) const
inline

Return an iterator to the first piece with code code.

The iterator will iterate over all pieces with code code; code may not be empty.

Definition at line 400 of file ChessPosition.h.

bool cwchess::ChessPosition::place ( Code const&   code,
Index const&   index 
)

Place a piece on the board.

Place a piece on the board. If there is already a piece there, it is replaced. It is also possible to place 'nothing', in which case any piece that was there is removed.

If the king or a rook is placed, then the castling flags are updated to allow castling by default. If this is not what you want, then call moved to signal that castling is not allowed after placing both, the king and the rook(s).

If a pawn is marked as En Passant then that is persistent: it will stay that way (placing a piece is not the same as making a move). However, if the pawn that can be taken en passant is moved, replaced (by again a pawn) or when a piece is put behind it, so that it's impossible that it was the last move (no logic will check other reasons, like being in check), then its En Passant status is reset. For example, if there is a white pawn on f4 that can be taken en passant by a black pawn on g4, and the pawn on g4 is removed or replaced then that doesn't change the status of the pawn on f4. If (later) a black pawn in placed on g4, then that pawn will be able to take the white pawn on f4, provided that nothing has been placed, even temporarily, on f3 or f2, and the pawn on f4 was never replaced.

At the moment the only placement that is refused is placing pawns on rank 1 or 8.

Parameters
code: The piece to be placed on the board.
index: Where to place the piece.
Returns
TRUE if the piece was placed, FALSE if it was refused.

Definition at line 316 of file ChessPosition.cc.

References cwchess::bishop, cwchess::black, cwchess::black_pawn, cwchess::Index::col(), cwchess::Code::color(), cwchess::file_a, cwchess::file_h, cwchess::fl_pawn_can_move_two_squares, cwchess::fl_pawn_can_take_king_side, cwchess::fl_pawn_can_take_queen_side, cwchess::fl_pawn_is_not_blocked, cwchess::Direction::from(), cwchess::EnPassant::index(), cwchess::index2mask(), cwchess::index_end, cwchess::Code::is_a(), cwchess::Code::is_nothing(), cwchess::king, cwchess::BitBoardData::M_bitmask, cwchess::TypeData::M_bits, cwchess::mask2index(), cwchess::nothing, cwchess::Color::opposite(), cwchess::pawn, cwchess::EnPassant::pawn_index(), cwchess::queen, cwchess::BitBoard::reset(), cwchess::rook, cwchess::Index::row(), cwchess::BitBoard::test(), cwchess::Color::toggle(), cwchess::Code::type(), cwchess::type_mask, cwchess::white, and cwchess::white_pawn.

Referenced by set_full_move_number(), and swap_colors().

BitBoard cwchess::ChessPosition::reachables ( Index const&   index,
bool  attacked_squares = false 
) const

Return a BitBoard with bits set for each square that a piece can reach in one move.

The parameter attacked_squares only influences pawns and the king. If set, the squares returned for a pawn are always the two attacked squares (independent of other pieces); and the king does not return any castling squares. The definition would be: squares that the opponent may not move to with his king.

Parameters
index: The position of the piece under investigation.
attacked_squares: If true, rather return if squares are attacked than whether the piece can move there.

Definition at line 2130 of file ChessPosition.cc.

References cwchess::b1, cwchess::b8, cwchess::bishop_bits, cwchess::black, cwchess::c1, cwchess::c8, cwchess::Piece::color(), cwchess::d1, cwchess::d8, cwchess::e1, cwchess::e8, cwchess::f1, cwchess::f8, cwchess::file_a, cwchess::file_h, cwchess::g1, cwchess::g8, cwchess::EnPassant::index(), cwchess::index2mask(), cwchess::king, cwchess::king_bits, cwchess::knight, cwchess::knight_bits, cwchess::BitBoardData::M_bitmask, cwchess::nothing_bits, cwchess::pawn_bits, cwchess::queen_bits, cwchess::BitBoard::reset(), cwchess::rook_bits, cwchess::Piece::type(), and cwchess::white.

Referenced by candidates().

bool cwchess::ChessPosition::set_en_passant ( Index const&   index)

Explicity set the en passant information.

This also sets whose turn it is. No error checking is done on whether there is actually a pawn that could have just moved two squares.

Parameters
index: The square that a pawn just passed by advancing two square.
Returns
TRUE if there are indeed one or two pawns that can take en passant.

Definition at line 1073 of file ChessPosition.cc.

References cwchess::black, cwchess::black_bishop, cwchess::black_pawn, cwchess::black_queen, cwchess::Index::col(), cwchess::Direction::from(), cwchess::index2mask(), cwchess::index_end, cwchess::index_pre_begin, cwchess::Color::is_white(), cwchess::king, cwchess::mask2index(), cwchess::Index::next_bit_in(), cwchess::Index::prev_bit_in(), cwchess::BitBoard::reset(), cwchess::Index::row(), cwchess::BitBoard::set(), cwchess::BitBoard::test(), cwchess::white, cwchess::white_bishop, cwchess::white_pawn, and cwchess::white_queen.

Referenced by set_full_move_number(), and swap_colors().

void cwchess::ChessPosition::set_full_move_number ( int  move)
inline

Explicitly set the move number.

This does not change the half move clock, en passant information or whose turn it is.

Definition at line 125 of file ChessPosition.h.

References place(), set_en_passant(), and swap_colors().

void cwchess::ChessPosition::set_half_move_clock ( int  count)
inline

Explicitly set the number of half moves since the last pawn move or capture.

This does not change the full move number, en passant information or whose turn it is.

Definition at line 119 of file ChessPosition.h.

void cwchess::ChessPosition::set_has_moved ( Index const&   index)
inline

Specifically specify that a king or rook has already moved.

Calling this function is only necessary if king and rook are both on the initial squares. Placing the rook or king after calling this function will reset the castling flags (to allow castling); therefore first place the pieces and then, if necessary, call this function.

Parameters
index: The (initial position of the) rook or king.

Definition at line 195 of file ChessPosition.h.

References piece_at().

bool cwchess::ChessPosition::skip_move ( void  )

Skip a move.

This resets the right to capture en passant. Skipping a move is counted as move.

Returns
TRUE if the result is a draw due to the 50 moves rule.

Definition at line 123 of file ChessPosition.cc.

Referenced by ChessPosition().

void cwchess::ChessPosition::swap_colors ( void  )

Swap colors.

This changes the color of each piece, toggles whose turn it is and mirrors the position vertically. The resulting position is technically identical to the original position, but with different colors for each piece.

In order to make the position technically equivalent, the half move clock is unchanged; however, the full move number is reset (to one) because it would impossible to really have the same history: it is not possible that black started the game.

Definition at line 137 of file ChessPosition.cc.

References cwchess::black, cwchess::black_pawn, clear(), cwchess::Piece::code(), cwchess::Piece::color(), cwchess::Code::color(), cwchess::Code::has_opposite_color_of(), cwchess::ia1, cwchess::EnPassant::index(), cwchess::PieceIterator::index(), cwchess::index_end, cwchess::Code::is_a(), cwchess::Code::moves_along(), cwchess::Color::opposite(), cwchess::pawn, place(), set_en_passant(), cwchess::white, and cwchess::white_pawn.

Referenced by set_full_move_number().

void cwchess::ChessPosition::to_move ( Color const&   color)

Explicitly set whose turn it is.

This does not change the half move clock or full move number, nor does it change the en passant information. The reason for that is that this call is probably done as part of a position setup and calls that explicitely set en passant information and the move counters might already have been called. However, calling this an odd number of times after en passant information was set therefore leaves the position is an erroneous state.

If you just want to change who is to move from a given position, then use skip_move.

Parameters
color: The color that has to move next from this position.

Definition at line 131 of file ChessPosition.cc.


The documentation for this class was generated from the following files:

Copyright © 2006 - 2010 Carlo Wood.  All rights reserved.