aboutsummaryrefslogtreecommitdiff
path: root/src/board/board.hpp
blob: 91a38f9a3e905704d20b0c229879d15094e251a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef SRC_BOARD_H_
#define SRC_BOARD_H_

#include <cstdint>
#include <unordered_map>

enum PieceType {
  NONEPIECE,
  PAWN,
  KNIGHT,
  BISHOP,
  ROOK,
  QUEEN,
  KING,
};

enum GameState {
  TURN,
  WHITE_WON,
  BLACK_WON,
  DRAW,
};

struct Piece {
  bool color = false;
  PieceType type = NONEPIECE;
};

struct Position {
  uint8_t rank = 0;
  uint8_t file = 0;

  bool operator==(const Position &) const = default;
};
struct Move {
  Position From;
  Position To;

  PieceType promotion = NONEPIECE;
  bool operator==(const Move &other) const {
    return From == other.From && To == other.To && promotion == other.promotion;
  }
};

enum Flag { EXACT, LOWERBOUND, UPPERBOUND };

struct TranspositionsEntry {
  int depth;
  int Eval;
  Flag flag;
  Move bestMove = {};
};
struct Game {
  Piece pieces[64];
  uint64_t PieceBitboard = 0; // used for quicly iterating over all squares
  bool turn = true;           // 1 white; 0 black
  bool whiteCastleKing = false;
  bool whiteCastleQueen = false;
  bool blackCastleKing = false;
  bool blackCastleQueen = false;
  uint8_t halfMoveClock = 0;
  uint16_t MoveClock = 0;
  Position enPassant;
  bool canEnpassant = false;
  GameState state = TURN;
  std::unordered_map<uint64_t, int> ThreeFoldMap;
  std::unordered_map<uint64_t, TranspositionsEntry> *Transpositions = nullptr;
};

struct UndoMove {
  Piece movedPiece;
  Piece capturedPiece;

  Position from;
  Position to;

  bool wasEnPassantCapture = false;
  Position enPassantCapturedSquare;
  Piece enPassantCapturedPiece;

  bool oldTurn;
  bool oldCanEnpassant;
  Position oldEnPassant;

  bool OldWhiteCastleKing;
  bool OldWhiteCastleQueen;
  bool OldBlackCastleKing;
  bool OldBlackCastleQueen;

  int oldHalfMoveClock;
  int oldMoveClock;
  uint64_t ZobristKey;
  GameState oldState;

  // castling
  bool wasCastle = false;
  bool CastledSide = false; // 0 for queen side, 1 for king side
};
Piece createPiece(PieceType type, bool color);

int PositionToIndex(Position i);
UndoMove MakeMove(Move move, Game *g);
void UnMakeMove(UndoMove undo, Game *g);

Position FindKing(Game *g, bool white);
void UpdateBitboards(Game *g);
#endif /* SRC_BOARD_H_ */