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
|
#include "board.hpp"
#include <cassert>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <print>
#include <string>
char toChar(PieceType type) {
switch (type) {
case NONE:
return '.';
case PAWN:
return 'P';
case KNIGHT:
return 'N';
case BISHOP:
return 'B';
case ROOK:
return 'R';
case QUEEN:
return 'Q';
case KING:
return 'K';
default:
std::cout << "\n" << type << "\n";
assert(false && "Unknown piece" && type);
return '?';
}
}
Piece createPiece(PieceType type, bool color, bool moved) {
Piece piece;
piece.type = type;
piece.color = color;
piece.moved = moved;
return piece;
}
void printBoard(Board *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";
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];
char symbol = toChar(piece.type);
if (!piece.color)
symbol = std::tolower(static_cast<unsigned char>(symbol));
std::printf(" %c", symbol);
}
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];
char symbol = toChar(piece.type);
if (!piece.color)
symbol = std::tolower(static_cast<unsigned char>(symbol));
std::printf(" %c", symbol);
}
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);
}
void PlayMove(Move move, Board *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;
};
b->pieces[PositionToIndex(move.To)] = piece;
b->pieces[PositionToIndex(move.From)] = {false, NONE, false};
return;
}
};
int PositionToIndex(Position i) { return i.rank * 8 + i.file; }
|