diff options
-rw-r--r-- | src/main.c | 66 | ||||
-rw-r--r-- | src/pieces.h | 6 |
2 files changed, 35 insertions, 37 deletions
@@ -6,11 +6,11 @@ #include "pieces.h" -void print_board(int board[BOARD_SIZE][BOARD_SIZE]) { - for (int rank = BOARD_SIZE - 1; rank >= 0; rank--) { +void print_board(int board[128]) { + for (int rank = 7; rank >= 0; rank--) { printf("%i|", rank + 1); - for (int file = 0; file < BOARD_SIZE; file++) { - int piece = board[rank][file]; + for (int file = 0; file < 8; file++) { + int piece = board[rank * 16 + file]; printf("%s ", pieces[piece]); } printf("\n"); @@ -18,7 +18,7 @@ void print_board(int board[BOARD_SIZE][BOARD_SIZE]) { printf(" a b c d e f g h\n"); } -void parse_FEN(char* FEN, int board[BOARD_SIZE][BOARD_SIZE]) { +void parse_FEN(char* FEN, int board[128]) { // TODO: parse everything, not position only int rank = 7; int file = 0; @@ -28,45 +28,45 @@ void parse_FEN(char* FEN, int board[BOARD_SIZE][BOARD_SIZE]) { if (FEN[k] > '0' && FEN[k] <= '8') { int last_file = file + FEN[k] - '0'; for (file; file < last_file; file++) { - board[rank][file] = EMPTY; + board[rank * 16 + file] = EMPTY; } } else { switch (FEN[k]) { case 'r': - board[rank][file] = BLACK | ROOK; + board[rank * 16 + file] = BLACK | ROOK; break; case 'R': - board[rank][file] = ROOK; + board[rank * 16 + file] = ROOK; break; case 'n': - board[rank][file] = BLACK | KNIGHT; + board[rank * 16 + file] = BLACK | KNIGHT; break; case 'N': - board[rank][file] = KNIGHT; + board[rank * 16 + file] = KNIGHT; break; case 'b': - board[rank][file] = BLACK | BISHOP; + board[rank * 16 + file] = BLACK | BISHOP; break; case 'B': - board[rank][file] = BISHOP; + board[rank * 16 + file] = BISHOP; break; case 'q': - board[rank][file] = BLACK | QUEEN; + board[rank * 16 + file] = BLACK | QUEEN; break; case 'Q': - board[rank][file] = QUEEN; + board[rank * 16 + file] = QUEEN; break; case 'k': - board[rank][file] = BLACK | KING; + board[rank * 16 + file] = BLACK | KING; break; case 'K': - board[rank][file] = KING; + board[rank * 16 + file] = KING; break; case 'p': - board[rank][file] = BLACK | PAWN; + board[rank * 16 + file] = BLACK | PAWN; break; case 'P': - board[rank][file] = PAWN; + board[rank * 16 + file] = PAWN; break; case '/': rank--; @@ -75,30 +75,30 @@ void parse_FEN(char* FEN, int board[BOARD_SIZE][BOARD_SIZE]) { case ' ': return; default: - board[rank][file] = KNIGHT; + board[rank * 16 + file] = KNIGHT; } file++; } } } -int notation_to_position(int file, int rank) { - return ((rank - 1) << 3) + (file - 'a'); +int notation_to_index(int file, int rank) { + return (rank - 1) * 16 + (file - 'a'); } -void position_to_notation(int position, char notation[2]) { - notation[0] = (position & FILE_MASK) + 'a'; - notation[1] = (position >> 3) + '1'; +void index_to_notation(int index, char notation[2]) { + notation[0] = (index & FILE_MASK) + 'a'; + notation[1] = (index >> 4) + '1'; } int positions_to_move(int origin, int destination) { - return (origin << 6) + destination; + return (origin << 8) + 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); + char move_in_notation[] = "xy XY"; + index_to_notation(move >> 8, move_in_notation); + index_to_notation(move & DESTINATION_MASK, move_in_notation + 3); printf("%s\n", move_in_notation); }; @@ -110,14 +110,14 @@ int input_move() { 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); + int pos = notation_to_index(file, rank); + int pos_destination = notation_to_index(file_destination, rank_destination); return positions_to_move(pos, pos_destination); }; void apply_move(int move, int* board) { - int origin = move >> 6; + int origin = move >> 8; int destination = move & DESTINATION_MASK; board[destination] = board[origin]; @@ -125,14 +125,14 @@ void apply_move(int move, int* board) { } int main() { - int board[BOARD_SIZE][BOARD_SIZE]; + int board[128]; parse_FEN(DEFAULT_FEN, board); print_board(board); int move; while (1) { move = input_move(); - apply_move(move, (int *)board); + apply_move(move, board); print_board(board); } diff --git a/src/pieces.h b/src/pieces.h index f8e50e7..2d5a02a 100644 --- a/src/pieces.h +++ b/src/pieces.h @@ -23,8 +23,6 @@ char* pieces[] = { }; -#define RANK_MASK 0b111000 -#define FILE_MASK 0b000111 +#define FILE_MASK 0b1111 -#define ORIGIN_MASK 0b111111000000 -#define DESTINATION_MASK 0b000000111111 +#define DESTINATION_MASK 0b1111111 |