aboutsummaryrefslogtreecommitdiff
path: root/src/bot.cpp
blob: f114f36b58f6a8ac12c5f2c20c18f0f86a72962d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "bot.hpp"
#include "board.hpp"
#include "moves.hpp"
#include "zobrist.hpp"
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <vector>

const int SEARCH_DEPTH = 5;

const int PAWN_VALUE = 100;
const int KNIGHT_VALUE = 320;
const int BISHOP_VALUE = 330;
const int ROOK_VALUE = 500;
const int QUEEN_VALUE = 900;
const int CHECK = 10;
const int MATE = 10000;

const int PAWN_TABLE[64] = {
    0,  0,  0,   0,   0,   0,   0,  0,  // last rank promotes to a quuen
    50, 50, 50,  50,  50,  50,  50, 50, // comments to stop formating to ruin it
    10, 10, 20,  30,  30,  20,  10, 10, //
    5,  5,  10,  25,  25,  10,  5,  5,  //
    0,  0,  0,   20,  20,  0,   0,  0,  //
    5,  -5, -10, 0,   0,   -10, -5, 5,  //
    5,  10, 10,  -20, -20, 10,  10, 5,  //
    0,  0,  0,   0,   0,   0,   0,  0   //
};

int KNIGHT_TABLE[64] = {
    -50, -40, -30, -30, -30, -30, -40, -50, //
    -40, -20, 0,   0,   0,   0,   -20, -40, //
    -30, 0,   10,  15,  15,  10,  0,   -30, //
    -30, 5,   15,  20,  20,  15,  5,   -30, //
    -30, 0,   15,  20,  20,  15,  0,   -30, //
    -30, 5,   10,  15,  15,  10,  5,   -30, //
    -40, -20, 0,   5,   5,   0,   -20, -40, //
    -50, -40, -30, -30, -30, -30, -40, -50, //
};

int BISHOP_TABLE[64] = {
    -20, -10, -10, -10, -10, -10, -10, -20, //
    -10, 5,   0,   0,   0,   0,   5,   -10, //
    -10, 10,  10,  10,  10,  10,  10,  -10, //
    -10, 0,   10,  15,  15,  10,  0,   -10, //
    -10, 5,   5,   10,  10,  5,   5,   -10, //
    -10, 0,   5,   10,  10,  5,   0,   -10, //
    -10, 0,   0,   0,   0,   0,   0,   -10, //
    -20, -10, -10, -10, -10, -10, -10, -20, //
};

int ROOK_TABLE[64] = {
    0,  0,  5,  10, 10, 5,  0,  0,  //
    5,  10, 10, 10, 10, 10, 10, 5,  //
    -5, 0,  0,  0,  0,  0,  0,  -5, //
    -5, 0,  0,  5,  5,  0,  0,  -5, //
    -5, 0,  0,  5,  5,  0,  0,  -5, //
    -5, 0,  0,  0,  0,  0,  0,  -5, //
    5,  10, 10, 10, 10, 10, 10, 5,  //
    0,  0,  5,  10, 10, 5,  0,  0,  //
};
int ScoreMove(const Game *board, const Move &move) {
  int score = 0;

  Piece moving = board->pieces[PositionToIndex(move.From)];
  Piece captured = board->pieces[PositionToIndex(move.To)];

  // Captures (MVV-LVA)
  if (captured.type != NONEPIECE) {
    static const int pieceValue[] = {
        0,     // NONE
        20000, // KING (should never happen)
        900,   // QUEEN
        500,   // ROOK
        330,   // BISHOP
        320,   // KNIGHT
        100    // PAWN
    };

    score += 10000;
    score += pieceValue[captured.type] * 10;
    score -= pieceValue[moving.type];
  }

  // Promotions
  if (move.promotion != NONEPIECE) {
    score += 8000;
  }

  return score;
}

