Compare commits

..

3 Commits

Author SHA1 Message Date
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
5 changed files with 147 additions and 18 deletions

4
headers/eval.h Normal file
View File

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

View File

@ -20,6 +20,7 @@ typedef struct {
typedef struct {
int From;
int To;
char Promo;
} move;
#endif

117
src/eval.c Normal file
View File

@ -0,0 +1,117 @@
#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 // Adjustable depth
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 = moves;
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 = score;
bestMove = &moves[i];
printf("info score cp %d\n", g->whiteToMove ? bestScore : -bestScore);
}
}
return bestMove;
}
int evaluateBoard(game *game) {
int score = 0;
for (int i = 0; i < 64; ++i) {
long long bit = 1LL << i;
if (game->white.pawns & bit) score += PAWN_VALUE;
else if (game->white.knights & bit) score += KNIGHT_VALUE;
else if (game->white.bishops & bit) score += BISHOP_VALUE;
else if (game->white.rooks & bit) score += ROOK_VALUE;
else if (game->white.queen & bit) score += QUEEN_VALUE;
else if (game->white.king & bit) score += KING_VALUE;
}
for (int i = 0; i < 64; ++i) {
long long bit = 1LL << i;
if (game->black.pawns & bit) score -= PAWN_VALUE;
else if (game->black.knights & bit) score -= KNIGHT_VALUE;
else if (game->black.bishops & bit) score -= BISHOP_VALUE;
else if (game->black.rooks & bit) score -= ROOK_VALUE;
else if (game->black.queen & bit) score -= QUEEN_VALUE;
else if (game->black.king & bit) score -= KING_VALUE;
}
return score;
}
void makeMove(game *g, move* m) {
//should replace inside of PlayMoves but im lazy
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;
}

View File

@ -9,6 +9,7 @@
#include "moves.h"
#include "types.h"
#include "help.h"
#include "eval.h"
#define BUFF_SIZE 4096
@ -27,7 +28,8 @@ int main() {
char ltz[] = "abcdefgh";
int cnt = 0;
while (1) {
(void)fgets(line, sizeof(line), stdin);
if(!fgets(line, sizeof(line), stdin))
return 0;
size_t len = strlen(line);
if (len - 1)
line[len - 1] = '\0';
@ -55,14 +57,14 @@ int main() {
printf("readyok\n");
} else if (!strcmp("go", token)) {
static move mov[1500];//big dumb buffer
int cnt = 0;
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 = &mov[rand() % cnt];
move *m = findBest(mov,cnt,g);
char *end = "";
int yTo, xTo, yFrom, xFrom;
@ -72,7 +74,7 @@ int main() {
yFrom = m->From / 8 + 1;
long long bit = 1LL << m->From;
if (((g->whiteToMove && yTo == 8) || (!g->whiteToMove && yTo == 1)) && ((findSet(g,bit) == &g->white.pawns) || (findSet(g,bit) == &g->black.pawns))) {
if (m->Promo) {
end = "q\n";
} else {
end = "\n";

View File

@ -12,6 +12,7 @@ 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;
@ -21,25 +22,29 @@ int pawnMove(game *g, move *moves) {
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};
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};
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};
moves[index++] = (move){.From = i, .To = to, .Promo = p};
}
}
return index;
@ -77,7 +82,7 @@ int knightMove(game *g, move *moves) {
if (occupied & destBit)
continue;
moves[index++] = (move){.From = i, .To = to};
moves[index++] = (move){.From = i, .To = to, .Promo=0};
}
}
return index;
@ -107,7 +112,7 @@ int rookScan(game *g, move *moves, long long w, long long b) {
long long lbit = 1LL << to;
if (occupied & lbit)
break;
moves[index++] = (move){.From = i, .To = to};
moves[index++] = (move){.From = i, .To = to, .Promo=0};
if (occupiedE & lbit)
break;
}
@ -118,7 +123,7 @@ int rookScan(game *g, move *moves, long long w, long long b) {
long long lbit = 1LL << to;
if (occupied & lbit)
break;
moves[index++] = (move){.From = i, .To = to};
moves[index++] = (move){.From = i, .To = to, .Promo=0};
if (occupiedE & lbit)
break;
}
@ -129,7 +134,7 @@ int rookScan(game *g, move *moves, long long w, long long b) {
long long lbit = 1LL << to;
if (occupied & lbit)
break;
moves[index++] = (move){.From = i, .To = to};
moves[index++] = (move){.From = i, .To = to, .Promo=0};
if (occupiedE & lbit)
break;
}
@ -140,7 +145,7 @@ int rookScan(game *g, move *moves, long long w, long long b) {
long long lbit = 1LL << to;
if (occupied & lbit)
break;
moves[index++] = (move){.From = i, .To = to};
moves[index++] = (move){.From = i, .To = to, .Promo=0};
if (occupiedE & lbit)
break;
}
@ -179,7 +184,7 @@ int bishopScan(game *g, move *moves, long long w, long long b) {
if (occupied & lbit)
break;
moves[index++] = (move){.From = i, .To = to};
moves[index++] = (move){.From = i, .To = to, .Promo=0};
if (occupiedE & lbit)
break;
}
@ -196,7 +201,7 @@ int bishopScan(game *g, move *moves, long long w, long long b) {
if (occupied & lbit)
break;
moves[index++] = (move){.From = i, .To = to};
moves[index++] = (move){.From = i, .To = to, .Promo=0};
if (occupiedE & lbit)
break;
}
@ -213,7 +218,7 @@ int bishopScan(game *g, move *moves, long long w, long long b) {
if (occupied & lbit)
break;
moves[index++] = (move){.From = i, .To = to};
moves[index++] = (move){.From = i, .To = to, .Promo=0};
if (occupiedE & lbit)
break;
}
@ -230,7 +235,7 @@ int bishopScan(game *g, move *moves, long long w, long long b) {
if (occupied & lbit)
break;
moves[index++] = (move){.From = i, .To = to};
moves[index++] = (move){.From = i, .To = to, .Promo=0};
if (occupiedE & lbit)
break;
}
@ -272,7 +277,7 @@ int kingMove(game *g, move *moves){
if (toX >= 0 && toX < 8 && toY >= 0 && toY < 8) {
if (!(occupied & destBit)) {
moves[index++] = (move){.From = i, .To = to};
moves[index++] = (move){.From = i, .To = to, .Promo=0};
}
}
}