Re organisation
This commit is contained in:
2
Makefile
2
Makefile
@@ -13,7 +13,7 @@ ifeq ($(OS),Windows_NT)
|
|||||||
MKDIR = mkdir
|
MKDIR = mkdir
|
||||||
else
|
else
|
||||||
EXECUTABLE = $(BUILDDIR)/$(TARGET)
|
EXECUTABLE = $(BUILDDIR)/$(TARGET)
|
||||||
RM = rm -f
|
RM = rm -rf
|
||||||
MKDIR = mkdir -p
|
MKDIR = mkdir -p
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
|||||||
10
src/Logger.c
Normal file
10
src/Logger.c
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#include "Logger.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
21
src/Logger.h
Normal file
21
src/Logger.h
Normal file
@@ -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
|
||||||
116
src/datastructures/LinkedList.c
Normal file
116
src/datastructures/LinkedList.c
Normal file
@@ -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");
|
||||||
|
}
|
||||||
25
src/datastructures/LinkedList.h
Normal file
25
src/datastructures/LinkedList.h
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef __LINKEDLIST_HEADER
|
||||||
|
#define __LINKEDLIST_HEADER
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef __ENGINE_HEADER
|
#ifndef __ENGINE_HEADER
|
||||||
#define __ENGINE_HEADER
|
#define __ENGINE_HEADER
|
||||||
|
|
||||||
|
#include "datastructures/LinkedList.h"
|
||||||
#include "engine/board.h"
|
#include "engine/board.h"
|
||||||
#include "engine/common.h"
|
#include "engine/common.h"
|
||||||
#include "engine/fen.h"
|
#include "engine/fen.h"
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ Piece piece_getColorlessPiece(const Pieces piece) {
|
|||||||
|
|
||||||
int board_getIndex(const Coordinates coords) {
|
int board_getIndex(const Coordinates coords) {
|
||||||
if (coords.row < 1 || coords.row > 8) {
|
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");
|
printf("[ERROR]: Row out of bounds.\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -38,8 +41,8 @@ int board_getIndex(const Coordinates coords) {
|
|||||||
return (8 - (coords.row + 7) % 8) * 8 - (8 - (coords.column + 7) % 8);
|
return (8 - (coords.row + 7) % 8) * 8 - (8 - (coords.column + 7) % 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
void piece_getDiagonalMoves(const Coordinates relative_coords,
|
void piece_getDiagonalMoves(const Coordinates relative_coords, Moves *moves,
|
||||||
Coordinates *moves, uint *moves_i) {
|
uint *moves_i) {
|
||||||
struct temp {
|
struct temp {
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
@@ -55,30 +58,29 @@ void piece_getDiagonalMoves(const Coordinates relative_coords,
|
|||||||
mul++;
|
mul++;
|
||||||
index = board_getIndex(new_coordinates);
|
index = board_getIndex(new_coordinates);
|
||||||
if (index != -1)
|
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) {
|
uint *moves_i) {
|
||||||
for (uint i = 0; i < ROOK_MAXMOVES; i++) {
|
for (uint i = 0; i < ROOK_MAXMOVES; i++) {
|
||||||
if (i + 1 != relative_coords.column) {
|
if (i + 1 != relative_coords.column) {
|
||||||
Coordinates horizontal_coordinates = {relative_coords.row, i + 1};
|
Coordinates horizontal_coordinates = {relative_coords.row, i + 1};
|
||||||
if (board_getIndex(horizontal_coordinates) != -1)
|
if (board_getIndex(horizontal_coordinates) != -1)
|
||||||
moves[(*moves_i)++] = horizontal_coordinates;
|
moves->coords[(*moves_i)++] = horizontal_coordinates;
|
||||||
}
|
}
|
||||||
if (i + 1 != relative_coords.row) {
|
if (i + 1 != relative_coords.row) {
|
||||||
Coordinates vertical_coordinates = {.row = i + 1,
|
Coordinates vertical_coordinates = {.row = i + 1,
|
||||||
.column = relative_coords.column};
|
.column = relative_coords.column};
|
||||||
if (board_getIndex(vertical_coordinates) != -1)
|
if (board_getIndex(vertical_coordinates) != -1)
|
||||||
moves[(*moves_i)++] = vertical_coordinates;
|
moves->coords[(*moves_i)++] = vertical_coordinates;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Coordinates *piece_getMoves(const Pieces piece,
|
Moves *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);
|
||||||
int board_index = board_getIndex(piece_coords);
|
int board_index = board_getIndex(piece_coords);
|
||||||
@@ -87,13 +89,14 @@ Coordinates *piece_getMoves(const Pieces piece,
|
|||||||
|
|
||||||
switch (colorless_piece) {
|
switch (colorless_piece) {
|
||||||
case Pawn: {
|
case Pawn: {
|
||||||
Coordinates *moves = malloc(PAWN_MAXMOVES * sizeof(Coordinates));
|
Moves *moves = malloc(sizeof(Moves));
|
||||||
|
moves->max_capacity = PAWN_MAXMOVES;
|
||||||
uint moves_i = 0;
|
uint moves_i = 0;
|
||||||
Coordinates new_coordinates = {.column = piece_coords.column,
|
Coordinates new_coordinates = {.column = piece_coords.column,
|
||||||
.row = piece_color == Black
|
.row = piece_color == Black
|
||||||
? piece_coords.row - 1
|
? piece_coords.row - 1
|
||||||
: piece_coords.row + 1};
|
: piece_coords.row + 1};
|
||||||
moves[moves_i++] = new_coordinates;
|
moves->coords[moves_i++] = new_coordinates;
|
||||||
|
|
||||||
bool is_starting_pos =
|
bool is_starting_pos =
|
||||||
piece_color == Black ? piece_coords.row == 7 : piece_coords.row == 2;
|
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
|
.row = piece_color == Black
|
||||||
? piece_coords.row - 2
|
? piece_coords.row - 2
|
||||||
: 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;
|
return moves;
|
||||||
}
|
}
|
||||||
case Knight: {
|
case Knight: {
|
||||||
Coordinates *moves = malloc(KNIGHT_MAXMOVES * sizeof(Coordinates));
|
Moves *moves = malloc(sizeof(Moves));
|
||||||
|
moves->max_capacity = KNIGHT_MAXMOVES;
|
||||||
uint moves_i = 0;
|
uint moves_i = 0;
|
||||||
struct temp {
|
struct temp {
|
||||||
int x;
|
int x;
|
||||||
@@ -121,24 +126,30 @@ Coordinates *piece_getMoves(const Pieces piece,
|
|||||||
piece_coords.column + temps[i].x};
|
piece_coords.column + temps[i].x};
|
||||||
int index = board_getIndex(new_coordinates);
|
int index = board_getIndex(new_coordinates);
|
||||||
if (index != -1)
|
if (index != -1)
|
||||||
moves[moves_i++] = new_coordinates;
|
moves->coords[moves_i++] = new_coordinates;
|
||||||
}
|
}
|
||||||
|
moves->length = moves_i;
|
||||||
return moves;
|
return moves;
|
||||||
};
|
};
|
||||||
case Bishop: {
|
case Bishop: {
|
||||||
Coordinates *moves = malloc(BISHOP_MAXMOVES * sizeof(Coordinates));
|
Moves *moves = malloc(sizeof(Moves));
|
||||||
|
moves->max_capacity = BISHOP_MAXMOVES;
|
||||||
uint moves_i = 0;
|
uint moves_i = 0;
|
||||||
piece_getDiagonalMoves(piece_coords, moves, &moves_i);
|
piece_getDiagonalMoves(piece_coords, moves, &moves_i);
|
||||||
|
moves->length = moves_i;
|
||||||
return moves;
|
return moves;
|
||||||
};
|
};
|
||||||
case Rook: {
|
case Rook: {
|
||||||
Coordinates *moves = malloc(ROOK_MAXMOVES * sizeof(Coordinates));
|
Moves *moves = malloc(sizeof(Moves));
|
||||||
|
moves->max_capacity = ROOK_MAXMOVES;
|
||||||
uint moves_i = 0;
|
uint moves_i = 0;
|
||||||
piece_getLinearMoves(piece_coords, moves, &moves_i);
|
piece_getLinearMoves(piece_coords, moves, &moves_i);
|
||||||
|
moves->length = moves_i;
|
||||||
return moves;
|
return moves;
|
||||||
};
|
};
|
||||||
case King: {
|
case King: {
|
||||||
Coordinates *moves = malloc(KING_MAXMOVES * sizeof(Coordinates));
|
Moves *moves = malloc(sizeof(Moves));
|
||||||
|
moves->max_capacity = KING_MAXMOVES;
|
||||||
uint moves_i = 0;
|
uint moves_i = 0;
|
||||||
struct temp {
|
struct temp {
|
||||||
int x;
|
int x;
|
||||||
@@ -151,15 +162,18 @@ Coordinates *piece_getMoves(const Pieces piece,
|
|||||||
.column =
|
.column =
|
||||||
piece_coords.column + temps[i].x};
|
piece_coords.column + temps[i].x};
|
||||||
if (board_getIndex(new_coordinates) != -1)
|
if (board_getIndex(new_coordinates) != -1)
|
||||||
moves[moves_i++] = new_coordinates;
|
moves->coords[moves_i++] = new_coordinates;
|
||||||
}
|
}
|
||||||
|
moves->length = moves_i;
|
||||||
return moves;
|
return moves;
|
||||||
}
|
}
|
||||||
case Queen: {
|
case Queen: {
|
||||||
Coordinates *moves = malloc(QUEEN_MAXMOVES * sizeof(Coordinates));
|
Moves *moves = malloc(sizeof(Moves));
|
||||||
|
moves->max_capacity = QUEEN_MAXMOVES;
|
||||||
uint moves_i = 0;
|
uint moves_i = 0;
|
||||||
piece_getDiagonalMoves(piece_coords, moves, &moves_i);
|
piece_getDiagonalMoves(piece_coords, moves, &moves_i);
|
||||||
piece_getLinearMoves(piece_coords, moves, &moves_i);
|
piece_getLinearMoves(piece_coords, moves, &moves_i);
|
||||||
|
moves->length = moves_i;
|
||||||
return moves;
|
return moves;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -167,6 +181,23 @@ Coordinates *piece_getMoves(const Pieces piece,
|
|||||||
return NULL;
|
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,
|
bool board_movePiece(Board board, const Coordinates from,
|
||||||
const Coordinates to) {
|
const Coordinates to) {
|
||||||
Square *b = board.squares;
|
Square *b = board.squares;
|
||||||
@@ -210,7 +241,7 @@ void board_print(const Board board) {
|
|||||||
|
|
||||||
printf("│ %s ", board.squares[i].piece == -1 ? " " : unicode);
|
printf("│ %s ", board.squares[i].piece == -1 ? " " : unicode);
|
||||||
if ((i + 1) % 8 == 0) {
|
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);
|
printf("│ %d\n", row);
|
||||||
if (row != 1)
|
if (row != 1)
|
||||||
printf("├───┼───┼───┼───┼───┼───┼───┼───┤\n");
|
printf("├───┼───┼───┼───┼───┼───┼───┼───┤\n");
|
||||||
@@ -218,5 +249,5 @@ void board_print(const Board board) {
|
|||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
printf("└───┴───┴───┴───┴───┴───┴───┴───┘\n");
|
printf("└───┴───┴───┴───┴───┴───┴───┴───┘\n");
|
||||||
printf(" A B C D E F G H");
|
printf(" A B C D E F G H\n");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#ifndef __BOARD_HEADER
|
#ifndef __BOARD_HEADER
|
||||||
#define __BOARD_HEADER
|
#define __BOARD_HEADER
|
||||||
|
#include "../datastructures/LinkedList.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
typedef int Piece;
|
typedef int Piece;
|
||||||
@@ -11,15 +12,22 @@ typedef struct {
|
|||||||
uint column;
|
uint column;
|
||||||
} Coordinates;
|
} Coordinates;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint max_capacity;
|
||||||
|
uint length;
|
||||||
|
Coordinates coords[];
|
||||||
|
} Moves;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Piece piece;
|
Piece piece;
|
||||||
Coordinates coords;
|
Coordinates coords;
|
||||||
} Square;
|
} Square;
|
||||||
|
|
||||||
// typedef Square Board[64];
|
typedef LinkedList FEN_History;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Square squares[64];
|
Square squares[64];
|
||||||
LinkedList *history; // History in FEN strings
|
FEN_History *history; // History in FEN strings
|
||||||
} Board;
|
} Board;
|
||||||
|
|
||||||
PieceColors piece_getColor(const Pieces piece);
|
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);
|
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 Pieces piece, const Coordinates piece_coords);
|
Moves *piece_getMoves(const Pieces piece, const Coordinates piece_coords);
|
||||||
void board_print(const Board board);
|
void board_print(const Board board);
|
||||||
|
|
||||||
#endif // !__BOARD_HEADER
|
#endif // !__BOARD_HEADER
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
|
Logger engine_logger = {.name = "Engine"};
|
||||||
|
|
||||||
char chrToLower(char chr) {
|
char chrToLower(char chr) {
|
||||||
if (chr >= 'A' && chr <= 'Z')
|
if (chr >= 'A' && chr <= 'Z')
|
||||||
return chr + 32;
|
return chr + 32;
|
||||||
@@ -11,118 +13,3 @@ char chrToUpper(char chr) {
|
|||||||
return chr - 32;
|
return chr - 32;
|
||||||
return chr;
|
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");
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#ifndef __COMMON_HEADER
|
#ifndef __COMMON_HEADER
|
||||||
#define __COMMON_HEADER
|
#define __COMMON_HEADER
|
||||||
|
#include "../Logger.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -9,19 +10,6 @@ typedef unsigned int uint;
|
|||||||
char chrToLower(char chr);
|
char chrToLower(char chr);
|
||||||
char chrToUpper(char chr);
|
char chrToUpper(char chr);
|
||||||
|
|
||||||
typedef struct t_node_ll {
|
extern Logger engine_logger;
|
||||||
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 // !__COMMON_HEADER
|
#endif // !__COMMON_HEADER
|
||||||
|
|||||||
@@ -10,9 +10,12 @@ int main() {
|
|||||||
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);
|
||||||
board_print(board);
|
board_print(board);
|
||||||
// board.history = linkedList_init(&fen);
|
board.history = linkedList_init(&fen);
|
||||||
// Coordinates *coords =
|
Moves *moves =
|
||||||
// piece_getMoves(board.squares[3].piece, board.squares[3].coords);
|
piece_getMoves(board.squares[3].piece, board.squares[3].coords);
|
||||||
|
printf("hello");
|
||||||
|
free(moves);
|
||||||
|
linkedList_removeAll(board.history);
|
||||||
// char a[] = "hello";
|
// char a[] = "hello";
|
||||||
// linkedList_append(board.history, &a);
|
// linkedList_append(board.history, &a);
|
||||||
// printf("%d\n", linkedList_length(board.history));
|
// printf("%d\n", linkedList_length(board.history));
|
||||||
|
|||||||
Reference in New Issue
Block a user