C go added and uci ret type
All checks were successful
Verify build / verify_build (push) Successful in 1m3s

This commit is contained in:
k 2025-07-12 15:08:12 -04:00
parent f9cdd9a259
commit da8eb581e3
3 changed files with 65 additions and 21 deletions

View File

@ -5,6 +5,6 @@
int cmain(); int cmain();
game *fenGame(char *str); game *fenGame(char *str);
void playMoves(game *g, char *moves); void playMoves(game *g, char *moves);
void uciGo(game *g); char* uciGo(game *g);
#endif #endif

View File

@ -61,7 +61,7 @@ int cmain() {
} }
} }
void uciGo(game *g){ char* uciGo(game *g){
char ltz[] = "abcdefgh"; char ltz[] = "abcdefgh";
static move mov[1500];//big dumb buffer static move mov[1500];//big dumb buffer
size_t cnt = 0; size_t cnt = 0;
@ -74,7 +74,7 @@ void uciGo(game *g){
move *m = findBest(mov,cnt,g); move *m = findBest(mov,cnt,g);
if(m == NULL){ if(m == NULL){
printf("bestmove 0000\n"); printf("bestmove 0000\n");
return; return "";
} }
char *end = ""; char *end = "";
@ -90,7 +90,10 @@ void uciGo(game *g){
} else { } else {
end = "\n"; 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;
} }

View File

@ -4,6 +4,22 @@ const c = @cImport({
@cInclude("types.h"); @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 { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();
@ -11,22 +27,39 @@ pub fn main() !void {
const stdin = std.io.getStdIn(); const stdin = std.io.getStdIn();
var reader = stdin.reader(); var reader = stdin.reader();
var game: *c.game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
while (true) { while (true) {
const line = try reader.readUntilDelimiterAlloc(alloc, '\n', std.math.maxInt(usize)); const line = try reader.readUntilDelimiterAlloc(alloc, '\n', std.math.maxInt(usize));
defer alloc.free(line); defer alloc.free(line);
const l = uci(line); switch (uci(line, game)) {
try std.io.getStdOut().writer().print("{s}", .{l}); .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 pos = std.mem.indexOfAny(u8, str, " \t\n\r") orelse str.len;
const tok = str[0..pos]; 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, "uci")) return .{ .text = "id name RatChess 0.1\nid author rat<3\nuciok\n" };
if (std.mem.eql(u8, tok, "isready")) return "readyok\n"; if (std.mem.eql(u8, tok, "isready")) return .{ .text = "readyok\n" };
if (std.mem.eql(u8, tok, "go")) return "bestmove "; if (std.mem.eql(u8, tok, "go")) return .{ .move = uciGo(game) };
if (std.mem.eql(u8, tok, "position")) _ = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); if (std.mem.eql(u8, tok, "position")) return .{ .game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1") };
return ""; if (std.mem.eql(u8, tok, "exit")) return .{ .exit = {} };
return .{ .pass = {} };
} }
fn uciPos(str: []const u8) [*c]c.game { fn uciPos(str: []const u8) [*c]c.game {
@ -39,22 +72,30 @@ fn uciPos(str: []const u8) [*c]c.game {
return game; return game;
} }
fn uciGo() []const u8 { fn uciGo(game: *c.game) []const u8 {
return "todo"; return std.mem.span(c.uciGo(game));
} }
test "uci uci" { test "uci uci" {
const out = uci("uci"); const game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
try std.testing.expect(std.mem.eql(u8, out, "id name RatChess 0.1\nid author rat<3\nuciok")); const out = uci("uci", game);
try std.testing.expect(out == .text);
} }
test "uci ready" { test "uci ready" {
const out = uci("isready"); const game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
try std.testing.expect(std.mem.eql(u8, out, "readyok\n")); const out = uci("isready", game);
try std.testing.expect(out == .text);
} }
test "uci go" { test "uci go" {
const out = uci("go"); const game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
const pos = std.mem.indexOfAny(u8, out, " \t\n\r") orelse out.len; const out = uci("go", game);
try std.testing.expect(std.mem.eql(u8, out[0..pos], "bestmove")); 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);
} }