2CU67A3QXXPH722WBAL74O6P2N2RRXFBQVPT3A5QAXTUCQ3UUU5AC
fn show(self: @This()) void {
std.debug.print("\nsource: {any}\n", .{self.source});
std.debug.print("target: {any}\n", .{self.target});
std.debug.print("piece: {any}\n", .{self.piece});
std.debug.print("promote: {any}\n", .{self.prom});
std.debug.print("capture: {any}\n", .{self.capture});
std.debug.print("double: {any}\n", .{self.double});
std.debug.print("enpassant: {any}\n", .{self.enpassant});
std.debug.print("castling: {any}\n", .{self.castling});
}
'P' => self.bitboards[Chess.PE.P.int()].set(rank * SIZE + file),
'N' => self.bitboards[Chess.PE.N.int()].set(rank * SIZE + file),
'B' => self.bitboards[Chess.PE.B.int()].set(rank * SIZE + file),
'R' => self.bitboards[Chess.PE.R.int()].set(rank * SIZE + file),
'Q' => self.bitboards[Chess.PE.Q.int()].set(rank * SIZE + file),
'K' => self.bitboards[Chess.PE.K.int()].set(rank * SIZE + file),
'P' => self.bitboards[Chess.PE.P.int()].set(square),
'N' => self.bitboards[Chess.PE.N.int()].set(square),
'B' => self.bitboards[Chess.PE.B.int()].set(square),
'R' => self.bitboards[Chess.PE.R.int()].set(square),
'Q' => self.bitboards[Chess.PE.Q.int()].set(square),
'K' => self.bitboards[Chess.PE.K.int()].set(square),
'p' => self.bitboards[Chess.PE.p.int()].set(rank * SIZE + file),
'n' => self.bitboards[Chess.PE.n.int()].set(rank * SIZE + file),
'b' => self.bitboards[Chess.PE.b.int()].set(rank * SIZE + file),
'r' => self.bitboards[Chess.PE.r.int()].set(rank * SIZE + file),
'q' => self.bitboards[Chess.PE.q.int()].set(rank * SIZE + file),
'k' => self.bitboards[Chess.PE.k.int()].set(rank * SIZE + file),
'p' => self.bitboards[Chess.PE.p.int()].set(square),
'n' => self.bitboards[Chess.PE.n.int()].set(square),
'b' => self.bitboards[Chess.PE.b.int()].set(square),
'r' => self.bitboards[Chess.PE.r.int()].set(square),
'q' => self.bitboards[Chess.PE.q.int()].set(square),
'k' => self.bitboards[Chess.PE.k.int()].set(square),
fn makeMove(self: *@This(), move: BitMove, flag: Moves) void {
switch (flag) {
.all => {
// preserve board state
// const bckup = self.backup(); FIXME
// move piece
self.bitboards[move.piece.int()].pop(move.source);
self.bitboards[move.piece.int()].set(move.target);
// TODO: reset enpassant
// self.enpassant = null;
// handling captures
if (move.capture) {
const pieces = switch (self.side) {
.white => [_]Chess.PE{ .p, .n, .b, .r, .q, .k },
.black => [_]Chess.PE{ .P, .N, .B, .R, .Q, .K },
};
// loop over enemy bitboards, when target occupancy found pop it
for (pieces) |piece| {
if (self.bitboards[piece.int()].isSet(move.target)) {
self.bitboards[piece.int()].pop(move.target);
break;
}
}
}
// handling promotions
if (move.prom != .none) {
// erase pawn from target square (already moved)
self.bitboards[move.piece.int()].pop(move.target);
// add promoted item to target peace
self.bitboards[move.prom.int()].set(move.target);
}
// handling enpassant
if (move.enpassant) {
const target = switch (self.side) {
.white => @intToEnum(Square, @enumToInt(move.target) + 8),
.black => @intToEnum(Square, @enumToInt(move.target) - 8),
};
self.bitboards[move.piece.int()].pop(target);
}
// handle double pawn step
if (move.double) {
const enpassant = switch (self.side) {
.white => @intToEnum(Square, @enumToInt(move.target) + 8),
.black => @intToEnum(Square, @enumToInt(move.target) - 8),
};
self.enpassant = enpassant;
}
},
.captures => {
if (move.capture) {
self.makeMove(move, .all);
}
// the move is not a capture
return;
},
}
}
}
test "makeMove" {
const test_pos = "r3k2r/pPppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq c6 0 1";
var gs = try GameState.init(test_pos);
try gs.generateMoves();
for (gs.moves.slice()) |move| {
if (move.castling) {
gs.show();
move.show();
const bk = gs.backup();
gs.makeMove(move, .all);
gs.show();
gs.restore(bk);
}
}