summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--assets/board.js18
-rw-r--r--generate/find_tests.c1
-rw-r--r--generated/tests.c13
-rw-r--r--src/engine.c15
-rw-r--r--src/engine/moves/king.c82
-rw-r--r--src/engine/moves/knight.c72
-rw-r--r--src/server/comms.c9
7 files changed, 152 insertions, 58 deletions
diff --git a/assets/board.js b/assets/board.js
index 8c4fdfc..c8e2194 100644
--- a/assets/board.js
+++ b/assets/board.js
@@ -50,6 +50,11 @@ function checkForPiece(square, bitIndex, set = true) {
return null;
}
+function isYourTurn(piece) {
+ if (turn == "w") return piece < 6;
+ return piece >= 6;
+}
+
const selectedAction = trackBitboard;
window.onkeydown = (e) => {
@@ -90,6 +95,8 @@ function handleTransmission(data) {
let from = parseInt(moveData.slice(0, 2));
let to = parseInt(moveData.slice(2));
+ console.log(from, to, moveData);
+
if (validMoves[from] == undefined) validMoves[from] = []
validMoves[from].push(to);
}
@@ -153,8 +160,9 @@ window.onload = () => {
if (e.data == "END-TRANSMISSION") {
handleTransmission(data);
data = [];
+ } else {
+ data.push(e.data);
}
- data.push(e.data);
}
socket.onopen = () => {
console.log(old_fen);
@@ -202,6 +210,7 @@ window.onload = () => {
const sqr_id = parseInt(square.id.slice(7));
if (hoveringPiece == -1) {
if (piece == null) return;
+ if (!isYourTurn(piece)) return;
if (validMoves[bitIndex]) renderingMoves = validMoves[bitIndex];
setHoverpiece(piece);
pickedUpFrom = sqr_id;
@@ -216,13 +225,6 @@ window.onload = () => {
setHoverpiece(-1);
}
console.log(piece, hoveringPiece, square, renderingMoves);
- // if (
- // (hoveringPiece < 0 && piece == null) ||
- // (hoveringPiece >= 0 && piece != null)
- // ) return;
- // if (hoveringPiece < 0) {
- // } else {
- // }
updateRenderingMoves();
};
board.appendChild(square);
diff --git a/generate/find_tests.c b/generate/find_tests.c
index 0530ae5..30fb024 100644
--- a/generate/find_tests.c
+++ b/generate/find_tests.c
@@ -150,6 +150,7 @@ void handle_fn(const char* input_path, int path_strip_index, FILE* file) {
}
output[k][kk++] = ch;
+ output[k][kk] = 0;
}
}
struct file_tests tests = { .test_count = k };
diff --git a/generated/tests.c b/generated/tests.c
index 4439cdb..a9f7788 100644
--- a/generated/tests.c
+++ b/generated/tests.c
@@ -1,15 +1,20 @@
#include "../src/engine/fen.c"
+#include "../src/engine/moves/king.c"
-int total_test_count = 3;
-bool (*tests[3])(void) = {
+int total_test_count = 5;
+bool (*tests[5])(void) = {
test_fen_no_passant,
test_fen_passant,
test_starting_position,
+ test_white_king_corners,
+ test_black_king_corners,
};
-int max_test_name_size = 17;
-char test_names[3][100] = {
+int max_test_name_size = 18;
+char test_names[5][100] = {
"fen_no_passant",
"fen_passant",
"starting_position",
+ "white_king_corners",
+ "black_king_corners",
};
diff --git a/src/engine.c b/src/engine.c
index 187be28..1ff4fef 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -1,4 +1,5 @@
#include <assert.h>
+#include <string.h>
#include "engine/bitboard.h"
#include "engine/moves.h"
#include "engine/fen.h"
@@ -49,12 +50,16 @@ int main(void) {
buf[k++] = from_str.data[1];
buf[k++] = to_str.data[0];
buf[k++] = to_str.data[1];
- if (k == 64) {
- buf[k] = 0;
- send_queue(tx, buf, sizeof(buf));
- k = 0;
- }
+ if (k != 64) continue;
+ buf[k] = 0;
+ send_queue(tx, buf, sizeof(buf));
+ k = 0;
}
+ if (k != 0) send_queue(tx, buf, sizeof(buf));
+ char end_transmission[] = "END-TRANSMISSION";
+ int n = strlen(end_transmission);
+ log_infof("%s", end_transmission);
+ send_queue(tx, end_transmission, n + 1);
}
return 0;
diff --git a/src/engine/moves/king.c b/src/engine/moves/king.c
index 9ce3eb8..29bca79 100644
--- a/src/engine/moves/king.c
+++ b/src/engine/moves/king.c
@@ -1,7 +1,8 @@
#include "../moves.h"
+#include <stdio.h>
void get_king_moves(moves_t* moves, position_t position) {
- assert_valid_position(position);
+ // assert_valid_position(position);
bitboard_t friendly_king;
int king_square;
@@ -34,3 +35,82 @@ void get_king_moves(moves_t* moves, position_t position) {
add_move(moves, king_square, to);
}
}
+
+#ifdef TEST_MOD
+#include <stdbool.h>
+#include "vec.c"
+
+bool test_white_king_corners() {
+ moves_t moves = moves_init();
+ position_t p = {0};
+ p.bitboards[WHITE_KING] = 1;
+ p.turn = WHITE_TURN;
+ get_king_moves(&moves, p);
+ if (moves.length != 3) return false;
+ if (moves.moves[0].from != 0) return false;
+ if (moves.moves[0].to != 1) return false;
+ if (moves.moves[1].from != 0) return false;
+ if (moves.moves[1].to != 8) return false;
+ if (moves.moves[2].from != 0) return false;
+ if (moves.moves[2].to != 9) return false;
+ moves.length = 0;
+ p.bitboards[WHITE_KING] = 128;
+ get_king_moves(&moves, p);
+ if (moves.length != 3) return false;
+ if (moves.moves[0].from != 7) return false;
+ if (moves.moves[0].to != 6) return false;
+ if (moves.moves[1].from != 7) return false;
+ if (moves.moves[1].to != 14) return false;
+ if (moves.moves[2].from != 7) return false;
+ if (moves.moves[2].to != 15) return false;
+ moves.length = 0;
+ p.bitboards[WHITE_KING] = 72057594037927936ULL;
+ get_king_moves(&moves, p);
+ if (moves.length != 3) return false;
+ if (moves.moves[0].from != 56) return false;
+ if (moves.moves[0].to != 48) return false;
+ if (moves.moves[1].from != 56) return false;
+ if (moves.moves[1].to != 49) return false;
+ if (moves.moves[2].from != 56) return false;
+ if (moves.moves[2].to != 57) return false;
+
+ return true;
+}
+
+bool test_black_king_corners() {
+ moves_t moves = moves_init();
+ position_t p = {0};
+ p.bitboards[BLACK_KING] = 1;
+ p.turn = BLACK_TURN;
+ get_king_moves(&moves, p);
+ if (moves.length != 3) return false;
+ if (moves.moves[0].from != 0) return false;
+ if (moves.moves[0].to != 1) return false;
+ if (moves.moves[1].from != 0) return false;
+ if (moves.moves[1].to != 8) return false;
+ if (moves.moves[2].from != 0) return false;
+ if (moves.moves[2].to != 9) return false;
+ moves.length = 0;
+ p.bitboards[BLACK_KING] = 128;
+ get_king_moves(&moves, p);
+ if (moves.length != 3) return false;
+ if (moves.moves[0].from != 7) return false;
+ if (moves.moves[0].to != 6) return false;
+ if (moves.moves[1].from != 7) return false;
+ if (moves.moves[1].to != 14) return false;
+ if (moves.moves[2].from != 7) return false;
+ if (moves.moves[2].to != 15) return false;
+ moves.length = 0;
+ p.bitboards[BLACK_KING] = 72057594037927936ULL;
+ get_king_moves(&moves, p);
+ if (moves.length != 3) return false;
+ if (moves.moves[0].from != 56) return false;
+ if (moves.moves[0].to != 48) return false;
+ if (moves.moves[1].from != 56) return false;
+ if (moves.moves[1].to != 49) return false;
+ if (moves.moves[2].from != 56) return false;
+ if (moves.moves[2].to != 57) return false;
+
+ return true;
+}
+#endif
diff --git a/src/engine/moves/knight.c b/src/engine/moves/knight.c
index c48b48e..5f5f9ad 100644
--- a/src/engine/moves/knight.c
+++ b/src/engine/moves/knight.c
@@ -4,63 +4,63 @@ bitboard_t knight_moves[64] = {
132096ULL,
329728ULL,
659712ULL,
- 659712ULL >> 1,
- 659712ULL >> 2,
- 659712ULL >> 3,
+ 659712ULL << 1,
+ 659712ULL << 2,
+ 659712ULL << 3,
10489856ULL,
4202496ULL,
33816580ULL,
84410376ULL,
168886289ULL,
- 168886289ULL >> 1,
- 168886289ULL >> 2,
- 168886289ULL >> 3,
+ 168886289ULL << 1,
+ 168886289ULL << 2,
+ 168886289ULL << 3,
2685403152ULL,
1075839008ULL,
8657044482ULL,
21609056261ULL,
43234889994ULL,
- 43234889994ULL >> 1,
- 43234889994ULL >> 2,
- 43234889994ULL >> 3,
+ 43234889994ULL << 1,
+ 43234889994ULL << 2,
+ 43234889994ULL << 3,
687463207072ULL,
275414786112ULL,
- 8657044482ULL >> 8,
- 21609056261ULL >> 8,
- 43234889994ULL >> 8,
- 43234889994ULL >> (8 + 1),
- 43234889994ULL >> (8 + 2),
- 43234889994ULL >> (8 + 3),
- 687463207072ULL >> 8,
- 275414786112ULL >> 8,
+ 8657044482ULL << 8,
+ 21609056261ULL << 8,
+ 43234889994ULL << 8,
+ 43234889994ULL << (8 + 1),
+ 43234889994ULL << (8 + 2),
+ 43234889994ULL << (8 + 3),
+ 687463207072ULL << 8,
+ 275414786112ULL << 8,
- 8657044482ULL >> 16,
- 21609056261ULL >> 16,
- 43234889994ULL >> 16,
- 43234889994ULL >> (16 + 1),
- 43234889994ULL >> (16 + 2),
- 43234889994ULL >> (16 + 3),
- 687463207072ULL >> 16,
- 275414786112ULL >> 16,
+ 8657044482ULL << 16,
+ 21609056261ULL << 16,
+ 43234889994ULL << 16,
+ 43234889994ULL << (16 + 1),
+ 43234889994ULL << (16 + 2),
+ 43234889994ULL << (16 + 3),
+ 687463207072ULL << 16,
+ 275414786112ULL << 16,
- 8657044482ULL >> 24,
- 21609056261ULL >> 24,
- 43234889994ULL >> 24,
- 43234889994ULL >> (24 + 1),
- 43234889994ULL >> (24 + 2),
- 43234889994ULL >> (24 + 3),
- 687463207072ULL >> 24,
- 275414786112ULL >> 24,
+ 8657044482ULL << 24,
+ 21609056261ULL << 24,
+ 43234889994ULL << 24,
+ 43234889994ULL << (24 + 1),
+ 43234889994ULL << (24 + 2),
+ 43234889994ULL << (24 + 3),
+ 687463207072ULL << 24,
+ 275414786112ULL << 24,
288234782788157440ULL,
576469569871282176ULL,
1224997833292120064ULL,
- 1224997833292120064ULL >> 1,
- 1224997833292120064ULL >> 2,
- 1224997833292120064ULL >> 3,
+ 1224997833292120064ULL << 1,
+ 1224997833292120064ULL << 2,
+ 1224997833292120064ULL << 3,
1152939783987658752ULL,
2305878468463689728ULL,
diff --git a/src/server/comms.c b/src/server/comms.c
index cc5a0ce..67e42a2 100644
--- a/src/server/comms.c
+++ b/src/server/comms.c
@@ -55,13 +55,14 @@ struct strs handle_input(mqd_t logging, mqd_t tx, mqd_t rx, char* payload, int l
return output;
}
- int n = get_num_messages(rx);
- while (n > 0) {
+ while (1) {
char* buf = read_queue(rx);
+ if (strcmp(buf, "END-TRANSMISSION") == 0) {
+ free(buf);
+ break;
+ }
add_str(&output, buf);
free(buf);
- n--;
- if (n == 0) n = get_num_messages(rx);
}
return output;
}