From 62a03b01c0da7317be74bc1531e487a67fb9c43c Mon Sep 17 00:00:00 2001 From: cdricms <36056008+cdricms@users.noreply.github.com> Date: Thu, 21 Dec 2023 23:56:24 +0100 Subject: [PATCH] Bishop moves --- src/board.c | 89 +++++++++++++++++++++++++++++++++++++++++++++------- src/board.h | 6 ++-- src/common.h | 1 + src/main.c | 7 ++--- 4 files changed, 84 insertions(+), 19 deletions(-) diff --git a/src/board.c b/src/board.c index 20370b1..dfbf631 100644 --- a/src/board.c +++ b/src/board.c @@ -1,7 +1,7 @@ #include "board.h" -PieceColors piece_getColor(const Piece piece) { return Black & piece; } -Piece piece_getColorlessPiece(const Piece piece) { +PieceColors piece_getColor(const Pieces piece) { return Black & piece; } +Piece piece_getColorlessPiece(const Pieces piece) { return piece_getColor(piece) ^ piece; } @@ -17,17 +17,17 @@ int board_getIndex(const Coordinates coords) { return (8 - (coords.row + 7) % 8) * 8 - (8 - (coords.column + 7) % 8); } -void piece_getMoves(const Board board, const Piece piece, - const Coordinates piece_coords) { - Pieces colorless_piece = piece_getColorlessPiece(piece); +Coordinates *piece_getMoves(const Board board, const Pieces piece, + const Coordinates piece_coords) { + Piece colorless_piece = piece_getColorlessPiece(piece); PieceColors piece_color = piece_getColor(piece); int board_index = board_getIndex(piece_coords); if (board_index == -1) - return; - Coordinates moves[27]; - uint moves_i = 0; + return NULL; switch (colorless_piece) { case Pawn: { + Coordinates *moves = malloc(3 * sizeof(Coordinates)); + uint moves_i; Coordinates new_coordinates = {.column = piece_coords.column, .row = piece_color == Black ? piece_coords.row - 1 @@ -43,15 +43,18 @@ void piece_getMoves(const Board board, const Piece piece, : piece_coords.row + 2}; moves[moves_i++] = leap_coords; } - } break; + return moves; + } case Knight: { + Coordinates *moves = malloc(8 * sizeof(Coordinates)); + uint moves_i; 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 (int i = 0; i < 8; i++) { + for (uint i = 0; i < 8; i++) { Coordinates new_coordinates = {.row = piece_coords.row + temps[i].y, .column = piece_coords.column + temps[i].x}; @@ -59,12 +62,74 @@ void piece_getMoves(const Board board, const Piece piece, if (index != -1) moves[moves_i++] = new_coordinates; } - } break; + return moves; + }; case Bishop: { - uint max = 13; + Coordinates *moves = malloc(13 * 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; + } + } + 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; + } + } + return moves; + }; + case King: { + Coordinates *moves = malloc(8 * sizeof(Coordinates)); + uint moves_i; + 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++) { + Coordinates new_coordinates = {.row = piece_coords.row + temps[i].y, + .column = + piece_coords.column + temps[i].x}; + if (board_getIndex(new_coordinates) != -1) + moves[moves_i++] = new_coordinates; + } + return moves; + } break; + case Queen: { } break; } + + return NULL; } bool board_movePiece(Board board, const Coordinates from, diff --git a/src/board.h b/src/board.h index 55c91b3..3b5a097 100644 --- a/src/board.h +++ b/src/board.h @@ -17,10 +17,12 @@ typedef struct { typedef Square Board[64]; -PieceColors piece_getColor(const Piece piece); -Piece piece_getColorlessPiece(const Piece piece); +PieceColors piece_getColor(const Pieces piece); +Piece piece_getColorlessPiece(const Pieces piece); bool board_movePiece(Board board, const Coordinates from, const Coordinates to); int board_getIndex(const Coordinates coords); +Coordinates *piece_getMoves(const Board board, const Pieces piece, + const Coordinates piece_coords); #endif // !__BOARD_HEADER diff --git a/src/common.h b/src/common.h index 5969acf..da5d67d 100644 --- a/src/common.h +++ b/src/common.h @@ -1,6 +1,7 @@ #ifndef __COMMON_HEADER #include #include +#include typedef unsigned int uint; diff --git a/src/main.c b/src/main.c index c4aaeb0..db70366 100644 --- a/src/main.c +++ b/src/main.c @@ -4,10 +4,7 @@ int main() { Board board; FEN fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"; fen_toBoard(fen, board); - Coordinates from = {.row = 8, .column = 3}; - Coordinates to = {.row = 6, .column = 3}; - board_movePiece(board, from, to); - fen_fromBoard(board, fen); - printf("%s", fen); + Coordinates *coords = piece_getMoves(board, board[2].piece, board[2].coords); + printf("%d,%d", coords->row, coords->column); return 0; }