Compare commits
15 Commits
main
...
008abd6444
| Author | SHA1 | Date | |
|---|---|---|---|
| 008abd6444 | |||
| d6fa809505 | |||
| eccd8efdba | |||
| ca1e62397c | |||
| 059f672c17 | |||
| f9dda928b2 | |||
| bb3bc4442a | |||
| 93e7ff61e0 | |||
| da8eb581e3 | |||
| f9cdd9a259 | |||
| 5a7b5f1642 | |||
| 34601f9c9d | |||
| 3a9b53d384 | |||
| 986e8c8334 | |||
| fc32b56a2f |
13
.gitea/workflows/nix-check.yaml
Normal file
13
.gitea/workflows/nix-check.yaml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
name: Verify build
|
||||||
|
run-name: ${{ gitea.actor }} is building
|
||||||
|
on: [push]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
verify_build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: cachix/install-nix-action@v30
|
||||||
|
with:
|
||||||
|
nix_path: nixpkgs=channel:nixos-25.05
|
||||||
|
- run: nix-build
|
||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,4 @@
|
|||||||
/a.out
|
/a.out
|
||||||
|
/.zig-cache/
|
||||||
|
/zig-out/
|
||||||
|
/result
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
# RatChess
|
# RatChess
|
||||||
|
|
||||||
A chess engine made for fun
|
A chess engine made for fun
|
||||||
|
|
||||||
|
## Branch
|
||||||
|
Port to zig for fun and to learn zig
|
||||||
|
|||||||
36
build.zig
Normal file
36
build.zig
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub fn build(b: *std.Build) void {
|
||||||
|
const target = b.standardTargetOptions(.{});
|
||||||
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
||||||
|
const exe_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/main.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
|
const exe = b.addExecutable(.{ .name = "RatChess", .root_module = exe_mod });
|
||||||
|
|
||||||
|
exe.linkLibC();
|
||||||
|
exe.addIncludePath(b.path("headers/"));
|
||||||
|
exe.addCSourceFile(.{ .file = b.path("src/eval.c") });
|
||||||
|
exe.addCSourceFile(.{ .file = b.path("src/help.c") });
|
||||||
|
exe.addCSourceFile(.{ .file = b.path("src/main.c") });
|
||||||
|
exe.addCSourceFile(.{ .file = b.path("src/moves.c") });
|
||||||
|
b.installArtifact(exe);
|
||||||
|
|
||||||
|
const run_cmd = b.addRunArtifact(exe);
|
||||||
|
run_cmd.step.dependOn(b.getInstallStep());
|
||||||
|
|
||||||
|
if (b.args) |args| run_cmd.addArgs(args);
|
||||||
|
|
||||||
|
const run_step = b.step("run", "Run the app");
|
||||||
|
run_step.dependOn(&run_cmd.step);
|
||||||
|
|
||||||
|
const exe_unit_tests = b.addTest(.{ .root_module = exe_mod });
|
||||||
|
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
|
||||||
|
|
||||||
|
const test_step = b.step("test", "Run unit tests");
|
||||||
|
test_step.dependOn(&run_exe_unit_tests.step);
|
||||||
|
}
|
||||||
86
build.zig.zon
Normal file
86
build.zig.zon
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
.{
|
||||||
|
// This is the default name used by packages depending on this one. For
|
||||||
|
// example, when a user runs `zig fetch --save <url>`, this field is used
|
||||||
|
// as the key in the `dependencies` table. Although the user can choose a
|
||||||
|
// different name, most users will stick with this provided value.
|
||||||
|
//
|
||||||
|
// It is redundant to include "zig" in this name because it is already
|
||||||
|
// within the Zig package namespace.
|
||||||
|
.name = .RatChess,
|
||||||
|
|
||||||
|
// This is a [Semantic Version](https://semver.org/).
|
||||||
|
// In a future version of Zig it will be used for package deduplication.
|
||||||
|
.version = "0.0.0",
|
||||||
|
|
||||||
|
// Together with name, this represents a globally unique package
|
||||||
|
// identifier. This field is generated by the Zig toolchain when the
|
||||||
|
// package is first created, and then *never changes*. This allows
|
||||||
|
// unambiguous detection of one package being an updated version of
|
||||||
|
// another.
|
||||||
|
//
|
||||||
|
// When forking a Zig project, this id should be regenerated (delete the
|
||||||
|
// field and run `zig build`) if the upstream project is still maintained.
|
||||||
|
// Otherwise, the fork is *hostile*, attempting to take control over the
|
||||||
|
// original project's identity. Thus it is recommended to leave the comment
|
||||||
|
// on the following line intact, so that it shows up in code reviews that
|
||||||
|
// modify the field.
|
||||||
|
.fingerprint = 0xad90dd593d9885b0, // Changing this has security and trust implications.
|
||||||
|
|
||||||
|
// Tracks the earliest Zig version that the package considers to be a
|
||||||
|
// supported use case.
|
||||||
|
.minimum_zig_version = "0.14.1",
|
||||||
|
|
||||||
|
// This field is optional.
|
||||||
|
// Each dependency must either provide a `url` and `hash`, or a `path`.
|
||||||
|
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
|
||||||
|
// Once all dependencies are fetched, `zig build` no longer requires
|
||||||
|
// internet connectivity.
|
||||||
|
.dependencies = .{
|
||||||
|
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
|
||||||
|
//.example = .{
|
||||||
|
// // When updating this field to a new URL, be sure to delete the corresponding
|
||||||
|
// // `hash`, otherwise you are communicating that you expect to find the old hash at
|
||||||
|
// // the new URL. If the contents of a URL change this will result in a hash mismatch
|
||||||
|
// // which will prevent zig from using it.
|
||||||
|
// .url = "https://example.com/foo.tar.gz",
|
||||||
|
//
|
||||||
|
// // This is computed from the file contents of the directory of files that is
|
||||||
|
// // obtained after fetching `url` and applying the inclusion rules given by
|
||||||
|
// // `paths`.
|
||||||
|
// //
|
||||||
|
// // This field is the source of truth; packages do not come from a `url`; they
|
||||||
|
// // come from a `hash`. `url` is just one of many possible mirrors for how to
|
||||||
|
// // obtain a package matching this `hash`.
|
||||||
|
// //
|
||||||
|
// // Uses the [multihash](https://multiformats.io/multihash/) format.
|
||||||
|
// .hash = "...",
|
||||||
|
//
|
||||||
|
// // When this is provided, the package is found in a directory relative to the
|
||||||
|
// // build root. In this case the package's hash is irrelevant and therefore not
|
||||||
|
// // computed. This field and `url` are mutually exclusive.
|
||||||
|
// .path = "foo",
|
||||||
|
//
|
||||||
|
// // When this is set to `true`, a package is declared to be lazily
|
||||||
|
// // fetched. This makes the dependency only get fetched if it is
|
||||||
|
// // actually used.
|
||||||
|
// .lazy = false,
|
||||||
|
//},
|
||||||
|
},
|
||||||
|
|
||||||
|
// Specifies the set of files and directories that are included in this package.
|
||||||
|
// Only files and directories listed here are included in the `hash` that
|
||||||
|
// is computed for this package. Only files listed here will remain on disk
|
||||||
|
// when using the zig package manager. As a rule of thumb, one should list
|
||||||
|
// files required for compilation plus any license(s).
|
||||||
|
// Paths are relative to the build root. Use the empty string (`""`) to refer to
|
||||||
|
// the build root itself.
|
||||||
|
// A directory listed here means that all files within, recursively, are included.
|
||||||
|
.paths = .{
|
||||||
|
"build.zig",
|
||||||
|
"build.zig.zon",
|
||||||
|
"src",
|
||||||
|
// For example...
|
||||||
|
//"LICENSE",
|
||||||
|
//"README.md",
|
||||||
|
},
|
||||||
|
}
|
||||||
33
default.nix
Normal file
33
default.nix
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
# default.nix
|
||||||
|
{ pkgs ? import <nixpkgs> {} }:
|
||||||
|
|
||||||
|
pkgs.stdenv.mkDerivation {
|
||||||
|
pname = "RatChess";
|
||||||
|
version = "0.0.1";
|
||||||
|
|
||||||
|
src = ./.;
|
||||||
|
|
||||||
|
nativeBuildInputs = with pkgs; [
|
||||||
|
zig
|
||||||
|
zls
|
||||||
|
];
|
||||||
|
|
||||||
|
buildInputs = with pkgs; [
|
||||||
|
];
|
||||||
|
|
||||||
|
buildPhase = ''
|
||||||
|
echo "Building project..."
|
||||||
|
${pkgs.zig}/bin/zig build --global-cache-dir ./cache --release=fast
|
||||||
|
'';
|
||||||
|
|
||||||
|
installPhase = ''
|
||||||
|
mkdir $out
|
||||||
|
cp -r zig-out/* $out/
|
||||||
|
'';
|
||||||
|
|
||||||
|
checkPhase = ''
|
||||||
|
echo "Testing project..."
|
||||||
|
${pkgs.zig}/bin/zig build test --global-cache-dir ./cache
|
||||||
|
'';
|
||||||
|
doCheck = true;
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
#ifndef EVAL_H
|
#ifndef EVAL_H
|
||||||
#define EVAL_H
|
#define EVAL_H
|
||||||
move *findBest(move* move, size_t size, game* g);
|
move *findBest(move* move, long long size, game* g);
|
||||||
void makeMove(game *g, move* m);
|
void makeMove(game *g, move* m);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
long long fullSet(sets *s);
|
long long fullSet(sets *s);
|
||||||
long long fullSetBoth(game *g);
|
long long fullSetBoth(game *g);
|
||||||
void print_bitboard(long long bitboard);
|
void print_bitboard(long long bitboard);
|
||||||
long long *findSet(game *g, long long bit);
|
unsigned long long *findSet(game *g, long long bit);
|
||||||
long long *charToSet(game *g, char c);
|
unsigned long long *charToSet(game *g, char c);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
10
headers/main.h
Normal file
10
headers/main.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef MAIN_H
|
||||||
|
#define MAIN_H
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
int cmain();
|
||||||
|
game *fenGame(char *str);
|
||||||
|
game *playMoves(game *g, char *moves);
|
||||||
|
char* uciGo(game *g);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -3,12 +3,12 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
long long pawns;
|
unsigned long long pawns;
|
||||||
long long knights;
|
unsigned long long knights;
|
||||||
long long bishops;
|
unsigned long long bishops;
|
||||||
long long rooks;
|
unsigned long long rooks;
|
||||||
long long queen;
|
unsigned long long queen;
|
||||||
long long king;
|
unsigned long long king;
|
||||||
} sets;
|
} sets;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
{pkgs ? import <nixpkgs> {}}:
|
{pkgs ? import <nixpkgs> {}}:
|
||||||
with pkgs;
|
with pkgs;
|
||||||
mkShell rec {
|
mkShell rec {
|
||||||
packages = [gdb clang-tools uchess cutechess];
|
packages = [gdb zls uchess cutechess];
|
||||||
nativeBuildInputs = [
|
nativeBuildInputs = [
|
||||||
pkg-config
|
zig
|
||||||
gcc
|
|
||||||
gnumake
|
|
||||||
];
|
];
|
||||||
buildInputs = [
|
buildInputs = [
|
||||||
];
|
];
|
||||||
|
|||||||
10
src/eval.c
10
src/eval.c
@@ -56,20 +56,20 @@ int evaluateBoard(game *game) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void makeMove(game *g, move* m) {
|
void makeMove(game *g, move* m) {
|
||||||
long long from_bit = 1LL << m->From;
|
unsigned long long from_bit = 1ULL << m->From;
|
||||||
long long to_bit = 1LL << m->To;
|
unsigned long long to_bit = 1ULL << m->To;
|
||||||
|
|
||||||
long long *set = findSet(g, from_bit);
|
unsigned long long *set = findSet(g, from_bit);
|
||||||
if (!set)
|
if (!set)
|
||||||
return;
|
return;
|
||||||
*set &= ~from_bit;
|
*set &= ~from_bit;
|
||||||
|
|
||||||
long long *captured = findSet(g, to_bit);
|
unsigned long long *captured = findSet(g, to_bit);
|
||||||
if (captured) *captured &= ~to_bit;
|
if (captured) *captured &= ~to_bit;
|
||||||
|
|
||||||
if (m->Promo) {
|
if (m->Promo) {
|
||||||
char promoChar = g->whiteToMove ? toupper(m->Promo) : m->Promo;
|
char promoChar = g->whiteToMove ? toupper(m->Promo) : m->Promo;
|
||||||
long long *newSet = charToSet(g, promoChar);
|
unsigned long long *newSet = charToSet(g, promoChar);
|
||||||
if (newSet)
|
if (newSet)
|
||||||
*newSet |= to_bit;
|
*newSet |= to_bit;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
33
src/main.c
33
src/main.c
@@ -10,15 +10,14 @@
|
|||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "help.h"
|
#include "help.h"
|
||||||
#include "eval.h"
|
#include "eval.h"
|
||||||
|
#include "main.h"
|
||||||
|
|
||||||
#define BUFF_SIZE 4096
|
#define BUFF_SIZE 4096
|
||||||
|
|
||||||
game *fenGame(char *str);
|
game *fenGame(char *str);
|
||||||
void playMoves(game *g, char *moves);
|
game *playMoves(game *g, char *moves);
|
||||||
long long *findSet(game *g, long long bit);
|
|
||||||
long long *charToSet(game *g, char c);
|
|
||||||
|
|
||||||
int main() {
|
int cmain() {
|
||||||
setbuf(stdin, NULL);
|
setbuf(stdin, NULL);
|
||||||
setbuf(stdout, NULL);
|
setbuf(stdout, NULL);
|
||||||
game *g = NULL;
|
game *g = NULL;
|
||||||
@@ -55,6 +54,13 @@ int main() {
|
|||||||
} else if (!strcmp("isready", token)) {
|
} else if (!strcmp("isready", token)) {
|
||||||
printf("readyok\n");
|
printf("readyok\n");
|
||||||
} else if (!strcmp("go", token)) {
|
} else if (!strcmp("go", token)) {
|
||||||
|
uciGo(g);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char* uciGo(game *g){
|
||||||
|
char ltz[] = "abcdefgh";
|
||||||
static move mov[1500];//big dumb buffer
|
static move mov[1500];//big dumb buffer
|
||||||
size_t cnt = 0;
|
size_t cnt = 0;
|
||||||
cnt += pawnMove(g,mov);
|
cnt += pawnMove(g,mov);
|
||||||
@@ -65,8 +71,8 @@ int main() {
|
|||||||
cnt += queenMove(g,(mov+cnt));
|
cnt += queenMove(g,(mov+cnt));
|
||||||
move *m = findBest(mov,cnt,g);
|
move *m = findBest(mov,cnt,g);
|
||||||
if(m == NULL){
|
if(m == NULL){
|
||||||
printf("bestmove 0000\n");
|
//printf("bestmove 0000\n");
|
||||||
continue;
|
return "0000";
|
||||||
}
|
}
|
||||||
|
|
||||||
char *end = "";
|
char *end = "";
|
||||||
@@ -82,11 +88,13 @@ int main() {
|
|||||||
} else {
|
} else {
|
||||||
end = "\n";
|
end = "\n";
|
||||||
}
|
}
|
||||||
printf("bestmove %c%d%c%d%s", ltz[xFrom], yFrom, ltz[xTo], yTo, end);
|
|
||||||
}
|
char* resultString = (char*)malloc(10 * sizeof(char));
|
||||||
}
|
sprintf(resultString, "%c%d%c%d%s", ltz[xFrom], yFrom, ltz[xTo], yTo, end);
|
||||||
|
return resultString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
game *fenGame(char *str) {
|
game *fenGame(char *str) {
|
||||||
int rank = 7;
|
int rank = 7;
|
||||||
int file = 0;
|
int file = 0;
|
||||||
@@ -112,8 +120,8 @@ game *fenGame(char *str) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
long long bit = 1LL << (rank * 8 + file);
|
long long bit = 1ULL << (rank * 8 + file);
|
||||||
long long *set = charToSet(g, *str);
|
unsigned long long *set = charToSet(g, *str);
|
||||||
if (set)
|
if (set)
|
||||||
*set |= bit;
|
*set |= bit;
|
||||||
file++;
|
file++;
|
||||||
@@ -124,7 +132,7 @@ game *fenGame(char *str) {
|
|||||||
return g;
|
return g;
|
||||||
}
|
}
|
||||||
|
|
||||||
void playMoves(game *g, char *moves) {
|
game* playMoves(game *g, char *moves) {
|
||||||
char *moveStr;
|
char *moveStr;
|
||||||
moveStr = strtok_r(moves, " ", &moves);
|
moveStr = strtok_r(moves, " ", &moves);
|
||||||
moveStr = strtok_r(moves, " ", &moves);
|
moveStr = strtok_r(moves, " ", &moves);
|
||||||
@@ -136,5 +144,6 @@ void playMoves(game *g, char *moves) {
|
|||||||
makeMove(g, &m);
|
makeMove(g, &m);
|
||||||
moveStr = strtok_r(moves, " ", &moves);
|
moveStr = strtok_r(moves, " ", &moves);
|
||||||
}
|
}
|
||||||
|
return g;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
177
src/main.zig
Normal file
177
src/main.zig
Normal file
@@ -0,0 +1,177 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const mov = @import("move.zig");
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
|
||||||
|
const uciRet = union(uciTag) {
|
||||||
|
text: []const u8,
|
||||||
|
move: []u8,
|
||||||
|
game: *c.game,
|
||||||
|
exit: void,
|
||||||
|
pass: void,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn main() !void {
|
||||||
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
|
defer _ = gpa.deinit();
|
||||||
|
const alloc = gpa.allocator();
|
||||||
|
const stdin = std.io.getStdIn();
|
||||||
|
var reader = stdin.reader();
|
||||||
|
|
||||||
|
var game: *c.game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc);
|
||||||
|
defer alloc.destroy(game);
|
||||||
|
while (true) {
|
||||||
|
const line = try reader.readUntilDelimiterAlloc(alloc, '\n', std.math.maxInt(usize));
|
||||||
|
defer alloc.free(line);
|
||||||
|
switch (uci(line, game, alloc)) {
|
||||||
|
.text => |value| {
|
||||||
|
try std.io.getStdOut().writer().print("{s}", .{value});
|
||||||
|
},
|
||||||
|
.move => |value| {
|
||||||
|
try std.io.getStdOut().writer().print("bestmove {s}\n", .{value});
|
||||||
|
alloc.free(value);
|
||||||
|
},
|
||||||
|
.game => |value| {
|
||||||
|
alloc.destroy(game);
|
||||||
|
game = value;
|
||||||
|
},
|
||||||
|
.exit => {
|
||||||
|
break;
|
||||||
|
},
|
||||||
|
.pass => {
|
||||||
|
try std.io.getStdOut().writer().print("info bad input\n", .{});
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn uci(str: []const u8, game: *c.game, alloc: std.mem.Allocator) uciRet {
|
||||||
|
const pos = std.mem.indexOfAny(u8, str, " \t\n\r") orelse str.len;
|
||||||
|
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, "isready")) return .{ .text = "readyok\n" };
|
||||||
|
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, "exit")) return .{ .exit = {} };
|
||||||
|
return .{ .pass = {} };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn uciPos(str: []const u8, alloc: std.mem.Allocator) *c.game {
|
||||||
|
const pos = std.mem.indexOfAny(u8, str, " \t\n\r") orelse str.len;
|
||||||
|
const tok = str[0..pos];
|
||||||
|
//var game = fenGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc);
|
||||||
|
if (std.mem.eql(u8, tok, "fen")) return fenGame(str[pos..], alloc);
|
||||||
|
if (std.mem.eql(u8, tok, "startpos")) {
|
||||||
|
var game = fenGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc);
|
||||||
|
game = playMoves(game, str[pos..]);
|
||||||
|
return game;
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
var pos: u8 = 0;
|
||||||
|
var space: u8 = 0;
|
||||||
|
const g = alloc.create(c.game) catch return null;
|
||||||
|
for (str) |chr| {
|
||||||
|
if (pos > 64) {
|
||||||
|
if (chr == ' ') space += 1;
|
||||||
|
if (space == 1 and chr == 'b') g.whiteToMove = false;
|
||||||
|
if (space == 1 and chr == 'w') g.whiteToMove = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (std.ascii.isDigit(chr)) pos += @truncate(chr - '0');
|
||||||
|
if (chr == '/') continue;
|
||||||
|
const set: [*c]c_ulonglong = c.charToSet(g, chr);
|
||||||
|
const bit: u64 = @as(u64, 1) << @truncate(pos);
|
||||||
|
if (set != null)
|
||||||
|
set.* |= @as(c_ulonglong, bit);
|
||||||
|
pos += 1;
|
||||||
|
}
|
||||||
|
return g;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn playMoves(game: [*c]c.game, str: []const u8) [*c]c.game {
|
||||||
|
if (str.len < 4) return game;
|
||||||
|
var splitItr = std.mem.splitSequence(u8, str, " ");
|
||||||
|
while (splitItr.next()) |moveString| {
|
||||||
|
var move: c.move = .{ .To = 0, .From = 0, .Promo = 0 };
|
||||||
|
if (moveString.len < 4) continue;
|
||||||
|
move.From = (moveString[0] - 'a') + (moveString[1] - '1') * 8;
|
||||||
|
move.To = (moveString[2] - 'a') + (moveString[3] - '1') * 8;
|
||||||
|
move.Promo = if (moveString.len == 5) moveString[4] else 0;
|
||||||
|
c.makeMove(game, &move);
|
||||||
|
}
|
||||||
|
return game;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn uciGo(game: *c.game, alloc: std.mem.Allocator) []u8 {
|
||||||
|
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" {
|
||||||
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
|
defer _ = gpa.deinit();
|
||||||
|
const alloc = gpa.allocator();
|
||||||
|
const game = uciPos("fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc);
|
||||||
|
defer alloc.destroy(game);
|
||||||
|
|
||||||
|
const out = uci("uci", game, alloc);
|
||||||
|
try std.testing.expect(out == .text);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "uci ready" {
|
||||||
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
|
defer _ = gpa.deinit();
|
||||||
|
const alloc = gpa.allocator();
|
||||||
|
const game = uciPos("fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc);
|
||||||
|
defer alloc.destroy(game);
|
||||||
|
|
||||||
|
const out = uci("isready", game, alloc);
|
||||||
|
try std.testing.expect(out == .text);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "uci go" {
|
||||||
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
|
defer _ = gpa.deinit();
|
||||||
|
const alloc = gpa.allocator();
|
||||||
|
const game = uciPos("fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc);
|
||||||
|
defer alloc.destroy(game);
|
||||||
|
|
||||||
|
const out = uci("go", game, alloc);
|
||||||
|
try std.testing.expect(out == .move);
|
||||||
|
alloc.free(out.move);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "uci position" {
|
||||||
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
|
defer _ = gpa.deinit();
|
||||||
|
const alloc = gpa.allocator();
|
||||||
|
const game = uciPos("fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc);
|
||||||
|
defer alloc.destroy(game);
|
||||||
|
|
||||||
|
const out = uci("position startpos", game, alloc);
|
||||||
|
defer alloc.destroy(out.game);
|
||||||
|
try std.testing.expect(out == .game);
|
||||||
|
try std.testing.expect(out.game.whiteToMove == true);
|
||||||
|
}
|
||||||
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 };
|
||||||
|
// }
|
||||||
59
src/moves.c
59
src/moves.c
@@ -12,7 +12,7 @@ int pawnMove(game *g, move *moves) {
|
|||||||
long long pawns = g->whiteToMove ? g->white.pawns : g->black.pawns;
|
long long pawns = g->whiteToMove ? g->white.pawns : g->black.pawns;
|
||||||
long long enemy = g->whiteToMove ? fullSet(&g->black) : fullSet(&g->white);
|
long long enemy = g->whiteToMove ? fullSet(&g->black) : fullSet(&g->white);
|
||||||
long long occupied = fullSetBoth(g);
|
long long occupied = fullSetBoth(g);
|
||||||
long long promo = (0xFFLL) << (g->whiteToMove ? 56 : 0);
|
long long promo = (0xFFULL) << (g->whiteToMove ? 56 : 0);
|
||||||
|
|
||||||
int movdir = g->whiteToMove ? 8 : -8;
|
int movdir = g->whiteToMove ? 8 : -8;
|
||||||
int capLeft = g->whiteToMove ? 7 : -9;
|
int capLeft = g->whiteToMove ? 7 : -9;
|
||||||
@@ -21,29 +21,29 @@ int pawnMove(game *g, move *moves) {
|
|||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
|
|
||||||
for (int i = 0; i < 64; ++i) {
|
for (int i = 0; i < 64; ++i) {
|
||||||
long long bit = 1LL << i;
|
long long bit = 1ULL << i;
|
||||||
char p = 0;
|
char p = 0;
|
||||||
if (!(pawns & bit))
|
if (!(pawns & bit))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// forword
|
// forword
|
||||||
int to = i + movdir;
|
int to = i + movdir;
|
||||||
p = (promo & (1LL << to)) ? 'q' : 0;
|
if (to >= 0 && to < 64 && !(occupied & (1ULL << to))) {
|
||||||
if (to >= 0 && to < 64 && !(occupied & (1LL << to))) {
|
p = (promo & (1ULL << to)) ? 'q' : 0;
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo = p};
|
moves[index++] = (move){.From = i, .To = to, .Promo = p};
|
||||||
}
|
}
|
||||||
|
|
||||||
// left
|
// left
|
||||||
to = i + capLeft;
|
to = i + capLeft;
|
||||||
p = (promo & (1LL << to)) ? 'q' : 0;
|
if (i % 8 > 0 && (to >= 0 && to < 64) && (enemy & (1ULL << to))) {
|
||||||
if (i % 8 > 0 && (to >= 0 && to < 64) && (enemy & (1LL << to))) {
|
p = (promo & (1ULL << to)) ? 'q' : 0;
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo = p};
|
moves[index++] = (move){.From = i, .To = to, .Promo = p};
|
||||||
}
|
}
|
||||||
|
|
||||||
// right
|
// right
|
||||||
to = i + capRight;
|
to = i + capRight;
|
||||||
p = (promo & (1LL << to)) ? 'q' : 0;
|
if (i % 8 < 7 && (to >= 0 && to < 64) && (enemy & (1ULL << to))) {
|
||||||
if (i % 8 < 7 && (to >= 0 && to < 64) && (enemy & (1LL << to))) {
|
p = (promo & (1ULL << to)) ? 'q' : 0;
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo = p};
|
moves[index++] = (move){.From = i, .To = to, .Promo = p};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -58,7 +58,7 @@ int knightMove(game *g, move *moves) {
|
|||||||
int index = 0;
|
int index = 0;
|
||||||
|
|
||||||
for (int i = 0; i < 64; i++) {
|
for (int i = 0; i < 64; i++) {
|
||||||
long long bit = 1LL << i;
|
long long bit = 1ULL << i;
|
||||||
if (!(knights & bit))
|
if (!(knights & bit))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@@ -78,7 +78,8 @@ int knightMove(game *g, move *moves) {
|
|||||||
(fileDiff == 2 && rankDiff == 1)))
|
(fileDiff == 2 && rankDiff == 1)))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
long long destBit = 1LL << to;
|
if(to < 0 || to > 63) continue;
|
||||||
|
long long destBit = 1ULL << to;
|
||||||
if (occupied & destBit)
|
if (occupied & destBit)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@@ -100,7 +101,7 @@ int rookScan(game *g, move *moves, long long w, long long b) {
|
|||||||
int index = 0;
|
int index = 0;
|
||||||
|
|
||||||
for (int i = 0; i < 64; i++) {
|
for (int i = 0; i < 64; i++) {
|
||||||
long long bit = 1LL << i;
|
long long bit = 1ULL << i;
|
||||||
int x = i % 8;
|
int x = i % 8;
|
||||||
int y = i / 8;
|
int y = i / 8;
|
||||||
if (!(rooks & bit))
|
if (!(rooks & bit))
|
||||||
@@ -109,7 +110,7 @@ int rookScan(game *g, move *moves, long long w, long long b) {
|
|||||||
// fwd
|
// fwd
|
||||||
for (int j = y + 1; j < 8; j++) {
|
for (int j = y + 1; j < 8; j++) {
|
||||||
int to = (j * 8) + x;
|
int to = (j * 8) + x;
|
||||||
long long lbit = 1LL << to;
|
long long lbit = 1ULL << to;
|
||||||
if (occupied & lbit)
|
if (occupied & lbit)
|
||||||
break;
|
break;
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
||||||
@@ -120,7 +121,7 @@ int rookScan(game *g, move *moves, long long w, long long b) {
|
|||||||
// bck
|
// bck
|
||||||
for (int j = y - 1; j >= 0; j--) {
|
for (int j = y - 1; j >= 0; j--) {
|
||||||
int to = (j * 8) + x;
|
int to = (j * 8) + x;
|
||||||
long long lbit = 1LL << to;
|
long long lbit = 1ULL << to;
|
||||||
if (occupied & lbit)
|
if (occupied & lbit)
|
||||||
break;
|
break;
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
||||||
@@ -131,7 +132,7 @@ int rookScan(game *g, move *moves, long long w, long long b) {
|
|||||||
// rht
|
// rht
|
||||||
for (int j = x + 1; j < 8; j++) {
|
for (int j = x + 1; j < 8; j++) {
|
||||||
int to = (y * 8) + j;
|
int to = (y * 8) + j;
|
||||||
long long lbit = 1LL << to;
|
long long lbit = 1ULL << to;
|
||||||
if (occupied & lbit)
|
if (occupied & lbit)
|
||||||
break;
|
break;
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
||||||
@@ -142,7 +143,7 @@ int rookScan(game *g, move *moves, long long w, long long b) {
|
|||||||
// lft
|
// lft
|
||||||
for (int j = x - 1; j >= 0; j--) {
|
for (int j = x - 1; j >= 0; j--) {
|
||||||
int to = (y * 8) + j;
|
int to = (y * 8) + j;
|
||||||
long long lbit = 1LL << to;
|
long long lbit = 1ULL << to;
|
||||||
if (occupied & lbit)
|
if (occupied & lbit)
|
||||||
break;
|
break;
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
||||||
@@ -158,14 +159,14 @@ int bishopMove(game *g, move *moves) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int bishopScan(game *g, move *moves, long long w, long long b) {
|
int bishopScan(game *g, move *moves, long long w, long long b) {
|
||||||
long long bishops = g->whiteToMove ? w : b;
|
unsigned long long bishops = g->whiteToMove ? w : b;
|
||||||
long long occupied = g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
unsigned long long occupied = g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
||||||
long long occupiedE =
|
unsigned long long occupiedE =
|
||||||
!g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
!g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
|
||||||
for (int i = 0; i < 64; i++) {
|
for (int i = 0; i < 64; i++) {
|
||||||
long long bit = 1LL << i;
|
unsigned long long bit = 1ULL << i;
|
||||||
int x = i % 8;
|
int x = i % 8;
|
||||||
int y = i / 8;
|
int y = i / 8;
|
||||||
|
|
||||||
@@ -180,7 +181,7 @@ int bishopScan(game *g, move *moves, long long w, long long b) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
int to = (yTo * 8) + xTo;
|
int to = (yTo * 8) + xTo;
|
||||||
long long lbit = 1LL << to;
|
unsigned long long lbit = 1ULL << to;
|
||||||
|
|
||||||
if (occupied & lbit)
|
if (occupied & lbit)
|
||||||
break;
|
break;
|
||||||
@@ -197,7 +198,7 @@ int bishopScan(game *g, move *moves, long long w, long long b) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
int to = (yTo * 8) + xTo;
|
int to = (yTo * 8) + xTo;
|
||||||
long long lbit = 1LL << to;
|
unsigned long long lbit = 1ULL << to;
|
||||||
|
|
||||||
if (occupied & lbit)
|
if (occupied & lbit)
|
||||||
break;
|
break;
|
||||||
@@ -214,7 +215,7 @@ int bishopScan(game *g, move *moves, long long w, long long b) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
int to = (yTo * 8) + xTo;
|
int to = (yTo * 8) + xTo;
|
||||||
long long lbit = 1LL << to;
|
unsigned long long lbit = 1ULL << to;
|
||||||
|
|
||||||
if (occupied & lbit)
|
if (occupied & lbit)
|
||||||
break;
|
break;
|
||||||
@@ -231,7 +232,7 @@ int bishopScan(game *g, move *moves, long long w, long long b) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
int to = (yTo * 8) + xTo;
|
int to = (yTo * 8) + xTo;
|
||||||
long long lbit = 1LL << to;
|
unsigned long long lbit = 1ULL << to;
|
||||||
|
|
||||||
if (occupied & lbit)
|
if (occupied & lbit)
|
||||||
break;
|
break;
|
||||||
@@ -245,7 +246,7 @@ int bishopScan(game *g, move *moves, long long w, long long b) {
|
|||||||
|
|
||||||
int queenMove(game *g, move *moves){
|
int queenMove(game *g, move *moves){
|
||||||
int size = 0;
|
int size = 0;
|
||||||
long long w,b;
|
unsigned long long w,b;
|
||||||
w = g->white.queen;
|
w = g->white.queen;
|
||||||
b = g->black.queen;
|
b = g->black.queen;
|
||||||
size += rookScan(g,moves,w,b);
|
size += rookScan(g,moves,w,b);
|
||||||
@@ -255,12 +256,12 @@ int queenMove(game *g, move *moves){
|
|||||||
|
|
||||||
|
|
||||||
int kingMove(game *g, move *moves){
|
int kingMove(game *g, move *moves){
|
||||||
long long king = g->whiteToMove ? g->white.king : g->black.king;
|
unsigned long long king = g->whiteToMove ? g->white.king : g->black.king;
|
||||||
long long occupied = g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
unsigned long long occupied = g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
|
||||||
for (int i = 0; i < 64; i++){
|
for (int i = 0; i < 64; i++){
|
||||||
long long bit = 1LL << i;
|
unsigned long long bit = 1ULL << i;
|
||||||
int x = i % 8;
|
int x = i % 8;
|
||||||
int y = i / 8;
|
int y = i / 8;
|
||||||
int movesX[] = {-1, 0, 1, -1, 1, -1, 0, 1};
|
int movesX[] = {-1, 0, 1, -1, 1, -1, 0, 1};
|
||||||
@@ -273,7 +274,9 @@ int kingMove(game *g, move *moves){
|
|||||||
int toX = x + movesX[j];
|
int toX = x + movesX[j];
|
||||||
int toY = y + movesY[j];
|
int toY = y + movesY[j];
|
||||||
int to = (toY * 8) + toX;
|
int to = (toY * 8) + toX;
|
||||||
long long destBit = 1LL << to;
|
if(to > 63 || to < 1)
|
||||||
|
continue;
|
||||||
|
unsigned long long destBit = 1ULL << to;
|
||||||
|
|
||||||
if (toX >= 0 && toX < 8 && toY >= 0 && toY < 8) {
|
if (toX >= 0 && toX < 8 && toY >= 0 && toY < 8) {
|
||||||
if (!(occupied & destBit)) {
|
if (!(occupied & destBit)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user