// vim: set ts=4 sw=4 et tw=80: #include #include "./chessboard.h" #include #include #include void init_chessboard(struct chessboard * cb) { int i; for (i = 0; i < 8; i++) { cb->position[1][i] = WHITE_PAWN; cb->position[6][i] = BLACK_PAWN; } cb->position[0][0] = WHITE_ROOK; cb->position[0][1] = WHITE_KNIGHT; cb->position[0][2] = WHITE_BISHOP; cb->position[0][3] = WHITE_QUEEN; cb->position[0][4] = WHITE_KING; cb->position[0][5] = WHITE_BISHOP; cb->position[0][6] = WHITE_KNIGHT; cb->position[0][7] = WHITE_ROOK; cb->position[7][0] = BLACK_ROOK; cb->position[7][1] = BLACK_KNIGHT; cb->position[7][2] = BLACK_BISHOP; cb->position[7][3] = BLACK_QUEEN; cb->position[7][4] = BLACK_KING; cb->position[7][5] = BLACK_BISHOP; cb->position[7][6] = BLACK_KNIGHT; cb->position[7][7] = BLACK_ROOK; int j; for (i = 0; i < 8; i++) { for (j = 2; j < 6; j++) { cb->position[j][i] = EMPTY; } } } void print_chessboard(struct chessboard * cb) { printf(" a b c d e f g h\n" " ┌───┬───┬───┬───┬───┬───┬───┬───┐\n"); int i; for (i = 7; i >= 0; i--) { printf("%d │", i + 1); int j; for (j = 0; j < 8; j++) { printf(" %s │", utf_8_pieces[cb->position[i][j]]); } putchar('\n'); if (i != 0) { printf(" ├───┼───┼───┼───┼───┼───┼───┼───┤\n"); } } printf(" └───┴───┴───┴───┴───┴───┴───┴───┘\n" " a b c d e f g h\n"); } 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; } inline bool is_empty(struct chessboard* cb, int row, int col) { return cb->position[row][col] == EMPTY; } 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 if (((p == WHITE && from_row == 1 && to_row == 3 && is_empty(cb, 2, from_col)) || (p == BLACK && from_row == 7 && to_row == 5 && is_empty(cb, 6, from_col))) && from_col == to_col && is_empty(cb, to_row, to_col)) { cb_move(cb, from_row, from_col, to_row, to_col); return VALID; // one forward, no capture is valid } else if (((p == WHITE && to_row - from_row == 1) || (p == BLACK && from_row - to_row == 1)) && from_col == to_col && is_empty(cb, to_row, to_col)) { 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) && abs(from_col - to_col) == 1 && !is_empty(cb, to_row, to_col) && player(cb, to_row, to_col) != p) { cb_move(cb, from_row, from_col, to_row, to_col); return VALID; } else { return INVALID; } } 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) { // if you are giving us garbage, we behave like Rome's municipal services if (strlen(from) != 2 || strlen(to) != 2 || from[0] < 'a' || from[0] > 'h' || to[0] < 'a' || to[0] > 'h' || from[1] < '1' || from[1] > '8' || to[1] < '1' || to[1] > '8') { return INVALID; } int from_row = from[0] - 'a', to_row = to[0] - 'a'; int from_col = from[1] - '1', to_col = to[0] - '1'; // if you are moving thin air, EH VOLEVIH if (is_empty(cb, from_row, from_col)) { return INVALID; } // if you are moving someone else's pieces, "thou shall not steal" if (player(cb, from_row, from_col) != p) { return INVALID; } // if you are trying to suicide (capture your piecies), sorry but assisted // suicide is not legal in italy if (cb->position[to_row][to_col] != EMPTY && cb->position[from_row][from_col] / 7 == cb->position[to_row][to_col] / 7) { 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; } }