From c15804f0d9735ab36af0b9bb9174a1eafddbb094 Mon Sep 17 00:00:00 2001 From: tommi27 Date: Sun, 11 Aug 2019 13:23:29 +0200 Subject: [PATCH] done queen and knight --- assignment1-18/chessboard.c | 40 +++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/assignment1-18/chessboard.c b/assignment1-18/chessboard.c index 71a5175..661ebc8 100644 --- a/assignment1-18/chessboard.c +++ b/assignment1-18/chessboard.c @@ -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); return 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 && !is_empty(cb, to_row, to_col) && 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, 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; for(; i != to_col || j != to_row; i += ii; j += jj) { 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, 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; } @@ -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, 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 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( struct chessboard * cb, enum player p, @@ -196,6 +222,12 @@ enum mstatus move( case WHITE_BISHOP: case BLACK_BISHOP: 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: return INVALID; }