diff --git a/headers/main.h b/headers/main.h index 767bf47..e000f6d 100644 --- a/headers/main.h +++ b/headers/main.h @@ -4,7 +4,7 @@ int cmain(); game *fenGame(char *str); -void playMoves(game *g, char *moves); +game *playMoves(game *g, char *moves); char* uciGo(game *g); #endif diff --git a/src/main.c b/src/main.c index a5920a0..3f16ed0 100644 --- a/src/main.c +++ b/src/main.c @@ -15,7 +15,7 @@ #define BUFF_SIZE 4096 game *fenGame(char *str); -void playMoves(game *g, char *moves); +game *playMoves(game *g, char *moves); long long *findSet(game *g, long long bit); long long *charToSet(game *g, char c); @@ -73,8 +73,8 @@ char* uciGo(game *g){ cnt += queenMove(g,(mov+cnt)); move *m = findBest(mov,cnt,g); if(m == NULL){ - printf("bestmove 0000\n"); - return ""; + //printf("bestmove 0000\n"); + return "0000"; } char *end = ""; @@ -134,7 +134,7 @@ game *fenGame(char *str) { return g; } -void playMoves(game *g, char *moves) { +game* playMoves(game *g, char *moves) { char *moveStr; moveStr = strtok_r(moves, " ", &moves); moveStr = strtok_r(moves, " ", &moves); @@ -146,5 +146,6 @@ void playMoves(game *g, char *moves) { makeMove(g, &m); moveStr = strtok_r(moves, " ", &moves); } + return g; } diff --git a/src/main.zig b/src/main.zig index cd79cf4..4fa2a61 100644 --- a/src/main.zig +++ b/src/main.zig @@ -57,19 +57,38 @@ fn uci(str: []const u8, game: *c.game) uciRet { 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, "position")) return .{ .game = uciPos(str[(pos + 1)..]) }; if (std.mem.eql(u8, tok, "exit")) return .{ .exit = {} }; return .{ .pass = {} }; } fn uciPos(str: []const u8) [*c]c.game { + const pos = std.mem.indexOfAny(u8, str, " \t\n\r") orelse str.len; + const tok = str[0..pos]; + var game = fenGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); + if (std.mem.eql(u8, tok, "fen")) return fenGame(str[pos..]); + if (std.mem.eql(u8, tok, "startpos")) { + game = playMoves(game, str[pos..]); + } + return game; +} + +fn fenGame(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; + return c.fenGame(&buffer); +} + +fn playMoves(game: [*c]c.game, str: []const u8) [*c]c.game { + var buffer: [2560]u8 = undefined; //this is bad + const len = @min(str.len, buffer.len - 1); + @memcpy(buffer[0..len], str[0..len]); + buffer[len] = 0; + + return c.playMoves(game, &buffer); } fn uciGo(game: *c.game) []const u8 { @@ -77,25 +96,25 @@ fn uciGo(game: *c.game) []const u8 { } test "uci uci" { - const game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); + const game = uciPos("fen 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 game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); + const game = uciPos("fen 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 game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); + const game = uciPos("fen 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 game = uciPos("fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); const out = uci("position startpos", game); try std.testing.expect(out == .game); }