From a86664a49d3e148bd8d636c177d9f65bdb2c8dbd Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 10 Jun 2021 15:14:36 +0300 Subject: feat: read and apply user moves --- src/main.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 62b4692..931d1df 100644 --- a/src/main.c +++ b/src/main.c @@ -82,10 +82,59 @@ void parse_FEN(char* FEN, int board[BOARD_SIZE][BOARD_SIZE]) { } } +int notation_to_position(int file, int rank) { + return ((rank - 1) << 3) + (file - 'a'); +} + +void position_to_notation(int position, char notation[2]) { + notation[0] = (position & FILE_MASK) + 'a'; + notation[1] = (position >> 3) + '1'; +} + +int positions_to_move(int origin, int destination) { + return (origin << 6) + destination; +}; + +void print_move(int move) { + char move_in_notation[5] = "xy XY"; + position_to_notation(move >> 6, move_in_notation); + position_to_notation(move & DESTINATION_MASK, move_in_notation + 3); + printf("%s\n", move_in_notation); +}; + +int input_move() { + char file; + int rank; + char file_destination; + int rank_destination; + + scanf(" %c%i %c%i", &file, &rank, &file_destination, &rank_destination); + + int pos = notation_to_position(file, rank); + int pos_destination = notation_to_position(file_destination, rank_destination); + + return positions_to_move(pos, pos_destination); +}; + +void apply_move(int move, int* board) { + int origin = move >> 6; + int destination = move & DESTINATION_MASK; + + board[destination] = board[origin]; + board[origin] = EMPTY; +} + int main() { int board[BOARD_SIZE][BOARD_SIZE]; parse_FEN(DEFAULT_FEN, board); print_board(board); + int move; + while (1) { + move = input_move(); + apply_move(move, (int *)board); + print_board(board); + } + return 0; } -- cgit v1.2.3