Updated types to use more zig types in zig code

This commit is contained in:
k 2025-10-21 19:11:07 -04:00
parent 054cd41cf6
commit 10f81cf7f0
3 changed files with 55 additions and 30 deletions

View File

@ -1,28 +1,6 @@
const std = @import("std"); const std = @import("std");
const mov = @import("move.zig"); const mov = @import("move.zig");
const c = @cImport({ const types = @import("types.zig");
@cInclude("main.h");
@cInclude("types.h");
@cInclude("help.h");
@cInclude("eval.h");
@cInclude("moves.h");
});
const uciTag = enum {
text,
move,
game,
exit,
pass,
};
const uciRet = union(uciTag) {
text: []const u8,
move: []u8,
game: *c.game,
exit: void,
pass: void,
};
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
@ -31,7 +9,7 @@ pub fn main() !void {
const stdin = std.io.getStdIn(); const stdin = std.io.getStdIn();
var reader = stdin.reader(); var reader = stdin.reader();
var game: *c.game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc); var game: *types.game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc);
defer alloc.destroy(game); defer alloc.destroy(game);
while (true) { while (true) {
const line = try reader.readUntilDelimiterAlloc(alloc, '\n', std.math.maxInt(usize)); const line = try reader.readUntilDelimiterAlloc(alloc, '\n', std.math.maxInt(usize));
@ -58,7 +36,7 @@ pub fn main() !void {
} }
} }
fn uci(str: []const u8, game: *c.game, alloc: std.mem.Allocator) uciRet { fn uci(str: []const u8, game: *types.game, alloc: std.mem.Allocator) types.uciRet {
const pos = std.mem.indexOfAny(u8, str, " \t\n\r") orelse str.len; const pos = std.mem.indexOfAny(u8, str, " \t\n\r") orelse str.len;
const tok = str[0..pos]; 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, "uci")) return .{ .text = "id name RatChess 0.1\nid author rat<3\nuciok\n" };
@ -69,7 +47,7 @@ fn uci(str: []const u8, game: *c.game, alloc: std.mem.Allocator) uciRet {
return .{ .pass = {} }; return .{ .pass = {} };
} }
fn uciPos(str: []const u8, alloc: std.mem.Allocator) *c.game { 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 pos = std.mem.indexOfAny(u8, str, " \t\n\r") orelse str.len;
const tok = str[0..pos]; 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);
@ -82,7 +60,7 @@ fn uciPos(str: []const u8, alloc: std.mem.Allocator) *c.game {
return fenGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc); //this should be an error 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 { fn fenGame(str: []const u8, alloc: std.mem.Allocator) *types.game {
var pos: u8 = 0; var pos: u8 = 0;
var space: u8 = 0; var space: u8 = 0;
const g = alloc.create(c.game) catch return null; const g = alloc.create(c.game) catch return null;
@ -118,8 +96,8 @@ fn playMoves(game: [*c]c.game, str: []const u8) [*c]c.game {
return game; return game;
} }
fn uciGo(game: *c.game, alloc: std.mem.Allocator) []u8 { fn uciGo(game: *types.game, alloc: std.mem.Allocator) []u8 {
var moves = std.ArrayList(c.move).init(alloc); var moves = std.ArrayList(types.move).init(alloc);
defer moves.deinit(); defer moves.deinit();
const str = alloc.alloc(u8, 5) catch unreachable; const str = alloc.alloc(u8, 5) catch unreachable;
const m = c.move{ .From = 0, .To = 8, .Promo = 0 }; const m = c.move{ .From = 0, .To = 8, .Promo = 0 };

View File

@ -1,4 +1,5 @@
const std = @import("std"); const std = @import("std");
const types = @import("types.zig");
const c = @cImport({ const c = @cImport({
@cInclude("main.h"); @cInclude("main.h");
@cInclude("types.h"); @cInclude("types.h");
@ -7,7 +8,7 @@ const c = @cImport({
@cInclude("moves.h"); @cInclude("moves.h");
}); });
pub fn moveTypeToStr(move: c.move, buf: []u8) void { pub fn moveTypeToStr(move: types.move, buf: []u8) void {
const xTo = @mod(move.To, 8); const xTo = @mod(move.To, 8);
const yTo = @divTrunc(move.To, 8); const yTo = @divTrunc(move.To, 8);
const xFrom = @mod(move.From, 8); const xFrom = @mod(move.From, 8);

46
src/types.zig Normal file
View File

@ -0,0 +1,46 @@
const c = @cImport({
@cInclude("main.h");
@cInclude("types.h");
@cInclude("help.h");
@cInclude("eval.h");
@cInclude("moves.h");
});
const uciTag = enum {
text,
move,
game,
exit,
pass,
};
pub const uciRet = union(uciTag) {
text: []const u8,
move: []u8,
game: *game,
exit: void,
pass: void,
};
const set = struct {
pawns: u64,
knights: u64,
bishops: u64,
rooks: u64,
queen: u64,
king: u64,
};
pub const game = struct {
black: set,
white: set,
whiteToMove: bool,
};
pub const move = struct {
From: u32,
To: u32,
Promo: u8,
};
// struct {
// To: u8,
// From: u8,
// Promo: u8,
// };