diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 49 | ||||
-rw-r--r-- | src/pieces.h | 6 |
2 files changed, 55 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; } diff --git a/src/pieces.h b/src/pieces.h index 10d4eec..f8e50e7 100644 --- a/src/pieces.h +++ b/src/pieces.h @@ -22,3 +22,9 @@ char* pieces[] = { "♚", "♔", }; + +#define RANK_MASK 0b111000 +#define FILE_MASK 0b000111 + +#define ORIGIN_MASK 0b111111000000 +#define DESTINATION_MASK 0b000000111111 |