From 009fff56e8420ed122f67cf682da3aaa0504d514 Mon Sep 17 00:00:00 2001 From: cdricms <36056008+cdricms@users.noreply.github.com> Date: Wed, 27 Dec 2023 15:51:56 +0100 Subject: [PATCH] Some bug fixes --- src/board.c | 10 +++++----- src/board.h | 11 +++++++---- src/common.c | 22 ++++++++++++++++++++++ src/common.h | 1 + src/fen.c | 11 ++++++----- src/fen.h | 2 +- src/main.c | 39 ++++++++++++++++++++++----------------- 7 files changed, 64 insertions(+), 32 deletions(-) diff --git a/src/board.c b/src/board.c index ce5b6dc..81b50bf 100644 --- a/src/board.c +++ b/src/board.c @@ -1,12 +1,11 @@ #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 +#define KING_MAXMOVES 10 // Castling has to be taken into account PieceColors piece_getColor(const Pieces piece) { return Black & piece; } Piece piece_getColorlessPiece(const Pieces piece) { @@ -64,7 +63,7 @@ void piece_getLinearMoves(const Coordinates relative_coords, Coordinates *moves, } } -Coordinates *piece_getMoves(const Board board, const Pieces piece, +Coordinates *piece_getMoves(const Pieces piece, const Coordinates piece_coords) { Piece colorless_piece = piece_getColorlessPiece(piece); PieceColors piece_color = piece_getColor(piece); @@ -156,12 +155,13 @@ Coordinates *piece_getMoves(const Board board, const Pieces piece, bool board_movePiece(Board board, const Coordinates from, const Coordinates to) { + Square *b = board.squares; int from_index = board_getIndex(from); int to_index = board_getIndex(to); if (from_index == -1 || to_index == -1) return false; - board[to_index].piece = board[from_index].piece; - board[from_index].piece = -1; + b[to_index].piece = b[from_index].piece; + b[from_index].piece = -1; return true; } diff --git a/src/board.h b/src/board.h index 85ba9b0..c9b04bb 100644 --- a/src/board.h +++ b/src/board.h @@ -2,7 +2,7 @@ #define __BOARD_HEADER #include "common.h" -typedef uint Piece; +typedef int Piece; typedef enum { Pawn = 0, Knight, Bishop, Rook, Queen, King } Pieces; typedef enum { White = 0, Black = 8 } PieceColors; @@ -16,14 +16,17 @@ typedef struct { Coordinates coords; } Square; -typedef Square Board[64]; +// typedef Square Board[64]; +typedef struct { + Square squares[64]; + LinkedList *history; // History in FEN strings +} Board; 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); +Coordinates *piece_getMoves(const Pieces piece, const Coordinates piece_coords); #endif // !__BOARD_HEADER diff --git a/src/common.c b/src/common.c index d6e6ad7..a87b90a 100644 --- a/src/common.c +++ b/src/common.c @@ -53,6 +53,28 @@ bool linkedList_insertAt(LinkedList *head, void *value, const uint index) { 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) diff --git a/src/common.h b/src/common.h index 746496b..fddaec6 100644 --- a/src/common.h +++ b/src/common.h @@ -19,6 +19,7 @@ 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); void linkedList_print(LinkedList *head, void (*callback)(void *)); #endif // !__COMMON_HEADER diff --git a/src/fen.c b/src/fen.c index de3720d..67a57d5 100644 --- a/src/fen.c +++ b/src/fen.c @@ -1,7 +1,7 @@ #include "fen.h" // Starter fen: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR -void fen_toBoard(const FEN fen, Board board) { +void fen_toBoard(const FEN fen, Board *board) { uint i = 0; char fen_char; uint row = 8; @@ -46,7 +46,7 @@ void fen_toBoard(const FEN fen, Board board) { Piece piece = p ^ color; Coordinates coords = {.row = row, .column = ++column}; Square square = {.piece = piece, .coords = coords}; - board[board_i++] = square; + board->squares[board_i++] = square; } else if (fen_char > '0' && fen_char < '9') { // if the char is a number bet. 1 and 8 // we increment the i by its real value (1..=8) @@ -55,7 +55,7 @@ void fen_toBoard(const FEN fen, Board board) { for (int j = 0; j < offset; j++) { Coordinates coords = {.row = row, .column = ++column}; Square square = {.piece = -1, .coords = coords}; - board[board_i++] = square; + board->squares[board_i++] = square; } } else { printf("[ERROR]: On character %d of FEN, %c is out of range.", i, @@ -79,10 +79,11 @@ void fen_fromBoard(const Board board, FEN fen) { if (board_i != 0 && board_i % 8 == 0) fen[fen_i++] = '/'; - Square square = board[board_i++]; + Square square = board.squares[board_i++]; if (square.piece == -1) { uint num = 1; - while (board_i % 8 != 0 && (square = board[board_i]).piece == -1) { + while (board_i % 8 != 0 && + (square = board.squares[board_i]).piece == -1) { board_i++; num++; } diff --git a/src/fen.h b/src/fen.h index e37a640..beb3bb7 100644 --- a/src/fen.h +++ b/src/fen.h @@ -4,6 +4,6 @@ typedef char FEN[75]; void fen_fromBoard(const Board board, FEN fen); -void fen_toBoard(const FEN fen, Board board); +void fen_toBoard(const FEN fen, Board *board); #endif // !__FEN_HEADER diff --git a/src/main.c b/src/main.c index 1d36bc5..a281ded 100644 --- a/src/main.c +++ b/src/main.c @@ -1,23 +1,28 @@ +#include "board.h" +#include "common.h" #include "fen.h" -void printInt(void *value) { printf("%d", *(int *)value); } +void printFEN(void *value) { printf("%s", (char *)value); } +void printString(void *value) { printf("%s", *(char **)value); } int main() { - // Board board; - // FEN fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"; - // fen_toBoard(fen, board); - // Coordinates *coords = piece_getMoves(board, board[3].piece, - // board[3].coords); printf("%d,%d", coords->row, coords->column); - // free(coords); - int a = 2; - LinkedList *head = linkedList_init(&a); - int b = 3; - linkedList_insertAt(head, &b, 0); - int c = 6; - linkedList_insertAt(head, &c, 1); - linkedList_print(head, printInt); - linkedList_removeAt(&head, 3); - linkedList_print(head, printInt); - // printf("%d", *(int *)first.value); + Board board; + FEN fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"; + fen_toBoard(fen, &board); + board.history = linkedList_init(&fen); + Coordinates *coords = + piece_getMoves(board.squares[3].piece, board.squares[3].coords); + linkedList_print(board.history, printFEN); + free(coords); + free(board.history); + // char *a = "hello"; + // LinkedList *head = linkedList_init(&a); + // char *b = "world"; + // linkedList_append(head, &b); + // char *c = "coucou"; + // linkedList_append(head, &c); + // linkedList_print(head, printString); + // linkedList_removeAt(&head, 2); + // linkedList_print(head, printString); return 0; }