diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2022-12-01 23:40:53 -0800 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2022-12-01 23:40:53 -0800 |
| commit | fcf95bcc01558d88f93152166ac9e2cbc700444b (patch) | |
| tree | 98d4a09cec78def78396f2796be8d7f41bc612cd /chess.c | |
| parent | b0928a2d8072f30849aac3a9e684410b434b4b3f (diff) | |
can't move other player's pieces, can't make invalid moves
Diffstat (limited to 'chess.c')
| -rw-r--r-- | chess.c | 20 |
1 files changed, 18 insertions, 2 deletions
@@ -185,13 +185,16 @@ int main() { }; int turn = 0; while(1) { + //draw the board, write some info clear_screen(board); draw_board(board); printf(turn == 0 ? "\nWhite's turn\n" : "\nBlack's turn\n"); printf("Enter the coordinates of the piece you want to move\n"); + //get the selection for the piece to be moved vector_t selection = user_input_pos(); int selectedPiece = board[selection.mx][selection.my]; - if(selectedPiece == 0) { + //check if the piece is of the right colour, and that there is a piece there + if(selectedPiece == 0 || (selectedPiece > 6 && turn == 0) || (selectedPiece <= 6 && turn == 1)) { warn_user("Invalid selection!"); continue; } @@ -204,6 +207,7 @@ int main() { continue; } printf("Valid moves: "); + //write all the valid moves for(int i = 0; i < moves.length; i++) { char* readable_coords = get_readable_coords(moves.vectors[i]); printf(readable_coords); @@ -211,11 +215,23 @@ int main() { printf("; "); } printf("\nEnter the coordinates you would like to move that piece to, or enter < to go back\n"); + //get the selection for the location to move the piece to vector_t target = user_input_pos(); if(target.mx == -1) { continue; } - //TODO: check that target is in list of valid moves + //check if target is in list of valid moves + int match = 0; + for(int i = 0; i < moves.length; i++) { + if(moves.vectors[i].mx == target.mx && moves.vectors[i].my == target.my) { + match = 1; + break; + } + } + if(!match) { + warn_user("Invalid move"); + continue; + } board[selection.mx][selection.my] = 0; board[target.mx][target.my] = selectedPiece; printf("\n"); |
