Finished version, might have memory leaks
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
#include "game.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <limits>
|
||||
|
||||
uint64_t time_milli() {
|
||||
using namespace std::chrono;
|
||||
@@ -12,6 +16,7 @@ Game::Game(SDL_Window* window, SDL_Surface* surface)
|
||||
this->window = window;
|
||||
this->surface = surface;
|
||||
running = false;
|
||||
holding_k = false;
|
||||
}
|
||||
|
||||
Game::Game(SDL_Window* window, SDL_Surface* surface, std::string board_fen)
|
||||
@@ -20,6 +25,7 @@ Game::Game(SDL_Window* window, SDL_Surface* surface, std::string board_fen)
|
||||
this->surface = surface;
|
||||
board = Board(board_fen);
|
||||
running = false;
|
||||
holding_k = false;
|
||||
}
|
||||
|
||||
void Game::run()
|
||||
@@ -44,22 +50,34 @@ void Game::run()
|
||||
void Game::tick()
|
||||
{
|
||||
SDL_Event e;
|
||||
while(SDL_PollEvent(&e) != 0)
|
||||
while(running && SDL_PollEvent(&e) != 0)
|
||||
{
|
||||
if (e.type == SDL_QUIT || (e.type == SDL_KEYUP && e.key.keysym.sym == SDLK_q))
|
||||
{
|
||||
running = false;
|
||||
}
|
||||
|
||||
if (e.type == SDL_MOUSEBUTTONDOWN)
|
||||
else if (e.type == SDL_KEYUP && e.key.keysym.sym == SDLK_k)
|
||||
{
|
||||
holding_k = false;
|
||||
}
|
||||
else if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_k)
|
||||
{
|
||||
holding_k = true;
|
||||
}
|
||||
else if (e.type == SDL_MOUSEBUTTONDOWN)
|
||||
{
|
||||
int x,y;
|
||||
SDL_GetMouseState(&x, &y);
|
||||
x/=SPRITE_SIZE;
|
||||
y/=SPRITE_SIZE;
|
||||
board.set_selected_space(x,y);
|
||||
process_click(x,y);
|
||||
}
|
||||
}
|
||||
|
||||
if (BLACK == board.get_active_color())
|
||||
{
|
||||
do_ai_move();
|
||||
}
|
||||
}
|
||||
|
||||
void Game::draw()
|
||||
@@ -67,4 +85,284 @@ void Game::draw()
|
||||
board.draw_board(surface);
|
||||
|
||||
SDL_UpdateWindowSurface(window);
|
||||
}
|
||||
|
||||
void Game::process_click(int x, int y)
|
||||
{
|
||||
if (WHITE == board.get_active_color())
|
||||
{
|
||||
int current_selected_space = board.get_selected_space();
|
||||
int target_selected_space = x+y*BOARD_SIZE;
|
||||
if (current_selected_space == -1)
|
||||
{
|
||||
board.set_selected_space(x,y);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::vector<int> target_spaces = board.get_moves_for_space(current_selected_space, true);
|
||||
|
||||
if (target_spaces.end() != std::find(target_spaces.begin(), target_spaces.end(), target_selected_space))
|
||||
{
|
||||
board.do_move(current_selected_space, target_selected_space, holding_k);
|
||||
}
|
||||
else
|
||||
{
|
||||
board.set_selected_space(target_selected_space);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Game::do_ai_move()
|
||||
{
|
||||
int best_move_weight = std::numeric_limits<int>::min();
|
||||
int best_move_from = -1;
|
||||
int best_move_to = -1;
|
||||
bool best_move_makes_queen = false;
|
||||
for(int x = 0; x < BOARD_SIZE; x++)
|
||||
{
|
||||
for(int y = 0; y < BOARD_SIZE; y++)
|
||||
{
|
||||
Piece cur_piece = board.get_piece(x,y);
|
||||
if (cur_piece.get_team() == BLACK)
|
||||
{
|
||||
std::vector<int> piece_moves = board.get_moves_for_space(x,y, true);
|
||||
for (unsigned int i = 0; i < piece_moves.size(); i++)
|
||||
{
|
||||
Board test_board(board.make_FEN());
|
||||
test_board.do_move(x+y*BOARD_SIZE, piece_moves[i], false);
|
||||
|
||||
int board_value = minimax(test_board, 3, std::numeric_limits<int>::min(), std::numeric_limits<int>::max(), false);
|
||||
|
||||
if (board_value > best_move_weight)
|
||||
{
|
||||
best_move_weight = board_value;
|
||||
best_move_from = x+y*BOARD_SIZE;
|
||||
best_move_to = piece_moves[i];
|
||||
best_move_makes_queen = false;
|
||||
}
|
||||
|
||||
if (cur_piece.get_type() == PAWN || (piece_moves[i]/BOARD_SIZE) == 7)
|
||||
{
|
||||
Board test_board1 = Board(board.make_FEN());
|
||||
test_board1.do_move(x+y*BOARD_SIZE, piece_moves[i], true);
|
||||
|
||||
if (test_board1.make_FEN().compare(test_board.make_FEN()) != 0)
|
||||
{
|
||||
board_value = minimax(test_board1, 3, std::numeric_limits<int>::min(), std::numeric_limits<int>::max(), false);
|
||||
|
||||
if (board_value > best_move_weight)
|
||||
{
|
||||
best_move_weight = board_value;
|
||||
best_move_from = x+y*BOARD_SIZE;
|
||||
best_move_to = piece_moves[i];
|
||||
best_move_makes_queen = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
board.do_move(best_move_from, best_move_to, best_move_makes_queen);
|
||||
}
|
||||
|
||||
int Game::minimax(Board current_board, int depth, int a, int b, bool maximizing)
|
||||
{
|
||||
if (depth == 0 || current_board.is_mate(current_board.get_active_color()))
|
||||
{
|
||||
return board_heuristic(current_board);
|
||||
}
|
||||
|
||||
if (maximizing)
|
||||
{
|
||||
int best_move_weight = std::numeric_limits<int>::min();
|
||||
for(int x = 0; x < BOARD_SIZE; x++)
|
||||
{
|
||||
for(int y = 0; y < BOARD_SIZE; y++)
|
||||
{
|
||||
Piece cur_piece = current_board.get_piece(x,y);
|
||||
if (cur_piece.get_team() == BLACK)
|
||||
{
|
||||
std::vector<int> piece_moves = current_board.get_moves_for_space(x,y, true);
|
||||
for (unsigned int i = 0; i < piece_moves.size(); i++)
|
||||
{
|
||||
Board test_board(current_board.make_FEN());
|
||||
test_board.do_move(x+y*BOARD_SIZE, piece_moves[i], false);
|
||||
|
||||
int board_value = minimax(test_board, depth-1, a, b, false);
|
||||
|
||||
if (board_value > best_move_weight)
|
||||
{
|
||||
best_move_weight = board_value;
|
||||
}
|
||||
|
||||
if (best_move_weight >= b)
|
||||
{
|
||||
return best_move_weight;
|
||||
}
|
||||
|
||||
if (best_move_weight > a)
|
||||
{
|
||||
a = best_move_weight;
|
||||
}
|
||||
|
||||
if (cur_piece.get_type() == PAWN || (piece_moves[i]/BOARD_SIZE) == 7)
|
||||
{
|
||||
test_board = Board(current_board.make_FEN());
|
||||
test_board.do_move(x+y*BOARD_SIZE, piece_moves[i], true);
|
||||
|
||||
board_value = minimax(test_board, depth-1, a, b, false);
|
||||
|
||||
if (board_value > best_move_weight)
|
||||
{
|
||||
best_move_weight = board_value;
|
||||
}
|
||||
|
||||
if (best_move_weight >= b)
|
||||
{
|
||||
return best_move_weight;
|
||||
}
|
||||
|
||||
if (best_move_weight > a)
|
||||
{
|
||||
a = best_move_weight;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return best_move_weight;
|
||||
}
|
||||
else
|
||||
{
|
||||
int best_move_weight = std::numeric_limits<int>::max();
|
||||
for(int x = 0; x < BOARD_SIZE; x++)
|
||||
{
|
||||
for(int y = 0; y < BOARD_SIZE; y++)
|
||||
{
|
||||
Piece cur_piece = current_board.get_piece(x,y);
|
||||
if (cur_piece.get_team() == WHITE)
|
||||
{
|
||||
std::vector<int> piece_moves = current_board.get_moves_for_space(x,y, true);
|
||||
for (unsigned int i = 0; i < piece_moves.size(); i++)
|
||||
{
|
||||
Board test_board(current_board.make_FEN());
|
||||
test_board.do_move(x+y*BOARD_SIZE, piece_moves[i], false);
|
||||
|
||||
int board_value = minimax(test_board, depth-1, a, b, true);
|
||||
|
||||
if (board_value < best_move_weight)
|
||||
{
|
||||
best_move_weight = board_value;
|
||||
}
|
||||
|
||||
if (best_move_weight <= a)
|
||||
{
|
||||
return best_move_weight;
|
||||
}
|
||||
|
||||
if (best_move_weight < b)
|
||||
{
|
||||
b = best_move_weight;
|
||||
}
|
||||
|
||||
if (cur_piece.get_type() == PAWN || (piece_moves[i]/BOARD_SIZE) == 0)
|
||||
{
|
||||
test_board = Board(current_board.make_FEN());
|
||||
test_board.do_move(x+y*BOARD_SIZE, piece_moves[i], true);
|
||||
|
||||
board_value = minimax(test_board, depth-1, a, b, true);
|
||||
|
||||
if (board_value < best_move_weight)
|
||||
{
|
||||
best_move_weight = board_value;
|
||||
}
|
||||
|
||||
if (best_move_weight <= a)
|
||||
{
|
||||
return best_move_weight;
|
||||
}
|
||||
|
||||
if (best_move_weight < b)
|
||||
{
|
||||
b = best_move_weight;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return best_move_weight;
|
||||
}
|
||||
}
|
||||
|
||||
int Game::board_heuristic(Board current_board)
|
||||
{
|
||||
int heuristic = 0;
|
||||
int piece_weights[2][7] = {{-900, -90, -30, -30, -50, -10, 0}, {900, 90, 30, 30, 50, 10, 0}};
|
||||
|
||||
int unknown_white_count = 0;
|
||||
int unknown_black_count = 0;
|
||||
for(int x = 0; x < BOARD_SIZE; x++)
|
||||
{
|
||||
for(int y = 0; y < BOARD_SIZE; y++)
|
||||
{
|
||||
Piece cur_piece = current_board.get_piece(x,y);
|
||||
|
||||
if (cur_piece.get_type() == NO_TYPE)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (cur_piece.get_vis() == HIDDEN)
|
||||
{
|
||||
if (cur_piece.get_team() == WHITE)
|
||||
{
|
||||
piece_weights[WHITE][UNKNOWN] += piece_weights[WHITE][cur_piece.get_type()];
|
||||
unknown_white_count++;
|
||||
}
|
||||
else
|
||||
{
|
||||
piece_weights[BLACK][UNKNOWN] += piece_weights[BLACK][cur_piece.get_type()];
|
||||
unknown_black_count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (unknown_white_count != 0)
|
||||
{
|
||||
piece_weights[WHITE][UNKNOWN] /= unknown_white_count;
|
||||
}
|
||||
if (unknown_black_count != 0)
|
||||
{
|
||||
piece_weights[BLACK][UNKNOWN] /= unknown_black_count;
|
||||
}
|
||||
|
||||
for(int x = 0; x < BOARD_SIZE; x++)
|
||||
{
|
||||
for(int y = 0; y < BOARD_SIZE; y++)
|
||||
{
|
||||
Piece cur_piece = current_board.get_piece(x,y);
|
||||
|
||||
if (cur_piece.get_type() == NO_TYPE)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (cur_piece.get_vis() == HIDDEN)
|
||||
{
|
||||
cur_piece = Piece(UNKNOWN, cur_piece.get_team(), SHOWN);
|
||||
}
|
||||
|
||||
heuristic += piece_weights[cur_piece.get_team()][cur_piece.get_type()];
|
||||
}
|
||||
}
|
||||
|
||||
return heuristic;
|
||||
}
|
||||
Reference in New Issue
Block a user