Skip to content
Gilles Maire edited this page Feb 4, 2025 · 10 revisions

Presentation

Chess.hpp

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

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.

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

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()

PackedBoard

The are also Packboard a concurent of FEN not readable but more compact.

Moves

Presentation

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);

makeMove::xxx

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 to call makeMove in both cases.

b.makeMove(m)

make Move

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. See after.

Square s(Rank::RANK_1,File::FILE_A);
cout<<"ok :"<<b.at(s)<<endl;

Function at

Square : at template <typename T = Piece>
    [[nodiscard]] T at(Square sq) const

The function at is a template method.

By default without any specification type it will return the Piece

  • By default at(sq) return a Piece WHITEPAWN,WHITEKNIGHT,WHITEBISHOP,WHITEROOK,WHITEQUEEN, WHITEKING,BLACKPAWN, BLACKKNIGHT,BLACKBISHOP,BLACKROOK, BLACKQUEEN,BLACKKING, NONE
  • with type PieceType at(sq) return a PieceType PAWN KNIGHT BISHOP ROOK QUEEN KING NONE

CASTLING

The function Move::makeMove::CASTLING(Square::underlying::SQ_E1,Square::underlying::SQ_H1);

You must gibe the From square of KING anf the From square on ROOK. You don't have to add the thirf default argument reserverd for the PROMOTION.

EN PASSANT

The function move=Move::makeMove::ENPASSANT(Square::underlying::SQ_E5,Square::underlying::SQ_F6); will remove the black pawn on the square F5

Functions with moves

  • isCapture : check if a move is a capture, enpassant moves are also considered captures.
bool isCapture(const Move move);

Miscellaneous

  • 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;
  • 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

Moveto functions

  • moveToSan function
cout<<uci::moveToSan(b,m)<<endl;
bxa8=Q+
  • moveToLan function
cout<<uci::moveToLan(b,m)<<endl;
b7bxa8=Q+

Movegen

  • 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.

File

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 to a8

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

File(int file) cast the file to FILE_A for 0 FILE_B for 1 File(0) is FILE_A File(FILE_A) is FILE_A File("A") is FILE_A

Rank

The Rank 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 Rank(int RANK) cast the file to RANK_1 for 0 RANK_2 for 1 RANK(0) is RANK_A 1 is RANK_2 File(FILE_A) is FILE_A File("A") is FILE_A

Square

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

Color

Color can be WHITE (0) BLACK(1) and NONE (-1)

Several constructor :

Color() : With the constructor Color() the object contains the color NONE Color(underlying c) : initialize a Color object with color c Color(int c) : can be initialized with 0, 1 or -1 Color( std::string_view str) : where str can be "w" or "b" with other string_view the color is 0

The operator ~ betwen contexpr Color returns 1 if color is black

The operator string() return ths std::string "w" "b" or "NONE" The operator == and != test if two colors are equal The operator int() return the color in integer value

string_view operator split

std::vectorstd::string_view splitString(std::string_view string, const char &delimiter)

split a string_view around a caracter into a vector of string_view