From 93e7ff61e05294d4513d3d0690464a6b1fa2dcfd Mon Sep 17 00:00:00 2001 From: k Date: Fri, 18 Jul 2025 14:45:47 -0400 Subject: [PATCH 1/7] playMoves called corectly --- headers/main.h | 2 +- src/main.c | 9 +++++---- src/main.zig | 33 ++++++++++++++++++++++++++------- 3 files changed, 32 insertions(+), 12 deletions(-) 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); } From bb3bc4442a6c985c438efef7ec3b0eda149fa9c8 Mon Sep 17 00:00:00 2001 From: k Date: Thu, 25 Sep 2025 19:46:02 -0400 Subject: [PATCH 2/7] fenGame now in zig --- headers/help.h | 4 ++-- src/main.c | 4 +--- src/main.zig | 44 ++++++++++++++++++++++++++++++-------------- src/moves.c | 7 ++++--- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/headers/help.h b/headers/help.h index 56a533d..7b57f31 100644 --- a/headers/help.h +++ b/headers/help.h @@ -5,7 +5,7 @@ long long fullSet(sets *s); long long fullSetBoth(game *g); void print_bitboard(long long bitboard); -long long *findSet(game *g, long long bit); -long long *charToSet(game *g, char c); +unsigned long long *findSet(game *g, long long bit); +unsigned long long *charToSet(game *g, char c); #endif diff --git a/src/main.c b/src/main.c index 3f16ed0..1e67a3c 100644 --- a/src/main.c +++ b/src/main.c @@ -16,8 +16,6 @@ game *fenGame(char *str); game *playMoves(game *g, char *moves); -long long *findSet(game *g, long long bit); -long long *charToSet(game *g, char c); int cmain() { setbuf(stdin, NULL); @@ -123,7 +121,7 @@ game *fenGame(char *str) { } long long bit = 1ULL << (rank * 8 + file); - long long *set = charToSet(g, *str); + unsigned long long *set = charToSet(g, *str); if (set) *set |= bit; file++; diff --git a/src/main.zig b/src/main.zig index 4fa2a61..d5195dc 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,6 +2,7 @@ const std = @import("std"); const c = @cImport({ @cInclude("main.h"); @cInclude("types.h"); + @cInclude("help.h"); }); const uciTag = enum { @@ -27,11 +28,12 @@ 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"); + var game: *c.game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc); + defer alloc.destroy(game); while (true) { const line = try reader.readUntilDelimiterAlloc(alloc, '\n', std.math.maxInt(usize)); defer alloc.free(line); - switch (uci(line, game)) { + switch (uci(line, game, alloc)) { .text => |value| { try std.io.getStdOut().writer().print("{s}", .{value}); }, @@ -39,6 +41,7 @@ pub fn main() !void { try std.io.getStdOut().writer().print("bestmove {s}\n", .{value}); }, .game => |value| { + alloc.destroy(game); game = value; }, .exit => { @@ -51,35 +54,48 @@ pub fn main() !void { } } -fn uci(str: []const u8, game: *c.game) uciRet { +fn uci(str: []const u8, game: *c.game, alloc: std.mem.Allocator) 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 .{ .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(str[(pos + 1)..]) }; + 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 = {} }; } -fn uciPos(str: []const u8) [*c]c.game { +fn uciPos(str: []const u8, alloc: std.mem.Allocator) [*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..]); + var game = fenGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc); + if (std.mem.eql(u8, tok, "fen")) return fenGame(str[pos..], alloc); 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; - - return c.fenGame(&buffer); +fn fenGame(str: []const u8, alloc: std.mem.Allocator) [*c]c.game { + var pos: u8 = 0; + var space: u8 = 0; + const g = alloc.create(c.game) catch return null; + for (str) |chr| { + if (pos > 64) { + if (chr == ' ') space += 1; + if (space == 1 and chr == 'b') g.whiteToMove = false; + if (space == 1 and chr == 'w') g.whiteToMove = false; + continue; + } + if (std.ascii.isDigit(chr)) pos += @truncate(chr - '0'); + if (chr == '/') continue; + const set: [*c]c_ulonglong = c.charToSet(g, chr); + const bit: u64 = @as(u64, 1) << @truncate(pos); + if (set != null) + set.* |= @as(c_ulonglong, bit); + pos += 1; + } + return g; } fn playMoves(game: [*c]c.game, str: []const u8) [*c]c.game { diff --git a/src/moves.c b/src/moves.c index e1ba8d1..735e9f5 100644 --- a/src/moves.c +++ b/src/moves.c @@ -28,22 +28,22 @@ int pawnMove(game *g, move *moves) { // forword int to = i + movdir; - p = (promo & (1ULL << to)) ? 'q' : 0; if (to >= 0 && to < 64 && !(occupied & (1ULL << to))) { + p = (promo & (1ULL << to)) ? 'q' : 0; moves[index++] = (move){.From = i, .To = to, .Promo = p}; } // left to = i + capLeft; - p = (promo & (1ULL << to)) ? 'q' : 0; if (i % 8 > 0 && (to >= 0 && to < 64) && (enemy & (1ULL << to))) { + p = (promo & (1ULL << to)) ? 'q' : 0; moves[index++] = (move){.From = i, .To = to, .Promo = p}; } // right to = i + capRight; - p = (promo & (1ULL << to)) ? 'q' : 0; if (i % 8 < 7 && (to >= 0 && to < 64) && (enemy & (1ULL << to))) { + p = (promo & (1ULL << to)) ? 'q' : 0; moves[index++] = (move){.From = i, .To = to, .Promo = p}; } } @@ -78,6 +78,7 @@ int knightMove(game *g, move *moves) { (fileDiff == 2 && rankDiff == 1))) continue; + if(to < 0 || to > 63) continue; long long destBit = 1ULL << to; if (occupied & destBit) continue; From f9dda928b2b9da0c5c27143859ae98b03fd9ba5f Mon Sep 17 00:00:00 2001 From: k Date: Fri, 26 Sep 2025 11:04:35 -0400 Subject: [PATCH 3/7] updated tests --- src/main.zig | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/main.zig b/src/main.zig index d5195dc..486a233 100644 --- a/src/main.zig +++ b/src/main.zig @@ -65,7 +65,7 @@ fn uci(str: []const u8, game: *c.game, alloc: std.mem.Allocator) uciRet { return .{ .pass = {} }; } -fn uciPos(str: []const u8, alloc: std.mem.Allocator) [*c]c.game { +fn uciPos(str: []const u8, alloc: std.mem.Allocator) *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", alloc); @@ -112,25 +112,46 @@ fn uciGo(game: *c.game) []const u8 { } test "uci uci" { - const game = uciPos("fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); - const out = uci("uci", game); + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const alloc = gpa.allocator(); + const game = uciPos("fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc); + defer alloc.destroy(game); + + const out = uci("uci", game, alloc); try std.testing.expect(out == .text); } test "uci ready" { - const game = uciPos("fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); - const out = uci("isready", game); + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const alloc = gpa.allocator(); + const game = uciPos("fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc); + defer alloc.destroy(game); + + const out = uci("isready", game, alloc); try std.testing.expect(out == .text); } test "uci go" { - const game = uciPos("fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); - const out = uci("go", game); + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const alloc = gpa.allocator(); + const game = uciPos("fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc); + defer alloc.destroy(game); + + const out = uci("go", game, alloc); try std.testing.expect(out == .move); } test "uci position" { - const game = uciPos("fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); - const out = uci("position startpos", game); + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const alloc = gpa.allocator(); + const game = uciPos("fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc); + defer alloc.destroy(game); + + const out = uci("position startpos", game, alloc); try std.testing.expect(out == .game); + alloc.destroy(out.game); } From 059f672c1761dad16d6aeffaa1b4216e99544b04 Mon Sep 17 00:00:00 2001 From: k Date: Fri, 26 Sep 2025 11:18:46 -0400 Subject: [PATCH 4/7] fixed memory leak --- src/main.zig | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main.zig b/src/main.zig index 486a233..84d0897 100644 --- a/src/main.zig +++ b/src/main.zig @@ -68,12 +68,14 @@ fn uci(str: []const u8, game: *c.game, alloc: std.mem.Allocator) uciRet { fn uciPos(str: []const u8, alloc: std.mem.Allocator) *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", alloc); + //var game = fenGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc); if (std.mem.eql(u8, tok, "fen")) return fenGame(str[pos..], alloc); if (std.mem.eql(u8, tok, "startpos")) { + var game = fenGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc); game = playMoves(game, str[pos..]); + return game; } - return game; + return fenGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc); //this should be an error } fn fenGame(str: []const u8, alloc: std.mem.Allocator) [*c]c.game { From ca1e62397c6633c6faae12fed8b02493029df399 Mon Sep 17 00:00:00 2001 From: k Date: Fri, 26 Sep 2025 13:30:32 -0400 Subject: [PATCH 5/7] fixed fenGame not updateing whiteToMove --- src/main.zig | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main.zig b/src/main.zig index 84d0897..a6b00a8 100644 --- a/src/main.zig +++ b/src/main.zig @@ -86,7 +86,7 @@ fn fenGame(str: []const u8, alloc: std.mem.Allocator) [*c]c.game { if (pos > 64) { if (chr == ' ') space += 1; if (space == 1 and chr == 'b') g.whiteToMove = false; - if (space == 1 and chr == 'w') g.whiteToMove = false; + if (space == 1 and chr == 'w') g.whiteToMove = true; continue; } if (std.ascii.isDigit(chr)) pos += @truncate(chr - '0'); @@ -154,6 +154,7 @@ test "uci position" { defer alloc.destroy(game); const out = uci("position startpos", game, alloc); + defer alloc.destroy(out.game); try std.testing.expect(out == .game); - alloc.destroy(out.game); + try std.testing.expect(out.game.whiteToMove == true); } From eccd8efdbac02b8113e9e90eff2aaa55d42bd94f Mon Sep 17 00:00:00 2001 From: k Date: Fri, 26 Sep 2025 13:32:22 -0400 Subject: [PATCH 6/7] playMoves now in zig --- headers/eval.h | 2 +- src/main.zig | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/headers/eval.h b/headers/eval.h index 3558b80..69c4c1c 100644 --- a/headers/eval.h +++ b/headers/eval.h @@ -1,5 +1,5 @@ #ifndef EVAL_H #define EVAL_H -move *findBest(move* move, size_t size, game* g); +move *findBest(move* move, long long size, game* g); void makeMove(game *g, move* m); #endif diff --git a/src/main.zig b/src/main.zig index a6b00a8..7733d9c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,6 +3,7 @@ const c = @cImport({ @cInclude("main.h"); @cInclude("types.h"); @cInclude("help.h"); + @cInclude("eval.h"); }); const uciTag = enum { @@ -101,12 +102,17 @@ fn fenGame(str: []const u8, alloc: std.mem.Allocator) [*c]c.game { } 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); + if (str.len < 4) return game; + var splitItr = std.mem.splitSequence(u8, str, " "); + 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.Promo = if (moveString.len == 5) moveString[4] else 0; + c.makeMove(game, &move); + } + return game; } fn uciGo(game: *c.game) []const u8 { From d6fa80950550fef630f0d48e9206c9ce48866477 Mon Sep 17 00:00:00 2001 From: k Date: Mon, 29 Sep 2025 11:29:55 -0400 Subject: [PATCH 7/7] started work on move gen --- src/main.zig | 22 ++++++++++++++++------ src/move.zig | 25 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 src/move.zig 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 }; +// }