From b6fb001f98e8f8b928f923f663f3d0554218ba8d Mon Sep 17 00:00:00 2001 From: cdricms <36056008+cdricms@users.noreply.github.com> Date: Thu, 28 Dec 2023 21:31:26 +0100 Subject: [PATCH] Re organisation --- Makefile | 2 +- src/Logger.c | 10 +++ src/Logger.h | 21 ++++++ src/datastructures/LinkedList.c | 116 +++++++++++++++++++++++++++++++ src/datastructures/LinkedList.h | 25 +++++++ src/engine.h | 1 + src/engine/board.c | 71 +++++++++++++------ src/engine/board.h | 14 +++- src/engine/common.c | 117 +------------------------------- src/engine/common.h | 16 +---- src/main.c | 9 ++- 11 files changed, 246 insertions(+), 156 deletions(-) create mode 100644 src/Logger.c create mode 100644 src/Logger.h create mode 100644 src/datastructures/LinkedList.c create mode 100644 src/datastructures/LinkedList.h diff --git a/Makefile b/Makefile index 7ca47cb..95112a7 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ ifeq ($(OS),Windows_NT) MKDIR = mkdir else EXECUTABLE = $(BUILDDIR)/$(TARGET) - RM = rm -f + RM = rm -rf MKDIR = mkdir -p endif diff --git a/src/Logger.c b/src/Logger.c new file mode 100644 index 0000000..dc3fd00 --- /dev/null +++ b/src/Logger.c @@ -0,0 +1,10 @@ +#include "Logger.h" +#include + +Logger *logger_init(char *name) { + Logger *logger = malloc(sizeof(Logger)); + logger->name = name; + Log log = {INFO, .message = "Logger initialized."}; + logger->logs = linkedList_init(&log); + return logger; +} diff --git a/src/Logger.h b/src/Logger.h new file mode 100644 index 0000000..7dcf487 --- /dev/null +++ b/src/Logger.h @@ -0,0 +1,21 @@ +#ifndef __LOGGER_HEADER +#define __LOGGER_HEADER + +#include "datastructures/LinkedList.h" +typedef enum { INFO = 0, WARNING, ERROR } Log_Level; + +typedef struct { + Log_Level level; + char *message; +} Log; + +typedef LinkedList Logs; + +typedef struct { + Logs *logs; + char *name; +} Logger; + +Logger *logger_init(char *name); + +#endif // !__LOGGER_HEADER diff --git a/src/datastructures/LinkedList.c b/src/datastructures/LinkedList.c new file mode 100644 index 0000000..4feccad --- /dev/null +++ b/src/datastructures/LinkedList.c @@ -0,0 +1,116 @@ +#include "LinkedList.h" + +LinkedList *linkedList_init(void *value) { + LinkedList *node = (LinkedList *)malloc(sizeof(LinkedList)); + node->value = value; + node->next = NULL; + node->prev = NULL; + return node; +} + +LinkedList *linkedList_getAt(LinkedList *head, const uint index) { + uint current_index = 0; + LinkedList *current_node = head; + while (current_index++ != index) { + if (current_node->next == NULL) { + printf("[ERROR]: Linked List out of bounds.\n"); + return NULL; + } + current_node = current_node->next; + } + return current_node; +} + +bool linkedList_insertAt(LinkedList *head, void *value, const uint index) { + LinkedList *current_node = linkedList_getAt(head, index); + if (current_node == NULL) + return false; + LinkedList *node = linkedList_init(value); + // Set node's next neighbor to current node's next neighbor. + node->next = current_node->next; + // The previous neighbor of the newly created node is the current node. + node->prev = current_node; + // The new neightbor of current node is the newly created node. + current_node->next = node; + + // Set the previous neighbor of node's next neighbor to node. + if (node->next != NULL) { + node->next->prev = node; + } + + return true; +} + +bool linkedList_append(LinkedList *head, void *value) { + LinkedList *current_node = head; + while (current_node != NULL && current_node->next != NULL) + current_node = current_node->next; + if (current_node == NULL) + return false; + + LinkedList *node = linkedList_init(value); + // Set node's next neighbor to current node's next neighbor. + node->next = current_node->next; + // The previous neighbor of the newly created node is the current node. + node->prev = current_node; + // The new neightbor of current node is the newly created node. + current_node->next = node; + + // Set the previous neighbor of node's next neighbor to node. + if (node->next != NULL) { + node->next->prev = node; + } + return true; +} + +bool linkedList_removeAt(LinkedList **head, const uint index) { + LinkedList *current_node = linkedList_getAt(*head, index); + if (current_node == NULL) + return false; + + LinkedList *prev = current_node->prev; + LinkedList *next = current_node->next; + if (prev != NULL) + prev->next = next; + if (next != NULL) + next->prev = prev; + if (index == 0) + *head = next; + free(current_node); + return true; +} + +uint linkedList_length(const LinkedList *head) { + uint current_index = 0; + const LinkedList *current_node = head; + while (current_node != NULL) { + current_node = current_node->next; + current_index++; + } + + return current_index; +} + +bool linkedList_removeAll(LinkedList *head) { + uint length = linkedList_length(head); + LinkedList *lastNode = linkedList_getAt(head, length - 1); + if (lastNode == NULL) + return false; + while (lastNode->prev != NULL) { + lastNode = lastNode->prev; + free(lastNode->next); + } + free(lastNode); + + return true; +} + +void linkedList_print(const LinkedList *head, void (*callback)(void *)) { + const LinkedList *current_node = head; + while (current_node != NULL) { + callback(current_node->value); + printf("->"); + current_node = current_node->next; + } + printf("NULL\n"); +} diff --git a/src/datastructures/LinkedList.h b/src/datastructures/LinkedList.h new file mode 100644 index 0000000..2b1a6ab --- /dev/null +++ b/src/datastructures/LinkedList.h @@ -0,0 +1,25 @@ +#ifndef __LINKEDLIST_HEADER +#define __LINKEDLIST_HEADER + +#include +#include +#include + +typedef unsigned int uint; + +typedef struct t_node_ll { + void *value; + struct t_node_ll *next; + struct t_node_ll *prev; +} LinkedList; + +LinkedList *linkedList_init(void *value); +LinkedList *linkedList_getAt(LinkedList *head, const uint index); +bool linkedList_insertAt(LinkedList *head, void *value, const uint index); +bool linkedList_removeAt(LinkedList **head, const uint index); +bool linkedList_append(LinkedList *head, void *value); +uint linkedList_length(const LinkedList *head); +bool linkedList_removeAll(LinkedList *head); +void linkedList_print(const LinkedList *head, void (*callback)(void *)); + +#endif // !__LINKEDLIST_HEADER diff --git a/src/engine.h b/src/engine.h index b2c3875..095420a 100644 --- a/src/engine.h +++ b/src/engine.h @@ -1,6 +1,7 @@ #ifndef __ENGINE_HEADER #define __ENGINE_HEADER +#include "datastructures/LinkedList.h" #include "engine/board.h" #include "engine/common.h" #include "engine/fen.h" diff --git a/src/engine/board.c b/src/engine/board.c index b21a94e..fdd6bac 100644 --- a/src/engine/board.c +++ b/src/engine/board.c @@ -28,6 +28,9 @@ Piece piece_getColorlessPiece(const Pieces piece) { int board_getIndex(const Coordinates coords) { if (coords.row < 1 || coords.row > 8) { + Log log = {ERROR, .message = " Row out of bounds."}; + if (engine_logger.logs == NULL) { + } printf("[ERROR]: Row out of bounds.\n"); return -1; } @@ -38,8 +41,8 @@ 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) { +void piece_getDiagonalMoves(const Coordinates relative_coords, Moves *moves, + uint *moves_i) { struct temp { int x; int y; @@ -55,30 +58,29 @@ void piece_getDiagonalMoves(const Coordinates relative_coords, mul++; index = board_getIndex(new_coordinates); if (index != -1) - moves[(*moves_i)++] = new_coordinates; + moves->coords[(*moves_i)++] = new_coordinates; } } } -void piece_getLinearMoves(const Coordinates relative_coords, Coordinates *moves, +void piece_getLinearMoves(const Coordinates relative_coords, Moves *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; + moves->coords[(*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; + moves->coords[(*moves_i)++] = vertical_coordinates; } } } -Coordinates *piece_getMoves(const Pieces piece, - const Coordinates piece_coords) { +Moves *piece_getMoves(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); @@ -87,13 +89,14 @@ Coordinates *piece_getMoves(const Pieces piece, switch (colorless_piece) { case Pawn: { - Coordinates *moves = malloc(PAWN_MAXMOVES * sizeof(Coordinates)); + Moves *moves = malloc(sizeof(Moves)); + moves->max_capacity = PAWN_MAXMOVES; uint moves_i = 0; Coordinates new_coordinates = {.column = piece_coords.column, .row = piece_color == Black ? piece_coords.row - 1 : piece_coords.row + 1}; - moves[moves_i++] = new_coordinates; + moves->coords[moves_i++] = new_coordinates; bool is_starting_pos = piece_color == Black ? piece_coords.row == 7 : piece_coords.row == 2; @@ -102,12 +105,14 @@ Coordinates *piece_getMoves(const Pieces piece, .row = piece_color == Black ? piece_coords.row - 2 : piece_coords.row + 2}; - moves[moves_i++] = leap_coords; + moves->coords[moves_i++] = leap_coords; } + moves->length = moves_i; return moves; } case Knight: { - Coordinates *moves = malloc(KNIGHT_MAXMOVES * sizeof(Coordinates)); + Moves *moves = malloc(sizeof(Moves)); + moves->max_capacity = KNIGHT_MAXMOVES; uint moves_i = 0; struct temp { int x; @@ -121,24 +126,30 @@ Coordinates *piece_getMoves(const Pieces piece, piece_coords.column + temps[i].x}; int index = board_getIndex(new_coordinates); if (index != -1) - moves[moves_i++] = new_coordinates; + moves->coords[moves_i++] = new_coordinates; } + moves->length = moves_i; return moves; }; case Bishop: { - Coordinates *moves = malloc(BISHOP_MAXMOVES * sizeof(Coordinates)); + Moves *moves = malloc(sizeof(Moves)); + moves->max_capacity = BISHOP_MAXMOVES; uint moves_i = 0; piece_getDiagonalMoves(piece_coords, moves, &moves_i); + moves->length = moves_i; return moves; }; case Rook: { - Coordinates *moves = malloc(ROOK_MAXMOVES * sizeof(Coordinates)); + Moves *moves = malloc(sizeof(Moves)); + moves->max_capacity = ROOK_MAXMOVES; uint moves_i = 0; piece_getLinearMoves(piece_coords, moves, &moves_i); + moves->length = moves_i; return moves; }; case King: { - Coordinates *moves = malloc(KING_MAXMOVES * sizeof(Coordinates)); + Moves *moves = malloc(sizeof(Moves)); + moves->max_capacity = KING_MAXMOVES; uint moves_i = 0; struct temp { int x; @@ -151,15 +162,18 @@ Coordinates *piece_getMoves(const Pieces piece, .column = piece_coords.column + temps[i].x}; if (board_getIndex(new_coordinates) != -1) - moves[moves_i++] = new_coordinates; + moves->coords[moves_i++] = new_coordinates; } + moves->length = moves_i; return moves; } case Queen: { - Coordinates *moves = malloc(QUEEN_MAXMOVES * sizeof(Coordinates)); + Moves *moves = malloc(sizeof(Moves)); + moves->max_capacity = QUEEN_MAXMOVES; uint moves_i = 0; piece_getDiagonalMoves(piece_coords, moves, &moves_i); piece_getLinearMoves(piece_coords, moves, &moves_i); + moves->length = moves_i; return moves; } } @@ -167,6 +181,23 @@ Coordinates *piece_getMoves(const Pieces piece, return NULL; } +bool board_getLegalMoves(const Board board, const Coordinates piece_coords, + const Coordinates *moves) { + const uint piece_index = board_getIndex(piece_coords); + if (piece_index == -1) { + return false; + } + const Pieces piece = board.squares[piece_index].piece; + if (piece == -1) + return false; + const PieceColors piece_color = piece_getColor(piece); + const Piece bare_piece = piece_getColorlessPiece(piece); + + // Remove squares that are occupied by allied forces. + + return true; +} + bool board_movePiece(Board board, const Coordinates from, const Coordinates to) { Square *b = board.squares; @@ -210,7 +241,7 @@ void board_print(const Board board) { printf("│ %s ", board.squares[i].piece == -1 ? " " : unicode); if ((i + 1) % 8 == 0) { - const uint row = ((64 - i + 1) / 8) % 8 + 1; + const uint row = ((65 - i) / 8) + 1; printf("│ %d\n", row); if (row != 1) printf("├───┼───┼───┼───┼───┼───┼───┼───┤\n"); @@ -218,5 +249,5 @@ void board_print(const Board board) { i++; } printf("└───┴───┴───┴───┴───┴───┴───┴───┘\n"); - printf(" A B C D E F G H"); + printf(" A B C D E F G H\n"); } diff --git a/src/engine/board.h b/src/engine/board.h index 5c6f99e..221f996 100644 --- a/src/engine/board.h +++ b/src/engine/board.h @@ -1,5 +1,6 @@ #ifndef __BOARD_HEADER #define __BOARD_HEADER +#include "../datastructures/LinkedList.h" #include "common.h" typedef int Piece; @@ -11,15 +12,22 @@ typedef struct { uint column; } Coordinates; +typedef struct { + uint max_capacity; + uint length; + Coordinates coords[]; +} Moves; + typedef struct { Piece piece; Coordinates coords; } Square; -// typedef Square Board[64]; +typedef LinkedList FEN_History; + typedef struct { Square squares[64]; - LinkedList *history; // History in FEN strings + FEN_History *history; // History in FEN strings } Board; PieceColors piece_getColor(const Pieces piece); @@ -27,7 +35,7 @@ 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 Pieces piece, const Coordinates piece_coords); +Moves *piece_getMoves(const Pieces piece, const Coordinates piece_coords); void board_print(const Board board); #endif // !__BOARD_HEADER diff --git a/src/engine/common.c b/src/engine/common.c index 8a8dbcc..2353bb1 100644 --- a/src/engine/common.c +++ b/src/engine/common.c @@ -1,5 +1,7 @@ #include "common.h" +Logger engine_logger = {.name = "Engine"}; + char chrToLower(char chr) { if (chr >= 'A' && chr <= 'Z') return chr + 32; @@ -11,118 +13,3 @@ char chrToUpper(char chr) { return chr - 32; return chr; } - -LinkedList *linkedList_init(void *value) { - LinkedList *node = (LinkedList *)malloc(sizeof(LinkedList)); - node->value = value; - node->next = NULL; - node->prev = NULL; - return node; -} - -LinkedList *linkedList_getAt(LinkedList *head, const uint index) { - uint current_index = 0; - LinkedList *current_node = head; - while (current_index++ != index) { - if (current_node->next == NULL) { - printf("[ERROR]: Linked List out of bounds.\n"); - return NULL; - } - current_node = current_node->next; - } - return current_node; -} - -bool linkedList_insertAt(LinkedList *head, void *value, const uint index) { - LinkedList *current_node = linkedList_getAt(head, index); - if (current_node == NULL) - return false; - LinkedList *node = linkedList_init(value); - // Set node's next neighbor to current node's next neighbor. - node->next = current_node->next; - // The previous neighbor of the newly created node is the current node. - node->prev = current_node; - // The new neightbor of current node is the newly created node. - current_node->next = node; - - // Set the previous neighbor of node's next neighbor to node. - if (node->next != NULL) { - node->next->prev = node; - } - - return true; -} - -bool linkedList_append(LinkedList *head, void *value) { - LinkedList *current_node = head; - while (current_node != NULL && current_node->next != NULL) - current_node = current_node->next; - if (current_node == NULL) - return false; - - LinkedList *node = linkedList_init(value); - // Set node's next neighbor to current node's next neighbor. - node->next = current_node->next; - // The previous neighbor of the newly created node is the current node. - node->prev = current_node; - // The new neightbor of current node is the newly created node. - current_node->next = node; - - // Set the previous neighbor of node's next neighbor to node. - if (node->next != NULL) { - node->next->prev = node; - } - return true; -} - -bool linkedList_removeAt(LinkedList **head, const uint index) { - LinkedList *current_node = linkedList_getAt(*head, index); - if (current_node == NULL) - return false; - - LinkedList *prev = current_node->prev; - LinkedList *next = current_node->next; - if (prev != NULL) - prev->next = next; - if (next != NULL) - next->prev = prev; - if (index == 0) - *head = next; - free(current_node); - return true; -} - -uint linkedList_length(const LinkedList *head) { - uint current_index = 0; - const LinkedList *current_node = head; - while (current_node != NULL) { - current_node = current_node->next; - current_index++; - } - - return current_index; -} - -bool linkedList_removeAll(LinkedList *head) { - uint length = linkedList_length(head); - LinkedList *lastNode = linkedList_getAt(head, length - 1); - if (lastNode == NULL) - return false; - while (lastNode->prev != NULL) { - lastNode = lastNode->prev; - free(lastNode->next); - } - free(lastNode); - - return true; -} - -void linkedList_print(const LinkedList *head, void (*callback)(void *)) { - const LinkedList *current_node = head; - while (current_node != NULL) { - callback(current_node->value); - printf("->"); - current_node = current_node->next; - } - printf("NULL\n"); -} diff --git a/src/engine/common.h b/src/engine/common.h index 59f5ea7..d1d6887 100644 --- a/src/engine/common.h +++ b/src/engine/common.h @@ -1,5 +1,6 @@ #ifndef __COMMON_HEADER #define __COMMON_HEADER +#include "../Logger.h" #include #include #include @@ -9,19 +10,6 @@ typedef unsigned int uint; char chrToLower(char chr); char chrToUpper(char chr); -typedef struct t_node_ll { - void *value; - struct t_node_ll *next; - struct t_node_ll *prev; -} LinkedList; - -LinkedList *linkedList_init(void *value); -LinkedList *linkedList_getAt(LinkedList *head, const uint index); -bool linkedList_insertAt(LinkedList *head, void *value, const uint index); -bool linkedList_removeAt(LinkedList **head, const uint index); -bool linkedList_append(LinkedList *head, void *value); -uint linkedList_length(const LinkedList *head); -bool linkedList_removeAll(LinkedList *head); -void linkedList_print(const LinkedList *head, void (*callback)(void *)); +extern Logger engine_logger; #endif // !__COMMON_HEADER diff --git a/src/main.c b/src/main.c index 030f3a2..cab7b69 100644 --- a/src/main.c +++ b/src/main.c @@ -10,9 +10,12 @@ int main() { FEN fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"; fen_toBoard(fen, &board); board_print(board); - // board.history = linkedList_init(&fen); - // Coordinates *coords = - // piece_getMoves(board.squares[3].piece, board.squares[3].coords); + board.history = linkedList_init(&fen); + Moves *moves = + piece_getMoves(board.squares[3].piece, board.squares[3].coords); + printf("hello"); + free(moves); + linkedList_removeAll(board.history); // char a[] = "hello"; // linkedList_append(board.history, &a); // printf("%d\n", linkedList_length(board.history));