From 5d830528cdadf31f7ae6e06b055fd482d69f1b86 Mon Sep 17 00:00:00 2001 From: cdricms <36056008+cdricms@users.noreply.github.com> Date: Tue, 26 Dec 2023 23:03:50 +0100 Subject: [PATCH] Queen moves --- src/board.c | 114 +++++++++++++++++++++++++++++++--------------------- src/main.c | 2 +- 2 files changed, 69 insertions(+), 47 deletions(-) diff --git a/src/board.c b/src/board.c index dfbf631..ce5b6dc 100644 --- a/src/board.c +++ b/src/board.c @@ -1,4 +1,12 @@ #include "board.h" +#include + +#define PAWN_MAXMOVES 4 +#define KNIGHT_MAXMOVES 8 +#define BISHOP_MAXMOVES 13 +#define ROOK_MAXMOVES 14 +#define QUEEN_MAXMOVES 27 +#define KING_MAXMOVES 8 PieceColors piece_getColor(const Pieces piece) { return Black & piece; } Piece piece_getColorlessPiece(const Pieces piece) { @@ -17,6 +25,45 @@ int board_getIndex(const Coordinates coords) { return (8 - (coords.row + 7) % 8) * 8 - (8 - (coords.column + 7) % 8); } +void piece_getDiagonalMoves(const Coordinates relative_coords, + Coordinates *moves, uint *moves_i) { + struct temp { + int x; + int y; + }; + struct temp temps[4] = {{-1, 1}, {1, 1}, {1, -1}, {-1, -1}}; + for (uint i = 0; i < 4; i++) { + Coordinates new_coordinates; + int index = 0; + uint mul = 1; + while (index != -1) { + new_coordinates.row = relative_coords.row + (temps[i].y * mul); + new_coordinates.column = relative_coords.column + (temps[i].x * mul); + mul++; + index = board_getIndex(new_coordinates); + if (index != -1) + moves[(*moves_i)++] = new_coordinates; + } + } +} + +void piece_getLinearMoves(const Coordinates relative_coords, Coordinates *moves, + uint *moves_i) { + for (uint i = 0; i < ROOK_MAXMOVES; i++) { + if (i + 1 != relative_coords.column) { + Coordinates horizontal_coordinates = {relative_coords.row, i + 1}; + if (board_getIndex(horizontal_coordinates) != -1) + moves[(*moves_i)++] = horizontal_coordinates; + } + if (i + 1 != relative_coords.row) { + Coordinates vertical_coordinates = {.row = i + 1, + .column = relative_coords.column}; + if (board_getIndex(vertical_coordinates) != -1) + moves[(*moves_i)++] = vertical_coordinates; + } + } +} + Coordinates *piece_getMoves(const Board board, const Pieces piece, const Coordinates piece_coords) { Piece colorless_piece = piece_getColorlessPiece(piece); @@ -24,10 +71,11 @@ Coordinates *piece_getMoves(const Board board, const Pieces piece, int board_index = board_getIndex(piece_coords); if (board_index == -1) return NULL; + switch (colorless_piece) { case Pawn: { - Coordinates *moves = malloc(3 * sizeof(Coordinates)); - uint moves_i; + Coordinates *moves = malloc(PAWN_MAXMOVES * sizeof(Coordinates)); + uint moves_i = 0; Coordinates new_coordinates = {.column = piece_coords.column, .row = piece_color == Black ? piece_coords.row - 1 @@ -46,15 +94,15 @@ Coordinates *piece_getMoves(const Board board, const Pieces piece, return moves; } case Knight: { - Coordinates *moves = malloc(8 * sizeof(Coordinates)); - uint moves_i; + Coordinates *moves = malloc(KNIGHT_MAXMOVES * sizeof(Coordinates)); + uint moves_i = 0; struct temp { int x; int y; }; struct temp temps[8] = {{-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}}; - for (uint i = 0; i < 8; i++) { + for (uint i = 0; i < KNIGHT_MAXMOVES; i++) { Coordinates new_coordinates = {.row = piece_coords.row + temps[i].y, .column = piece_coords.column + temps[i].x}; @@ -65,57 +113,27 @@ Coordinates *piece_getMoves(const Board board, const Pieces piece, return moves; }; case Bishop: { - Coordinates *moves = malloc(13 * sizeof(Coordinates)); + Coordinates *moves = malloc(BISHOP_MAXMOVES * sizeof(Coordinates)); uint moves_i = 0; - struct temp { - int x; - int y; - }; - struct temp temps[4] = {{-1, 1}, {1, 1}, {1, -1}, {-1, -1}}; - for (uint i = 0; i < 4; i++) { - Coordinates new_coordinates; - int index = 0; - uint mul = 1; - while (index != -1) { - new_coordinates.row = piece_coords.row + (temps[i].y * mul); - new_coordinates.column = piece_coords.column + (temps[i].x * mul); - mul++; - index = board_getIndex(new_coordinates); - if (index != -1) - moves[moves_i++] = new_coordinates; - } - } + piece_getDiagonalMoves(piece_coords, moves, &moves_i); return moves; }; case Rook: { - uint max = 14; - Coordinates *moves = malloc(max * sizeof(Coordinates)); - uint moves_i; - for (uint i = 0; i < max; i++) { - if (i + 1 != piece_coords.column) { - Coordinates horizontal_coordinates = {piece_coords.row, i + 1}; - if (board_getIndex(horizontal_coordinates) != -1) - moves[moves_i++] = horizontal_coordinates; - } - if (i + 1 != piece_coords.row) { - Coordinates vertical_coordinates = {.row = i + 1, - .column = piece_coords.column}; - if (board_getIndex(vertical_coordinates) != -1) - moves[moves_i++] = vertical_coordinates; - } - } + Coordinates *moves = malloc(ROOK_MAXMOVES * sizeof(Coordinates)); + uint moves_i = 0; + piece_getLinearMoves(piece_coords, moves, &moves_i); return moves; }; case King: { - Coordinates *moves = malloc(8 * sizeof(Coordinates)); - uint moves_i; + Coordinates *moves = malloc(KING_MAXMOVES * sizeof(Coordinates)); + uint moves_i = 0; struct temp { int x; int y; }; struct temp temps[8] = {{-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}}; - for (uint i = 0; i < 8; i++) { + for (uint i = 0; i < KING_MAXMOVES; i++) { Coordinates new_coordinates = {.row = piece_coords.row + temps[i].y, .column = piece_coords.column + temps[i].x}; @@ -123,10 +141,14 @@ Coordinates *piece_getMoves(const Board board, const Pieces piece, moves[moves_i++] = new_coordinates; } return moves; - } break; + } case Queen: { - - } break; + Coordinates *moves = malloc(QUEEN_MAXMOVES * sizeof(Coordinates)); + uint moves_i = 0; + piece_getDiagonalMoves(piece_coords, moves, &moves_i); + piece_getLinearMoves(piece_coords, moves, &moves_i); + return moves; + } } return NULL; diff --git a/src/main.c b/src/main.c index c73de15..0422305 100644 --- a/src/main.c +++ b/src/main.c @@ -4,7 +4,7 @@ int main() { Board board; FEN fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"; fen_toBoard(fen, board); - Coordinates *coords = piece_getMoves(board, board[2].piece, board[2].coords); + Coordinates *coords = piece_getMoves(board, board[3].piece, board[3].coords); printf("%d,%d", coords->row, coords->column); free(coords); return 0;