From da8eb581e3c936843403ab71edea819b164ab9f0 Mon Sep 17 00:00:00 2001 From: k Date: Sat, 12 Jul 2025 15:08:12 -0400 Subject: [PATCH] C go added and uci ret type --- headers/main.h | 2 +- src/main.c | 9 ++++-- src/main.zig | 75 ++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 65 insertions(+), 21 deletions(-) diff --git a/headers/main.h b/headers/main.h index 6b4fd6e..767bf47 100644 --- a/headers/main.h +++ b/headers/main.h @@ -5,6 +5,6 @@ int cmain(); game *fenGame(char *str); void playMoves(game *g, char *moves); -void uciGo(game *g); +char* uciGo(game *g); #endif diff --git a/src/main.c b/src/main.c index 5512ea5..a5920a0 100644 --- a/src/main.c +++ b/src/main.c @@ -61,7 +61,7 @@ int cmain() { } } -void uciGo(game *g){ +char* uciGo(game *g){ char ltz[] = "abcdefgh"; static move mov[1500];//big dumb buffer size_t cnt = 0; @@ -74,7 +74,7 @@ void uciGo(game *g){ move *m = findBest(mov,cnt,g); if(m == NULL){ printf("bestmove 0000\n"); - return; + return ""; } char *end = ""; @@ -90,7 +90,10 @@ void uciGo(game *g){ } else { end = "\n"; } - printf("bestmove %c%d%c%d%s", ltz[xFrom], yFrom, ltz[xTo], yTo, end); + + char* resultString = (char*)malloc(10 * sizeof(char)); + sprintf(resultString, "%c%d%c%d%s", ltz[xFrom], yFrom, ltz[xTo], yTo, end); + return resultString; } diff --git a/src/main.zig b/src/main.zig index 2800300..cd79cf4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4,6 +4,22 @@ const c = @cImport({ @cInclude("types.h"); }); +const uciTag = enum { + text, + move, + game, + exit, + pass, +}; + +const uciRet = union(uciTag) { + text: []const u8, + move: []const u8, + game: *c.game, + exit: void, + pass: void, +}; + pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); @@ -11,22 +27,39 @@ pub fn main() !void { const stdin = std.io.getStdIn(); var reader = stdin.reader(); + var game: *c.game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); 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}); + switch (uci(line, game)) { + .text => |value| { + try std.io.getStdOut().writer().print("{s}", .{value}); + }, + .move => |value| { + try std.io.getStdOut().writer().print("bestmove {s}\n", .{value}); + }, + .game => |value| { + game = value; + }, + .exit => { + break; + }, + .pass => { + try std.io.getStdOut().writer().print("info bad input\n", .{}); + }, + } } } -fn uci(str: []const u8) []const u8 { +fn uci(str: []const u8, game: *c.game) uciRet { 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 ""; + if (std.mem.eql(u8, tok, "uci")) return .{ .text = "id name RatChess 0.1\nid author rat<3\nuciok\n" }; + if (std.mem.eql(u8, tok, "isready")) return .{ .text = "readyok\n" }; + if (std.mem.eql(u8, tok, "go")) return .{ .move = uciGo(game) }; + if (std.mem.eql(u8, tok, "position")) return .{ .game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1") }; + if (std.mem.eql(u8, tok, "exit")) return .{ .exit = {} }; + return .{ .pass = {} }; } fn uciPos(str: []const u8) [*c]c.game { @@ -39,22 +72,30 @@ fn uciPos(str: []const u8) [*c]c.game { return game; } -fn uciGo() []const u8 { - return "todo"; +fn uciGo(game: *c.game) []const u8 { + return std.mem.span(c.uciGo(game)); } 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")); + const game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); + const out = uci("uci", game); + try std.testing.expect(out == .text); } test "uci ready" { - const out = uci("isready"); - try std.testing.expect(std.mem.eql(u8, out, "readyok\n")); + const game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); + const out = uci("isready", game); + try std.testing.expect(out == .text); } 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")); + const game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); + const out = uci("go", game); + try std.testing.expect(out == .move); +} + +test "uci position" { + const game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); + const out = uci("position startpos", game); + try std.testing.expect(out == .game); }