Makefile for Windows + Unix; and some new function for LinkedList

This commit is contained in:
cdricms
2023-12-27 16:27:46 +01:00
parent 009fff56e8
commit 66c434a799
5 changed files with 57 additions and 11 deletions

View File

@@ -7,16 +7,26 @@ TARGET = main
SOURCES = $(wildcard $(SRCDIR)/*.c)
OBJECTS = $(patsubst $(SRCDIR)/%.c, $(BUILDDIR)/%.o, $(SOURCES))
all: $(BUILDDIR)/$(TARGET)
ifeq ($(OS),Windows_NT)
EXECUTABLE = $(BUILDDIR)/$(TARGET).exe
RM = del /Q
MKDIR = mkdir
else
EXECUTABLE = $(BUILDDIR)/$(TARGET)
RM = rm -f
MKDIR = mkdir -p
endif
$(BUILDDIR)/$(TARGET): $(OBJECTS)
all: $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(CFLAGS) -o $@ $^
$(BUILDDIR)/%.o: $(SRCDIR)/%.c
@mkdir -p $(BUILDDIR)
$(MKDIR) $(BUILDDIR)
$(CC) $(CFLAGS) -c -o $@ $<
clean:
rm -rf $(BUILDDIR)/*
$(RM) $(BUILDDIR)/*
.PHONY: clean

View File

@@ -92,8 +92,33 @@ bool linkedList_removeAt(LinkedList **head, const uint index) {
return true;
}
void linkedList_print(LinkedList *head, void (*callback)(void *)) {
LinkedList *current_node = head;
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("->");

View File

@@ -20,6 +20,8 @@ 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 *));
uint linkedList_length(const LinkedList *head);
bool linkedList_removeAll(LinkedList *head);
void linkedList_print(const LinkedList *head, void (*callback)(void *));
#endif // !__COMMON_HEADER

8
src/engine.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef __ENGINE_HEADER
#define __ENGINE_HEADER
#include "board.h"
#include "common.h"
#include "fen.h"
#endif // !__ENGINE_HEADER

View File

@@ -1,6 +1,4 @@
#include "board.h"
#include "common.h"
#include "fen.h"
#include "engine.h"
void printFEN(void *value) { printf("%s", (char *)value); }
void printString(void *value) { printf("%s", *(char **)value); }
@@ -12,9 +10,12 @@ int main() {
board.history = linkedList_init(&fen);
Coordinates *coords =
piece_getMoves(board.squares[3].piece, board.squares[3].coords);
char a[] = "hello";
linkedList_append(board.history, &a);
printf("%d\n", linkedList_length(board.history));
linkedList_print(board.history, printFEN);
free(coords);
free(board.history);
linkedList_removeAll(board.history);
// char *a = "hello";
// LinkedList *head = linkedList_init(&a);
// char *b = "world";