From 357e22fd383c71ef5daec219d9270ab748837416 Mon Sep 17 00:00:00 2001 From: Odweta Date: Wed, 18 Feb 2026 15:11:50 +0100 Subject: [PATCH] probably made the ROOK, BISHOP and QUEEN moves work --- src/chess.c | 58 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/src/chess.c b/src/chess.c index c769c89..5568dc4 100644 --- a/src/chess.c +++ b/src/chess.c @@ -75,23 +75,59 @@ bool compare_pos(pos_t a, pos_t b) { return a.x == b.x && a.y == b.y; } +pos_t pos_multiply_scalar(pos_t a, int b) { + return (pos_t){a.x*b, a.y*b}; +} + /** * check the wanted pos_t against all valid move options * of the current piece */ bool is_move_legal(move_t move) { valid_move_t valid_moves[PIECE_COUNT] = DEFAULT_VALID_MOVES; - 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++ - ) { - pos_t new_pos = {move.from.x+move.to.x, move.from.y+move.to.y}; - if (compare_pos(new_pos, valid_moves[i].options[j]) == true) { - return true; + switch (move.piece) { + case NONE: + case PAWN: + case KNIGHT: + case KING: + 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++ + ) { + pos_t new_pos = {move.from.x+move.to.x, move.from.y+move.to.y}; + if (compare_pos(new_pos, valid_moves[i].options[j]) == true) { + return true; + } + } } - } + break; + case ROOK: + case BISHOP: + case QUEEN: + 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++) { + pos_t new_pos = { + move.from.x+move.to.x, + move.from.y+move.to.y + }; + if (compare_pos( + new_pos, + pos_multiply_scalar(valid_moves[i].options[j], k) + ) == true) { + return true; + } + } + } + } + break; } return false; @@ -129,7 +165,7 @@ bool perform_move(board_t *board, player_e player, move_t move) { } end_condition_e check_edge_cases(board_t board) { - + } void announce_results(end_condition_e end_condition, player_e player) {