diff options
author | eug-vs <eug-vs@keemail.me> | 2021-06-10 15:14:36 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2021-06-10 15:14:49 +0300 |
commit | a86664a49d3e148bd8d636c177d9f65bdb2c8dbd (patch) | |
tree | 8a6103b6bbcddab22bffabf7721035ac16e253e1 /src/main.c | |
parent | 3d356b409005ffeadfc8493dadfec5dc8cd4f4d4 (diff) | |
download | c-chess-a86664a49d3e148bd8d636c177d9f65bdb2c8dbd.tar.gz |
feat: read and apply user moves
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 49 |
1 files changed, 49 insertions, 0 deletions
@@ -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; } |