Compare commits
20 Commits
da8eb581e3
...
zig
| Author | SHA1 | Date | |
|---|---|---|---|
| 47dcfcfeb0 | |||
| 69f11f7220 | |||
| a7c9f4f472 | |||
| 775228d691 | |||
| c1eade9440 | |||
| 94b501fd17 | |||
| ded1566f53 | |||
| 1d41b76fa8 | |||
| 7cded275f2 | |||
| 6af6a16a77 | |||
| 10f81cf7f0 | |||
| 054cd41cf6 | |||
| 008abd6444 | |||
| d6fa809505 | |||
| eccd8efdba | |||
| ca1e62397c | |||
| 059f672c17 | |||
| f9dda928b2 | |||
| bb3bc4442a | |||
| 93e7ff61e0 |
35
TODO.org
Normal file
35
TODO.org
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
* Rewrite[25%]
|
||||||
|
- [ ] UciGo
|
||||||
|
- [ ] move gen
|
||||||
|
- [ ] pawn
|
||||||
|
- [ ] knight
|
||||||
|
- [ ] rook
|
||||||
|
- [ ] bishop
|
||||||
|
- [ ] king
|
||||||
|
- [ ] queen
|
||||||
|
- [ ] move eval
|
||||||
|
- [X] uci interface
|
||||||
|
- [-] uciPos
|
||||||
|
- [ ] fengame
|
||||||
|
- [ ] charToSet
|
||||||
|
- [X] playmoves
|
||||||
|
- [ ] types
|
||||||
|
- [ ] game
|
||||||
|
- [ ] move
|
||||||
|
- [ ] sets
|
||||||
|
* Misc [0%]
|
||||||
|
- [ ] errors
|
||||||
|
- [ ] Proper zig errors
|
||||||
|
- [ ] Proper error handleing
|
||||||
|
- [ ] Speed up
|
||||||
|
- [ ] GPU move gen
|
||||||
|
- [ ] GPU move eval
|
||||||
|
- [ ] clean code
|
||||||
|
- [ ] split code into smaller files
|
||||||
|
- [ ] remove "//this is bad" comments for good code
|
||||||
|
- [ ] more zig stiled controle flow
|
||||||
|
- [ ] uci Config
|
||||||
|
* Bugs
|
||||||
|
** C move gen broken
|
||||||
|
** C findBest slow
|
||||||
|
** FenGame failes to set white to move
|
||||||
@@ -11,13 +11,6 @@ pub fn build(b: *std.Build) void {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const exe = b.addExecutable(.{ .name = "RatChess", .root_module = exe_mod });
|
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);
|
b.installArtifact(exe);
|
||||||
|
|
||||||
const run_cmd = b.addRunArtifact(exe);
|
const run_cmd = b.addRunArtifact(exe);
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
#ifndef EVAL_H
|
|
||||||
#define EVAL_H
|
|
||||||
move *findBest(move* move, size_t size, game* g);
|
|
||||||
void makeMove(game *g, move* m);
|
|
||||||
#endif
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
#ifndef HELP_H
|
|
||||||
#define HELP_H
|
|
||||||
#include "types.h"
|
|
||||||
|
|
||||||
long long fullSet(sets *s);
|
|
||||||
long long fullSetBoth(game *g);
|
|
||||||
void print_bitboard(long long bitboard);
|
|
||||||
long long *findSet(game *g, long long bit);
|
|
||||||
long long *charToSet(game *g, char c);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
#ifndef MAIN_H
|
|
||||||
#define MAIN_H
|
|
||||||
#include "types.h"
|
|
||||||
|
|
||||||
int cmain();
|
|
||||||
game *fenGame(char *str);
|
|
||||||
void playMoves(game *g, char *moves);
|
|
||||||
char* uciGo(game *g);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
#ifndef MOVES_H
|
|
||||||
#define MOVES_H
|
|
||||||
#include "types.h"
|
|
||||||
int pawnMove(game *g, move *moves);
|
|
||||||
int knightMove(game *g, move* moves);
|
|
||||||
int rookMove(game *g, move *moves);
|
|
||||||
int bishopMove(game *g, move *moves);
|
|
||||||
int kingMove(game *g, move *moves);
|
|
||||||
int queenMove(game *g, move *moves);
|
|
||||||
#endif
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
#ifndef TYPES_H
|
|
||||||
#define TYPES_H
|
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
unsigned long long pawns;
|
|
||||||
unsigned long long knights;
|
|
||||||
unsigned long long bishops;
|
|
||||||
unsigned long long rooks;
|
|
||||||
unsigned long long queen;
|
|
||||||
unsigned long long king;
|
|
||||||
} sets;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
sets white;
|
|
||||||
sets black;
|
|
||||||
bool whiteToMove;
|
|
||||||
} game;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int From;
|
|
||||||
int To;
|
|
||||||
char Promo;
|
|
||||||
} move;
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
{pkgs ? import <nixpkgs> {}}:
|
{pkgs ? import <nixpkgs> {}}:
|
||||||
with pkgs;
|
with pkgs;
|
||||||
mkShell rec {
|
mkShell rec {
|
||||||
packages = [gdb zls uchess cutechess];
|
packages = [gdb zls uchess cutechess stockfish];
|
||||||
nativeBuildInputs = [
|
nativeBuildInputs = [
|
||||||
zig
|
zig
|
||||||
];
|
];
|
||||||
|
|||||||
109
src/eval.c
109
src/eval.c
@@ -1,109 +0,0 @@
|
|||||||
#include "types.h"
|
|
||||||
#include "help.h"
|
|
||||||
#include "moves.h"
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#define PAWN_VALUE 100
|
|
||||||
#define KNIGHT_VALUE 320
|
|
||||||
#define BISHOP_VALUE 330
|
|
||||||
#define ROOK_VALUE 500
|
|
||||||
#define QUEEN_VALUE 900
|
|
||||||
#define KING_VALUE 20000
|
|
||||||
#define INF 9000000
|
|
||||||
#define MAX_DEPTH 3
|
|
||||||
|
|
||||||
int evaluateBoard(game *game);
|
|
||||||
void makeMove(game *g, move* m);
|
|
||||||
int negamax(game *g, int depth, int alpha, int beta, int color);
|
|
||||||
|
|
||||||
move *findBest(move* moves, size_t size, game* g){
|
|
||||||
int bestScore = -INF;
|
|
||||||
move *bestMove = NULL;
|
|
||||||
int color = g->whiteToMove ? 1 : -1;
|
|
||||||
|
|
||||||
for (int i = 0; i < size; i++) {
|
|
||||||
game gg = *g;
|
|
||||||
makeMove(&gg, &moves[i]);
|
|
||||||
int score = -negamax(&gg, MAX_DEPTH, -INF, INF, -color);
|
|
||||||
if (score > bestScore || bestScore == -INF) {
|
|
||||||
bestScore = score;
|
|
||||||
bestMove = &moves[i];
|
|
||||||
printf("info score cp %d\n", g->whiteToMove ? bestScore : -bestScore);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return bestMove;
|
|
||||||
}
|
|
||||||
|
|
||||||
int evaluateBoard(game *game) {
|
|
||||||
int score = 0;
|
|
||||||
score += __builtin_popcountll(game->white.pawns) * PAWN_VALUE;
|
|
||||||
score += __builtin_popcountll(game->white.knights) * KNIGHT_VALUE;
|
|
||||||
score += __builtin_popcountll(game->white.bishops) * BISHOP_VALUE;
|
|
||||||
score += __builtin_popcountll(game->white.rooks) * ROOK_VALUE;
|
|
||||||
score += __builtin_popcountll(game->white.queen) * QUEEN_VALUE;
|
|
||||||
score += __builtin_popcountll(game->white.king) * KING_VALUE;
|
|
||||||
|
|
||||||
score -= __builtin_popcountll(game->black.pawns) * PAWN_VALUE;
|
|
||||||
score -= __builtin_popcountll(game->black.knights) * KNIGHT_VALUE;
|
|
||||||
score -= __builtin_popcountll(game->black.bishops) * BISHOP_VALUE;
|
|
||||||
score -= __builtin_popcountll(game->black.rooks) * ROOK_VALUE;
|
|
||||||
score -= __builtin_popcountll(game->black.queen) * QUEEN_VALUE;
|
|
||||||
score -= __builtin_popcountll(game->black.king) * KING_VALUE;
|
|
||||||
|
|
||||||
return score;
|
|
||||||
}
|
|
||||||
|
|
||||||
void makeMove(game *g, move* m) {
|
|
||||||
unsigned long long from_bit = 1ULL << m->From;
|
|
||||||
unsigned long long to_bit = 1ULL << m->To;
|
|
||||||
|
|
||||||
unsigned long long *set = findSet(g, from_bit);
|
|
||||||
if (!set)
|
|
||||||
return;
|
|
||||||
*set &= ~from_bit;
|
|
||||||
|
|
||||||
unsigned long long *captured = findSet(g, to_bit);
|
|
||||||
if (captured) *captured &= ~to_bit;
|
|
||||||
|
|
||||||
if (m->Promo) {
|
|
||||||
char promoChar = g->whiteToMove ? toupper(m->Promo) : m->Promo;
|
|
||||||
unsigned long long *newSet = charToSet(g, promoChar);
|
|
||||||
if (newSet)
|
|
||||||
*newSet |= to_bit;
|
|
||||||
} else {
|
|
||||||
*set |= to_bit;
|
|
||||||
}
|
|
||||||
|
|
||||||
g->whiteToMove = !g->whiteToMove;
|
|
||||||
}
|
|
||||||
|
|
||||||
int negamax(game *g, int depth, int alpha, int beta, int color) {
|
|
||||||
if (depth == 0)
|
|
||||||
return color * evaluateBoard(g);
|
|
||||||
|
|
||||||
move moves[1500];
|
|
||||||
int cnt = pawnMove(g, moves);
|
|
||||||
cnt += knightMove(g, moves + cnt);
|
|
||||||
cnt += rookMove(g, moves + cnt);
|
|
||||||
cnt += bishopMove(g, moves + cnt);
|
|
||||||
cnt += kingMove(g, moves + cnt);
|
|
||||||
cnt += queenMove(g, moves + cnt);
|
|
||||||
|
|
||||||
if (cnt == 0)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
int bestValue = -INF;
|
|
||||||
for (int i = 0; i < cnt; i++) {
|
|
||||||
game gg = *g;
|
|
||||||
makeMove(&gg, &moves[i]);
|
|
||||||
|
|
||||||
int value = -negamax(&gg, depth - 1, -beta, -alpha, -color);
|
|
||||||
|
|
||||||
if (value > bestValue) bestValue = value;
|
|
||||||
if (value > alpha) alpha = value;
|
|
||||||
if (alpha >= beta) break;
|
|
||||||
}
|
|
||||||
return bestValue;
|
|
||||||
}
|
|
||||||
87
src/help.c
87
src/help.c
@@ -1,87 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include "types.h"
|
|
||||||
|
|
||||||
long long fullSet(sets *s) {
|
|
||||||
return s->bishops | s->king | s->knights | s->pawns | s->queen | s->rooks;
|
|
||||||
}
|
|
||||||
|
|
||||||
long long fullSetBoth(game *g) {
|
|
||||||
return fullSet(&g->white) ^ fullSet(&g->black);
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_bitboard(long long bitboard) {
|
|
||||||
for (int i = 63; i >= 0; i--) {
|
|
||||||
// Check if the i-th bit is set
|
|
||||||
if ((bitboard >> i) & 1) {
|
|
||||||
printf("1 ");
|
|
||||||
} else {
|
|
||||||
printf("0 ");
|
|
||||||
}
|
|
||||||
// Print a newline every 8 bits (for rows)
|
|
||||||
if (i % 8 == 0) {
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
long long *findSet(game *g, long long bit) {
|
|
||||||
if (g->white.pawns & bit) {
|
|
||||||
return &g->white.pawns;
|
|
||||||
} else if (g->white.knights & bit) {
|
|
||||||
return &g->white.knights;
|
|
||||||
} else if (g->white.bishops & bit) {
|
|
||||||
return &g->white.bishops;
|
|
||||||
} else if (g->white.rooks & bit) {
|
|
||||||
return &g->white.rooks;
|
|
||||||
} else if (g->white.queen & bit) {
|
|
||||||
return &g->white.queen;
|
|
||||||
} else if (g->white.king & bit) {
|
|
||||||
return &g->white.king;
|
|
||||||
} else if (g->black.pawns & bit) {
|
|
||||||
return &g->black.pawns;
|
|
||||||
} else if (g->black.knights & bit) {
|
|
||||||
return &g->black.knights;
|
|
||||||
} else if (g->black.bishops & bit) {
|
|
||||||
return &g->black.bishops;
|
|
||||||
} else if (g->black.rooks & bit) {
|
|
||||||
return &g->black.rooks;
|
|
||||||
} else if (g->black.queen & bit) {
|
|
||||||
return &g->black.queen;
|
|
||||||
} else if (g->black.king & bit) {
|
|
||||||
return &g->black.king;
|
|
||||||
} else {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
long long *charToSet(game *g, char c) {
|
|
||||||
switch (c) {
|
|
||||||
case 'P':
|
|
||||||
return &g->white.pawns;
|
|
||||||
case 'N':
|
|
||||||
return &g->white.knights;
|
|
||||||
case 'B':
|
|
||||||
return &g->white.bishops;
|
|
||||||
case 'R':
|
|
||||||
return &g->white.rooks;
|
|
||||||
case 'Q':
|
|
||||||
return &g->white.queen;
|
|
||||||
case 'K':
|
|
||||||
return &g->white.king;
|
|
||||||
case 'p':
|
|
||||||
return &g->black.pawns;
|
|
||||||
case 'n':
|
|
||||||
return &g->black.knights;
|
|
||||||
case 'b':
|
|
||||||
return &g->black.bishops;
|
|
||||||
case 'r':
|
|
||||||
return &g->black.rooks;
|
|
||||||
case 'q':
|
|
||||||
return &g->black.queen;
|
|
||||||
case 'k':
|
|
||||||
return &g->black.king;
|
|
||||||
default:
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
150
src/main.c
150
src/main.c
@@ -1,150 +0,0 @@
|
|||||||
#include <assert.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include "moves.h"
|
|
||||||
#include "types.h"
|
|
||||||
#include "help.h"
|
|
||||||
#include "eval.h"
|
|
||||||
#include "main.h"
|
|
||||||
|
|
||||||
#define BUFF_SIZE 4096
|
|
||||||
|
|
||||||
game *fenGame(char *str);
|
|
||||||
void playMoves(game *g, char *moves);
|
|
||||||
long long *findSet(game *g, long long bit);
|
|
||||||
long long *charToSet(game *g, char c);
|
|
||||||
|
|
||||||
int cmain() {
|
|
||||||
setbuf(stdin, NULL);
|
|
||||||
setbuf(stdout, NULL);
|
|
||||||
game *g = NULL;
|
|
||||||
|
|
||||||
char line[BUFF_SIZE];
|
|
||||||
char *lineRest, *token;
|
|
||||||
char ltz[] = "abcdefgh";
|
|
||||||
int cnt = 0;
|
|
||||||
while (1) {
|
|
||||||
if(!fgets(line, sizeof(line), stdin))
|
|
||||||
return 0;
|
|
||||||
size_t len = strlen(line);
|
|
||||||
if (len - 1)
|
|
||||||
line[len - 1] = '\0';
|
|
||||||
token = strtok_r(line, " ", &lineRest);
|
|
||||||
if (!strcmp("uci", token)) {
|
|
||||||
printf("id name RatChess 0.0\n");
|
|
||||||
printf("id author rat<3\n");
|
|
||||||
printf("uciok\n");
|
|
||||||
} else if (!strcmp("quit", token)) {
|
|
||||||
return 0;
|
|
||||||
} else if (!strcmp("setoption", token)) {
|
|
||||||
} else if (!strcmp("position", token)) {
|
|
||||||
token = strtok_r(lineRest, " ", &lineRest);
|
|
||||||
if (!strcmp("fen", token)) {
|
|
||||||
g = fenGame(lineRest + 1);
|
|
||||||
for (int i = 0; i < 6; i++)
|
|
||||||
token = strtok_r(lineRest, " ", &lineRest);
|
|
||||||
} else {
|
|
||||||
g = fenGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
|
|
||||||
}
|
|
||||||
playMoves(g, lineRest);
|
|
||||||
} else if (!strcmp("ucinewgame", token)) {
|
|
||||||
} else if (!strcmp("isready", token)) {
|
|
||||||
printf("readyok\n");
|
|
||||||
} else if (!strcmp("go", token)) {
|
|
||||||
uciGo(g);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
char* uciGo(game *g){
|
|
||||||
char ltz[] = "abcdefgh";
|
|
||||||
static move mov[1500];//big dumb buffer
|
|
||||||
size_t cnt = 0;
|
|
||||||
cnt += pawnMove(g,mov);
|
|
||||||
cnt += knightMove(g,(mov+cnt));
|
|
||||||
cnt += rookMove(g,(mov+cnt));
|
|
||||||
cnt += bishopMove(g,(mov+cnt));
|
|
||||||
cnt += kingMove(g,(mov+cnt));
|
|
||||||
cnt += queenMove(g,(mov+cnt));
|
|
||||||
move *m = findBest(mov,cnt,g);
|
|
||||||
if(m == NULL){
|
|
||||||
printf("bestmove 0000\n");
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
char *end = "";
|
|
||||||
int yTo, xTo, yFrom, xFrom;
|
|
||||||
xTo = m->To % 8;
|
|
||||||
yTo = m->To / 8 + 1;
|
|
||||||
xFrom = m->From % 8;
|
|
||||||
yFrom = m->From / 8 + 1;
|
|
||||||
|
|
||||||
long long bit = 1LL << m->From;
|
|
||||||
if (m->Promo) {
|
|
||||||
end = "q\n";
|
|
||||||
} else {
|
|
||||||
end = "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
|
||||||
int rank = 7;
|
|
||||||
int file = 0;
|
|
||||||
size_t pos = 0;
|
|
||||||
game *g;
|
|
||||||
g = malloc(sizeof(game));
|
|
||||||
memset(g, 0, sizeof(game));
|
|
||||||
|
|
||||||
while (str[pos] != '\0' && str[pos] != ' ') {
|
|
||||||
char current_char = *str;
|
|
||||||
|
|
||||||
if (isdigit(*str)) {
|
|
||||||
int empty_squares = *str - '0';
|
|
||||||
file += empty_squares;
|
|
||||||
str++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current_char == '/') {
|
|
||||||
rank--;
|
|
||||||
file = 0;
|
|
||||||
str++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
long long bit = 1ULL << (rank * 8 + file);
|
|
||||||
long long *set = charToSet(g, *str);
|
|
||||||
if (set)
|
|
||||||
*set |= bit;
|
|
||||||
file++;
|
|
||||||
str++;
|
|
||||||
}
|
|
||||||
str++;
|
|
||||||
g->whiteToMove = (*str == 'w') ? true : false;
|
|
||||||
return g;
|
|
||||||
}
|
|
||||||
|
|
||||||
void playMoves(game *g, char *moves) {
|
|
||||||
char *moveStr;
|
|
||||||
moveStr = strtok_r(moves, " ", &moves);
|
|
||||||
moveStr = strtok_r(moves, " ", &moves);
|
|
||||||
while (moveStr) {
|
|
||||||
move m;
|
|
||||||
m.From = (moveStr[0]-'a') + (moveStr[1]-'1')*8;
|
|
||||||
m.To = (moveStr[2]-'a') + (moveStr[3]-'1')*8;
|
|
||||||
m.Promo = (strlen(moveStr) == 5) ? moveStr[4] : 0;
|
|
||||||
makeMove(g, &m);
|
|
||||||
moveStr = strtok_r(moves, " ", &moves);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
146
src/main.zig
146
src/main.zig
@@ -1,24 +1,6 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const c = @cImport({
|
const mov = @import("move.zig");
|
||||||
@cInclude("main.h");
|
const types = @import("types.zig");
|
||||||
@cInclude("types.h");
|
|
||||||
});
|
|
||||||
|
|
||||||
const uciTag = enum {
|
|
||||||
text,
|
|
||||||
move,
|
|
||||||
game,
|
|
||||||
exit,
|
|
||||||
pass,
|
|
||||||
};
|
|
||||||
|
|
||||||
const uciRet = union(uciTag) {
|
|
||||||
text: []const u8,
|
|
||||||
move: []const u8,
|
|
||||||
game: *c.game,
|
|
||||||
exit: void,
|
|
||||||
pass: void,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
@@ -27,18 +9,21 @@ pub fn main() !void {
|
|||||||
const stdin = std.io.getStdIn();
|
const stdin = std.io.getStdIn();
|
||||||
var reader = stdin.reader();
|
var reader = stdin.reader();
|
||||||
|
|
||||||
var game: *c.game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
|
var game: *types.game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc);
|
||||||
|
defer alloc.destroy(game);
|
||||||
while (true) {
|
while (true) {
|
||||||
const line = try reader.readUntilDelimiterAlloc(alloc, '\n', std.math.maxInt(usize));
|
const line = try reader.readUntilDelimiterAlloc(alloc, '\n', std.math.maxInt(usize));
|
||||||
defer alloc.free(line);
|
defer alloc.free(line);
|
||||||
switch (uci(line, game)) {
|
switch (uci(line, game, alloc)) {
|
||||||
.text => |value| {
|
.text => |value| {
|
||||||
try std.io.getStdOut().writer().print("{s}", .{value});
|
try std.io.getStdOut().writer().print("{s}", .{value});
|
||||||
},
|
},
|
||||||
.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);
|
||||||
game = value;
|
game = value;
|
||||||
},
|
},
|
||||||
.exit => {
|
.exit => {
|
||||||
@@ -51,51 +36,126 @@ pub fn main() !void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn uci(str: []const u8, game: *c.game) uciRet {
|
fn uci(str: []const u8, game: *types.game, alloc: std.mem.Allocator) types.uciRet {
|
||||||
const pos = std.mem.indexOfAny(u8, str, " \t\n\r") orelse str.len;
|
const pos = std.mem.indexOfAny(u8, str, " \t\n\r") orelse str.len;
|
||||||
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("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1") };
|
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 = {} };
|
||||||
}
|
}
|
||||||
|
|
||||||
fn uciPos(str: []const u8) [*c]c.game {
|
fn uciPos(str: []const u8, alloc: std.mem.Allocator) *types.game {
|
||||||
var buffer: [256]u8 = undefined;
|
const pos = std.mem.indexOfAny(u8, str, " \t\n\r") orelse str.len;
|
||||||
const len = @min(str.len, buffer.len - 1);
|
const tok = str[0..pos];
|
||||||
@memcpy(buffer[0..len], str[0..len]);
|
if (std.mem.eql(u8, tok, "fen")) return fenGame(str[pos..], alloc);
|
||||||
buffer[len] = 0;
|
if (std.mem.eql(u8, tok, "startpos")) {
|
||||||
|
var game = fenGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc);
|
||||||
const game = c.fenGame(&buffer);
|
game = mov.playMoves(game, str[(pos)..]);
|
||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
return fenGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", alloc); //this should be an error
|
||||||
|
}
|
||||||
|
|
||||||
fn uciGo(game: *c.game) []const u8 {
|
fn fenGame(str: []const u8, alloc: std.mem.Allocator) *types.game {
|
||||||
return std.mem.span(c.uciGo(game));
|
var pos: u8 = 63;
|
||||||
|
var space: u8 = 0;
|
||||||
|
var g = alloc.create(types.game) catch unreachable;
|
||||||
|
g.* = std.mem.zeroes(types.game);
|
||||||
|
for (str) |chr| {
|
||||||
|
if (chr == ' ') {
|
||||||
|
space += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (space == 1) {
|
||||||
|
if (chr == 'b') g.whiteToMove = false;
|
||||||
|
if (chr == 'w') g.whiteToMove = true;
|
||||||
|
//continue;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (std.ascii.isDigit(chr)) {
|
||||||
|
pos += @truncate(chr - '0');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (chr == '/') continue;
|
||||||
|
const set: *u64 = mov.charToSet(g, chr);
|
||||||
|
const bit: u64 = @as(u64, 1) << @truncate(pos);
|
||||||
|
set.* |= bit;
|
||||||
|
pos -= 1;
|
||||||
|
}
|
||||||
|
return g;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn uciGo(game: *types.game, alloc: std.mem.Allocator) []u8 {
|
||||||
|
var moves = std.ArrayList(types.move).init(alloc);
|
||||||
|
defer moves.deinit();
|
||||||
|
const str = alloc.alloc(u8, 5) catch unreachable;
|
||||||
|
mov.pawnMove(game, &moves);
|
||||||
|
mov.rookMove(game, &moves);
|
||||||
|
mov.knightMove(game, &moves);
|
||||||
|
mov.bishipMove(game, &moves);
|
||||||
|
mov.kingMove(game, &moves);
|
||||||
|
mov.quenMove(game, &moves);
|
||||||
|
if (moves.capacity == 0) {
|
||||||
|
@memcpy(str.ptr, "0000");
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
mov.moveTypeToStr(moves.items[0], str);
|
||||||
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
test "uci uci" {
|
test "uci uci" {
|
||||||
const game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
const out = uci("uci", game);
|
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);
|
try std.testing.expect(out == .text);
|
||||||
}
|
}
|
||||||
|
|
||||||
test "uci ready" {
|
test "uci ready" {
|
||||||
const game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
const out = uci("isready", game);
|
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);
|
try std.testing.expect(out == .text);
|
||||||
}
|
}
|
||||||
|
|
||||||
test "uci go" {
|
test "uci go" {
|
||||||
const game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
const out = uci("go", game);
|
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);
|
try std.testing.expect(out == .move);
|
||||||
|
alloc.free(out.move);
|
||||||
}
|
}
|
||||||
|
|
||||||
test "uci position" {
|
test "uci position" {
|
||||||
const game = uciPos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
const out = uci("position startpos", game);
|
defer _ = gpa.deinit();
|
||||||
|
const alloc = gpa.allocator();
|
||||||
|
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(gameb);
|
||||||
|
|
||||||
|
const out = uci("position startpos", game, alloc);
|
||||||
|
defer alloc.destroy(out.game);
|
||||||
try std.testing.expect(out == .game);
|
try std.testing.expect(out == .game);
|
||||||
|
try std.testing.expect(out.game.whiteToMove == true);
|
||||||
|
try std.testing.expect(out.game.white.king != 0);
|
||||||
|
try std.testing.expect(out.game.black.king != 0);
|
||||||
|
try std.testing.expectEqualDeep(game, gameb);
|
||||||
}
|
}
|
||||||
|
|||||||
261
src/move.zig
Normal file
261
src/move.zig
Normal file
@@ -0,0 +1,261 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const types = @import("types.zig");
|
||||||
|
|
||||||
|
pub fn moveTypeToStr(move: types.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 playMoves(game: *types.game, str: []const u8) *types.game {
|
||||||
|
if (str.len < 4) return game;
|
||||||
|
var splitItr = std.mem.splitSequence(u8, str, " ");
|
||||||
|
while (splitItr.next()) |moveString| {
|
||||||
|
if (moveString.len < 4 or moveString[0] > 'h')
|
||||||
|
continue;
|
||||||
|
var move: types.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;
|
||||||
|
makeMove(@ptrCast(game), @ptrCast(&move));
|
||||||
|
}
|
||||||
|
return game;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fullSet(set: *types.set) u64 {
|
||||||
|
return set.bishops | set.king | set.knights | set.pawns | set.queen | set.rooks;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn findSet(game: *types.game, bit: u64) ?*u64 {
|
||||||
|
if (game.white.pawns & bit != 0) {
|
||||||
|
return &game.white.pawns;
|
||||||
|
} else if (game.white.knights & bit != 0) {
|
||||||
|
return &game.white.knights;
|
||||||
|
} else if (game.white.bishops & bit != 0) {
|
||||||
|
return &game.white.bishops;
|
||||||
|
} else if (game.white.rooks & bit != 0) {
|
||||||
|
return &game.white.rooks;
|
||||||
|
} else if (game.white.queen & bit != 0) {
|
||||||
|
return &game.white.queen;
|
||||||
|
} else if (game.white.king & bit != 0) {
|
||||||
|
return &game.white.king;
|
||||||
|
} else if (game.black.pawns & bit != 0) {
|
||||||
|
return &game.black.pawns;
|
||||||
|
} else if (game.black.knights & bit != 0) {
|
||||||
|
return &game.black.knights;
|
||||||
|
} else if (game.black.bishops & bit != 0) {
|
||||||
|
return &game.black.bishops;
|
||||||
|
} else if (game.black.rooks & bit != 0) {
|
||||||
|
return &game.black.rooks;
|
||||||
|
} else if (game.black.queen & bit != 0) {
|
||||||
|
return &game.black.queen;
|
||||||
|
} else if (game.black.king & bit != 0) {
|
||||||
|
return &game.black.king;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn makeMove(game: *types.game, move: *types.move) void {
|
||||||
|
const from = @as(u64, 1) << @truncate(move.From);
|
||||||
|
const to = @as(u64, 1) << @truncate(move.To);
|
||||||
|
const set = findSet(game, from);
|
||||||
|
if (set == null)
|
||||||
|
return;
|
||||||
|
set.?.* &= ~from;
|
||||||
|
|
||||||
|
const cap = findSet(game, to);
|
||||||
|
if (cap != null)
|
||||||
|
cap.?.* &= ~to;
|
||||||
|
|
||||||
|
if (move.Promo != 0) {
|
||||||
|
charToSet(game, move.Promo).* |= to;
|
||||||
|
} else {
|
||||||
|
set.?.* |= to;
|
||||||
|
}
|
||||||
|
|
||||||
|
game.whiteToMove = !game.whiteToMove;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn charToSet(g: *types.game, chr: u8) *u64 {
|
||||||
|
return switch (chr) {
|
||||||
|
'P' => &g.white.pawns,
|
||||||
|
'N' => &g.white.knights,
|
||||||
|
'B' => &g.white.bishops,
|
||||||
|
'R' => &g.white.rooks,
|
||||||
|
'Q' => &g.white.queen,
|
||||||
|
'K' => &g.white.king,
|
||||||
|
'p' => &g.black.pawns,
|
||||||
|
'n' => &g.black.knights,
|
||||||
|
'b' => &g.black.bishops,
|
||||||
|
'r' => &g.black.rooks,
|
||||||
|
'q' => &g.black.queen,
|
||||||
|
'k' => &g.black.king,
|
||||||
|
else => {
|
||||||
|
std.log.err("you should not be here ${c}$", .{chr});
|
||||||
|
unreachable;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pawnMove(game: *types.game, arr: *std.ArrayList(types.move)) void {
|
||||||
|
_ = game;
|
||||||
|
_ = arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bishipMove(game: *types.game, arr: *std.ArrayList(types.move)) void {
|
||||||
|
_ = 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 hits = hit;
|
||||||
|
if (flip) {
|
||||||
|
center = @bitReverse(rook);
|
||||||
|
hits = @bitReverse(hit);
|
||||||
|
}
|
||||||
|
|
||||||
|
const vec = vecLut[@ctz(center)];
|
||||||
|
|
||||||
|
var attackedBits = hits & vec;
|
||||||
|
const n = @popCount(attackedBits);
|
||||||
|
var min: u64 = vec;
|
||||||
|
for (0..n) |_| {
|
||||||
|
const pos = @ctz(attackedBits);
|
||||||
|
const bit = @as(u64, 1) << @truncate(pos);
|
||||||
|
attackedBits = attackedBits & ~bit;
|
||||||
|
if (bit - 1 < min) min = bit - 1;
|
||||||
|
}
|
||||||
|
min = min & (~center);
|
||||||
|
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 {
|
||||||
|
const moveLut: [64]u64 = comptime blk: {
|
||||||
|
var value: [64]u64 = undefined;
|
||||||
|
for (0..64) |i| {
|
||||||
|
value[i] = knightCalc(@truncate(i));
|
||||||
|
}
|
||||||
|
break :blk value;
|
||||||
|
};
|
||||||
|
const set = if (g.whiteToMove) &g.white.knights else &g.black.knights;
|
||||||
|
const fset: u64 = if (g.whiteToMove) @as(u64, fullSet(@ptrCast(&g.white))) else @as(u64, fullSet(@ptrCast(&g.black)));
|
||||||
|
var val = set.*; //local copy
|
||||||
|
const n = @popCount(val);
|
||||||
|
for (0..n) |_| {
|
||||||
|
const pos = @ctz(val);
|
||||||
|
const moves = moveLut[pos] & ~fset;
|
||||||
|
val = val ^ (@as(u64, 1) << @truncate(pos));
|
||||||
|
bitboardToMoves(pos, moves, arr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn knightCalc(index: u8) u64 {
|
||||||
|
var moves: u64 = 0;
|
||||||
|
const offsets = [_]i8{ 17, -17, 15, -15, 10, -10, 6, -6 };
|
||||||
|
var cnt: u8 = undefined;
|
||||||
|
for (offsets) |off| {
|
||||||
|
if (off > 0) {
|
||||||
|
cnt = index + @abs(off);
|
||||||
|
} else if (index > @abs(off)) { //Icky bad that zig makes me do
|
||||||
|
cnt = index - @abs(off);
|
||||||
|
}
|
||||||
|
const fromFile: i32 = index % 8;
|
||||||
|
const toFile: i32 = cnt % 8;
|
||||||
|
const fromRank: i32 = index / 8;
|
||||||
|
const toRank: i32 = cnt / 8;
|
||||||
|
|
||||||
|
const fileDiff = @abs(fromFile - toFile);
|
||||||
|
const rankDiff = @abs(fromRank - toRank);
|
||||||
|
if (!((fileDiff == 1 and rankDiff == 2) or
|
||||||
|
(fileDiff == 2 and rankDiff == 1)) or cnt > 63)
|
||||||
|
continue;
|
||||||
|
moves = moves | @as(u64, 1) << @truncate(cnt);
|
||||||
|
}
|
||||||
|
return moves;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bitboardToMoves(start: u8, moves: u64, arr: *std.ArrayList(types.move)) void {
|
||||||
|
var lmoves = moves;
|
||||||
|
while (lmoves != 0) {
|
||||||
|
const pos = @ctz(lmoves);
|
||||||
|
const m: types.move = .{ .From = start, .Promo = 0, .To = pos };
|
||||||
|
lmoves = lmoves & ~@as(u64, 1) << @truncate(pos);
|
||||||
|
arr.append(m) catch unreachable;
|
||||||
|
}
|
||||||
|
}
|
||||||
288
src/moves.c
288
src/moves.c
@@ -1,288 +0,0 @@
|
|||||||
#include "moves.h"
|
|
||||||
#include "help.h"
|
|
||||||
#include "types.h"
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
int rookScan(game *g, move *moves, long long w, long long b);
|
|
||||||
int bishopScan(game *g, move *moves, long long w, long long b);
|
|
||||||
int kingScan(game *g, move *moves, long long w, long long b);
|
|
||||||
|
|
||||||
int pawnMove(game *g, move *moves) {
|
|
||||||
long long pawns = g->whiteToMove ? g->white.pawns : g->black.pawns;
|
|
||||||
long long enemy = g->whiteToMove ? fullSet(&g->black) : fullSet(&g->white);
|
|
||||||
long long occupied = fullSetBoth(g);
|
|
||||||
long long promo = (0xFFULL) << (g->whiteToMove ? 56 : 0);
|
|
||||||
|
|
||||||
int movdir = g->whiteToMove ? 8 : -8;
|
|
||||||
int capLeft = g->whiteToMove ? 7 : -9;
|
|
||||||
int capRight = g->whiteToMove ? 9 : -7;
|
|
||||||
|
|
||||||
size_t index = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < 64; ++i) {
|
|
||||||
long long bit = 1ULL << i;
|
|
||||||
char p = 0;
|
|
||||||
if (!(pawns & bit))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// forword
|
|
||||||
int to = i + movdir;
|
|
||||||
p = (promo & (1ULL << to)) ? 'q' : 0;
|
|
||||||
if (to >= 0 && to < 64 && !(occupied & (1ULL << to))) {
|
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo = p};
|
|
||||||
}
|
|
||||||
|
|
||||||
// left
|
|
||||||
to = i + capLeft;
|
|
||||||
p = (promo & (1ULL << to)) ? 'q' : 0;
|
|
||||||
if (i % 8 > 0 && (to >= 0 && to < 64) && (enemy & (1ULL << to))) {
|
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo = p};
|
|
||||||
}
|
|
||||||
|
|
||||||
// right
|
|
||||||
to = i + capRight;
|
|
||||||
p = (promo & (1ULL << to)) ? 'q' : 0;
|
|
||||||
if (i % 8 < 7 && (to >= 0 && to < 64) && (enemy & (1ULL << to))) {
|
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo = p};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
|
|
||||||
int knightMove(game *g, move *moves) {
|
|
||||||
long long knights = g->whiteToMove ? g->white.knights : g->black.knights;
|
|
||||||
long long occupied = g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
|
||||||
|
|
||||||
int kMoves[] = {-17, -15, -10, -6, 6, 10, 15, 17};
|
|
||||||
int index = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < 64; i++) {
|
|
||||||
long long bit = 1ULL << i;
|
|
||||||
if (!(knights & bit))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
for (int j = 0; j < 8; ++j) {
|
|
||||||
int to = i + kMoves[j];
|
|
||||||
if (to < 0 || to > 64)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
int fromFile = i % 8;
|
|
||||||
int toFile = to % 8;
|
|
||||||
int fromRank = i / 8;
|
|
||||||
int toRank = to / 8;
|
|
||||||
|
|
||||||
int fileDiff = abs(fromFile - toFile);
|
|
||||||
int rankDiff = abs(fromRank - toRank);
|
|
||||||
if (!((fileDiff == 1 && rankDiff == 2) ||
|
|
||||||
(fileDiff == 2 && rankDiff == 1)))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
long long destBit = 1ULL << to;
|
|
||||||
if (occupied & destBit)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
|
|
||||||
int rookMove(game *g, move *moves) {
|
|
||||||
return rookScan(g, moves, g->white.rooks, g->black.rooks);
|
|
||||||
}
|
|
||||||
|
|
||||||
int rookScan(game *g, move *moves, long long w, long long b) {
|
|
||||||
long long rooks = g->whiteToMove ? w : b;
|
|
||||||
long long occupied = g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
|
||||||
long long occupiedE =
|
|
||||||
!g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
|
||||||
int index = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < 64; i++) {
|
|
||||||
long long bit = 1ULL << i;
|
|
||||||
int x = i % 8;
|
|
||||||
int y = i / 8;
|
|
||||||
if (!(rooks & bit))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// fwd
|
|
||||||
for (int j = y + 1; j < 8; j++) {
|
|
||||||
int to = (j * 8) + x;
|
|
||||||
long long lbit = 1ULL << to;
|
|
||||||
if (occupied & lbit)
|
|
||||||
break;
|
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
|
||||||
if (occupiedE & lbit)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// bck
|
|
||||||
for (int j = y - 1; j >= 0; j--) {
|
|
||||||
int to = (j * 8) + x;
|
|
||||||
long long lbit = 1ULL << to;
|
|
||||||
if (occupied & lbit)
|
|
||||||
break;
|
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
|
||||||
if (occupiedE & lbit)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// rht
|
|
||||||
for (int j = x + 1; j < 8; j++) {
|
|
||||||
int to = (y * 8) + j;
|
|
||||||
long long lbit = 1ULL << to;
|
|
||||||
if (occupied & lbit)
|
|
||||||
break;
|
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
|
||||||
if (occupiedE & lbit)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// lft
|
|
||||||
for (int j = x - 1; j >= 0; j--) {
|
|
||||||
int to = (y * 8) + j;
|
|
||||||
long long lbit = 1ULL << to;
|
|
||||||
if (occupied & lbit)
|
|
||||||
break;
|
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
|
||||||
if (occupiedE & lbit)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
|
|
||||||
int bishopMove(game *g, move *moves) {
|
|
||||||
return bishopScan(g, moves, g->white.bishops, g->black.bishops);
|
|
||||||
}
|
|
||||||
|
|
||||||
int bishopScan(game *g, move *moves, long long w, long long b) {
|
|
||||||
unsigned long long bishops = g->whiteToMove ? w : b;
|
|
||||||
unsigned long long occupied = g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
|
||||||
unsigned long long occupiedE =
|
|
||||||
!g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
|
||||||
int index = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < 64; i++) {
|
|
||||||
unsigned long long bit = 1ULL << i;
|
|
||||||
int x = i % 8;
|
|
||||||
int y = i / 8;
|
|
||||||
|
|
||||||
if (!(bishops & bit))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// ur
|
|
||||||
for (int j = 1;; j++) {
|
|
||||||
int xTo = x + j;
|
|
||||||
int yTo = y + j;
|
|
||||||
if (xTo >= 8 || yTo >= 8)
|
|
||||||
break;
|
|
||||||
|
|
||||||
int to = (yTo * 8) + xTo;
|
|
||||||
unsigned long long lbit = 1ULL << to;
|
|
||||||
|
|
||||||
if (occupied & lbit)
|
|
||||||
break;
|
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
|
||||||
if (occupiedE & lbit)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ul
|
|
||||||
for (int j = 1;; j++) {
|
|
||||||
int xTo = x - j;
|
|
||||||
int yTo = y + j;
|
|
||||||
if (xTo < 0 || yTo >= 8)
|
|
||||||
break;
|
|
||||||
|
|
||||||
int to = (yTo * 8) + xTo;
|
|
||||||
unsigned long long lbit = 1ULL << to;
|
|
||||||
|
|
||||||
if (occupied & lbit)
|
|
||||||
break;
|
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
|
||||||
if (occupiedE & lbit)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// dr
|
|
||||||
for (int j = 1;; j++) {
|
|
||||||
int xTo = x + j;
|
|
||||||
int yTo = y - j;
|
|
||||||
if (xTo >= 8 || yTo < 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
int to = (yTo * 8) + xTo;
|
|
||||||
unsigned long long lbit = 1ULL << to;
|
|
||||||
|
|
||||||
if (occupied & lbit)
|
|
||||||
break;
|
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
|
||||||
if (occupiedE & lbit)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// dl
|
|
||||||
for (int j = 1;; j++) {
|
|
||||||
int xTo = x - j;
|
|
||||||
int yTo = y - j;
|
|
||||||
if (xTo < 0 || yTo < 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
int to = (yTo * 8) + xTo;
|
|
||||||
unsigned long long lbit = 1ULL << to;
|
|
||||||
|
|
||||||
if (occupied & lbit)
|
|
||||||
break;
|
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
|
||||||
if (occupiedE & lbit)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
|
|
||||||
int queenMove(game *g, move *moves){
|
|
||||||
int size = 0;
|
|
||||||
unsigned long long w,b;
|
|
||||||
w = g->white.queen;
|
|
||||||
b = g->black.queen;
|
|
||||||
size += rookScan(g,moves,w,b);
|
|
||||||
size += bishopScan(g,(moves+size),w,b);
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int kingMove(game *g, move *moves){
|
|
||||||
unsigned long long king = g->whiteToMove ? g->white.king : g->black.king;
|
|
||||||
unsigned long long occupied = g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
|
||||||
int index = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < 64; i++){
|
|
||||||
unsigned long long bit = 1ULL << i;
|
|
||||||
int x = i % 8;
|
|
||||||
int y = i / 8;
|
|
||||||
int movesX[] = {-1, 0, 1, -1, 1, -1, 0, 1};
|
|
||||||
int movesY[] = {-1, -1, -1, 0, 0, 1, 1, 1};
|
|
||||||
|
|
||||||
if(!(king & bit))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
for (int j = 0; j < 8; ++j) {
|
|
||||||
int toX = x + movesX[j];
|
|
||||||
int toY = y + movesY[j];
|
|
||||||
int to = (toY * 8) + toX;
|
|
||||||
if(to > 63 || to < 1)
|
|
||||||
continue;
|
|
||||||
unsigned long long destBit = 1ULL << to;
|
|
||||||
|
|
||||||
if (toX >= 0 && toX < 8 && toY >= 0 && toY < 8) {
|
|
||||||
if (!(occupied & destBit)) {
|
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
47
src/types.zig
Normal file
47
src/types.zig
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
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,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const uciRet = union(uciTag) {
|
||||||
|
text: []const u8,
|
||||||
|
move: []u8,
|
||||||
|
game: *game,
|
||||||
|
exit: void,
|
||||||
|
pass: void,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const set = struct {
|
||||||
|
pawns: u64,
|
||||||
|
knights: u64,
|
||||||
|
bishops: u64,
|
||||||
|
rooks: u64,
|
||||||
|
queen: u64,
|
||||||
|
king: u64,
|
||||||
|
};
|
||||||
|
pub const game = struct {
|
||||||
|
black: set,
|
||||||
|
white: set,
|
||||||
|
whiteToMove: bool,
|
||||||
|
};
|
||||||
|
pub const move = struct {
|
||||||
|
From: u32,
|
||||||
|
To: u32,
|
||||||
|
Promo: u8,
|
||||||
|
};
|
||||||
|
// struct {
|
||||||
|
// To: u8,
|
||||||
|
// From: u8,
|
||||||
|
// Promo: u8,
|
||||||
|
// };
|
||||||
@@ -5,12 +5,12 @@
|
|||||||
"uciEngines": [
|
"uciEngines": [
|
||||||
{
|
{
|
||||||
"name":"rat",
|
"name":"rat",
|
||||||
"engine":"./a.out",
|
"engine":"./zig-out/bin/RatChess",
|
||||||
"ponder":false
|
"ponder":false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "stockfish",
|
"name": "stockfish",
|
||||||
"engine": "/nix/store/5d7aqjdak73qlsimiismbj0m1h9b566j-stockfish-17/bin/stockfish",
|
"engine": "/nix/store/xrzjqi5m9yphj4p5wgpvnamxs38ap0wv-stockfish-17/bin/stockfish",
|
||||||
"hash": 128,
|
"hash": 128,
|
||||||
"ponder": false,
|
"ponder": false,
|
||||||
"ownBook": false,
|
"ownBook": false,
|
||||||
|
|||||||
Reference in New Issue
Block a user