This commit is contained in:
parent
eccd8efdba
commit
d6fa809505
22
src/main.zig
22
src/main.zig
@ -1,9 +1,11 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const mov = @import("move.zig");
|
||||||
const c = @cImport({
|
const c = @cImport({
|
||||||
@cInclude("main.h");
|
@cInclude("main.h");
|
||||||
@cInclude("types.h");
|
@cInclude("types.h");
|
||||||
@cInclude("help.h");
|
@cInclude("help.h");
|
||||||
@cInclude("eval.h");
|
@cInclude("eval.h");
|
||||||
|
@cInclude("moves.h");
|
||||||
});
|
});
|
||||||
|
|
||||||
const uciTag = enum {
|
const uciTag = enum {
|
||||||
@ -16,7 +18,7 @@ const uciTag = enum {
|
|||||||
|
|
||||||
const uciRet = union(uciTag) {
|
const uciRet = union(uciTag) {
|
||||||
text: []const u8,
|
text: []const u8,
|
||||||
move: []const u8,
|
move: []u8,
|
||||||
game: *c.game,
|
game: *c.game,
|
||||||
exit: void,
|
exit: void,
|
||||||
pass: void,
|
pass: void,
|
||||||
@ -40,6 +42,7 @@ pub fn main() !void {
|
|||||||
},
|
},
|
||||||
.move => |value| {
|
.move => |value| {
|
||||||
try std.io.getStdOut().writer().print("bestmove {s}\n", .{value});
|
try std.io.getStdOut().writer().print("bestmove {s}\n", .{value});
|
||||||
|
alloc.free(value);
|
||||||
},
|
},
|
||||||
.game => |value| {
|
.game => |value| {
|
||||||
alloc.destroy(game);
|
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];
|
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" };
|
||||||
if (std.mem.eql(u8, tok, "isready")) return .{ .text = "readyok\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, "position")) return .{ .game = uciPos(str[(pos + 1)..], alloc) };
|
||||||
if (std.mem.eql(u8, tok, "exit")) return .{ .exit = {} };
|
if (std.mem.eql(u8, tok, "exit")) return .{ .exit = {} };
|
||||||
return .{ .pass = {} };
|
return .{ .pass = {} };
|
||||||
@ -107,16 +110,23 @@ fn playMoves(game: [*c]c.game, str: []const u8) [*c]c.game {
|
|||||||
while (splitItr.next()) |moveString| {
|
while (splitItr.next()) |moveString| {
|
||||||
var move: c.move = .{ .To = 0, .From = 0, .Promo = 0 };
|
var move: c.move = .{ .To = 0, .From = 0, .Promo = 0 };
|
||||||
if (moveString.len < 4) continue;
|
if (moveString.len < 4) continue;
|
||||||
move.From = (moveString[0] - 'a') + (moveString[1] - '1');
|
move.From = (moveString[0] - 'a') + (moveString[1] - '1') * 8;
|
||||||
move.To = (moveString[2] - 'a') + (moveString[3] - '1');
|
move.To = (moveString[2] - 'a') + (moveString[3] - '1') * 8;
|
||||||
move.Promo = if (moveString.len == 5) moveString[4] else 0;
|
move.Promo = if (moveString.len == 5) moveString[4] else 0;
|
||||||
c.makeMove(game, &move);
|
c.makeMove(game, &move);
|
||||||
}
|
}
|
||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn uciGo(game: *c.game) []const u8 {
|
fn uciGo(game: *c.game, alloc: std.mem.Allocator) []u8 {
|
||||||
return std.mem.span(c.uciGo(game));
|
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" {
|
test "uci uci" {
|
||||||
|
|||||||
25
src/move.zig
Normal file
25
src/move.zig
Normal file
@ -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 };
|
||||||
|
// }
|
||||||
Loading…
x
Reference in New Issue
Block a user