Quick pice counting

This commit is contained in:
k 2025-06-19 15:46:48 -04:00
parent 9534803a60
commit c25de2cb55
3 changed files with 90 additions and 2 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

83
src/eval.c Normal file
View File

@ -0,0 +1,83 @@
#include "types.h"
#include "help.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
int evaluateBoard(game *game);
void makeMove(game *g, move* m);
move *findBest(move* moves, size_t size, game* g){
int bestScore = -999999;
move *m = moves;
for (int i = 0; i < size; i++) {
game gg = *g;
makeMove(&gg, &moves[i]);
int score = evaluateBoard(&gg);
if (score > bestScore) {
bestScore = score;
m = &moves[i];
printf("info score cp %d\n", g->whiteToMove ? bestScore : -bestScore);
}
}
return m;
}
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;
}
score *= game->whiteToMove ? -1 : 1;
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;
}

View File

@ -9,6 +9,7 @@
#include "moves.h" #include "moves.h"
#include "types.h" #include "types.h"
#include "help.h" #include "help.h"
#include "eval.h"
#define BUFF_SIZE 4096 #define BUFF_SIZE 4096
@ -56,14 +57,14 @@ int main() {
printf("readyok\n"); printf("readyok\n");
} else if (!strcmp("go", token)) { } else if (!strcmp("go", token)) {
static move mov[1500];//big dumb buffer static move mov[1500];//big dumb buffer
int cnt = 0; size_t cnt = 0;
cnt += pawnMove(g,mov); cnt += pawnMove(g,mov);
cnt += knightMove(g,(mov+cnt)); cnt += knightMove(g,(mov+cnt));
cnt += rookMove(g,(mov+cnt)); cnt += rookMove(g,(mov+cnt));
cnt += bishopMove(g,(mov+cnt)); cnt += bishopMove(g,(mov+cnt));
cnt += kingMove(g,(mov+cnt)); cnt += kingMove(g,(mov+cnt));
cnt += queenMove(g,(mov+cnt)); cnt += queenMove(g,(mov+cnt));
move *m = &mov[rand() % cnt]; move *m = findBest(mov,cnt,g);
char *end = ""; char *end = "";
int yTo, xTo, yFrom, xFrom; int yTo, xTo, yFrom, xFrom;