-
Notifications
You must be signed in to change notification settings - Fork 0
Home
The chess.hpp is a nice library from disservin
https://disservin.github.io/chess-library/
This wiki is complement of the Disservin's documentation with more examples.
The file chess.hpp is to include in you project and contains all the code.
You don't need to make and compile a library
Board b(constants::STARTPOS)generate a Board initalized as regular game
You can debug a board for example with one of the two functions, the first one display chess board from Black player, the other from White player.
These functions I have added are for test purpose.
The library is usefull to manipulate Chess Board objects.
void printBoardBlack( Board &p )
{
cout <<"--------"<<endl;
for ( int i=0; i<64; i++) {
cout << std::string(p.at(Square(i))) ;
if ( (i+1)%8 == 0 ) cout << endl;
}
cout <<"--------"<<endl;
}
void printBoardWhite( Board &p )
{
cout <<"--------"<<endl;
for ( int line=7; line>=0; line--)
{
for ( int col=0; col<=7; col++) {
cout << std::string(p.at(Square(line*8+col))) ;
}
cout <<endl;
}
cout <<"--------"<<endl;
}
The second function will display
--------
rnbqkbnr
pppppppp
........
........
........
........
PPPPPPPP
RNBQKBNR
--------
You can generate a Board from a FEN by the method
Board::Board(std::string_view fen)or
void setFen(std::string_view fen);From a Board you can generate a FEN by the function
std::string getFen(bool moveCounters = true);BitBoard is use to see the presence of piece on a square :
- 0 : no piece
- 1 : a piece
The value 1 can be 1 black or 1 white piece on the square
BitBoard are construct from Board with one on the following methods
- us(Color color)
- them(Color color)
- all to see the non empty cases
- pieces show the presence of a piece ( with or without color, you have the choice with 2 functions.
Bitboard c= b.pieces(PieceType::ROOK);us Color black is same a them color white
cout<<b.us(Color::WHITE)<<endl;The us method gives the chessboard with 1 where you have pieces
00000000
00000000
00000000
00000000
00001000
00000000
11110111
11111111
The them method gives the chessboard with 1 whee your oppenent has pieces
The methods to produce Bitboard are all() and occ()
The are also Packboard a concurent of FEN not readable but more compact.
You can make a move on the board with makeMove(move)
Board b(constants::STARTPOS)
Move m;
m.uci::uciToMove(board,"e2e4");
b.makemove(move)
uciToMove read the move in e2e4 syntax without any information about the piece unless the piece on the departure square : e2 ie a pawn.
The Move will be seen with the method printBoardWhite
But wih this method you will not be able to suppress moves or to suppress a move. You can add many moves :
m=uci::uciToMove(b,"d7d5");b.makeMove(m);list.add(m);
m=uci::uciToMove(b,"e4d5");b.makeMove(m);list.add(m);
We will see Movelist who is a list of moves.
If you add an erroneous move you will have a error message.
You can unmake a move by the function unmakeMove(const Move move);
You can use a other powerfull syntax :
m=Move::make<Move::NORMAL>(Square::underlying::SQ_B7,
Square::underlying::SQ_A8);It exactly the same as
m=uci::uciToMove(b,"d7d5")You have call makeMove in both cases.
b.makeMove(m)The make syntax is used when you make a promotion, for example to ask a QUEEN :
m=Move::make<Move::PROMOTION>(Square::underlying::SQ_B7,
Square::underlying::SQ_B8,PieceType::QUEEN);
b.makemove(m)You can replace PROMOTION by ENPASSANT when you need or CASTLING if you make CASTLE
- isCapture : check if a move is a capture, enpassant moves are also considered captures.
bool isCapture(const Move move);- Color sideToMove() give the side to move
- bool isRepetition(int count = 2) move is a repetition ?
- bool isInsufficientMaterial() is the material insufficient to win ?
cout<<b.isInsufficientMaterial()<<endl;- Square : at
Square s(Rank::RANK_1,File::FILE_A);
cout<<"ok :"<<b.at(s)<<endl;- CastlingRights castlingRights()
cout<<b.getCastleString()<<endl;Return KQk for example to inform : King white side castle is possible, Queen black side castle is possible and King black side is also possible but not Queen black side
- moveToSan function
cout<<uci::moveToSan(b,m)<<endl;
bxa8=Q+
- moveToLan function
cout<<uci::moveToLan(b,m)<<endl;
b7bxa8=Q+
- movegen is a class with just one public template method allowing to find all the possible moves non the board (named board)
template <MoveGenType mt = MoveGenType::ALL>
void static legalmoves(Movelist &movelist, const Board &board,
int pieces = PieceGenType::PAWN | PieceGenType::KNIGHT | PieceGenType::BISHOP |
PieceGenType::ROOK | PieceGenType::QUEEN | PieceGenType::KING=63);You can ommit the last parameter, concerning the type of piece. In this case you will search all piece. Generally you get all the move possible for example for the first move you get the list :
a2a3 b2b3 c2c3 d2d3 e2e3 f2f3 g2g3 h2h3 a2a4 b2b4 c2c4 d2d4 e2e4 f2f4 g2g4 h2h4 b1a3 b1c3 g1f3 g1h3
If you click on the e2 your search for moves starting by e2 to find e3 and e4 You can combine the third argument to get QUEEN and KING moves. But this case is not very usefull.
Because movelist is a reference, you will find all your moves in it.
You remark the argument mt who is a generic type argument. It can reveive three type of list :
- ALL = default
- CAPTURE = moves
- QUIET = no capture
The generic type is :
enum class MoveGenType : std::uint8_t { ALL, CAPTURE, QUIET };movegen::legalmoves<movegen::MoveGenType::CAPTURE> (moves,b);
for ( auto move: moves)
cout<<move<<endl;Will give the list of pieces captured you can keep only the pieces on one case.
The File is { FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, NO_FILE }
They are a representation for the first digit of case like a1 or f7
The operators == != >= <= > < int() are defined.
The string() function give a for FILE_A, b for FILE_B etc
The cout operator diplay filea to filez on console
The File is { RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, NO_RANK }
They are a representation for the second digit of case like a1 or f7
The operators == != >= <= > < int() are defined.
The string() function give 1 for RANK_1, 2 for RANK_2 etc
The cout operator display rank1 to rank8 on the console
Square is
enum class underlying {
SQ_A1, SQ_B1, SQ_C1, SQ_D1, SQ_E1, SQ_F1, SQ_G1, SQ_H1,
SQ_A2, SQ_B2, SQ_C2, SQ_D2, SQ_E2, SQ_F2, SQ_G2, SQ_H2,
SQ_A3, SQ_B3, SQ_C3, SQ_D3, SQ_E3, SQ_F3, SQ_G3, SQ_H3,
SQ_A4, SQ_B4, SQ_C4, SQ_D4, SQ_E4, SQ_F4, SQ_G4, SQ_H4,
SQ_A5, SQ_B5, SQ_C5, SQ_D5, SQ_E5, SQ_F5, SQ_G5, SQ_H5,
SQ_A6, SQ_B6, SQ_C6, SQ_D6, SQ_E6, SQ_F6, SQ_G6, SQ_H6,
SQ_A7, SQ_B7, SQ_C7, SQ_D7, SQ_E7, SQ_F7, SQ_G7, SQ_H7,
SQ_A8, SQ_B8, SQ_C8, SQ_D8, SQ_E8, SQ_F8, SQ_G8, SQ_H8,
NO_SQ
};
The cout operator display squarea1 to squareh8 on the console