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
|
#include "bitboard.h"
#include <stdio.h>
#include <assert.h>
position_t position_starting() {
return (position_t) {
.bitboards = {
16ULL,
8ULL,
129ULL,
36ULL,
66ULL,
65280ULL,
1152921504606846976ULL,
576460752303423488ULL,
9295429630892703744ULL,
2594073385365405696ULL,
4755801206503243776ULL,
71776119061217280ULL,
},
.castling =
WHITE_SHORT_CASTLE | WHITE_LONG_CASTLE |
BLACK_SHORT_CASTLE | BLACK_LONG_CASTLE,
.turn = WHITE_TURN,
.passantable_file = 0,
.halfmove_clock = 0,
.fullmove_clock = 1
};
}
void assert_valid_position(position_t position) {
assert(check_valid_position(position) == true);
}
bool check_valid_position(position_t position) {
if (position.castling >
(
WHITE_SHORT_CASTLE | WHITE_LONG_CASTLE |
BLACK_SHORT_CASTLE | BLACK_LONG_CASTLE
)
) return false;
if (position.turn != WHITE_TURN && position.turn != BLACK_TURN) return false;
if (position.passantable_file > 8) return false; // 0 means no passant
if (position.bitboards[WHITE_KING] == 0) return false;
if (position.bitboards[BLACK_KING] == 0) return false;
return true;
}
char get_piece_char(size_t piece_type) {
assert(piece_type < PIECE_TYPE_COUNT);
if (WHITE_KING == piece_type) return 'K';
if (WHITE_QUEEN == piece_type) return 'Q';
if (WHITE_ROOK == piece_type) return 'R';
if (WHITE_BISHOP == piece_type) return 'B';
if (WHITE_KNIGHT == piece_type) return 'N';
if (WHITE_PAWN == piece_type) return 'P';
if (BLACK_KING == piece_type) return 'k';
if (BLACK_QUEEN == piece_type) return 'q';
if (BLACK_ROOK == piece_type) return 'r';
if (BLACK_BISHOP == piece_type) return 'b';
if (BLACK_KNIGHT == piece_type) return 'n';
if (BLACK_PAWN == piece_type) return 'p';
}
char get_piece_char_from_sqr(position_t position, size_t square) {
assert(square < 64);
for (int i = 0; i < PIECE_TYPE_COUNT; i++) {
if ((position.bitboards[i] >> square) & (bitboard_t)1) {
return get_piece_char(i);
}
}
return ' ';
}
void print_position(position_t position) {
for (int i = 8; i > 0; i--) {
printf("+---+---+---+---+---+---+---+---+\n");
printf(
"| %c | %c | %c | %c | %c | %c | %c | %c | %d\n",
get_piece_char_from_sqr(position, 8 * i - 8),
get_piece_char_from_sqr(position, 8 * i - 7),
get_piece_char_from_sqr(position, 8 * i - 6),
get_piece_char_from_sqr(position, 8 * i - 5),
get_piece_char_from_sqr(position, 8 * i - 4),
get_piece_char_from_sqr(position, 8 * i - 3),
get_piece_char_from_sqr(position, 8 * i - 2),
get_piece_char_from_sqr(position, 8 * i - 1),
i
);
}
printf("+---+---+---+---+---+---+---+---+\n");
printf(" a b c d e f g h\n");
}
void print_bitboard(bitboard_t bitboard) {
printf("Bitboard(%" PRIu64 ")\n", bitboard);
for (int i = 7; i >= 0; i--) {
for (int j = 0; j < 8; j++) {
printf("%" PRIu64 "", (bitboard >> (8 * i + j)) & 1);
}
printf("\n");
}
}
|