aboutsummaryrefslogtreecommitdiff
path: root/src/moves.cpp
blob: edf6c1e27a99a1437f2f75812286610134a1a6f6 (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
278
279
280
281
282
#include <array>
#include <cassert>
#include <cstdint>
#include <sys/types.h>
#include <vector>

#include "board.hpp"
#include "moves.hpp"
void GenerateKnightMoves(Game *b, int from, std::vector<Move> &moves) {

  Piece knight = b->pieces[from];
  if (knight.type != KNIGHT) {
    assert(false && "Calling generate knight moves on non knight");
    return;
  }
  // Knight moves: https://www.chessprogramming.org/Knight_Pattern
  constexpr std::array<int, 8> knight_moves{-10, 6, 15, 17, 10, -6, -15, -17};

  for (int offset : knight_moves) {
    int next = from + offset;
    if (next >= 64 || next < 0) {
      continue;
    }
    const int fileDelta = std::abs((next % 8) - (from % 8));
    if (fileDelta != 1 && fileDelta != 2) {
      continue;
    };
    if (b->pieces[next].type != NONEPIECE) {
      if (b->pieces[next].color == b->turn) {
        continue;
      };
    }

    moves.push_back({IndexToPosition(from), IndexToPosition(next)});
  }
};
void GeneratePawnMoves(Game *b, int from, std::vector<Move> &moves) {
  Piece pawn = b->pieces[from];
  if (pawn.type != PAWN) {
    assert(false && "Calling generate pawn moves on non pawn");
    return;
  }
  Position position = IndexToPosition(from);
  int step = (pawn.color ? -1 : 1) * 8;
  int next = from + step;

  // wrap check
  if (position.rank + (pawn.color ? -1 : 1) < 0 ||
      position.rank + (pawn.color ? -1 : 1) >= 8) {
    return;
  }

  // if piece it want to move to is none and it as legal move
  if (b->pieces[next].type == NONEPIECE) {
    if (position.rank + (pawn.color ? -1 : 1) == 0 ||
        position.rank + (pawn.color ? -1 : 1) == 7) {
      moves.push_back({position, IndexToPosition(next), QUEEN});
      moves.push_back({position, IndexToPosition(next), ROOK});
      moves.push_back({position, IndexToPosition(next), BISHOP});
      moves.push_back({position, IndexToPosition(next), KNIGHT});
    } else {
      moves.push_back({position, IndexToPosition(next)});
    }

    int startingRank = pawn.color ? 6 : 1;
    int twoSteps = from + step * 2;
    if (position.rank == startingRank &&
        b->pieces[twoSteps].type == NONEPIECE) {
      moves.push_back({position, IndexToPosition(twoSteps)});
    }
  }
  for (int fileOffset : {-1, 1}) {
    int targetFile = position.file + fileOffset;
    int targetRank = position.rank + (pawn.color ? -1 : 1);

    if (targetFile < 0 || targetFile >= 8)
      continue;

    if (targetRank < 0 || targetRank >= 8)
      continue;

    int target = targetRank * 8 + targetFile;

    if (IndexToPosition(target) == b->enPassant && b->canEnpassant) {
      moves.push_back({position, IndexToPosition(target)});
    }

    if (b->pieces[target].type != NONEPIECE &&
        b->pieces[target].color != pawn.color) {
      if (targetRank == 0 || targetRank == 7) {
        moves.push_back({position, IndexToPosition(target), QUEEN});
        moves.push_back({position, IndexToPosition(target), ROOK});
        moves.push_back({position, IndexToPosition(target), BISHOP});
        moves.push_back({position, IndexToPosition(target), KNIGHT});
      } else {
        moves.push_back({position, IndexToPosition(target)});
      }
    }
  }
  // the end
}

std::vector<Move> GetPseudoLegalMoves(Game *b) {
  std::vector<Move> moves;
  moves.reserve(50); // almost all position dont have that many moves

  constexpr std::array<int, 4> rook_Moves{-1, 1, 8, -8};
  constexpr std::array<int, 4> bishop_Moves{-9, 9, -7, 7};

  for (uint i = 0; i < sizeof(b->pieces) / sizeof(b->pieces[0]); i++) {
    Piece piece = b->pieces[i];
    if (piece.type == NONEPIECE) {
      continue;
    }
    if (piece.color != b->turn)
      continue;

    // add support for knight and king later

    if (piece.type == PAWN) {
      GeneratePawnMoves(b, i, moves);
    }
    if (piece.type == KNIGHT) {
      GenerateKnightMoves(b, i, moves);
    };
    if (piece.type == BISHOP) {
      GenerateSlidingMoves(b, i, bishop_Moves, moves);
    }
    if (piece.type == KING) {
      GenerateKingMoves(b, i, moves);
    };
    if (piece.type == ROOK) {
      GenerateSlidingMoves(b, i, rook_Moves, moves);
    }
    if (piece.type == QUEEN) {
      GenerateSlidingMoves(b, i, rook_Moves, moves);
      GenerateSlidingMoves(b, i, bishop_Moves, moves);
    }
  };

  return moves;
}
bool IsSquareAttacked(Game *board, Position square, bool white) {
  Game temp = *board;

  // Generate moves for the attacking side
  temp.turn = white;

  auto moves = GetPseudoLegalMoves(&temp);

  for (const Move &move : moves) {
    if (move.To == square)
      return true;
  }

  return false;
}
bool IsPieceTypeAttacked(Game *board, PieceType type, bool white) {
  Game temp = *board;

  // Generate moves for the attacking side
  temp.turn = white;

  auto moves = GetPseudoLegalMoves(&temp);

  for (const Move &move : moves) {
    if (temp.pieces[PositionToIndex(move.To)].type == type)
      return true;
  }

  return false;
}
std::vector<Move> GetLegalMoves(Game *b) {
  std::vector<Move> moves = GetPseudoLegalMoves(b);
  std::vector<Move> legalMove;
  for (Move move : moves) {
    bool isLegal = true;
    UndoMove undo = MakeMove(move, b);
    auto opponentResponse = GetPseudoLegalMoves(b);
    for (Move move : opponentResponse) {
      if (b->pieces[PositionToIndex(move.To)].type == KING) {
        isLegal = false;
      }
    }
    UnMakeMove(undo, b);
    if (isLegal) {
      legalMove.push_back(move);
    };
  }
  if (legalMove.size() == 0) {
    bool isCheck = false;

    Game testBoard = *b;
    testBoard.turn = !testBoard.turn;
    auto opponentResponse = GetPseudoLegalMoves(&testBoard);
    for (Move move : opponentResponse) {
      if (testBoard.pieces[PositionToIndex(move.To)].type == KING) {
        isCheck = true;
      }
    }

    if (isCheck) {
      if (b->turn) {
        b->state = BLACK_WON;
      } else {
        b->state = WHITE_WON;
      };
    } else {
      b->state = STALEMATE;
    }
  }
  return legalMove;
}

Position IndexToPosition(int i) {
  uint8_t rank = i / 8; // 0-7
  uint8_t file = i % 8; // 0-7
  return {rank, file};
}

void GenerateSlidingMoves(Game *b, int from,
                          const std::array<int, 4> &directions,
                          std::vector<Move> &moves) {
  for (uint i = 0; i < directions.size(); i++) {
    int direction = directions[i];

    int i2 = from;
    while (true) {
      i2 += direction;
      int oldFile = (i2 - direction) % 8;
      int newFile = i2 % 8;

      if (direction == 7 || direction == -7 || direction == 9 ||
          direction == -9) {
        if (std::abs(newFile - oldFile) != 1)
          break;
      }
      if (i2 >= 64 || i2 < 0) {
        break;
      }
      if (b->pieces[i2].color == b->turn && b->pieces[i2].type != NONEPIECE) {
        break;
      }
      if ((direction == 1 || direction == -1) &&
          (i2 / 8 != (i2 - direction) / 8)) {
        break;
      }
      moves.push_back({IndexToPosition(from), IndexToPosition(i2)});
      if (b->pieces[i2].color != b->turn && b->pieces[i2].type != NONEPIECE) {
        break;
      }
    }
  };
};

void GenerateKingMoves(Game *b, int from, std::vector<Move> &moves) {
  if (b->pieces[from].type != KING) {
    assert(false && "calling generate king moves on non king");
    return;
  }
  if (b->pieces[from].color != b->turn) {
    assert(false && "calling generate king moves on king of opposite color");
    return;
  }
  constexpr std::array<int, 8> king_moves{-1, 1, 8, -8, -9, 9, -7, 7};
  for (int offset : king_moves) {
    int next = from + offset;
    if (next >= 64 || next < 0) {
      continue;
    }
    if (std::abs((next % 8) - (from % 8)) > 1) {
      continue;
    };
    if (b->pieces[next].type != NONEPIECE) {
      if (b->pieces[next].color == b->turn) {
        continue;
      };
    }

    moves.push_back({IndexToPosition(from), IndexToPosition(next)});
  }
}