aboutsummaryrefslogtreecommitdiff
path: root/src/board.cpp
blob: e49aaf7d84618eb1763d97f159cc57f1af662bdd (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "board.hpp"
#include "bot.hpp"
#include "zobrist.hpp"
#include <cassert>
#include <cstdio>
#include <iostream>
#include <print>
#include <string>

std::string toChar(PieceType type, bool white) {
  switch (type) {
  case NONE:
    return " ";
  case KING:
    return white ? "♚" : "♔";
  case QUEEN:
    return white ? "♛" : "♕";
  case ROOK:
    return white ? "♜" : "♖";
  case BISHOP:
    return white ? "♝" : "♗";
  case KNIGHT:
    return white ? "♞" : "♘";
  case PAWN:
    return white ? "♟" : "♙";
  default:
    assert(false && "Unknown piece");
    return " ";
  }
}

Piece createPiece(PieceType type, bool color, bool moved) {
  Piece piece;
  piece.type = type;
  piece.color = color;
  piece.moved = moved;
  return piece;
}
void printBoard(Game *b) {
  std::string line = "  +------------------------+\n";
  std::string whiteLetters = "    a  b  c  d  e  f  g  h \n";
  std::string blackLetters = "    h  g  f  e  d  c  b  a \n";

  const char *lightSquare = "\033[48;5;245m";
  const char *darkSquare = "\033[48;5;29m";
  const char *reset = "\033[0m";

  std::cout << (b->turn ? whiteLetters : blackLetters);
  std::cout << line;

  if (b->turn) {
    for (int rank = 0; rank < 8; rank++) {
      std::printf("%d |", 8 - rank);

      for (int file = 0; file < 8; file++) {
        Piece piece = b->pieces[rank * 8 + file];
        std::string symbol = toChar(piece.type, piece.color);

        const char *bg = ((rank + file) % 2 == 0) ? lightSquare : darkSquare;

        std::cout << bg << " " << symbol << " " << reset;
      }

      std::printf("| %d\n", 8 - rank);
    }
  } else {
    for (int rank = 7; rank >= 0; rank--) {
      std::printf("%d |", 8 - rank);

      for (int file = 7; file >= 0; file--) {
        Piece piece = b->pieces[rank * 8 + file];
        std::string symbol = toChar(piece.type, piece.color);

        const char *bg = ((rank + file) % 2 == 0) ? lightSquare : darkSquare;

        std::cout << bg << " " << symbol << " " << reset;
      }

      std::printf("| %d\n", 8 - rank);
    }
  }

  std::cout << line;
  std::cout << (b->turn ? whiteLetters : blackLetters);

  std::println();
  std::println("Turn: {}", b->turn ? "White" : "Black");

  assert(b->castle != "");

  std::println("Castling: {}", b->castle);
  std::println("Can enpassant: {}", b->canEnpassant ? "Yes" : "No");
  std::println("Where enpassant: {}{}", (char)((int)b->enPassant.file + 'A'),
               8 - (int)b->enPassant.rank);
  std::printf("Move clock: %d\n", b->MoveClock);
  std::printf("Half move clock: %d\n", b->halfMoveClock);
  std::printf("Board Eval: %f\n", EvaluateBoardForWhite(b, 0));
}

void PlayMove(Move move, Game *b) {
  // TODO: add all fide behavior
  Piece piece = b->pieces[PositionToIndex(move.From)];
  Piece piece2 = b->pieces[PositionToIndex(move.To)];
  if (piece2.type != NONE) {
    if (piece2.color == b->turn) {
      assert(false && "capturing friendly piece error");
    }
  }
  piece.moved = true;
  if (piece.type == PAWN || piece2.type != NONE) {
    b->halfMoveClock = 0;
  } else {
    b->halfMoveClock++;
  }
  if (piece.type == PAWN && b->canEnpassant && move.To == b->enPassant) {
    Position capturedPawn = move.To;
    capturedPawn.rank += piece.color ? 1 : -1;
    b->pieces[PositionToIndex(capturedPawn)] = {false, NONE, false};
  }

  b->canEnpassant = false;
  b->MoveClock++;

  if (piece.type == PAWN) {
    Position to = move.To;
    to.rank -= (piece.color ? -2 : 2);
    if (to == move.From) {
      b->canEnpassant = true;
      Position target = move.From;
      target.rank += piece.color ? -1 : 1;
      b->enPassant = target;
    };
  }

  if (piece.type == PAWN && move.To.rank == (piece.color ? 0 : 7)) {
    // TODO parse the promote type from uci isntead of ignoreting it
    piece.type = move.promotion == NONE ? QUEEN : move.promotion;
  }
  if (b->halfMoveClock >= 50) {
    b->state = DRAW;
  }
  b->pieces[PositionToIndex(move.To)] = piece;
  b->pieces[PositionToIndex(move.From)] = {false, NONE, false};

  b->turn = !b->turn;

  uint64_t key = GenerateZobristKey(b);

  b->ThreeFoldMap[key]++;
  if (b->ThreeFoldMap[key] >= 3) {
    b->state = DRAW;
  }
};
int PositionToIndex(Position i) { return i.rank * 8 + i.file; }

UndoMove MakeMove(Move move, Game *g) {
  UndoMove undo = {};

  undo.from = move.From;
  undo.to = move.To;

  undo.movedPiece = g->pieces[PositionToIndex(move.From)];
  undo.capturedPiece = g->pieces[PositionToIndex(move.To)];

  undo.oldTurn = g->turn;
  undo.oldCanEnpassant = g->canEnpassant;
  undo.oldEnPassant = g->enPassant;

  undo.oldHalfMoveClock = g->halfMoveClock;
  undo.oldMoveClock = g->MoveClock;

  undo.oldState = g->state;
  undo.oldCaslte = g->castle;

  // play move

  Piece piece = g->pieces[PositionToIndex(move.From)];
  Piece piece2 = g->pieces[PositionToIndex(move.To)];

  if (piece2.type != NONE) {
    if (piece2.color == g->turn) {
      assert(false && "capturing friendly piece error");
    }
  }
  piece.moved = true;

  // clock
  g->MoveClock++;
  if (piece.type == PAWN || piece2.type != NONE) {
    g->halfMoveClock = 0;
  } else {
    g->halfMoveClock++;
  }

  // Playing enpasstant
  if (piece.type == PAWN && g->canEnpassant && move.To == g->enPassant) {
    Position capturedPawn = move.To;
    capturedPawn.rank += piece.color ? 1 : -1;
    g->pieces[PositionToIndex(capturedPawn)] = {false, NONE, false};

    undo.wasEnPassantCapture = true;
    undo.enPassantCapturedSquare = capturedPawn;
    undo.enPassantCapturedPiece = g->pieces[PositionToIndex(capturedPawn)];
  }

  g->canEnpassant = false;

  // Adding enpassant
  if (piece.type == PAWN) {
    Position to = move.To;
    to.rank -= (piece.color ? -2 : 2);
    if (to == move.From) {
      g->canEnpassant = true;
      Position target = move.From;
      target.rank += piece.color ? -1 : 1;
      g->enPassant = target;
    };
  }

  // Promotions
  if (piece.type == PAWN && move.To.rank == (piece.color ? 0 : 7)) {
    // TODO parse the promote type from uci isntead of ignoreting it
    piece.type = move.promotion == NONE ? QUEEN : move.promotion;
  }

  // setting the moves
  g->pieces[PositionToIndex(move.To)] = piece;
  g->pieces[PositionToIndex(move.From)] = {false, NONE, false};

  // post move change

  // changing who turn it is
  g->turn = !g->turn;

  // 3 fold check
  uint64_t key = GenerateZobristKey(g);

  undo.zobristKey = key;

  if (g->ThreeFoldMap[key] >= 3) {
    g->state = DRAW;
  }

  g->ThreeFoldMap[key]++;

  if (g->halfMoveClock >= 50) {
    g->state = DRAW;
  }

  return undo;
};
void UnMake(UndoMove undo, Game *g) {
  g->pieces[PositionToIndex(undo.from)] = undo.movedPiece;

  g->pieces[PositionToIndex(undo.to)] = undo.capturedPiece;

  if (undo.wasEnPassantCapture) {
    g->pieces[PositionToIndex(undo.enPassantCapturedSquare)] =
        undo.enPassantCapturedPiece;
  }

  g->turn = undo.oldTurn;

  g->canEnpassant = undo.oldCanEnpassant;
  g->enPassant = undo.oldEnPassant;

  g->halfMoveClock = undo.oldHalfMoveClock;
  g->MoveClock = undo.oldMoveClock;

  g->state = undo.oldState;

  g->castle = undo.oldCaslte;

  if (--g->ThreeFoldMap[undo.zobristKey] <= 0) {
    g->ThreeFoldMap.erase(undo.zobristKey);
  }
};