~macaptain/lkdo-chess

db758ae9dc6301925efe5a32632a557092967656 — Michael Captain 9 months ago f747faa
Add functions for basic bitboard ops and printing the bitboard
2 files changed, 78 insertions(+), 7 deletions(-)

A src/board.zig
M src/main.zig
A src/board.zig => src/board.zig +64 -0
@@ 0,0 1,64 @@
const std = @import("std");

// zig fmt: off
pub const Square = packed enum(u6) {
    a8, b8, c8, d8, e8, f8, g8, h8,
    a7, b7, c7, d7, e7, f7, g7, h7,
    a6, b6, c6, d6, e6, f6, g6, h6,
    a5, b5, c5, d5, e5, f5, g5, h5,
    a4, b4, c4, d4, e4, f4, g4, h4,
    a3, b3, c3, d3, e3, f3, g3, h3,
    a2, b2, c2, d2, e2, f2, g2, h2,
    a1, b1, c1, d1, e1, f1, g1, h1,
};
// zig fmt: on

const Board = struct {
    wP: u64,
    wN: u64,
    wB: u64,
    wR: u64,
    wQ: u64,
    wK: u64,
    bP: u64,
    bN: u64,
    bB: u64,
    bR: u64,
    bQ: u64,
    bK: u64,
    wCastlePerm: bool,
    bCastlePerm: bool,
    movesSinceCapture: u16,
    whiteToMove: bool,
};

pub fn printBitboard(bitboard: u64) void {
    var rank: u6 = 0;
    while (rank < 8) : (rank += 1) {
        var file: u6 = 0;
        while (file < 8) : (file += 1) {
            const square = 8 * rank + file;
            const occupied: u8 = if (getBit(bitboard, square)) 1 else 0;
            std.debug.print("{d}, ", .{occupied});
        }
        std.debug.print("\n", .{});
    }
}

fn getBit(bitboard: u64, square: u6) bool {
    const mask = @as(u64, 1) << square;
    return bitboard & mask != 0;
}

pub fn setBit(bitboard: u64, square: Square) u64 {
    const mask = @as(u64, 1) << @enumToInt(square);
    return bitboard | mask;
}

pub fn resetBit(bitboard: u64, square: Square) u64 {
    const mask = @as(u64, 1) << @enumToInt(square);
    return if (getBit(bitboard, @enumToInt(square)))
        bitboard ^ mask
    else
        bitboard;
}

M src/main.zig => src/main.zig +14 -7
@@ 1,25 1,32 @@
const std = @import("std");
const board = @import("board.zig");

const VERSION = "0.0.1";

fn uci_loop() !void {
fn uciLoop() !void {
    const stdin = std.io.getStdIn().reader();
    const stdout = std.io.getStdOut().writer();
    var line_buf: [2000]u8 = undefined;
    while (true) {
        const uci_in = (try stdin.readUntilDelimiterOrEof(line_buf[0..], '\n')).?;
        if (std.mem.eql(u8, uci_in, "uci")) {
            try stdout.print("id name LKDO v{}\n", .{VERSION});
        const uci_in = (try stdin.readUntilDelimiterOrEof(line_buf[0..], '\n'));
        if (uci_in == null) {
            return;
        } else if (std.mem.eql(u8, uci_in.?, "uci")) {
            try stdout.print("id name LKDO v{s}\n", .{VERSION});
            try stdout.print("id author macaptain\n", .{});
            try stdout.print("uciok\n", .{});
        } else if (std.mem.eql(u8, uci_in, "quit")) {
        } else if (std.mem.eql(u8, uci_in.?, "quit")) {
            return;
        } else if (std.mem.eql(u8, uci_in, "isready")) {
        } else if (std.mem.eql(u8, uci_in.?, "isready")) {
            try stdout.print("readyok\n", .{});
        }
    }
}

pub fn main() !void {
    try uci_loop();
    //try uciLoop();
    var bitboard: u64 = 0;
    bitboard = board.setBit(bitboard, board.Square.e4);
    bitboard = board.setBit(bitboard, board.Square.d5);
    board.printBitboard(bitboard);
}