This is my first time back in C++ after a year of Java and essay-writing (and/or crying).
My current progress is, all by-definition moves implemented, including castling, en-passant, draw by threefold-repetition, and draw by 50 move rule (which I haven't tested yet). I am currently working on kings in check/next move gets king out of check.
I would like some feedback on whether this is good or not.
To test whether a king is in check, I would simulate the move and see if the opponent controls the square your king is on.
If that sounds like a sound idea, my current code (except output statements) is here:
bool simulate_move(std::vector<std::vector<Piece *> > board, int from_x, int from_y, int to_x, int to_y, bool turn) {
std::vector<Point> white_control;
std::vector<Point> black_control;
King *white_king = NULL;
King *black_king = NULL;
move_piece(board, from_x, from_y, to_x, to_y);
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] != NULL) {
board[i][j]->get_controlled_squares(board[i][j]->isWhite() ? white_control : black_control);
if (dynamic_cast<King *>(board[i][j]) != NULL) {
if (board[i][j]->isWhite()) {
white_king = dynamic_cast<King *>(board[i][j]);
}
else {
black_king = dynamic_cast<King *>(board[i][j]);
}
}
}
}
}
if (turn) {
if (vector_contains_point(black_control, white_king->get_x_position(), white_king->get_y_position())) {
return false;
}
}
else {
if (vector_contains_point(white_control, black_king->get_x_position(), black_king->get_y_position())) {
return false;
}
}
return true;
}
I am testing with the moveset e2-e4 d7-d5 (B)f1-b5(+) c7-c6
. According to the output, any move fails the simulation (king is in check).
Also, my bishop movement logic is:
void Bishop::get_controlled_squares(std::vector<Point>& point_list) {
bool stop_up_left = false;
bool stop_up_right = false;
bool stop_down_left = false;
bool stop_down_right = false;
for (int i = 1; !stop_up_left || !stop_up_right || !stop_down_left || !stop_down_right; i++) {
//upward left
if (x_position - i < 0 || y_position - i < 0) {
stop_up_left = true;
}
if (!stop_up_left) {
if (board[x_position - i][y_position - i] != NULL) {
stop_up_left = true;
}
point_list.push_back(Point(x_position - i, y_position - i));
}
//up right
if (x_position + i > 7 || y_position - i < 0) {
stop_up_right = true;
}
if (!stop_up_right) {
if (board[x_position + i][y_position - i] != NULL) {
stop_up_right = true;
}
point_list.push_back(Point(x_position + i, y_position - i));
}
//down left
if (x_position - i < 0 || y_position + i > 7) {
stop_down_left = true;
}
if (!stop_down_left) {
if (board[x_position - i][y_position + i] != NULL) {
stop_down_left = true;
}
point_list.push_back(Point(x_position - i, y_position + i));
}
//down right
if (x_position + i > 7 || y_position + i > 7) {
stop_down_right = true;
}
if (!stop_down_right) {
if (board[x_position + i][y_position + i] != NULL) {
stop_down_right = true;
}
point_list.push_back(Point(x_position + i, y_position + i));
}
}
}
Other thing: does anyone have a way to make the code look nicer?
Also, the board is as follows (left and bottom are chess-algebraic, top and right are array index):
0 1 2 3 4 5 6 7
8 r n b q k b n r 0
7 p p p p p p p p 1
6 2
5 3
4 4
3 5
2 P P P P P P P P 6
1 R N B Q K B N R 7
A B C D E F G H