From 71e1f16fac8bc71e8e4ca7610ba81c4ab652a7be Mon Sep 17 00:00:00 2001 From: Odweta Date: Wed, 18 Feb 2026 23:45:06 +0100 Subject: [PATCH] GET MOVE WORKS --- src/chess.c | 60 +++++++++++++++++++++++++++-------------------------- src/chess.h | 2 +- src/main.c | 2 +- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/chess.c b/src/chess.c index 67a4ab0..85cc514 100644 --- a/src/chess.c +++ b/src/chess.c @@ -50,7 +50,7 @@ bool is_move_in_bounds(move_t move) { } } -bool get_move(move_t *move) { +bool get_move(board_t board, move_t *move) { char _x_from, _x_to; int _y_from, _y_to; printf("move >> "); @@ -67,6 +67,8 @@ bool get_move(move_t *move) { move->to.x = _x_to-96-1; move->to.y = _y_to-1; + move->piece = board[move->from.y][move->from.x]; + return is_move_in_bounds(*move); } @@ -78,6 +80,10 @@ pos_t pos_multiply_scalar(pos_t a, int b) { return (pos_t){a.x*b, a.y*b}; } +pos_t pos_add(pos_t a, pos_t b) { + return (pos_t){a.x + b.x, a.y + b.y}; +} + // check whether a position is out of bounds of the board bool is_pos_oob(pos_t pos) { return pos.x < 0 || pos.x > BOARD_HORIZONTAL || @@ -88,6 +94,7 @@ bool is_pos_oob(pos_t pos) { * check the wanted pos_t against all valid move options * of the current piece */ + /** TODO: add roundcount check for PAWN */ bool is_move_legal(move_t move) { pos_t new_pos = move.to; @@ -105,20 +112,16 @@ bool is_move_legal(move_t move) { case BPAWN: case BKNIGHT: case BKING: - for (int i = 0; i < PIECE_COUNT; i++) { - for (int j = 0; - j < valid_moves[i].piece_move_count && - move.piece == valid_moves[i].piece; - j++ + for (int j = 0; j < valid_moves[move.piece].piece_move_count; j++) { + pos_t candidate = pos_add(move.from, valid_moves[move.piece].options[j]); + printf("cadidate {%d,%d}\n", candidate.x, candidate.y); + if ( + compare_pos( + new_pos, + candidate + ) ) { - if ( - compare_pos( - new_pos, - valid_moves[i].options[j] - ) - ) { - return true; - } + return true; } } break; @@ -128,22 +131,21 @@ bool is_move_legal(move_t move) { case BROOK: case BBISHOP: case BQUEEN: - for (int i = 0; i < PIECE_COUNT; i++) { - for (int j = 0; - j < valid_moves[i].piece_move_count && - move.piece == valid_moves[i].piece; - j++ - ) { - /** NOTE: BOARD_HORIZONTAL assuming HORIZONTAL & VERTICAL are the same */ - for (int k = 0; k < BOARD_HORIZONTAL; k++) { - if ( - compare_pos( - new_pos, - pos_multiply_scalar(valid_moves[i].options[j], k) + for (int j = 0; j < valid_moves[move.piece].piece_move_count; j++) { + /** NOTE: BOARD_HORIZONTAL assuming HORIZONTAL & VERTICAL are the same */ + for (int k = 0; k < BOARD_HORIZONTAL; k++) { + if ( + compare_pos( + new_pos, + pos_multiply_scalar( + pos_add( + move.from, + valid_moves[move.piece].options[j] + ), k ) - ) { - return true; - } + ) + ) { + return true; } } } diff --git a/src/chess.h b/src/chess.h index 7dd59ca..402267f 100644 --- a/src/chess.h +++ b/src/chess.h @@ -176,7 +176,7 @@ void init_board(board_t board); void select_player(player_e *player, unsigned int round_count); // listens for a command to make a move by the currently playing player -bool get_move(move_t *move); +bool get_move(board_t board, move_t *move); // performs the move if is it possible end_condition_e perform_move(board_t *board, player_e player, captured_t *captured, move_t move); diff --git a/src/main.c b/src/main.c index 33a2379..b108a0b 100644 --- a/src/main.c +++ b/src/main.c @@ -40,7 +40,7 @@ int main() { show_board(board, player); - get_move(&move); + get_move(board, &move); end_condition = perform_move(&board, player, &captured, move); if (end_condition == CHECKMATE || end_condition == STALEMATE) {