Compare commits
No commits in common. "eaaa0ca24dbc6d06233d5ad7ae0e77b030871f63" and "5dd8f042866e1c0c3f7ba19c3d76cbbfbdb59331" have entirely different histories.
eaaa0ca24d
...
5dd8f04286
@ -1,4 +0,0 @@
|
|||||||
#ifndef EVAL_H
|
|
||||||
#define EVAL_H
|
|
||||||
move *findBest(move* move, size_t size, game* g);
|
|
||||||
#endif
|
|
||||||
@ -20,7 +20,6 @@ typedef struct {
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
int From;
|
int From;
|
||||||
int To;
|
int To;
|
||||||
char Promo;
|
|
||||||
} move;
|
} move;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
117
src/eval.c
117
src/eval.c
@ -1,117 +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 // 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;
|
|
||||||
}
|
|
||||||
10
src/main.c
10
src/main.c
@ -9,7 +9,6 @@
|
|||||||
#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
|
||||||
|
|
||||||
@ -28,8 +27,7 @@ int main() {
|
|||||||
char ltz[] = "abcdefgh";
|
char ltz[] = "abcdefgh";
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
while (1) {
|
while (1) {
|
||||||
if(!fgets(line, sizeof(line), stdin))
|
(void)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';
|
||||||
@ -57,14 +55,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
|
||||||
size_t cnt = 0;
|
int 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 = findBest(mov,cnt,g);
|
move *m = &mov[rand() % cnt];
|
||||||
|
|
||||||
char *end = "";
|
char *end = "";
|
||||||
int yTo, xTo, yFrom, xFrom;
|
int yTo, xTo, yFrom, xFrom;
|
||||||
@ -74,7 +72,7 @@ int main() {
|
|||||||
yFrom = m->From / 8 + 1;
|
yFrom = m->From / 8 + 1;
|
||||||
|
|
||||||
long long bit = 1LL << m->From;
|
long long bit = 1LL << m->From;
|
||||||
if (m->Promo) {
|
if (((g->whiteToMove && yTo == 8) || (!g->whiteToMove && yTo == 1)) && ((findSet(g,bit) == &g->white.pawns) || (findSet(g,bit) == &g->black.pawns))) {
|
||||||
end = "q\n";
|
end = "q\n";
|
||||||
} else {
|
} else {
|
||||||
end = "\n";
|
end = "\n";
|
||||||
|
|||||||
33
src/moves.c
33
src/moves.c
@ -12,7 +12,6 @@ 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);
|
|
||||||
|
|
||||||
int movdir = g->whiteToMove ? 8 : -8;
|
int movdir = g->whiteToMove ? 8 : -8;
|
||||||
int capLeft = g->whiteToMove ? 7 : -9;
|
int capLeft = g->whiteToMove ? 7 : -9;
|
||||||
@ -22,29 +21,25 @@ int pawnMove(game *g, move *moves) {
|
|||||||
|
|
||||||
for (int i = 0; i < 64; ++i) {
|
for (int i = 0; i < 64; ++i) {
|
||||||
long long bit = 1LL << i;
|
long long bit = 1LL << i;
|
||||||
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 & (1LL << to))) {
|
if (to >= 0 && to < 64 && !(occupied & (1LL << to))) {
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo = p};
|
moves[index++] = (move){.From = i, .To = to};
|
||||||
}
|
}
|
||||||
|
|
||||||
// left
|
// left
|
||||||
to = i + capLeft;
|
to = i + capLeft;
|
||||||
p = (promo & (1LL << to)) ? 'q' : 0;
|
|
||||||
if (i % 8 > 0 && (to >= 0 && to < 64) && (enemy & (1LL << to))) {
|
if (i % 8 > 0 && (to >= 0 && to < 64) && (enemy & (1LL << to))) {
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo = p};
|
moves[index++] = (move){.From = i, .To = to};
|
||||||
}
|
}
|
||||||
|
|
||||||
// right
|
// right
|
||||||
to = i + capRight;
|
to = i + capRight;
|
||||||
p = (promo & (1LL << to)) ? 'q' : 0;
|
|
||||||
if (i % 8 < 7 && (to >= 0 && to < 64) && (enemy & (1LL << to))) {
|
if (i % 8 < 7 && (to >= 0 && to < 64) && (enemy & (1LL << to))) {
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo = p};
|
moves[index++] = (move){.From = i, .To = to};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return index;
|
return index;
|
||||||
@ -82,7 +77,7 @@ int knightMove(game *g, move *moves) {
|
|||||||
if (occupied & destBit)
|
if (occupied & destBit)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
moves[index++] = (move){.From = i, .To = to};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return index;
|
return index;
|
||||||
@ -96,7 +91,7 @@ int rookScan(game *g, move *moves, long long w, long long b) {
|
|||||||
long long rooks = g->whiteToMove ? w : b;
|
long long rooks = g->whiteToMove ? w : b;
|
||||||
long long occupied = g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
long long occupied = g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
|
||||||
long long occupiedE =
|
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++) {
|
||||||
@ -112,7 +107,7 @@ int rookScan(game *g, move *moves, long long w, long long b) {
|
|||||||
long long lbit = 1LL << to;
|
long long lbit = 1LL << 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};
|
||||||
if (occupiedE & lbit)
|
if (occupiedE & lbit)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -123,7 +118,7 @@ int rookScan(game *g, move *moves, long long w, long long b) {
|
|||||||
long long lbit = 1LL << to;
|
long long lbit = 1LL << 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};
|
||||||
if (occupiedE & lbit)
|
if (occupiedE & lbit)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -134,7 +129,7 @@ int rookScan(game *g, move *moves, long long w, long long b) {
|
|||||||
long long lbit = 1LL << to;
|
long long lbit = 1LL << 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};
|
||||||
if (occupiedE & lbit)
|
if (occupiedE & lbit)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -145,7 +140,7 @@ int rookScan(game *g, move *moves, long long w, long long b) {
|
|||||||
long long lbit = 1LL << to;
|
long long lbit = 1LL << 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};
|
||||||
if (occupiedE & lbit)
|
if (occupiedE & lbit)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -184,7 +179,7 @@ int bishopScan(game *g, move *moves, long long w, long long b) {
|
|||||||
|
|
||||||
if (occupied & lbit)
|
if (occupied & lbit)
|
||||||
break;
|
break;
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
moves[index++] = (move){.From = i, .To = to};
|
||||||
if (occupiedE & lbit)
|
if (occupiedE & lbit)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -201,7 +196,7 @@ int bishopScan(game *g, move *moves, long long w, long long b) {
|
|||||||
|
|
||||||
if (occupied & lbit)
|
if (occupied & lbit)
|
||||||
break;
|
break;
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
moves[index++] = (move){.From = i, .To = to};
|
||||||
if (occupiedE & lbit)
|
if (occupiedE & lbit)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -218,7 +213,7 @@ int bishopScan(game *g, move *moves, long long w, long long b) {
|
|||||||
|
|
||||||
if (occupied & lbit)
|
if (occupied & lbit)
|
||||||
break;
|
break;
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
moves[index++] = (move){.From = i, .To = to};
|
||||||
if (occupiedE & lbit)
|
if (occupiedE & lbit)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -235,7 +230,7 @@ int bishopScan(game *g, move *moves, long long w, long long b) {
|
|||||||
|
|
||||||
if (occupied & lbit)
|
if (occupied & lbit)
|
||||||
break;
|
break;
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
moves[index++] = (move){.From = i, .To = to};
|
||||||
if (occupiedE & lbit)
|
if (occupiedE & lbit)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -277,7 +272,7 @@ int kingMove(game *g, move *moves){
|
|||||||
|
|
||||||
if (toX >= 0 && toX < 8 && toY >= 0 && toY < 8) {
|
if (toX >= 0 && toX < 8 && toY >= 0 && toY < 8) {
|
||||||
if (!(occupied & destBit)) {
|
if (!(occupied & destBit)) {
|
||||||
moves[index++] = (move){.From = i, .To = to, .Promo=0};
|
moves[index++] = (move){.From = i, .To = to};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user