diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2022-12-01 23:15:15 -0800 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2022-12-01 23:15:15 -0800 |
| commit | 7e63656b6159cd991e287c925b6a4ba6b055eaa0 (patch) | |
| tree | 73bc6fbc387b0690bee1bd841693f0b1b9971922 /chess.c | |
| parent | 6dce989df638af374a0e3bb30978e3875f67cbd0 (diff) | |
interface loop mostly done, added some helper methods
Diffstat (limited to 'chess.c')
| -rw-r--r-- | chess.c | 131 |
1 files changed, 126 insertions, 5 deletions
@@ -1,13 +1,33 @@ #include <stdio.h> #include <stdlib.h> -enum piece { +typedef enum { pawn = 1, knight = 2, bishop = 3, rook = 4, queen = 5, - king = 6 -}; + king = 6, +} piece_t; +typedef struct Vector { + int mx; + int my; +} vector_t; +vector_t init_vector(int mx, int my) { + vector_t vector; + vector.mx = mx; + vector.my = my; + return vector; +} +typedef struct VectorP { + vector_t* vectors; + int length; +} vectorp_t; +vectorp_t init_vectorp(vector_t* vectors, int length) { + vectorp_t vectorP; + vectorP.vectors = vectors; + vectorP.length = length; + return vectorP; +} void draw_board(int board[8][8]) { printf(" ABCDEFGH\n"); for(int r = 0; r < 8; r++) { @@ -71,6 +91,76 @@ void draw_board(int board[8][8]) { free(line); } } +int in_board(vector_t position) { + return position.mx < 8 && position.mx >= 0 && position.my < 8 && position.my >= 0; +} +int is_empty(int board[8][8], vector_t position) { + return board[position.mx][position.my] == 0; +} +vectorp_t get_moves(int board[8][8], vector_t position, piece_t piece) { + vector_t* moves = malloc(27 * sizeof(vector_t)); + int moveCount = 0; + if(piece == pawn) { + vector_t move1 = init_vector(position.mx, position.my - 1); + if(in_board(move1) && is_empty(board, move1)) { + moves[0] = move1; + moveCount++; + if(position.my == 6) { + vector_t move2 = init_vector(position.mx, position.my - 2); + if(is_empty(board, move2)) { + moves[1] = move2; + moveCount++; + } + } + } + return init_vectorp(moves, moveCount); + } + else if(piece == pawn + 6) { + vector_t move1 = init_vector(position.mx, position.my + 1); + if(in_board(move1) && is_empty(board, move1)) { + moves[0] = move1; + moveCount++; + if(position.my == 1) { + vector_t move2 = init_vector(position.mx, position.my + 2); + if(is_empty(board, move2)) { + moves[1] = move2; + moveCount++; + } + } + } + return init_vectorp(moves, moveCount); + } +} +vector_t user_input_pos() { + char* inputString = malloc(3 * sizeof(char)); + vector_t pos; + printf("Enter coordinates: "); + scanf("%2s", inputString); + for(int i = 0; i < 2; i++) { + if(inputString[i] <= '9' && inputString[i] >= '0') { + pos.my = 8 - (inputString[i] - '0'); + } + else if(inputString[i] <= 'Z' && inputString[i] >= 'A') { + pos.mx = inputString[i] - 'A'; + } + else if(inputString[i] <= 'z' && inputString[i] >= 'a') { + pos.mx = inputString[i] - 'a'; + } + else { + return init_vector(-1, -1); + } + } + free(inputString); + return pos; +} +//converts board coordinates into algebraic notation +char* get_readable_coords(vector_t position) { + char* output = malloc(3 * sizeof(char)); + output[0] = 'A' + position.mx; + output[1] = '8' - position.my; + output[2] = '\0'; + return output; +} void clear_screen() { system("@cls||clear"); } @@ -85,7 +175,38 @@ int main() { { knight + 6, pawn + 6, 0, 0, 0, 0, pawn, knight }, { rook + 6, pawn + 6, 0, 0, 0, 0, pawn, rook } }; - clear_screen(board); - draw_board(board); + while(1) { + clear_screen(board); + draw_board(board); + printf("Enter the coordinates of the piece you want to move\n"); + vector_t selection = user_input_pos(); + int selectedPiece = board[selection.mx][selection.my]; + if(selectedPiece == 0) { + continue; + } + printf("Selected piece: "); + printf("%d", selectedPiece); + printf("\n"); + vectorp_t moves = get_moves(board, selection, selectedPiece); + if(moves.length == 0) { + continue; + } + printf("Valid moves: "); + for(int i = 0; i < moves.length; i++) { + char* readable_coords = get_readable_coords(moves.vectors[i]); + printf(readable_coords); + free(readable_coords); + printf("; "); + } + printf("\nEnter the coordinates you would like to move that piece to, or enter < to go back\n"); + vector_t target = user_input_pos(); + if(target.mx == -1) { + continue; + } + //TODO: check that target is in list of valid moves + board[selection.mx][selection.my] = 0; + board[target.mx][target.my] = selectedPiece; + printf("\n"); + } return 0; } |
