Compare commits

..

17 Commits

Author SHA1 Message Date
k
89e00df712 fix queen move bug 2025-06-20 03:56:20 -04:00
k
05a0b2b7f1 switch to builtin_popcountll for board eval 2025-06-19 19:41:44 -04:00
k
e1fe8ee603 misc improvements 2025-06-19 19:40:11 -04:00
k
eaaa0ca24d depth search 2025-06-19 17:28:00 -04:00
k
c25de2cb55 Quick pice counting 2025-06-19 15:46:48 -04:00
k
9534803a60 Improved promotion 2025-06-19 14:49:06 -04:00
k
5dd8f04286 queen Movement working 2025-06-19 04:45:30 -04:00
k
9f06b9d86c king move 2025-06-19 04:33:40 -04:00
k
76c69dbf18 bishop move + moves format 2025-06-19 04:00:17 -04:00
k
49befc72b6 Rook movement 2025-06-19 03:49:48 -04:00
k
bfe9335515 pic random move 2025-06-19 03:48:31 -04:00
k
3aa41364b3 quick code split. 2025-06-18 12:38:00 -04:00
k
8624840700 knight moves 2025-06-15 13:45:04 -04:00
k
b2433993a6 Pawn captures 2025-06-15 02:33:50 -04:00
k
33592762a7 fixed always promoteing to one color 2025-06-12 05:14:34 -04:00
k
1d7d15fb52 added uchess and cutechess for testing 2025-06-12 04:50:42 -04:00
k
64b979b9ab Bit board updates based on moves 2025-06-12 04:49:44 -04:00
10 changed files with 656 additions and 86 deletions

5
headers/eval.h Normal file
View File

@@ -0,0 +1,5 @@
#ifndef EVAL_H
#define EVAL_H
move *findBest(move* move, size_t size, game* g);
void makeMove(game *g, move* m);
#endif

11
headers/help.h Normal file
View File

@@ -0,0 +1,11 @@
#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

10
headers/moves.h Normal file
View File

@@ -0,0 +1,10 @@
#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

26
headers/types.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef TYPES_H
#define TYPES_H
#include <stdbool.h>
typedef struct {
long long pawns;
long long knights;
long long bishops;
long long rooks;
long long queen;
long long king;
} sets;
typedef struct {
sets white;
sets black;
bool whiteToMove;
} game;
typedef struct {
int From;
int To;
char Promo;
} move;
#endif

View File

@@ -1,7 +1,7 @@
{pkgs ? import <nixpkgs> {}}: {pkgs ? import <nixpkgs> {}}:
with pkgs; with pkgs;
mkShell rec { mkShell rec {
packages = [gdb clang-tools]; packages = [gdb clang-tools uchess cutechess];
nativeBuildInputs = [ nativeBuildInputs = [
pkg-config pkg-config
gcc gcc

109
src/eval.c Normal file
View File

@@ -0,0 +1,109 @@
#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) {
long long from_bit = 1LL << m->From;
long long to_bit = 1LL << m->To;
long long *set = findSet(g, from_bit);
if (!set)
return;
*set &= ~from_bit;
long long *captured = findSet(g, to_bit);
if (captured) *captured &= ~to_bit;
if (m->Promo) {
char promoChar = g->whiteToMove ? toupper(m->Promo) : m->Promo;
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 Normal file
View File

@@ -0,0 +1,87 @@
#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;
}
}

View File

@@ -5,41 +5,18 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#include "moves.h"
#include "types.h"
#include "help.h"
#include "eval.h"
#define BUFF_SIZE 4096 #define BUFF_SIZE 4096
typedef struct {
long long pawns;
long long knights;
long long bishops;
long long rooks;
long long queen;
long long king;
} sets;
typedef struct {
sets white;
sets black;
bool whiteToMove;
} game;
game *fenGame(char *str); game *fenGame(char *str);
void playMoves(game *g, char *moves);
void print_bitboard(long long bitboard) { long long *findSet(game *g, long long bit);
for (int i = 63; i >= 0; i--) { long long *charToSet(game *g, char c);
// 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");
}
int main() { int main() {
setbuf(stdin, NULL); setbuf(stdin, NULL);
@@ -51,7 +28,8 @@ int main() {
char ltz[] = "abcdefgh"; char ltz[] = "abcdefgh";
int cnt = 0; int cnt = 0;
while (1) { while (1) {
(void)fgets(line, sizeof(line), stdin); if(!fgets(line, sizeof(line), stdin))
return 0;
size_t len = strlen(line); size_t len = strlen(line);
if (len - 1) if (len - 1)
line[len - 1] = '\0'; line[len - 1] = '\0';
@@ -64,7 +42,6 @@ int main() {
return 0; return 0;
} else if (!strcmp("setoption", token)) { } else if (!strcmp("setoption", token)) {
} else if (!strcmp("position", token)) { } else if (!strcmp("position", token)) {
game *g;
token = strtok_r(lineRest, " ", &lineRest); token = strtok_r(lineRest, " ", &lineRest);
if (!strcmp("fen", token)) { if (!strcmp("fen", token)) {
g = fenGame(lineRest + 1); g = fenGame(lineRest + 1);
@@ -73,19 +50,39 @@ int main() {
} else { } else {
g = fenGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); g = fenGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
} }
token = strtok_r(lineRest, " ", &lineRest); playMoves(g, lineRest);
if (!strcmp("moves", token)) {
// TODO
}
print_bitboard(g->white.pawns);
print_bitboard(g->black.pawns);
} else if (!strcmp("ucinewgame", token)) { } else if (!strcmp("ucinewgame", token)) {
} 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)) {
printf("bestmove %c2%c4\n", ltz[cnt], ltz[cnt]); static move mov[1500];//big dumb buffer
cnt++; 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");
continue;
}
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";
}
printf("bestmove %c%d%c%d%s", ltz[xFrom], yFrom, ltz[xTo], yTo, end);
} }
} }
} }
@@ -116,49 +113,9 @@ game *fenGame(char *str) {
} }
long long bit = 1LL << (rank * 8 + file); long long bit = 1LL << (rank * 8 + file);
switch (*str) { long long *set = charToSet(g, *str);
case 'P': if (set)
g->white.pawns |= bit; *set |= bit;
break;
case 'N':
g->white.knights |= bit;
break;
case 'B':
g->white.bishops |= bit;
break;
case 'R':
g->white.rooks |= bit;
break;
case 'Q':
g->white.queen |= bit;
break;
case 'K':
g->white.king |= bit;
break;
case 'p':
g->black.pawns |= bit;
break;
case 'n':
g->black.knights |= bit;
break;
case 'b':
g->black.bishops |= bit;
break;
case 'r':
g->black.rooks |= bit;
break;
case 'q':
g->black.queen |= bit;
break;
case 'k':
g->black.king |= bit;
break;
default:
fprintf(stderr,
"Error: Unknown piece character in FEN: %c at position %zu\n",
*str, pos);
break;
}
file++; file++;
str++; str++;
} }
@@ -166,3 +123,18 @@ game *fenGame(char *str) {
g->whiteToMove = (*str == 'w') ? true : false; g->whiteToMove = (*str == 'w') ? true : false;
return g; 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);
}
}

