Cleaned chess binary and added comments
This commit is contained in:
parent
7ad853f0a3
commit
ad459c3b85
2 changed files with 12 additions and 1 deletions
Binary file not shown.
|
@ -298,6 +298,7 @@ bool player_checks(struct chessboard *cb, enum player p) {
|
||||||
|
|
||||||
bool player_checkmates(struct chessboard *cb, enum player p) {
|
bool player_checkmates(struct chessboard *cb, enum player p) {
|
||||||
int i, j;
|
int i, j;
|
||||||
|
// for every opponent's piece
|
||||||
for (i = 0; i < 8; i++) {
|
for (i = 0; i < 8; i++) {
|
||||||
for (j = 0; j < 8; j++) {
|
for (j = 0; j < 8; j++) {
|
||||||
if (is_empty(cb, i, j) || player(cb, i, j) == p) {
|
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;
|
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 (k = 0; k < 8; k++) {
|
||||||
for (l = 0; l < 8; l++) {
|
for (l = 0; l < 8; l++) {
|
||||||
if ((k == i && l == j) ||
|
if ((k == i && l == j) ||
|
||||||
|
@ -313,16 +317,20 @@ bool player_checkmates(struct chessboard *cb, enum player p) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if the move is valid
|
||||||
bool can = can_move(cb, p == BLACK ? WHITE : BLACK,
|
bool can = can_move(cb, p == BLACK ? WHITE : BLACK,
|
||||||
i, j, k, l);
|
i, j, k, l);
|
||||||
|
// if not, carry on
|
||||||
if (!can) {
|
if (!can) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if valid, perform the move and check for check.
|
||||||
enum pieces tmp = cb_move(cb, i, j, k, l);
|
enum pieces tmp = cb_move(cb, i, j, k, l);
|
||||||
bool check = player_checks(cb, p);
|
bool check = player_checks(cb, p);
|
||||||
cb_move_restore(cb, tmp, i, j, k, l);
|
cb_move_restore(cb, tmp, i, j, k, l);
|
||||||
|
|
||||||
|
// if check is no longer there, then this is not a checkmate
|
||||||
if (!check) {
|
if (!check) {
|
||||||
return false;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -380,13 +389,15 @@ enum mstatus move(
|
||||||
return INVALID;
|
return INVALID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if we are currently in check state
|
||||||
bool check_before = player_checks(cb, p == WHITE ? BLACK : WHITE);
|
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);
|
bool can = can_move(cb, p, from_row, from_col, to_row, to_col);
|
||||||
|
|
||||||
if (!can) {
|
if (!can) {
|
||||||
return INVALID;
|
return INVALID;
|
||||||
} else {
|
} 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);
|
enum pieces tmp = cb_move(cb, from_row, from_col, to_row, to_col);
|
||||||
if (player_checks(cb, p == WHITE ? BLACK : WHITE) && check_before) {
|
if (player_checks(cb, p == WHITE ? BLACK : WHITE) && check_before) {
|
||||||
DEBUG("move: move does not end check state, invalid");
|
DEBUG("move: move does not end check state, invalid");
|
||||||
|
|
Reference in a new issue