summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--assets/board.js2
-rw-r--r--generated/tests.c10
-rw-r--r--src/engine.c16
-rw-r--r--src/engine/bitboard.c2
-rw-r--r--src/engine/fen.c31
-rw-r--r--src/engine/fen.h5
-rw-r--r--src/engine/ints.h1
-rw-r--r--src/logger/logger.h4
-rw-r--r--src/server/comms.c7
-rw-r--r--tests/main.c5
10 files changed, 68 insertions, 15 deletions
diff --git a/assets/board.js b/assets/board.js
index 1ef6689..db3c282 100644
--- a/assets/board.js
+++ b/assets/board.js
@@ -107,7 +107,7 @@ window.onload = () => {
}
data.push(e.data);
}
- socket.onopen = () => socket.send("hello");
+ socket.onopen = () => socket.send("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
socket.onerror = e => {
console.log("error", e);
};
diff --git a/generated/tests.c b/generated/tests.c
index 7784b32..4439cdb 100644
--- a/generated/tests.c
+++ b/generated/tests.c
@@ -1,13 +1,15 @@
#include "../src/engine/fen.c"
-int total_test_count = 2;
-bool (*tests[2])(void) = {
+int total_test_count = 3;
+bool (*tests[3])(void) = {
test_fen_no_passant,
test_fen_passant,
+ test_starting_position,
};
-int max_test_name_size = 14;
-char test_names[2][100] = {
+int max_test_name_size = 17;
+char test_names[3][100] = {
"fen_no_passant",
"fen_passant",
+ "starting_position",
};
diff --git a/src/engine.c b/src/engine.c
index 8d22d23..52aa83b 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -1,6 +1,7 @@
#include <assert.h>
#include "engine/bitboard.h"
#include "engine/moves.h"
+#include "engine/fen.h"
#include "fcntl.h"
#include "ipc.c"
@@ -23,14 +24,22 @@ int main(void) {
while (1) {
char* x = read_queue(rx);
log_infof("Received: %s", x);
+
+ moves_t moves = moves_init();
+ struct fen_load result = load_fen(x);
free(x);
- send_queue(tx, "Sending", 8);
+ if (result.failed) {
+ log_warnf("Invalid fen");
+ send_queue(tx, "Failed", 7);
+ return 0;
+ }
+ log_infof("Valid fen");
- moves_t moves = moves_init();
- position_t position = position_starting();
+ position_t position = result.position;
get_moves(&moves, position);
+ send_queue(tx, "Sending", 8);
char buf[128];
for (int i = 0; i <= moves.length / 32; i++) {
for (int ii = 0; ii < 32; ii++) {
@@ -52,3 +61,4 @@ int main(void) {
#include "engine/bitboard.c"
#include "engine/moves.c"
+#include "engine/fen.c"
diff --git a/src/engine/bitboard.c b/src/engine/bitboard.c
index 6e4f1b6..80cdb78 100644
--- a/src/engine/bitboard.c
+++ b/src/engine/bitboard.c
@@ -24,6 +24,8 @@ position_t position_starting() {
BLACK_SHORT_CASTLE | BLACK_LONG_CASTLE,
.turn = WHITE_TURN,
.passantable_file = 0,
+ .halfmove_clock = 0,
+ .fullmove_clock = 1
};
}
diff --git a/src/engine/fen.c b/src/engine/fen.c
index a375b30..aa258c9 100644
--- a/src/engine/fen.c
+++ b/src/engine/fen.c
@@ -1,6 +1,5 @@
#include <assert.h>
#include <stdbool.h>
-#include <stdio.h>
#include "fen.h"
#include "bitboard.h"
@@ -98,7 +97,7 @@ struct fen_load load_fen(const char* fen) {
int piece_type = get_piece_enum_item(c);
if (piece_type == -1) return (struct fen_load) { true, position };
- position.bitboards[piece_type] |= (bitboard_t)1 << square;
+ position.bitboards[piece_type] |= (bitboard_t)1 << square++;
}
c = fen[++i];
@@ -167,4 +166,32 @@ bool test_fen_passant() {
if (p.passantable_file != 6) return false;
return true;
}
+
+bool test_starting_position() {
+ struct fen_load r = load_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
+ if (r.failed) return false;
+
+ position_t position = r.position;
+ position_t expected = position_starting();
+
+ if (position.castling != expected.castling) return false;
+ if (position.passantable_file != expected.passantable_file) return false;
+ if (position.turn != expected.turn) return false;
+ if (position.fullmove_clock != expected.fullmove_clock) return false;
+ if (position.halfmove_clock != expected.halfmove_clock) return false;
+ if (position.bitboards[0] != expected.bitboards[0]) return false;
+ if (position.bitboards[1] != expected.bitboards[1]) return false;
+ if (position.bitboards[2] != expected.bitboards[2]) return false;
+ if (position.bitboards[3] != expected.bitboards[3]) return false;
+ if (position.bitboards[4] != expected.bitboards[4]) return false;
+ if (position.bitboards[5] != expected.bitboards[5]) return false;
+ if (position.bitboards[6] != expected.bitboards[6]) return false;
+ if (position.bitboards[7] != expected.bitboards[7]) return false;
+ if (position.bitboards[8] != expected.bitboards[8]) return false;
+ if (position.bitboards[9] != expected.bitboards[9]) return false;
+ if (position.bitboards[10] != expected.bitboards[10]) return false;
+ if (position.bitboards[11] != expected.bitboards[11]) return false;
+
+ return true;
+}
#endif
diff --git a/src/engine/fen.h b/src/engine/fen.h
index 3e5cecf..d45240e 100644
--- a/src/engine/fen.h
+++ b/src/engine/fen.h
@@ -1,3 +1,6 @@
+#ifndef FEN_H
+#define FEN_H
+
#include "bitboard.h"
struct fen_load {
@@ -5,3 +8,5 @@ struct fen_load {
position_t position;
};
struct fen_load load_fen(const char* fen);
+
+#endif // FEN_H
diff --git a/src/engine/ints.h b/src/engine/ints.h
index b134fe6..4a0cfe3 100644
--- a/src/engine/ints.h
+++ b/src/engine/ints.h
@@ -2,6 +2,7 @@
#define INTS_H
#include <stdint.h>
+#include <inttypes.h>
typedef uint8_t u8;
typedef uint16_t u16;
diff --git a/src/logger/logger.h b/src/logger/logger.h
index 4a519aa..5c82985 100644
--- a/src/logger/logger.h
+++ b/src/logger/logger.h
@@ -32,7 +32,7 @@
n = snprintf(log_buf, 1024, "w(%s) ", LOG_MODULE); \
} \
snprintf(log_buf + n, 1024 - n, __VA_ARGS__); \
- send_log(logging, log_buf, 1024) \
+ send_log(logging, log_buf, 1024); \
} while(0);
#endif // LOG_LEVEL >= 2
@@ -44,7 +44,7 @@
n = snprintf(log_buf, 1024, "e(%s) ", LOG_MODULE); \
} \
snprintf(log_buf + n, 1024 - n, __VA_ARGS__); \
- send_log(logging, log_buf, 1024) \
+ send_log(logging, log_buf, 1024); \
} while(0);
#endif // LOG_LEVEL >= 1
diff --git a/src/server/comms.c b/src/server/comms.c
index a25752b..9dd83b9 100644
--- a/src/server/comms.c
+++ b/src/server/comms.c
@@ -45,14 +45,15 @@ void add_str(struct strs* strs, char* str) {
}
struct strs handle_input(mqd_t logging, mqd_t tx, mqd_t rx, char* payload, int len) {
- log_infof("Received %.*s from frontend", len, payload);
-
send_queue(tx, payload, len);
char* buf = read_queue(rx);
- assert(strcmp(buf, "Sending") == 0);
struct strs output = init_strs();
+ if (strcmp(buf, "Sending") != 0) {
+ return output;
+ }
+
int n = get_num_messages(rx);
while (n > 0) {
char* buf = read_queue(rx);
diff --git a/tests/main.c b/tests/main.c
index 2581a58..df802c2 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -14,6 +14,7 @@ double diff_time(struct timespec a, struct timespec b) {
int main() {
int i = 0;
+ int return_type = 0;
while (i < total_test_count) {
char padded_testname[max_test_name_size + 2];
int j = 0;
@@ -34,6 +35,8 @@ int main() {
bool result = tests[i]();
clock_gettime(CLOCK_MONOTONIC, &end);
+ return_type |= !result;
+
printf(
" %s %.2lfs\n",
result ? "\x1b[32mP\x1b[0m" : "\x1b[31mF\x1b[0m",
@@ -41,4 +44,6 @@ int main() {
);
i++;
}
+
+ return return_type;
}