From 7cded275f23d9e9f534632d61d1de5d6ea24ea54 Mon Sep 17 00:00:00 2001 From: k Date: Tue, 21 Oct 2025 19:16:21 -0400 Subject: [PATCH] updated fenGame function. --- src/main.zig | 55 +++++++++++++++++++++++++--------------------------- src/move.zig | 14 +++++++++++++ 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/src/main.zig b/src/main.zig index eea0b45..f6253df 100644 --- a/src/main.zig +++ b/src/main.zig @@ -50,11 +50,10 @@ fn uci(str: []const u8, game: *types.game, alloc: std.mem.Allocator) types.uciRe fn uciPos(str: []const u8, alloc: std.mem.Allocator) *types.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); 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..]); + game = mov.playMoves(game, str[pos..]); return game; } return fenGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc); //this should be an error @@ -63,47 +62,43 @@ fn uciPos(str: []const u8, alloc: std.mem.Allocator) *types.game { fn fenGame(str: []const u8, alloc: std.mem.Allocator) *types.game { var pos: u8 = 0; var space: u8 = 0; - const g = alloc.create(c.game) catch return null; + var g = alloc.create(types.game) catch unreachable; + g.* = std.mem.zeroes(types.game); 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 = true; + if (chr == ' ') { + space += 1; + continue; + } + if (space == 1) { + if (chr == 'b') g.whiteToMove = false; + if (chr == 'w') g.whiteToMove = true; + //continue; + break; + } + + if (std.ascii.isDigit(chr)) { + pos += @truncate(chr - '0'); continue; } - if (std.ascii.isDigit(chr)) pos += @truncate(chr - '0'); if (chr == '/') continue; - const set: [*c]c_ulonglong = c.charToSet(g, chr); + const set: *u64 = mov.charToSet(g, chr); const bit: u64 = @as(u64, 1) << @truncate(pos); - if (set != null) - set.* |= @as(c_ulonglong, bit); + set.* |= bit; pos += 1; } return g; } -fn playMoves(game: [*c]c.game, str: []const u8) [*c]c.game { - 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') * 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: *types.game, alloc: std.mem.Allocator) []u8 { var moves = std.ArrayList(types.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); + mov.knightMove(game, &moves); + if (moves.capacity == 0) { + @memcpy(str.ptr, "err"); + return str; + } + mov.moveTypeToStr(moves.items[0], str); return str; } @@ -152,4 +147,6 @@ test "uci position" { defer alloc.destroy(out.game); try std.testing.expect(out == .game); try std.testing.expect(out.game.whiteToMove == true); + try std.testing.expect(out.game.white.king != 0); + try std.testing.expect(out.game.black.king != 0); } diff --git a/src/move.zig b/src/move.zig index 29cb32d..f658614 100644 --- a/src/move.zig +++ b/src/move.zig @@ -21,6 +21,20 @@ pub fn moveTypeToStr(move: types.move, buf: []u8) void { buf[4] = move.Promo; } +pub fn playMoves(game: *types.game, str: []const u8) *types.game { + if (str.len < 4) return game; + var splitItr = std.mem.splitSequence(u8, str, " "); + while (splitItr.next()) |moveString| { + var move: types.move = .{ .To = 0, .From = 0, .Promo = 0 }; + if (moveString.len < 4) continue; + 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(@ptrCast(game), @ptrCast(&move)); + } + return game; +} + // pub fn pawnMove(arr: std.ArrayList(c.move)) void { // const move = c.move{ .From = 8, .To = 16, .Promo = 0 }; // }