summaryrefslogtreecommitdiff
path: root/src/engine/moves/king.c
blob: 98c6ab96aacdb40a8ca4102276278834c71c2e3d (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
#define MOVES_INTERNAL
#include "engine/moves.h"

void get_king_moves(moves_t* moves, position_t position) {
  assert_valid_position(position);

  bitboard_t friendly_king;
  int king_square;
  bitboard_t friendly_pieces;
  bitboard_t enemy_pieces;
  if (position.turn == WHITE_TURN) {
    friendly_pieces = whites(position);
    enemy_pieces = blacks(position);
    friendly_king = position.bitboards[WHITE_KING];
  } else {
    friendly_pieces = blacks(position);
    enemy_pieces = whites(position);
    friendly_king = position.bitboards[BLACK_KING];
  }
  king_square = __builtin_ctzll(friendly_king);

  bitboard_t movement;
  if (king_square >= 10) {
    movement = 920078ULL << (king_square - 10);
  } else {
    movement = 920078ULL >> -(king_square - 10);
  }
  if (friendly_king & BITMASK_FILE_A) {
    movement &= BITMASK_FILE_A | BITMASK_FILE_B;
  } else if (friendly_king & BITMASK_FILE_H) {
    movement &= BITMASK_FILE_G | BITMASK_FILE_H;
  }

  movement &= ~friendly_pieces;
  while (movement) {
    int to = __builtin_ctzll(movement);
    movement &= movement - 1;
    add_move(
      moves,
      king_square,
      to,
      .flags = ((enemy_pieces >> to) & 1) ? MOVE_CAPTURE : 0,
    );
  }

  bitboard_t all_occupied = friendly_pieces | enemy_pieces;
  if (position.turn == WHITE_TURN) {
    if ((position.castling & WHITE_SHORT_CASTLE) &&
        king_square == 4 &&
        !((all_occupied >> 5) & 1) &&
        !((all_occupied >> 6) & 1))
    {
      add_move(moves, 4, 6, .flags = MOVE_SHORT_CASTLE);
    }
    if ((position.castling & WHITE_LONG_CASTLE) &&
        king_square == 4 &&
        !((all_occupied >> 3) & 1) &&
        !((all_occupied >> 2) & 1) &&
        !((all_occupied >> 1) & 1))
    {
      add_move(moves, 4, 2, .flags = MOVE_LONG_CASTLE);
    }
  } else {
    if ((position.castling & BLACK_SHORT_CASTLE) &&
        king_square == 60 &&
        !((all_occupied >> 61) & 1) &&
        !((all_occupied >> 62) & 1))
    {
      add_move(moves, 60, 62, .flags = MOVE_SHORT_CASTLE);
    }
    if ((position.castling & BLACK_LONG_CASTLE) &&
        king_square == 60 &&
        !((all_occupied >> 59) & 1) &&
        !((all_occupied >> 58) & 1) &&
        !((all_occupied >> 57) & 1))
    {
      add_move(moves, 60, 58, .flags = MOVE_LONG_CASTLE);
    }
  }
}

#ifdef TEST_MOD
#include <stdbool.h>

bool test_white_king_corners() {
  moves_t moves;
  moves_init(&moves);
  position_t p = {0};
  p.bitboards[BLACK_KING] = 100;
  p.bitboards[WHITE_KING] = 1;
  p.turn = WHITE_TURN;
  get_king_moves(&moves, p);
  if (moves.length != 3) return false;
  if (moves.moves[0].from != 0) return false;
  if (moves.moves[0].to != 1) return false;
  if (moves.moves[1].from != 0) return false;
  if (moves.moves[1].to != 8) return false;
  if (moves.moves[2].from != 0) return false;
  if (moves.moves[2].to != 9) return false;
  moves.length = 0;
  p.bitboards[WHITE_KING] = 128;
  get_king_moves(&moves, p);
  if (moves.length != 3) return false;
  if (moves.moves[0].from != 7) return false;
  if (moves.moves[0].to != 6) return false;
  if (moves.moves[1].from != 7) return false;
  if (moves.moves[1].to != 14) return false;
  if (moves.moves[2].from != 7) return false;
  if (moves.moves[2].to != 15) return false;
  moves.length = 0;
  p.bitboards[WHITE_KING] = 72057594037927936ULL;
  get_king_moves(&moves, p);
  if (moves.length != 3) return false;
  if (moves.moves[0].from != 56) return false;
  if (moves.moves[0].to != 48) return false;
  if (moves.moves[1].from != 56) return false;
  if (moves.moves[1].to != 49) return false;
  if (moves.moves[2].from != 56) return false;
  if (moves.moves[2].to != 57) return false;

  moves_deinit(moves);
  return true;
}

bool test_black_king_corners() {
  moves_t moves;
  moves_init(&moves);

  position_t p = {0};
  p.bitboards[WHITE_KING] = 100;
  p.bitboards[BLACK_KING] = 1;
  p.turn = BLACK_TURN;
  get_king_moves(&moves, p);
  if (moves.length != 3) return false;
  if (moves.moves[0].from != 0) return false;
  if (moves.moves[0].to != 1) return false;
  if (moves.moves[1].from != 0) return false;
  if (moves.moves[1].to != 8) return false;
  if (moves.moves[2].from != 0) return false;
  if (moves.moves[2].to != 9) return false;
  moves.length = 0;
  p.bitboards[BLACK_KING] = 128;
  get_king_moves(&moves, p);
  if (moves.length != 3) return false;
  if (moves.moves[0].from != 7) return false;
  if (moves.moves[0].to != 6) return false;
  if (moves.moves[1].from != 7) return false;
  if (moves.moves[1].to != 14) return false;
  if (moves.moves[2].from != 7) return false;
  if (moves.moves[2].to != 15) return false;
  moves.length = 0;
  p.bitboards[BLACK_KING] = 72057594037927936ULL;
  get_king_moves(&moves, p);
  if (moves.length != 3) return false;
  if (moves.moves[0].from != 56) return false;
  if (moves.moves[0].to != 48) return false;
  if (moves.moves[1].from != 56) return false;
  if (moves.moves[1].to != 49) return false;
  if (moves.moves[2].from != 56) return false;
  if (moves.moves[2].to != 57) return false;

  moves_deinit(moves);
  return true;
}
#endif