done rook and bishop
This commit is contained in:
parent
9d3e28b9f0
commit
82beae1160
1 changed files with 54 additions and 5 deletions
|
@ -65,13 +65,13 @@ void print_chessboard(struct chessboard * cb) {
|
|||
|
||||
}
|
||||
|
||||
void cb_move(struct chessboard* cb, int from_row, int from_col, int to_row,
|
||||
inline void cb_move(struct chessboard* cb, int from_row, int from_col, int to_row,
|
||||
int to_col) {
|
||||
cb->position[to_row][to_col] = cb->position[from_row][from_col];
|
||||
cb->position[from_row][from_col] = EMPTY;
|
||||
}
|
||||
|
||||
bool is_empty(struct chessboard* cb, int row, int col) {
|
||||
inline bool is_empty(struct chessboard* cb, int row, int col) {
|
||||
return cb->position[row][col] == EMPTY;
|
||||
}
|
||||
|
||||
|
@ -79,6 +79,11 @@ inline enum player player(struct chessboard* cb, int row, int col) {
|
|||
return cb->position[row][col] < 7 ? WHITE : BLACK;
|
||||
}
|
||||
|
||||
|
||||
inline int sign(int n) {
|
||||
return n == 0 ? 0 : (n > 0 ? 1 : -1);
|
||||
}
|
||||
|
||||
enum mstatus move_pawn(struct chessboard* cb, enum player p, int from_row,
|
||||
int from_col, int to_row, int to_col) {
|
||||
// two forward if in the starting position, no capture is valid
|
||||
|
@ -109,6 +114,40 @@ 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 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)) {
|
||||
return INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
cb_move(cb, from_row, from_col, to_row, to_col);
|
||||
return VALID;
|
||||
}
|
||||
|
||||
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) {
|
||||
return INVALID;
|
||||
}
|
||||
|
||||
return check_empty_paths(cb, from_row, from_col, to_row, to_col);
|
||||
}
|
||||
|
||||
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) {
|
||||
return INVALID;
|
||||
}
|
||||
|
||||
return check_empty_paths(cb, from_row, from_col, to_row, to_col);
|
||||
}
|
||||
|
||||
|
||||
|
||||
enum mstatus move(
|
||||
struct chessboard * cb, enum player p,
|
||||
const char * from, const char * to)
|
||||
|
@ -124,12 +163,12 @@ enum mstatus move(
|
|||
int from_col = from[1] - '1', to_col = to[0] - '1';
|
||||
|
||||
// if you are moving thin air, EH VOLEVIH
|
||||
if (cb->position[from_row][from_col] == EMPTY) {
|
||||
if (is_empty(cb, from_row, from_col)) {
|
||||
return INVALID;
|
||||
}
|
||||
|
||||
// if you are moving someone else's pieces, "thou shall not steal"
|
||||
if (cb->position[from_row][from_col] / 7 != p) {
|
||||
if (player(cb, from_row, from_col) != p) {
|
||||
return INVALID;
|
||||
}
|
||||
|
||||
|
@ -141,13 +180,23 @@ enum mstatus move(
|
|||
return INVALID;
|
||||
}
|
||||
|
||||
// if you try to move on the same spot, don't do it you buffoon
|
||||
if (from_row == to_row && from_col == to_col) {
|
||||
return INVALID;
|
||||
}
|
||||
|
||||
// try to understand which piece we are moving
|
||||
switch (cb->position[from_row][from_col]) {
|
||||
case WHITE_PAWN:
|
||||
case BLACK_PAWN:
|
||||
return move_pawn(cb, p, from_row, from_col, to_row, to_col);
|
||||
case WHITE_ROOK:
|
||||
case BLACK_ROOK:
|
||||
return move_rook(cb, p, from_row, from_col, to_row, to_col);
|
||||
case WHITE_BISHOP:
|
||||
case BLACK_BISHOP:
|
||||
return move_bishop(cb, p, from_row, from_col, to_row, to_col);
|
||||
default:
|
||||
return INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue