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

int PAWN_VALUE = 100;
int KNIGHT_VALUE = 200;
int BISHOP_VALUE = 300;
int ROOK_VALUE = 400;
int QUEEN_VALUE = 900;

Move EngineGetBestMove(Board *b) {
  auto moves = GetLegalMoves(b);
  std::srand(std::time(0)); // use current time as seed for random generator
  int random_pos = std::rand() % moves.size();
  Move move = moves[random_pos];
  return move;
}

float EvaluateBoard(Board *b) {
  float score = 0;
  Board BoardCopy = *b;

  bool isStaleMate = false;

  auto legalMoves = GetLegalMoves(&BoardCopy);
  if (b->state == WHITE_WON) {
    score = INFINITY;
  }
  if (b->state == BLACK_WON) {
    score = INFINITY * -1;
  }
  if (b->state == STALEMATE || b->state == DRAW) {
    isStaleMate = true;
  }
  int white_pawn_count = 0;
  int white_knight_count = 0;
  int white_bishop_count = 0;
  int white_rook_count = 0;
  int white_queen_count = 0;

  int black_pawn_count = 0;
  int black_knight_count = 0;
  int black_bishop_count = 0;
  int black_rook_count = 0;
  int black_queen_count = 0;
  for (Piece piece : b->pieces) {
    if (piece.type == NONE) {
      continue;
    }
    if (piece.color) {
      // white
      if (piece.type == PAWN) {
        white_pawn_count++;
      }
      if (piece.type == KNIGHT) {
        white_knight_count++;
      }
      if (piece.type == BISHOP) {
        white_bishop_count++;
      }
      if (piece.type == ROOK) {
        white_rook_count++;
      }
      if (piece.type == QUEEN) {
        white_queen_count++;
      }
      continue;
    }

    // black
    if (piece.type == PAWN) {
      black_pawn_count++;
    }
    if (piece.type == KNIGHT) {
      black_knight_count++;
    }
    if (piece.type == BISHOP) {
      black_bishop_count++;
    }
    if (piece.type == ROOK) {
      black_rook_count++;
    }
    if (piece.type == QUEEN) {
      black_queen_count++;
    }
  }

  score += white_pawn_count * PAWN_VALUE;
  score += white_knight_count * KNIGHT_VALUE;
  score += white_bishop_count * BISHOP_VALUE;
  score += white_rook_count * ROOK;
  score += white_queen_count * QUEEN_VALUE;

  score -= black_pawn_count * PAWN_VALUE;
  score -= black_knight_count * KNIGHT_VALUE;
  score -= black_bishop_count * BISHOP_VALUE;
  score -= black_rook_count * ROOK;
  score -= black_queen_count * QUEEN_VALUE;
  if (isStaleMate) {
    return 0;
  }
  return score;
}