diff --git a/src/main.zig b/src/main.zig index 7733d9c..1e7c812 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,9 +1,11 @@ const std = @import("std"); +const mov = @import("move.zig"); const c = @cImport({ @cInclude("main.h"); @cInclude("types.h"); @cInclude("help.h"); @cInclude("eval.h"); + @cInclude("moves.h"); }); const uciTag = enum { @@ -16,7 +18,7 @@ const uciTag = enum { const uciRet = union(uciTag) { text: []const u8, - move: []const u8, + move: []u8, game: *c.game, exit: void, pass: void, @@ -40,6 +42,7 @@ pub fn main() !void { }, .move => |value| { try std.io.getStdOut().writer().print("bestmove {s}\n", .{value}); + alloc.free(value); }, .game => |value| { alloc.destroy(game); @@ -60,7 +63,7 @@ fn uci(str: []const u8, game: *c.game, alloc: std.mem.Allocator) uciRet { const tok = str[0..pos]; 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, "go")) return .{ .move = uciGo(game, alloc) }; if (std.mem.eql(u8, tok, "position")) return .{ .game = uciPos(str[(pos + 1)..], alloc) }; if (std.mem.eql(u8, tok, "exit")) return .{ .exit = {} }; return .{ .pass = {} }; @@ -107,16 +110,23 @@ fn playMoves(game: [*c]c.game, str: []const u8) [*c]c.game { while (splitItr.next()) |moveString| { var move: c.move = .{ .To = 0, .From = 0, .Promo = 0 }; if (moveString.len < 4) continue; - move.From = (moveString[0] - 'a') + (moveString[1] - '1'); - move.To = (moveString[2] - 'a') + (moveString[3] - '1'); + move.From = (moveString[0] - 'a') + (moveString[1] - '1') * 8; + move.To = (moveString[2] - 'a') + (moveString[3] - '1') * 8; move.Promo = if (moveString.len == 5) moveString[4] else 0; c.makeMove(game, &move); } return game; } -fn uciGo(game: *c.game) []const u8 { - return std.mem.span(c.uciGo(game)); +fn uciGo(game: *c.game, alloc: std.mem.Allocator) []u8 { + var moves = std.ArrayList(c.move).init(alloc); + defer moves.deinit(); + const str = alloc.alloc(u8, 5) catch unreachable; + const m = c.move{ .From = 0, .To = 8, .Promo = 0 }; + moves.append(m) catch return str; + _ = game; + mov.moveTypeToStr(m, str); + return str; } test "uci uci" { diff --git a/src/move.zig b/src/move.zig new file mode 100644 index 0000000..e3be055 --- /dev/null +++ b/src/move.zig @@ -0,0 +1,25 @@ +const std = @import("std"); +const c = @cImport({ + @cInclude("main.h"); + @cInclude("types.h"); + @cInclude("help.h"); + @cInclude("eval.h"); + @cInclude("moves.h"); +}); + +pub fn moveTypeToStr(move: c.move, buf: []u8) void { + const xTo = @mod(move.To, 8); + const yTo = @divTrunc(move.To, 8); + const xFrom = @mod(move.From, 8); + const yFrom = @divTrunc(move.From, 8); + + buf[0] = @intCast(xFrom + 'a'); + buf[1] = @intCast(yFrom + '0' + 1); + buf[2] = @intCast(xTo + 'a'); + buf[3] = @intCast(yTo + '0' + 1); + buf[4] = move.Promo; +} + +// pub fn pawnMove(arr: std.ArrayList(c.move)) void { +// const move = c.move{ .From = 8, .To = 16, .Promo = 0 }; +// }