aboutsummaryrefslogtreecommitdiff
path: root/tests/pawn_moves_test.cpp
blob: 47b8ceba11b6f446bfb20b27bbf0662e86b059c8 (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
#include "moves.hpp"
#include <cassert>

bool HasMove(const std::vector<Move> &moves, int from, int to) {
  Position expectedFrom = IndexToPosition(from);
  Position expectedTo = IndexToPosition(to);
  for (const Move &move : moves) {
    if (move.From.rank == expectedFrom.rank &&
        move.From.file == expectedFrom.file && move.To.rank == expectedTo.rank &&
        move.To.file == expectedTo.file) {
      return true;
    }
  }
  return false;
}

int CountMovesFrom(const std::vector<Move> &moves, int from) {
  Position expected = IndexToPosition(from);
  int count = 0;
  for (const Move &move : moves) {
    if (move.From.rank == expected.rank && move.From.file == expected.file) {
      count++;
    }
  }
  return count;
}

int main() {
  board white{};
  white.turn = true;
  white.pieces[52] = createPiece(PAWN, true);  // e2
  white.pieces[43] = createPiece(ROOK, false); // d3
  white.pieces[45] = createPiece(ROOK, true);  // f3

  std::vector<Move> whiteMoves = GetLegalMoves(&white);
  assert(CountMovesFrom(whiteMoves, 52) == 3);
  assert(HasMove(whiteMoves, 52, 44)); // e2-e3
  assert(HasMove(whiteMoves, 52, 36)); // e2-e4
  assert(HasMove(whiteMoves, 52, 43)); // e2xd3

  white.pieces[44] = createPiece(KING, true); // block e3 and e4
  std::vector<Move> blockedMoves = GetLegalMoves(&white);
  assert(CountMovesFrom(blockedMoves, 52) == 1);
  assert(HasMove(blockedMoves, 52, 43)); // capture remains legal

  board black{};
  black.turn = false;
  black.pieces[12] = createPiece(PAWN, false); // e7

  std::vector<Move> blackMoves = GetLegalMoves(&black);
  assert(CountMovesFrom(blackMoves, 12) == 2);
  assert(HasMove(blackMoves, 12, 20)); // e7-e6
  assert(HasMove(blackMoves, 12, 28)); // e7-e5
}