From 5a7b5f16426b072ab2ffe170ea90b54ee8fa630b Mon Sep 17 00:00:00 2001 From: k Date: Tue, 1 Jul 2025 15:59:39 -0400 Subject: [PATCH] fenGame fn connected to zig --- headers/main.h | 6 +++++ src/main.c | 63 ++++++++++++++++++++++++++++---------------------- src/main.zig | 54 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 94 insertions(+), 29 deletions(-) diff --git a/headers/main.h b/headers/main.h index d4157b5..6b4fd6e 100644 --- a/headers/main.h +++ b/headers/main.h @@ -1,4 +1,10 @@ #ifndef MAIN_H #define MAIN_H +#include "types.h" + int cmain(); +game *fenGame(char *str); +void playMoves(game *g, char *moves); +void uciGo(game *g); + #endif diff --git a/src/main.c b/src/main.c index f41bce7..5512ea5 100644 --- a/src/main.c +++ b/src/main.c @@ -10,6 +10,7 @@ #include "types.h" #include "help.h" #include "eval.h" +#include "main.h" #define BUFF_SIZE 4096 @@ -55,38 +56,44 @@ int cmain() { } else if (!strcmp("isready", token)) { printf("readyok\n"); } else if (!strcmp("go", token)) { - static move mov[1500];//big dumb buffer - size_t cnt = 0; - cnt += pawnMove(g,mov); - cnt += knightMove(g,(mov+cnt)); - cnt += rookMove(g,(mov+cnt)); - cnt += bishopMove(g,(mov+cnt)); - cnt += kingMove(g,(mov+cnt)); - cnt += queenMove(g,(mov+cnt)); - move *m = findBest(mov,cnt,g); - if(m == NULL){ - printf("bestmove 0000\n"); - continue; - } - - char *end = ""; - int yTo, xTo, yFrom, xFrom; - xTo = m->To % 8; - yTo = m->To / 8 + 1; - xFrom = m->From % 8; - yFrom = m->From / 8 + 1; - - long long bit = 1LL << m->From; - if (m->Promo) { - end = "q\n"; - } else { - end = "\n"; - } - printf("bestmove %c%d%c%d%s", ltz[xFrom], yFrom, ltz[xTo], yTo, end); + uciGo(g); } } } +void uciGo(game *g){ + char ltz[] = "abcdefgh"; + static move mov[1500];//big dumb buffer + size_t cnt = 0; + cnt += pawnMove(g,mov); + cnt += knightMove(g,(mov+cnt)); + cnt += rookMove(g,(mov+cnt)); + cnt += bishopMove(g,(mov+cnt)); + cnt += kingMove(g,(mov+cnt)); + cnt += queenMove(g,(mov+cnt)); + move *m = findBest(mov,cnt,g); + if(m == NULL){ + printf("bestmove 0000\n"); + return; + } + + char *end = ""; + int yTo, xTo, yFrom, xFrom; + xTo = m->To % 8; + yTo = m->To / 8 + 1; + xFrom = m->From % 8; + yFrom = m->From / 8 + 1; + + long long bit = 1LL << m->From; + if (m->Promo) { + end = "q\n"; + } else { + end = "\n"; + } + printf("bestmove %c%d%c%d%s", ltz[xFrom], yFrom, ltz[xTo], yTo, end); +} + + game *fenGame(char *str) { int rank = 7; int file = 0; diff --git a/src/main.zig b/src/main.zig index ff253a2..2800300 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,8 +1,60 @@ const std = @import("std"); const c = @cImport({ @cInclude("main.h"); + @cInclude("types.h"); }); pub fn main() !void { - _ = c.cmain(); + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const alloc = gpa.allocator(); + const stdin = std.io.getStdIn(); + var reader = stdin.reader(); + + while (true) { + const line = try reader.readUntilDelimiterAlloc(alloc, '\n', std.math.maxInt(usize)); + defer alloc.free(line); + const l = uci(line); + try std.io.getStdOut().writer().print("{s}", .{l}); + } +} + +fn uci(str: []const u8) []const u8 { + const pos = std.mem.indexOfAny(u8, str, " \t\n\r") orelse str.len; + const tok = str[0..pos]; + if (std.mem.eql(u8, tok, "uci")) return "id name RatChess 0.1\nid author rat<3\nuciok"; + if (std.mem.eql(u8, tok, "isready")) return "readyok\n"; + if (std.mem.eql(u8, tok, "go")) return "bestmove "; + if (std.mem.eql(u8, tok, "position")) _ = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); + return ""; +} + +fn uciPos(str: []const u8) [*c]c.game { + var buffer: [256]u8 = undefined; + const len = @min(str.len, buffer.len - 1); + @memcpy(buffer[0..len], str[0..len]); + buffer[len] = 0; + + const game = c.fenGame(&buffer); + return game; +} + +fn uciGo() []const u8 { + return "todo"; +} + +test "uci uci" { + const out = uci("uci"); + try std.testing.expect(std.mem.eql(u8, out, "id name RatChess 0.1\nid author rat<3\nuciok")); +} + +test "uci ready" { + const out = uci("isready"); + try std.testing.expect(std.mem.eql(u8, out, "readyok\n")); +} + +test "uci go" { + const out = uci("go"); + const pos = std.mem.indexOfAny(u8, out, " \t\n\r") orelse out.len; + try std.testing.expect(std.mem.eql(u8, out[0..pos], "bestmove")); }