Move EngineGetBestMove(Game *b) {
  auto moves = GetLegalMoves(b);
  if (moves.size() == 0) {
    std::cout << "Expected a position with legal moves";
    assert(false && "Unhanled error zero legal moves for bot");
    exit(1);
  }

  // sort legal moves
  std::sort(moves.begin(), moves.end(), [&](const Move &a, const Move &c) {
    return ScoreMove(b, a) > ScoreMove(b, c);
  });
  Move bestMove = moves[0];
  float BestEval = (b->turn ? -INFINITY : INFINITY);
  for (Move move : moves) {
    UndoMove undo = MakeMove(move, b);
    float alpha = -INFINITY;
    float beta = INFINITY;

    float eval = minimax(SEARCH_DEPTH - 1, b, alpha, beta);

    UnMakeMove(undo, b);
    if (b->turn) {
      if (eval > BestEval) {
        BestEval = eval;
        bestMove = move;
      }
    } else {
      if (eval < BestEval) {
        BestEval = eval;
        bestMove = move;
      }
    }
  }
  return bestMove;
}
float minimax(int depth, Game *b, float alpha, float beta) {
  uint64_t gameHash = GenerateZobristKey(b);

  if (b->Transpositions->contains(gameHash)) {
    TranspositionsEntry data = b->Transpositions->at(gameHash);
    if (data.depth >= depth) {
      return data.Eval;
    }
  }

  auto moves = GetLegalMoves(b);
  if (depth == 0 || moves.size() == 0) {
    return EvaluateBoardForWhite(b, depth);
  }

  float bestEval = b->turn ? -INFINITY : INFINITY;
  if (b->turn) {
    for (Move move : moves) {
      UndoMove undo = MakeMove(move, b);

      float eval = minimax(depth - 1, b, alpha, beta);

      UnMakeMove(undo, b);
      bestEval = std::max(bestEval, eval);

      alpha = std::max(alpha, bestEval);

      if (alpha >= beta) {
        break;
      }
    }
  } else {
    for (Move move : moves) {
      UndoMove undo = MakeMove(move, b);

      float eval = minimax(depth - 1, b, alpha, beta);

      UnMakeMove(undo, b);
      bestEval = std::min(bestEval, eval);

      beta = std::min(beta, bestEval);
      if (alpha >= beta) {
        break; // *snips*
      }
    }
  }

  b->Transpositions->operator[](gameHash) = {depth, bestEval};
  return bestEval;
}
int PSTIndex(int square, bool white) { return white ? square : (56 ^ square); }

float EvaluateBoardForWhite(Game *b, int depth) {
  float score = 0;

  bool isStaleMate = false;

  if (b->state == WHITE_WON) {
    return MATE + depth;
  }
  if (b->state == BLACK_WON) {
    return -MATE - depth;
  }
  if (b->state == STALEMATE || b->state == DRAW) {
    isStaleMate = true;
  }

  for (int i = 0; i < 64; i++) {
    Piece piece = b->pieces[i];
    if (piece.type == NONEPIECE)
      continue;

    int value = 0;

    switch (piece.type) {
    case PAWN:
      value = PAWN_VALUE;
      value += PAWN_TABLE[PSTIndex(i, piece.color)];
      break;
    case KNIGHT:
      value = KNIGHT_VALUE;
      value += KNIGHT_TABLE[PSTIndex(i, piece.color)];
      break;
    case BISHOP:
      value = BISHOP_VALUE;
      value += BISHOP_TABLE[PSTIndex(i, piece.color)];
      break;
    case ROOK:
      value = ROOK_VALUE;
      value += ROOK_TABLE[PSTIndex(i, piece.color)];
      break;
    case QUEEN:
      value = QUEEN_VALUE;
      break;
    default:
      break;
    }

    if (piece.color)
      score += value;
    else
      score -= value;
  }
  if (IsPieceTypeAttacked(b, KING, true))
    score -= CHECK; // white king attacked

  if (IsPieceTypeAttacked(b, KING, false))
    score += CHECK; // black king attacked
  if (isStaleMate) {
    return 0;
  }
  return score;
}