From 44c2ae5290b531170d8d4b1ab81635d2000f7643 Mon Sep 17 00:00:00 2001 From: Ole Morud Date: Sat, 25 Mar 2023 13:39:01 +0100 Subject: [PATCH] reformat and refactor a bit --- include/pieces.h | 2 +- include/util.h | 2 +- src/chess.c | 65 ++++++++++++++------------- src/common.c | 0 src/pieces.c | 115 ++++++++++++++++++++++++++--------------------- src/util.c | 38 +++++++++++----- 6 files changed, 125 insertions(+), 97 deletions(-) delete mode 100644 src/common.c diff --git a/include/pieces.h b/include/pieces.h index 2c3d295..7da6d80 100644 --- a/include/pieces.h +++ b/include/pieces.h @@ -7,6 +7,6 @@ bool move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to, - int player); + int player); #endif diff --git a/include/util.h b/include/util.h index 198d60e..a027e04 100644 --- a/include/util.h +++ b/include/util.h @@ -1,7 +1,7 @@ #include "common.h" bool tile_empty(tile_t t); -index_t abs_pos(index_t p); +index_t abs_index(index_t i); index_t column(index_t i); index_t row(index_t i); tile_t abs_tile(tile_t t); diff --git a/src/chess.c b/src/chess.c index 7f19281..f4eebd5 100644 --- a/src/chess.c +++ b/src/chess.c @@ -5,30 +5,32 @@ #include /* isalpha, isdigit ... */ #include /* setlocale */ +#include /* uint64_t */ #include /* printf, scanf */ #include /* memcpy */ -int do_turn(int turn_no, tile_t board[BOARD_SIZE]); +#define CLEAR_SCREEN() printf("\033[2J") + +int do_turn(int turn_no, tile_t board[BOARD_SIZE]); void init_board(tile_t board[BOARD_SIZE]); index_t input_to_index(char input[2]); int main() { - int turn = 0; - setlocale(LC_ALL, "C.UTF-8"); - tile_t board[8 * 8] = { 0 }; + tile_t board[BOARD_SIZE] = { 0 }; init_board(board); + int turn = 0; + while (true) { - printf("\033[2J"); // clear screen + CLEAR_SCREEN(); print_board(board); - // turn increments on valid inputs, - // otherwise refreshes screen - turn = do_turn(turn, board); + // turn increments on valid inputs, + turn += do_turn(turn, board); } return 0; @@ -59,7 +61,7 @@ void init_board(tile_t board[BOARD_SIZE]) * \param turn_no Turn number * \param board Pointer to list of tiles representing board state * - * \return turn+1 if successful, turn+0 if invalid input from player + * \return (int)1 if successful, (int)0 if invalid input from player * */ int do_turn(int turn_no, tile_t board[BOARD_SIZE]) { @@ -68,31 +70,31 @@ int do_turn(int turn_no, tile_t board[BOARD_SIZE]) int from = -1, to = -1; printf("\nPlayer %i, your turn to move", 1 + turn_no % 2); - printf("\nMove piece\nfrom: "); + printf("\nMove piece\nfrom: "); - scanf(" %2s", input); + scanf(" %2s", input); - from = input_to_index(input); + from = input_to_index(input); - if (from == -1) - return turn_no; + if (from == -1) + return 0; - printf("\nto: "); - scanf(" %2s", input); + printf("\nto: "); + scanf(" %2s", input); - to = input_to_index(input); + to = input_to_index(input); - if (to == -1) - return turn_no; + if (to == -1) + return 0; - if (! move_ok(board, from, to, turn_no % 2 ? BLACK : WHITE)) - return turn_no; + if (! move_ok(board, from, to, turn_no % 2 ? BLACK : WHITE)) + return 0; board[to] = board[from]; board[from] = E; - /* increment to make it next turn */ - return turn_no+1; + /* increment to make it next turn */ + return 1; } /** @@ -100,21 +102,22 @@ int do_turn(int turn_no, tile_t board[BOARD_SIZE]) * * \param input string of length 2 representing tile, e.g. "A3" * + * \return index value on valid input, -1 on invalid input + * * */ index_t input_to_index(char input[2]) { - int x = -1, y = -1, c; + int x = -1, y = -1; for (int i = 0; i < 2; i++) { - c = input[i]; + const char c = input[i]; - if (isalpha(c)) - c = toupper(input[0]); + const char v = isalpha(c) ? toupper(c) : c; - if ('A' <= c && c <= 'H') - x = c - 'A'; - else if ('1' <= c && c <= '8') - y = c - '1'; + if ('A' <= v && v <= 'H') + x = v - 'A'; + else if ('1' <= v && v <= '8') + y = v - '1'; } if (x != -1 && y != -1) diff --git a/src/common.c b/src/common.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/pieces.c b/src/pieces.c index 9ad1ae8..102b1b9 100644 --- a/src/pieces.c +++ b/src/pieces.c @@ -2,31 +2,27 @@ #include "common.h" #include "util.h" - bool -bishop_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to); - bool -cardinal_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to); - bool -diagonal_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to); - bool -king_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to); - bool knight_move_ok(index_t from, index_t to); - bool pawn_move_ok(const tile_t board[BOARD_SIZE], - index_t from, - index_t to, - int player); - bool -queen_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to); - bool -rook_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to); +bool bishop_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to); +bool cardinal_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to); +bool diagonal_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to); +bool king_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to); +bool knight_move_ok(index_t from, index_t to); +bool pawn_move_ok(const tile_t board[BOARD_SIZE], + index_t from, + index_t to, + int player); +bool queen_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to); +bool rook_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to); /** - * Returns true if move is a valid pawn move + * Check if move is a valid pawn move * - * \param board Array of tiles representing chess board state - * \param from Index of board piece starts at - * \param to Index of board piece wants to move to - * \param player Player to move + * \param board Array of tiles representing chess board state + * \param from Index of board piece starts at + * \param to Index of board piece wants to move to + * \param player Player to move + * + * \return true if move is valid, false otherwise */ bool pawn_move_ok(const tile_t board[BOARD_SIZE], index_t from, @@ -53,22 +49,24 @@ bool pawn_move_ok(const tile_t board[BOARD_SIZE], } /** - * Returns true if `to` is on a diagonal line of `from`, false otherwise + * Check if `to` is on a diagonal line of `from` * * \param board Array of tiles representing chess board state * \param from Index of board piece is at * \param to Index of board piece tries to move to + * + * \return true if \p to is on a diagonal line of \p from */ bool diagonal_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to) { const index_t col_diff = column(to) - column(from); const index_t row_diff = row(to) - row(from); - const index_t x_step = col_diff / abs_pos(col_diff); - const index_t y_step = ROW * row_diff / abs_pos(row_diff); + const index_t x_step = col_diff / abs_index(col_diff); + const index_t y_step = ROW * row_diff / abs_index(row_diff); const index_t step = x_step + y_step; - if (abs_pos(col_diff) != abs_pos(row_diff)) + if (abs_index(col_diff) != abs_index(row_diff)) return false; for (index_t p = from + step; p != to; p += step) @@ -79,23 +77,24 @@ bool diagonal_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to) } /** - * Returns true if index `to` is on a cardinal line of `from`, false otherwise + * Check if index `to` is on a cardinal line of `from` * * \param board Array of tiles representing chess board state * \param from Index of board piece is at * \param to Index of board piece tries to move to + * + * \return true if \p to and \p from share a column or row */ bool cardinal_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to) { const index_t col_diff = column(to) - column(from); - const index_t row_diff = row(to) - row(from); + const index_t row_diff = row(to - from); - /* cardinal moves means one direction has to be zero */ if (row_diff > 0 && col_diff > 0) return false; - index_t step = row_diff ? ROW * row_diff / abs_pos(row_diff) : - col_diff / abs_pos(col_diff); + index_t step = row_diff ? ROW * row_diff / abs_index(row_diff) : + col_diff / abs_index(col_diff); for (index_t p = from + step; p != to; p += step) if (! tile_empty(board[p])) @@ -105,11 +104,13 @@ bool cardinal_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to) } /** - * Returns true if move is a valid bishop move + * Check if move is a valid bishop move * - * \param board Array of tiles representing chess board state - * \param from Index of board piece is at - * \param to Index of board piece tries to move to + * \param board Array of tiles representing chess board state + * \param from Index of board piece is at + * \param to Index of board piece tries to move to + * + * \return true if move is valid, false otherwise */ bool bishop_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to) { @@ -117,11 +118,13 @@ bool bishop_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to) } /** - * Returns true if move is a valid rook move + * Check if move is a valid rook move * - * \param board Array of tiles representing chess board state - * \param from Index of board piece is at - * \param to Index of board piece tries to move to + * \param board Array of tiles representing chess board state + * \param from Index of board piece is at + * \param to Index of board piece tries to move to + * + * \return true if move is valid, false otherwise */ bool rook_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to) { @@ -129,30 +132,34 @@ bool rook_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to) } /** - * Returns true if move is a valid knight move + * Check if move is a valid knight move * - * \param from Index of board piece is at - * \param to Index of board piece tries to move to + * \param from Index of board piece is at + * \param to Index of board piece tries to move to + * + * \return true if move is valid, false otherwise */ bool knight_move_ok(index_t from, index_t to) { - const index_t c = abs_pos(column(to) - column(from)); - const index_t r = abs_pos(row(to - from)); + const index_t c = abs_index(column(to) - column(from)); + const index_t r = abs_index(row(to - from)); return (c == 1 && r == 2) || (c == 2 && r == 1); } /** - * Returns true if move is a valid king move + * Check if move is a valid king move * * \param board Array of tiles representing chess board state * \param from Index of board piece is at * \param to Index of board piece tries to move to + * + * \return true if move is valid, false otherwise */ bool king_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to) { - const index_t abs_col_diff = abs_pos(column(to) - column(from)); - const index_t abs_row_diff = abs_pos(row(to) - row(from)); + const index_t abs_col_diff = abs_index(column(to) - column(from)); + const index_t abs_row_diff = abs_index(row(to) - row(from)); (void)board; @@ -160,11 +167,13 @@ bool king_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to) } /** - * Returns true if move is a valid queen move + * Check if move is a valid queen move * - * \param board Array of tiles representing chess board state - * \param from Index of board piece is at - * \param to Index of board piece tries to move to + * \param board Array of tiles representing chess board state + * \param from Index of board piece is at + * \param to Index of board piece tries to move to + * + * \return true if move is valid, false otherwise */ bool queen_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to) { @@ -173,12 +182,14 @@ bool queen_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to) } /** - * Returns true if a move is valid, false otherwise + * Check if a move is valid * * \param board Pointer to list of tiles representing board state * \param from Tile to move piece from * \param to Tile to move piece to * \param player The current player to move + * + * \return true if move is valid, false otherwise * */ bool move_ok(const tile_t board[BOARD_SIZE], index_t from, diff --git a/src/util.c b/src/util.c index 3300c63..da66c03 100644 --- a/src/util.c +++ b/src/util.c @@ -2,22 +2,26 @@ #include "util.h" /** - * Returns the absolute value of an index_t value + * Calculate the absolute value of an index_t value * * \param p positive or negative index_t + * + * \return the absolute value of p */ -index_t abs_pos(index_t p) +index_t abs_index(index_t i) { - if (p < 0) - return -1 * p; + if (i < 0) + return -1 * i; - return p; + return i; } /** - * Returns the absolute value of a tile_t value + * Calculate the absolute value of a tile_t value * * \param t positive or negative tile_t + * + * \return the absolute value of t * */ tile_t abs_tile(tile_t t) { @@ -28,9 +32,11 @@ tile_t abs_tile(tile_t t) } /** - * Returns true if tile is empty, false otherwise + * Check if tile is has no piece on it * - * \param t tile to check if empty + * \param t tile to check if empty + * + * \return true if tile is empty, false otherwise * */ bool tile_empty(tile_t t) { @@ -38,9 +44,11 @@ bool tile_empty(tile_t t) } /** - * Returns row number of 1D board index + * Get row of index * * \param i index to get row number of + * + * \return row number of i * */ index_t row(index_t i) { @@ -48,9 +56,11 @@ index_t row(index_t i) } /** - * Returns column number of board index + * Get column of index * * \param i index to get column number of + * + * \return column number of i * */ index_t column(index_t i) { @@ -58,10 +68,12 @@ index_t column(index_t i) } /** - * Returns true if a and b are tiles of opposite color, false otherwise + * Check if two tiles have pieces of the opposite color * * \param a Tile to compare * \param b Tile to compare it with + * + * \return true if a and b are opposite colors, false otherwise * */ bool opposite_color(tile_t a, tile_t b) { @@ -69,10 +81,12 @@ bool opposite_color(tile_t a, tile_t b) } /** - * Returns true if a and b are pieces of the same color, false otherwise + * Check if two tiles have pieces of the same color * * \param a Tile to compare * \param b Tile to compare it with + * + * \return true if a and b are opposite colors, false otherwise * */ bool same_color(tile_t a, tile_t b) {