286
src/moves.c Normal file
View File

@@ -0,0 +1,286 @@
#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 = (0xFFLL) << (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 = 1LL << i;
char p = 0;
if (!(pawns & bit))
continue;
// forword
int to = i + movdir;
p = (promo & (1LL << to)) ? 'q' : 0;
if (to >= 0 && to < 64 && !(occupied & (1LL << to))) {
moves[index++] = (move){.From = i, .To = to, .Promo = p};
}
// left
to = i + capLeft;
p = (promo & (1LL << to)) ? 'q' : 0;
if (i % 8 > 0 && (to >= 0 && to < 64) && (enemy & (1LL << to))) {
moves[index++] = (move){.From = i, .To = to, .Promo = p};
}
// right
to = i + capRight;
p = (promo & (1LL << to)) ? 'q' : 0;
if (i % 8 < 7 && (to >= 0 && to < 64) && (enemy & (1LL << 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 = 1LL << 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 = 1LL << 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 = 1LL << 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 = 1LL << 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 = 1LL << 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 = 1LL << 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 = 1LL << 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) {
long long bishops = 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 = 1LL << 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;
long long lbit = 1LL << 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;
long long lbit = 1LL << 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;
long long lbit = 1LL << 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;
long long lbit = 1LL << 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;
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){
long long king = g->whiteToMove ? g->white.king : g->black.king;
long long occupied = g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
int index = 0;
for (int i = 0; i < 64; i++){
long long bit = 1LL << 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;
long long destBit = 1LL << to;
if (toX >= 0 && toX < 8 && toY >= 0 && toY < 8) {
if (!(occupied & destBit)) {
moves[index++] = (move){.From = i, .To = to, .Promo=0};
}
}
}
}
return index;
}

64
uchess.json Normal file
View File

@@ -0,0 +1,64 @@
{
"uciWhite": "rat",
"uciBlack": "rat",
"uciHint": "stockfish",
"uciEngines": [
{
"name":"rat",
"engine":"./a.out",
"ponder":false
},
{
"name": "stockfish",
"engine": "/nix/store/5d7aqjdak73qlsimiismbj0m1h9b566j-stockfish-17/bin/stockfish",
"hash": 128,
"ponder": false,
"ownBook": false,
"multiPV": 1,
"depth": 1,
"searchMoves": "",
"moveTime": 100,
"options": [
{
"name": "skill level",
"value": "3"
}
]
}
],
"fen": "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
"activeTheme": "basic",
"theme": [
{
"name": "basic",
"moveLabelBg": "#d0d0d0",
"moveLabelFg": "#000000",
"squareDark": "#d7d7d7",
"squareLight": "#ffffd7",
"squareHigh": "#ffff00",
"squareHint": "#ffd7af",
"squareCheck": "#ffafd7",
"white": "#080808",
"black": "#080808",
"msg": "#d70000",
"rank": "#9e9e9e",
"file": "#9e9e9e",
"prompt": "#d70000",
"meterBase": "#585858",
"meterMid": "#0",
"meterNeutral": "#00d7ff",
"meterWin": "#87ffd7",
"meterLose": "#d75f5f",
"playerNames": "#0",
"score": "#9e9e9e",
"moveBox": "#0",
"emoji": "#0",
"input": "#0",
"advantage": "#9e9e9e"
}
],
"whitePiece": "cpu",
"blackPiece": "cpu",
"whiteName": "",
"blackName": ""
}