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();
game *fenGame(char *str);
void playMoves(game *g, char *moves);
void uciGo(game *g);
char* uciGo(game *g);
#endif

View File

@ -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;
}

View File

@ -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);
}