ChessGame.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef CHESSY_CHESSGAME_H
  2. #define CHESSY_CHESSGAME_H
  3. #include <vector>
  4. #include <string>
  5. #include "Board.h"
  6. #include "TranspositionTable.h"
  7. namespace chessy
  8. {
  9. struct MoveInfo;
  10. struct UndoInfo;
  11. class ChessGame;
  12. }
  13. /*!
  14. * This structure holds additional information about a move.
  15. */
  16. struct chessy::MoveInfo
  17. {
  18. //! the move itself
  19. Move move;
  20. //! the type of the piece that was moved
  21. PieceType movedPiece;
  22. //! if the move is an en-passant move, this field holds the square of
  23. //! the pawn, that was captured. Otherwise this index should be zero.
  24. //Index enPassantTarget;
  25. MoveInfo(void) = default;
  26. MoveInfo(Move move, const ChessGame& cg);
  27. };
  28. struct chessy::UndoInfo
  29. {
  30. Bitboard before;
  31. Bitboard beforeCaptured;
  32. Bitboard promotionBefore;
  33. HashValue hash;
  34. PieceType type;
  35. PieceType captured;
  36. PieceType promotion;
  37. Index enPassantBefore;
  38. uint8_t reversibleHalfMoves;
  39. uint8_t castlingRights;
  40. };
  41. class chessy::ChessGame
  42. {
  43. Board board;
  44. short reversibleHalfMoves = 0;
  45. //! stores the castling rights according to the following scheme:
  46. //! <b> XXXXQKqk </b>,
  47. //! where X is an unused bit, Q and K store weather black can castle
  48. //! to queen side/king side and q and k store the same for white.
  49. uint8_t castlingRights = 0xF;
  50. //! file of en-passant-capturable pawn
  51. // -1 for no en passant possible
  52. int8_t enPassant = -1;
  53. Side turn = WHITE_SIDE;
  54. int moveCount = 1;
  55. //! holds the zobrist hash value for the current position
  56. HashValue hash;
  57. std::vector<HashValue> history;
  58. public:
  59. ChessGame(void);
  60. ChessGame(const std::string& fenString);
  61. ChessGame(const ChessGame&) = default;
  62. ~ChessGame(void) = default;
  63. inline const Board& getBoard(void) const { return board; }
  64. inline Board& getBoard(void) { return board; }
  65. inline bool getCanCastleKingSide(Side side) const
  66. {
  67. return castlingRights & (1 << (side == BLACK_SIDE ? 2 : 0));
  68. }
  69. inline void setCanCastleKingSide(Side side, bool canCastle)
  70. {
  71. castlingRights ^= (castlingRights ^ (canCastle ? -1 : 0)) &
  72. (1 << (side == BLACK_SIDE ? 2 : 0));
  73. }
  74. inline bool getCanCastleQueenSide(Side side) const
  75. {
  76. return castlingRights & (1 << (side == BLACK_SIDE ? 3 : 1));
  77. }
  78. inline void setCanCastleQueenSide(Side side, bool canCastle)
  79. {
  80. castlingRights ^= (castlingRights ^ (canCastle ? -1 : 0)) &
  81. (1 << (side == BLACK_SIDE ? 3 : 1));
  82. }
  83. inline int getCastlingRights(void) const { return castlingRights; }
  84. inline HashValue getHash(void) const { return hash; }
  85. inline const std::vector<HashValue>& getHistory(void) const { return history; }
  86. void move(Move move);
  87. inline Side getTurn(void) const { return turn; }
  88. void loadFromFen(const std::string& fenString);
  89. std::string generateFen(void) const;
  90. inline Index getEnPassantIndex(void) const { return enPassant; }
  91. UndoInfo doMove(const MoveInfo& mi);
  92. void undoMove(const UndoInfo& ui);
  93. };
  94. #endif // CHESSY_CHESSGAME_H