Some bug fixes

This commit is contained in:
cdricms
2023-12-27 15:51:56 +01:00
parent 8a91eb1fd0
commit 009fff56e8
7 changed files with 64 additions and 32 deletions

View File

@@ -1,12 +1,11 @@
#include "board.h" #include "board.h"
#include <stdlib.h>
#define PAWN_MAXMOVES 4 #define PAWN_MAXMOVES 4
#define KNIGHT_MAXMOVES 8 #define KNIGHT_MAXMOVES 8
#define BISHOP_MAXMOVES 13 #define BISHOP_MAXMOVES 13
#define ROOK_MAXMOVES 14 #define ROOK_MAXMOVES 14
#define QUEEN_MAXMOVES 27 #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; } PieceColors piece_getColor(const Pieces piece) { return Black & piece; }
Piece piece_getColorlessPiece(const Pieces 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) { const Coordinates piece_coords) {
Piece colorless_piece = piece_getColorlessPiece(piece); Piece colorless_piece = piece_getColorlessPiece(piece);
PieceColors piece_color = piece_getColor(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, bool board_movePiece(Board board, const Coordinates from,
const Coordinates to) { const Coordinates to) {
Square *b = board.squares;
int from_index = board_getIndex(from); int from_index = board_getIndex(from);
int to_index = board_getIndex(to); int to_index = board_getIndex(to);
if (from_index == -1 || to_index == -1) if (from_index == -1 || to_index == -1)
return false; return false;
board[to_index].piece = board[from_index].piece; b[to_index].piece = b[from_index].piece;
board[from_index].piece = -1; b[from_index].piece = -1;
return true; return true;
} }

View File

@@ -2,7 +2,7 @@
#define __BOARD_HEADER #define __BOARD_HEADER
#include "common.h" #include "common.h"
typedef uint Piece; typedef int Piece;
typedef enum { Pawn = 0, Knight, Bishop, Rook, Queen, King } Pieces; typedef enum { Pawn = 0, Knight, Bishop, Rook, Queen, King } Pieces;
typedef enum { White = 0, Black = 8 } PieceColors; typedef enum { White = 0, Black = 8 } PieceColors;
@@ -16,14 +16,17 @@ typedef struct {
Coordinates coords; Coordinates coords;
} Square; } 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); PieceColors piece_getColor(const Pieces piece);
Piece piece_getColorlessPiece(const Pieces piece); Piece piece_getColorlessPiece(const Pieces piece);
bool board_movePiece(Board board, const Coordinates from, const Coordinates to); bool board_movePiece(Board board, const Coordinates from, const Coordinates to);
int board_getIndex(const Coordinates coords); int board_getIndex(const Coordinates coords);
Coordinates *piece_getMoves(const Board board, const Pieces piece, Coordinates *piece_getMoves(const Pieces piece, const Coordinates piece_coords);
const Coordinates piece_coords);
#endif // !__BOARD_HEADER #endif // !__BOARD_HEADER

View File

@@ -53,6 +53,28 @@ bool linkedList_insertAt(LinkedList *head, void *value, const uint index) {
return true; 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) { bool linkedList_removeAt(LinkedList **head, const uint index) {
LinkedList *current_node = linkedList_getAt(*head, index); LinkedList *current_node = linkedList_getAt(*head, index);
if (current_node == NULL) if (current_node == NULL)

View File

@@ -19,6 +19,7 @@ LinkedList *linkedList_init(void *value);
LinkedList *linkedList_getAt(LinkedList *head, const uint index); LinkedList *linkedList_getAt(LinkedList *head, const uint index);
bool linkedList_insertAt(LinkedList *head, void *value, const uint index); bool linkedList_insertAt(LinkedList *head, void *value, const uint index);
bool linkedList_removeAt(LinkedList **head, 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 *)); void linkedList_print(LinkedList *head, void (*callback)(void *));
#endif // !__COMMON_HEADER #endif // !__COMMON_HEADER

View File

@@ -1,7 +1,7 @@
#include "fen.h" #include "fen.h"
// Starter fen: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR // 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; uint i = 0;
char fen_char; char fen_char;
uint row = 8; uint row = 8;
@@ -46,7 +46,7 @@ void fen_toBoard(const FEN fen, Board board) {
Piece piece = p ^ color; Piece piece = p ^ color;
Coordinates coords = {.row = row, .column = ++column}; Coordinates coords = {.row = row, .column = ++column};
Square square = {.piece = piece, .coords = coords}; Square square = {.piece = piece, .coords = coords};
board[board_i++] = square; board->squares[board_i++] = square;
} else if (fen_char > '0' && fen_char < '9') { } else if (fen_char > '0' && fen_char < '9') {
// if the char is a number bet. 1 and 8 // if the char is a number bet. 1 and 8
// we increment the i by its real value (1..=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++) { for (int j = 0; j < offset; j++) {
Coordinates coords = {.row = row, .column = ++column}; Coordinates coords = {.row = row, .column = ++column};
Square square = {.piece = -1, .coords = coords}; Square square = {.piece = -1, .coords = coords};
board[board_i++] = square; board->squares[board_i++] = square;
} }
} else { } else {
printf("[ERROR]: On character %d of FEN, %c is out of range.", i, 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) if (board_i != 0 && board_i % 8 == 0)
fen[fen_i++] = '/'; fen[fen_i++] = '/';
Square square = board[board_i++]; Square square = board.squares[board_i++];
if (square.piece == -1) { if (square.piece == -1) {
uint num = 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++; board_i++;
num++; num++;
} }

View File

@@ -4,6 +4,6 @@
typedef char FEN[75]; typedef char FEN[75];
void fen_fromBoard(const Board board, FEN fen); 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 #endif // !__FEN_HEADER

View File

@@ -1,23 +1,28 @@
#include "board.h"
#include "common.h"
#include "fen.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() { int main() {
// Board board; Board board;
// FEN fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"; FEN fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR";
// fen_toBoard(fen, board); fen_toBoard(fen, &board);
// Coordinates *coords = piece_getMoves(board, board[3].piece, board.history = linkedList_init(&fen);
// board[3].coords); printf("%d,%d", coords->row, coords->column); Coordinates *coords =
// free(coords); piece_getMoves(board.squares[3].piece, board.squares[3].coords);
int a = 2; linkedList_print(board.history, printFEN);
LinkedList *head = linkedList_init(&a); free(coords);
int b = 3; free(board.history);
linkedList_insertAt(head, &b, 0); // char *a = "hello";
int c = 6; // LinkedList *head = linkedList_init(&a);
linkedList_insertAt(head, &c, 1); // char *b = "world";
linkedList_print(head, printInt); // linkedList_append(head, &b);
linkedList_removeAt(&head, 3); // char *c = "coucou";
linkedList_print(head, printInt); // linkedList_append(head, &c);
// printf("%d", *(int *)first.value); // linkedList_print(head, printString);
// linkedList_removeAt(&head, 2);
// linkedList_print(head, printString);
return 0; return 0;
} }