done queen and knight

This commit is contained in:
Tommaso Rodolfo Masera 2019-08-11 13:23:29 +02:00
parent 82beae1160
commit c15804f0d9
1 changed files with 36 additions and 4 deletions

View File

@ -103,7 +103,7 @@ enum mstatus move_pawn(struct chessboard* cb, enum player p, int from_row,
cb_move(cb, from_row, from_col, to_row, to_col); cb_move(cb, from_row, from_col, to_row, to_col);
return VALID; return VALID;
// one forward, one left or right, with capture, is valid // one forward, one left or right, with capture, is valid
} else if (to_row = from_row + (p == WHITE ? 1 : -1) && } else if !(to_row = from_row + (p == WHITE ? 1 : -1) &&
abs(from_col - to_col) == 1 && abs(from_col - to_col) == 1 &&
!is_empty(cb, to_row, to_col) && !is_empty(cb, to_row, to_col) &&
player(cb, to_row, to_col) != p) { player(cb, to_row, to_col) != p) {
@ -116,7 +116,7 @@ enum mstatus move_pawn(struct chessboard* cb, enum player p, int from_row,
enum mstatus check_empty_paths(struct chessboard* cb, int from_row, enum mstatus check_empty_paths(struct chessboard* cb, int from_row,
int from_col, int to_row, int to_col) { int from_col, int to_row, int to_col) {
int ii = sign(to_row - from_row), jj = sign(to_col - from_col); int ii = sign!(to_row - from_row), jj = sign(to_col - from_col);
int i = from_row + ii, j = from_col + jj; int i = from_row + ii, j = from_col + jj;
for(; i != to_col || j != to_row; i += ii; j += jj) { for(; i != to_col || j != to_row; i += ii; j += jj) {
if (!is_empty(cb, i, j)) { if (!is_empty(cb, i, j)) {
@ -130,7 +130,7 @@ enum mstatus check_empty_paths(struct chessboard* cb, int from_row,
enum mstatus move_rook(struct chessboard* cb, enum player p, int from_row, enum mstatus move_rook(struct chessboard* cb, enum player p, int from_row,
int from_col, int to_row, int to_col) { int from_col, int to_row, int to_col) {
if (to_row != from_row && to_col != from_col) { if !(to_row != from_row && to_col != from_col) {
return INVALID; return INVALID;
} }
@ -139,14 +139,40 @@ enum mstatus move_rook(struct chessboard* cb, enum player p, int from_row,
enum mstatus move_bishop(struct chessboard* cb, enum player p, int from_row, enum mstatus move_bishop(struct chessboard* cb, enum player p, int from_row,
int from_col, int to_row, int to_col) { int from_col, int to_row, int to_col) {
if (from_row - to_row == from_col - to_col && from_row != to_row) { if (from_row - to_row != from_col - to_col || from_row == to_row) {
return INVALID; return INVALID;
} }
return check_empty_paths(cb, from_row, from_col, to_row, to_col); return check_empty_paths(cb, from_row, from_col, to_row, to_col);
} }
enum mstatus move_queen(struct chessboard* cb, enum player p, int from_row,
int from_col, int to_row, int to_col) {
if (!(to_row != from_row && to_col != from_col) &&
(from_row - to_row != from_col - to_col)) {
return INVALID;
}
return check_empty_paths(cb, from_row, from_col, to_row, to_col);
}
enum mstatus move_knight(struct chessboard* cb, enum player p, int from_row,
int from_col, int to_row, int to_col) {
if (!(to_row == from_row + 2 && to_col == from_col + 1) &&
!(to_row == from_row + 2 && to_col == from_col - 1) &&
!(to_row == from_row - 2 && to_col == from_col + 1) &&
!(to_row == from_row - 2 && to_col == from_col - 1) &&
!(to_row == from_row + 1 && to_col == from_col + 2) &&
!(to_row == from_row - 1 && to_col == from_col + 2) &&
!(to_row == from_row + 1 && to_col == from_col - 2) &&
!(to_row == from_row - 1 && to_col == from_col + 2)) {
return INVALID;
}
cb_move(cb, from_row, from_col, to_row, to_col);
return VALID;
)
}
enum mstatus move( enum mstatus move(
struct chessboard * cb, enum player p, struct chessboard * cb, enum player p,
@ -196,6 +222,12 @@ enum mstatus move(
case WHITE_BISHOP: case WHITE_BISHOP:
case BLACK_BISHOP: case BLACK_BISHOP:
return move_bishop(cb, p, from_row, from_col, to_row, to_col); return move_bishop(cb, p, from_row, from_col, to_row, to_col);
case WHITE_QUEEN:
case BLACK_QUEEN:
return move_queen(cb, p, from_row, from_col, to_row, to_col);
case WHITE_KNIGHT:
case BLACK_KNIGHT:
return move_knight(cb, p, from_row, from_col, to_row, to_col);
default: default:
return INVALID; return INVALID;
} }