This commit is contained in:
11
src/main.zig
11
src/main.zig
@@ -43,7 +43,7 @@ fn uci(str: []const u8, game: *types.game, alloc: std.mem.Allocator) types.uciRe
|
|||||||
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, alloc) };
|
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, "quit")) return .{ .exit = {} };
|
||||||
return .{ .pass = {} };
|
return .{ .pass = {} };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,11 +93,17 @@ fn uciGo(game: *types.game, alloc: std.mem.Allocator) []u8 {
|
|||||||
var moves = std.ArrayList(types.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;
|
||||||
|
mov.pawnMove(game, &moves);
|
||||||
|
mov.rookMove(game, &moves);
|
||||||
mov.knightMove(game, &moves);
|
mov.knightMove(game, &moves);
|
||||||
|
mov.bishipMove(game, &moves);
|
||||||
|
mov.kingMove(game, &moves);
|
||||||
|
mov.quenMove(game, &moves);
|
||||||
if (moves.capacity == 0) {
|
if (moves.capacity == 0) {
|
||||||
@memcpy(str.ptr, "0000");
|
@memcpy(str.ptr, "0000");
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
mov.moveTypeToStr(moves.items[0], str);
|
mov.moveTypeToStr(moves.items[0], str);
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
@@ -141,7 +147,9 @@ test "uci position" {
|
|||||||
defer _ = gpa.deinit();
|
defer _ = gpa.deinit();
|
||||||
const alloc = gpa.allocator();
|
const alloc = gpa.allocator();
|
||||||
const game = uciPos("fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc);
|
const game = uciPos("fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc);
|
||||||
|
const gameb = uciPos("startpos", alloc);
|
||||||
defer alloc.destroy(game);
|
defer alloc.destroy(game);
|
||||||
|
defer alloc.destroy(gameb);
|
||||||
|
|
||||||
const out = uci("position startpos", game, alloc);
|
const out = uci("position startpos", game, alloc);
|
||||||
defer alloc.destroy(out.game);
|
defer alloc.destroy(out.game);
|
||||||
@@ -149,4 +157,5 @@ test "uci position" {
|
|||||||
try std.testing.expect(out.game.whiteToMove == true);
|
try std.testing.expect(out.game.whiteToMove == true);
|
||||||
try std.testing.expect(out.game.white.king != 0);
|
try std.testing.expect(out.game.white.king != 0);
|
||||||
try std.testing.expect(out.game.black.king != 0);
|
try std.testing.expect(out.game.black.king != 0);
|
||||||
|
try std.testing.expectEqualDeep(game, gameb);
|
||||||
}
|
}
|
||||||
|
|||||||
84
src/move.zig
84
src/move.zig
@@ -106,13 +106,61 @@ pub fn charToSet(g: *types.game, chr: u8) *u64 {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn pawnMove(arr: std.ArrayList(c.move)) void {
|
pub fn pawnMove(game: *types.game, arr: *std.ArrayList(types.move)) void {
|
||||||
// const move = c.move{ .From = 8, .To = 16, .Promo = 0 };
|
_ = game;
|
||||||
// }
|
_ = arr;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn sqrScan(rook: u64, hit: u64, flip: bool) u64 {
|
pub fn bishipMove(game: *types.game, arr: *std.ArrayList(types.move)) void {
|
||||||
const vecLut: [64]u64 = undefined; //todo comptime init
|
_ = game;
|
||||||
|
_ = arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn kingMove(game: *types.game, arr: *std.ArrayList(types.move)) void {
|
||||||
|
_ = game;
|
||||||
|
_ = arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn quenMove(game: *types.game, arr: *std.ArrayList(types.move)) void {
|
||||||
|
const set = if (game.whiteToMove) &game.white.queen else &game.black.queen;
|
||||||
|
const full = fullSet(&game.white) | fullSet(&game.black);
|
||||||
|
var val = set.*;
|
||||||
|
const n = @popCount(val);
|
||||||
|
for (0..n) |_| {
|
||||||
|
const pos = @ctz(val);
|
||||||
|
const bit = @as(u64, 1) << @truncate(pos);
|
||||||
|
var moves = sqrScan(bit, full, false);
|
||||||
|
moves |= @bitReverse(sqrScan(bit, full, true));
|
||||||
|
val = val ^ (@as(u64, 1) << @truncate(pos));
|
||||||
|
|
||||||
|
bitboardToMoves(pos, moves, arr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rookMove(game: *types.game, arr: *std.ArrayList(types.move)) void {
|
||||||
|
const set = if (game.whiteToMove) &game.white.rooks else &game.black.rooks;
|
||||||
|
const full = fullSet(&game.white) | fullSet(&game.black);
|
||||||
|
var val = set.*;
|
||||||
|
const n = @popCount(val);
|
||||||
|
for (0..n) |_| {
|
||||||
|
const pos = @ctz(val);
|
||||||
|
const bit = @as(u64, 1) << @truncate(pos);
|
||||||
|
var moves = sqrScan(bit, full, false);
|
||||||
|
moves |= @bitReverse(sqrScan(bit, full, true));
|
||||||
|
val = val ^ (@as(u64, 1) << @truncate(pos));
|
||||||
|
|
||||||
|
bitboardToMoves(pos, moves, arr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sqrScan(rook: u64, hit: u64, flip: bool) u64 {
|
||||||
|
const vecLut: [64]u64 = comptime blk: {
|
||||||
|
var value: [64]u64 = undefined;
|
||||||
|
for (0..64) |i| {
|
||||||
|
value[i] = vecCalc(@truncate(i));
|
||||||
|
}
|
||||||
|
break :blk value;
|
||||||
|
};
|
||||||
var center = rook;
|
var center = rook;
|
||||||
var hits = hit;
|
var hits = hit;
|
||||||
if (flip) {
|
if (flip) {
|
||||||
@@ -127,14 +175,36 @@ pub fn sqrScan(rook: u64, hit: u64, flip: bool) u64 {
|
|||||||
var min: u64 = vec;
|
var min: u64 = vec;
|
||||||
for (0..n) |_| {
|
for (0..n) |_| {
|
||||||
const pos = @ctz(attackedBits);
|
const pos = @ctz(attackedBits);
|
||||||
const bit = @as(u64, 1) << pos;
|
const bit = @as(u64, 1) << @truncate(pos);
|
||||||
attackedBits = attackedBits & ~bit;
|
attackedBits = attackedBits & ~bit;
|
||||||
if (bit - 1 < min) min = bit - 1;
|
if (bit - 1 < min) min = bit - 1;
|
||||||
}
|
}
|
||||||
|
min = min & (~center);
|
||||||
return min;
|
return min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn vecCalc(i: u8) u64 {
|
||||||
|
var ret: u64 = 0;
|
||||||
|
const col = i % 8;
|
||||||
|
|
||||||
|
// "West" ray
|
||||||
|
for (1..8) |n| {
|
||||||
|
if (i < n or (i - n) % 8 != col)
|
||||||
|
break;
|
||||||
|
ret = ret | @as(u64, 1) << (i - n);
|
||||||
|
}
|
||||||
|
|
||||||
|
// "North" ray
|
||||||
|
for (1..8) |n| {
|
||||||
|
const new_pos = i + (n * 8);
|
||||||
|
if (new_pos > 63)
|
||||||
|
break;
|
||||||
|
ret = ret | @as(u64, 1) << new_pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn knightMove(g: *types.game, arr: *std.ArrayList(types.move)) void {
|
pub fn knightMove(g: *types.game, arr: *std.ArrayList(types.move)) void {
|
||||||
const moveLut: [64]u64 = comptime blk: {
|
const moveLut: [64]u64 = comptime blk: {
|
||||||
var value: [64]u64 = undefined;
|
var value: [64]u64 = undefined;
|
||||||
|
|||||||
Reference in New Issue
Block a user