From 82851b83ff9c0b8dd0251636814995c8603a381a Mon Sep 17 00:00:00 2001 From: eug-vs Date: Fri, 11 Jun 2021 17:21:35 +0300 Subject: refactor: store move in array --- src/main.c | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/main.c b/src/main.c index 5c8e7de..69f0b43 100644 --- a/src/main.c +++ b/src/main.c @@ -91,18 +91,14 @@ void index_to_notation(int index, char notation[2]) { notation[1] = (index >> 4) + '1'; } -int positions_to_move(int origin, int destination) { - return (origin << 8) + destination; -}; - -void print_move(int move) { +void print_move(int move[2]) { char move_in_notation[] = "xy XY"; - index_to_notation(move >> 8, move_in_notation); - index_to_notation(move & DESTINATION_MASK, move_in_notation + 3); + index_to_notation(move[0], move_in_notation); + index_to_notation(move[1], move_in_notation + 3); printf("%s\n", move_in_notation); }; -int input_move() { +void input_move(int move[2]) { char file; int rank; char file_destination; @@ -110,18 +106,19 @@ int input_move() { scanf(" %c%i %c%i", &file, &rank, &file_destination, &rank_destination); - int pos = notation_to_index(file, rank); - int pos_destination = notation_to_index(file_destination, rank_destination); - - return positions_to_move(pos, pos_destination); + move[0] = notation_to_index(file, rank); + move[1] = notation_to_index(file_destination, rank_destination); }; -void apply_move(int move, int* board) { - int origin = move >> 8; - int destination = move & DESTINATION_MASK; +int apply_move(int move[2], int* board) { + int origin = move[0]; + int destination = move[1]; + + if ((origin & 0x88) || (destination & 0x88)) return 0; board[destination] = board[origin]; board[origin] = EMPTY; + return board[destination]; } int main() { @@ -129,11 +126,11 @@ int main() { parse_FEN(DEFAULT_FEN, board); print_board(board); - int move; + int move[2]; while (1) { - move = input_move(); - apply_move(move, board); - print_board(board); + input_move(move); + if (apply_move(move, board)) print_board(board); + else puts("Invalid move!"); } return 0; -- cgit v1.2.3