aboutsummaryrefslogtreecommitdiff
path: root/src/bot.cpp
blob: e976d11a6f8ab861c63825306086b1f8d53d7086 (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
#include "bot.hpp"
#include "board.hpp"
#include "moves.hpp"
#include <algorithm>
#include <cassert>
#include <cmath>

int PAWN_VALUE = 100;
int KNIGHT_VALUE = 200;
int BISHOP_VALUE = 300;
int ROOK_VALUE = 400;
int QUEEN_VALUE = 900;
int CHECK = 20;
int MATE = 10000;

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};

const int SEARCH_DEPTH = 3;

#include <fstream>

void LogMove(const Move &move) {
  std::ofstream file("moves.txt", std::ios::app);

  if (!file.is_open())
    return;

  file << static_cast<char>('a' + move.From.file)
       << static_cast<char>('8' - move.From.rank)
       << static_cast<char>('a' + move.To.file)
       << static_cast<char>('8' - move.To.rank);

  file << '\n';
}
Move EngineGetBestMove(Game *b) {
  auto moves = GetLegalMoves(b);
  if (moves.size() == 0) {
    assert(false && "Unhanled error zero legal moves for bot");
  }
  Move bestMove = moves[0];
  float BestEval = (b->turn ? -INFINITY : INFINITY);
  for (Move move : moves) {
    LogMove(move);
    Game TestBoard = *b;
    PlayMove(move, &TestBoard);
    float alpha = -INFINITY;
    float beta = INFINITY;

    float eval = minimax(SEARCH_DEPTH - 1, &TestBoard, alpha, beta);
    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) {
  if (depth == 0) {
    return EvaluateBoardForWhite(b);
  }
  auto moves = GetLegalMoves(b);
  if (moves.size() == 0) {
    return EvaluateBoardForWhite(b);
  }
  if (b->turn) {
    float bestEval = -INFINITY;

    for (Move move : moves) {
      Game next = *b;
      PlayMove(move, &next);

      float eval = minimax(depth - 1, &next, alpha, beta);
      bestEval = std::max(bestEval, eval);

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

      if (alpha >= beta) {
        break;
      }
    }

    return bestEval;
  } else {
    float BestEval = INFINITY;

    for (Move move : moves) {
      Game next = *b;
      PlayMove(move, &next);

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

      BestEval = std::min(BestEval, eval);

      beta = std::min(beta, BestEval);
      if (alpha >= beta) {
        break; // *snips*
      }
    }
    return BestEval;
  }
}
int PSTIndex(int square, bool white) { return white ? square : (56 ^ square); }

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

  bool isStaleMate = false;
  GetLegalMoves(b);

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

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

    int value = 0;

    switch (piece.type) {
    case PAWN:
      value = PAWN_VALUE;
      break;
    case KNIGHT:
      value = KNIGHT_VALUE;
      value += KNIGHT_TABLE[PSTIndex(i, piece.color)];
      break;
    case BISHOP:
      value = BISHOP_VALUE;
      break;
    case ROOK:
      value = ROOK_VALUE;
      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;
}