Cleaned chess binary and added comments

This commit is contained in:
Claudio Maggioni 2019-08-20 22:28:33 +02:00
parent 7ad853f0a3
commit ad459c3b85
2 changed files with 12 additions and 1 deletions

Binary file not shown.

View File

@ -298,6 +298,7 @@ bool player_checks(struct chessboard *cb, enum player p) {
bool player_checkmates(struct chessboard *cb, enum player p) {
int i, j;
// for every opponent's piece
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
if (is_empty(cb, i, j) || player(cb, i, j) == p) {
@ -305,6 +306,9 @@ bool player_checkmates(struct chessboard *cb, enum player p) {
}
int k, l;
// for every possible position not equal to the current position of
// that piece, which is empty or occupied by our pieces (implying
// capture)
for (k = 0; k < 8; k++) {
for (l = 0; l < 8; l++) {
if ((k == i && l == j) ||
@ -313,16 +317,20 @@ bool player_checkmates(struct chessboard *cb, enum player p) {
continue;
}
// check if the move is valid
bool can = can_move(cb, p == BLACK ? WHITE : BLACK,
i, j, k, l);
// if not, carry on
if (!can) {
continue;
}
// if valid, perform the move and check for check.
enum pieces tmp = cb_move(cb, i, j, k, l);
bool check = player_checks(cb, p);
cb_move_restore(cb, tmp, i, j, k, l);
// if check is no longer there, then this is not a checkmate
if (!check) {
return false;
}
@ -331,6 +339,7 @@ bool player_checkmates(struct chessboard *cb, enum player p) {
}
}
// if no such move exists, then this is checkmate
return true;
}
@ -380,13 +389,15 @@ enum mstatus move(
return INVALID;
}
// check if we are currently in check state
bool check_before = player_checks(cb, p == WHITE ? BLACK : WHITE);
// determine if move is valid
bool can = can_move(cb, p, from_row, from_col, to_row, to_col);
if (!can) {
return INVALID;
} else {
// FIXME: check if move is allowed in check state
// try to move, and revert if invalid due to check in progress
enum pieces tmp = cb_move(cb, from_row, from_col, to_row, to_col);
if (player_checks(cb, p == WHITE ? BLACK : WHITE) && check_before) {
DEBUG("move: move does not end check state, invalid");