Added move finding/selection

This commit is contained in:
Marcus Penate 2022-04-14 22:40:00 -04:00
parent 8c7fe707a1
commit e6e567e734
8 changed files with 604 additions and 43 deletions

17
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,17 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/SDL2"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}

View File

@ -33,3 +33,10 @@ enum Visibility
SHOWN=0,
HIDDEN=1,
};
enum MoveType
{
BLOCKED,
FREE,
CAPTURE,
};

471
board.cpp
View File

@ -2,11 +2,13 @@
#include <cstdlib>
#include <algorithm>
#include "sprites.hpp"
Board::Board()
{
load_FEN(std::string("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/SOCQKCOS w KQkq - 0 1"));
load_FEN(std::string("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"));
}
Board::Board(std::string board_fen)
@ -21,22 +23,24 @@ void Board::load_FEN(std::string board_fen)
able_to_castle[0][1] = false;
able_to_castle[1][0] = false;
able_to_castle[1][1] = false;
en_passant = -1;
half_turn_count = 0;
full_turn_count = 0;
selected_space = -1;
int x=0,y=0;
std::string black_team("kqbnrpcos");
std::string white_team("KQBNRPCOS");
for(int i = 0; i < board_fen.size() && !(x==BOARD_SIZE && y==BOARD_SIZE); i++)
for(int i = 0; i < board_fen.size() && !(x==BOARD_SIZE-1 && y==BOARD_SIZE); i++)
{
if (board_fen[i] == '/')
{
x++;
y=0;
y++;
x=0;
}
else if (std::isdigit(board_fen[i]))
{
y += board_fen[i]-'0';
x += board_fen[i]-'0';
}
else
{
@ -74,7 +78,7 @@ void Board::load_FEN(std::string board_fen)
game_board[x][y] = Piece(type, team, vis);
}
}
y++;
x++;
}
}
@ -110,21 +114,19 @@ void Board::load_FEN(std::string board_fen)
}
size_t en_passant_loc = able_to_castle_loc+1;
while(board_fen[en_passant_loc] != ' ')
if (board_fen[en_passant_loc] != '-')
{
if (board_fen[en_passant_loc == '-'])
{
en_passant_loc++;
break;
}
x = board_fen[en_passant_loc]-'a';
y = board_fen[en_passant_loc]-'0';
y = board_fen[en_passant_loc+1]-'0';
en_passant.push_back(x+y*BOARD_SIZE);
en_passant = x+y*BOARD_SIZE;
en_passant_loc+=2;
}
else
{
en_passant_loc++;
}
size_t half_turn_count_loc = en_passant_loc+1;
const char* board_fen_cstr = board_fen.c_str();
@ -136,21 +138,67 @@ void Board::load_FEN(std::string board_fen)
void Board::draw_board(SDL_Surface* dest_surface)
{
bool light_tile = true;
for (int y = 0; y < BOARD_SIZE; y++)
{
std::vector<int> target_spaces = get_moves_for_space(selected_space);
for (int x = 0; x < BOARD_SIZE; x++)
{
SDL_Rect dest_rect({SPRITE_SIZE*y,SPRITE_SIZE*x,SPRITE_SIZE,SPRITE_SIZE});
for (int y = 0; y < BOARD_SIZE; y++)
{
SDL_Rect dest_rect({SPRITE_SIZE*x,SPRITE_SIZE*y,SPRITE_SIZE,SPRITE_SIZE});
uint32_t color = 0;
if (target_spaces.end() == std::find(target_spaces.begin(), target_spaces.end(), x+y*BOARD_SIZE))
{
if (light_tile)
{
if (x+y*BOARD_SIZE == selected_space)
{
color = SDL_MapRGB(dest_surface->format, 132, 133, 104);
}
else
{
color = SDL_MapRGB(dest_surface->format, 235, 236, 208);
}
}
else
{
if (x+y*BOARD_SIZE == selected_space)
{
color = SDL_MapRGB(dest_surface->format, 59, 74, 43);
}
else
{
color = SDL_MapRGB(dest_surface->format, 119, 149, 86);
}
}
}
else
{
if (CAPTURE == can_move(selected_space, x+y*BOARD_SIZE))
{
if (light_tile)
{
color = SDL_MapRGB(dest_surface->format, 255, 200, 200);
}
else
{
color = SDL_MapRGB(dest_surface->format, 128, 100, 100);
}
}
else
{
if (light_tile)
{
color = SDL_MapRGB(dest_surface->format, 200, 200, 255);
}
else
{
color = SDL_MapRGB(dest_surface->format, 100, 100, 128);
}
}
}
SDL_FillRect(dest_surface, &dest_rect, color);
SDL_Surface* piece_surface = SDL_CreateRGBSurfaceWithFormat(0, SPRITE_SIZE, SPRITE_SIZE, 32, dest_surface->format->format);
@ -169,3 +217,390 @@ void Board::draw_board(SDL_Surface* dest_surface)
light_tile = !light_tile;
}
}
Team Board::get_active_color()
{
return active_color;
}
Piece Board::get_piece(int x, int y)
{
if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE)
return Piece();
return game_board[x][y];
}
Piece Board::get_piece(int space)
{
if (space < 0 || space >= BOARD_SIZE*BOARD_SIZE)
return Piece();
return game_board[space%BOARD_SIZE][space/BOARD_SIZE];
}
bool Board::get_able_to_castle(Team team, Type board_half)
{
if ((board_half != KING && board_half != QUEEN) || team == NO_TEAM)
return false;
return able_to_castle[team][board_half];
}
int Board::get_en_passant()
{
return en_passant;
}
int Board::get_half_turn_count()
{
return half_turn_count;
}
int Board::get_full_turn_count()
{
return full_turn_count;
}
void Board::set_selected_space(int x, int y)
{
if (game_board[x][y].get_team() != WHITE)
return;
selected_space = x+y*BOARD_SIZE;
}
void Board::set_selected_space(int space)
{
if (game_board[space%BOARD_SIZE][space/BOARD_SIZE].get_team() != WHITE)
return;
selected_space = space;
}
void Board::get_selected_space(int* x, int* y)
{
if (nullptr != x)
*x = selected_space%BOARD_SIZE;
if (nullptr != y)
*y = selected_space/BOARD_SIZE;
}
int Board::get_selected_space()
{
return selected_space;
}
std::vector<int> Board::get_moves_for_space(int x, int y)
{
if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE || game_board[x][y].get_type() == NO_TYPE)
return std::vector<int>();
int forward_direction = (game_board[x][y].get_team() == WHITE)?-1:1;
std::vector<int> out_spaces;
Type piece_type = (game_board[x][y].get_vis() == VIS_NONE)?UNKNOWN:game_board[x][y].get_type();
if (PAWN == piece_type)
{
if (y == ((game_board[x][y].get_team()==WHITE)?6:1) && game_board[x][y+forward_direction].get_type() == NO_TYPE && FREE == can_move(x+y*BOARD_SIZE, x+(y+2*forward_direction)*BOARD_SIZE))
{
out_spaces.push_back(x+(y+2*forward_direction)*BOARD_SIZE);
}
if (y+forward_direction >= 0 && y+forward_direction < BOARD_SIZE && FREE == can_move(x+y*BOARD_SIZE, x+(y+forward_direction)*BOARD_SIZE))
{
out_spaces.push_back(x+(y+forward_direction)*BOARD_SIZE);
}
if (x-1 >= 0 && x-1 < BOARD_SIZE && y+forward_direction >= 0 && y+forward_direction < BOARD_SIZE && CAPTURE == can_move(x+y*BOARD_SIZE, x+(y+forward_direction)*BOARD_SIZE-1))
{
out_spaces.push_back(x+(y+forward_direction)*BOARD_SIZE-1);
}
if (x+1 >= 0 && x+1 < BOARD_SIZE && y+forward_direction >= 0 && y+forward_direction < BOARD_SIZE && CAPTURE == can_move(x+y*BOARD_SIZE, x+(y+forward_direction)*BOARD_SIZE+1))
{
out_spaces.push_back(x+(y+forward_direction)*BOARD_SIZE+1);
}
if (x+(y+forward_direction)*BOARD_SIZE-1 == en_passant && FREE == can_move(x+y*BOARD_SIZE, x+(y+forward_direction)*BOARD_SIZE-1))
{
out_spaces.push_back(x+(y+forward_direction)*BOARD_SIZE-1);
}
else if (x+(y+forward_direction)*BOARD_SIZE+1 == en_passant && FREE == can_move(x+y*BOARD_SIZE, x+(y+forward_direction)*BOARD_SIZE+1))
{
out_spaces.push_back(x+(y+forward_direction)*BOARD_SIZE+1);
}
}
if (QUEEN == piece_type || ROOK == piece_type)
{
for (int x1 = x-1; x1 > 0; x1--)
{
MoveType result = can_move(x+y*BOARD_SIZE, x1+y*BOARD_SIZE);
if (BLOCKED == result)
break;
out_spaces.push_back(x1+y*BOARD_SIZE);
if (CAPTURE == result)
break;
}
for (int x1 = x+1; x1 < BOARD_SIZE; x1++)
{
MoveType result = can_move(x+y*BOARD_SIZE, x1+y*BOARD_SIZE);
if (BLOCKED == result)
break;
out_spaces.push_back(x1+y*BOARD_SIZE);
if (CAPTURE == result)
break;
}
for (int y1 = y-1; y1 > 0; y1--)
{
MoveType result = can_move(x+y*BOARD_SIZE, x+y1*BOARD_SIZE);
if (BLOCKED == result)
break;
out_spaces.push_back(x+y1*BOARD_SIZE);
if (CAPTURE == result)
break;
}
for (int y1 = y+1; y1 > 0; y1++)
{
MoveType result = can_move(x+y*BOARD_SIZE, x+y1*BOARD_SIZE);
if (BLOCKED == result)
break;
out_spaces.push_back(x+y1*BOARD_SIZE);
if (CAPTURE == result)
break;
}
}
if (QUEEN == piece_type || BISHOP == piece_type)
{
for(int x1 = x-1, y1 = y-1; x1 >= 0 && y1 >= 0; x1--, y1--)
{
MoveType result = can_move(x+y*BOARD_SIZE, x1+y1*BOARD_SIZE);
if (BLOCKED == result)
break;
out_spaces.push_back(x1+y1*BOARD_SIZE);
if (CAPTURE == result)
break;
}
for(int x1 = x-1, y1 = y+1; x1 >= 0 && y1 < BOARD_SIZE; x1--, y1++)
{
MoveType result = can_move(x+y*BOARD_SIZE, x1+y1*BOARD_SIZE);
if (BLOCKED == result)
break;
out_spaces.push_back(x1+y1*BOARD_SIZE);
if (CAPTURE == result)
break;
}
for(int x1 = x+1, y1 = y-1; x1 < BOARD_SIZE && y1 >= 0; x1++, y1--)
{
MoveType result = can_move(x+y*BOARD_SIZE, x1+y1*BOARD_SIZE);
if (BLOCKED == result)
break;
out_spaces.push_back(x1+y1*BOARD_SIZE);
if (CAPTURE == result)
break;
}
for(int x1 = x+1, y1 = y+1; x1 < BOARD_SIZE && y1 < BOARD_SIZE; x1++, y1++)
{
MoveType result = can_move(x+y*BOARD_SIZE, x1+y1*BOARD_SIZE);
if (BLOCKED == result)
break;
out_spaces.push_back(x1+y1*BOARD_SIZE);
if (CAPTURE == result)
break;
}
}
if (KNIGHT == piece_type)
{
int knight_offsets[8][2] = {{-1,2},{1,2},{-1,-2},{1,-2},{-2,1},{2,1},{-2,-1},{2,-1}};
for(int i = 0; i < 8; i++)
{
int x1 = x+knight_offsets[i][0];
int y1 = y+knight_offsets[i][1];
if (x1 < 0 || x1 >= BOARD_SIZE || y1 < 0 || y1 >= BOARD_SIZE || BLOCKED == can_move(x+y*BOARD_SIZE, x1+y1*BOARD_SIZE))
{
continue;
}
out_spaces.push_back(x1+y1*BOARD_SIZE);
}
}
if (KING == piece_type)
{
int king_offsets[8][2] = {{-1,-1},{0,-1},{1,-1},{-1,0},{1,0},{1,-1},{1,0},{1,1}};
for(int i = 0; i < 8; i++)
{
int x1 = x+king_offsets[i][0];
int y1 = y+king_offsets[i][1];
if (x1 < 0 || x1 >= BOARD_SIZE || y1 < 0 || y1 >= BOARD_SIZE || BLOCKED == can_move(x+y*BOARD_SIZE, x1+y1*BOARD_SIZE))
{
continue;
}
Piece old_piece = game_board[x1][y1];
game_board[x1][y1] = game_board[x][y];
game_board[x][y] = Piece();
bool was_check = is_check(game_board[x1][y1].get_team());
game_board[x][y] = game_board[x1][y1];
game_board[x1][y1] = old_piece;
if (!was_check)
{
out_spaces.push_back(x1+y1*BOARD_SIZE);
}
}
if (able_to_castle[game_board[x][y].get_team()][KING] &&
game_board[7][y].get_team() == game_board[x][y].get_team() && game_board[7][y].get_type() == ROOK && game_board[7][y].get_vis() == SHOWN &&
game_board[6][y].get_type() == NO_TYPE && game_board[5][y].get_type() == NO_TYPE)
{
Piece rook_piece = game_board[7][y];
Piece king_piece = game_board[x][y];
game_board[7][y] = king_piece;
game_board[x][y] = rook_piece;
if (!is_check(game_board[x][y].get_team()))
out_spaces.push_back(7+y*BOARD_SIZE);
game_board[7][y] = rook_piece;
game_board[x][y] = king_piece;
}
if (able_to_castle[game_board[x][y].get_team()][QUEEN] &&
game_board[0][y].get_team() == game_board[x][y].get_team() && game_board[0][y].get_type() == ROOK && game_board[0][y].get_vis() == SHOWN &&
game_board[1][y].get_type() == NO_TYPE && game_board[2][y].get_type() == NO_TYPE && game_board[3][y].get_type() == NO_TYPE)
{
Piece rook_piece = game_board[0][y];
Piece king_piece = game_board[x][y];
game_board[0][y] = king_piece;
game_board[x][y] = rook_piece;
if (!is_check(game_board[x][y].get_team()))
out_spaces.push_back(y*BOARD_SIZE);
game_board[0][y] = rook_piece;
game_board[x][y] = king_piece;
}
}
if (UNKNOWN == piece_type)
{
int unknown_offsets[8][2] = {{-1,-1},{0,-1},{1,-1},{-1,0},{1,0},{1,-1},{1,0},{1,1}};
for(int i = 0; i < 8; i++)
{
int x1 = x+unknown_offsets[i][0];
int y1 = y+unknown_offsets[i][1];
if (x1 < 0 || x1 >= BOARD_SIZE || y1 < 0 || y1 >= BOARD_SIZE)
{
continue;
}
MoveType result = can_move(x+y*BOARD_SIZE, x1+y1*BOARD_SIZE);
if (BLOCKED != result)
{
out_spaces.push_back(x1+y1*BOARD_SIZE);
if (CAPTURE != result)
{
int x1 = x+2*unknown_offsets[i][0];
int y1 = y+2*unknown_offsets[i][1];
if (x1 >= 0 && x1 < BOARD_SIZE && y1 >= 0 && y1 < BOARD_SIZE || BLOCKED == can_move(x+y*BOARD_SIZE, x1+y1*BOARD_SIZE))
{
out_spaces.push_back(x1+y1*BOARD_SIZE);
}
}
}
}
out_spaces.push_back(x+y*BOARD_SIZE);
}
return out_spaces;
}
std::vector<int> Board::get_moves_for_space(int space)
{
if (space < 0 || space >= BOARD_SIZE*BOARD_SIZE)
return std::vector<int>();
return get_moves_for_space(space%BOARD_SIZE, space/BOARD_SIZE);
}
bool Board::is_check(Team team)
{
int king_space = -1;
Team enemy_team = (team == WHITE)?BLACK:WHITE;
for(int x = 0; x < BOARD_SIZE && king_space == -1; x++)
{
for(int y = 0; y < BOARD_SIZE && king_space == -1; y++)
{
if (game_board[x][y].get_team() == team && game_board[x][y].get_type() == KING)
{
king_space = x+y*BOARD_SIZE;
break;
}
}
}
if (-1 == king_space)
{
return false;
}
for(int x = 0; x < BOARD_SIZE && king_space == -1; x++)
{
for(int y = 0; y < BOARD_SIZE && king_space == -1; y++)
{
if (game_board[x][y].get_team() == enemy_team)
{
std::vector<int> possible_moves = get_moves_for_space(x,y);
if (possible_moves.end() != std::find(possible_moves.begin(), possible_moves.end(), king_space))
{
return true;
}
}
}
}
return false;
}
MoveType Board::can_move(int space_from, int space_to)
{
if (space_from < 0 || space_to < 0 || space_from >= BOARD_SIZE*BOARD_SIZE || space_to >= BOARD_SIZE*BOARD_SIZE)
{
return BLOCKED;
}
int xf=space_from%BOARD_SIZE,yf=space_from/BOARD_SIZE,xt=space_to%BOARD_SIZE,yt=space_to/BOARD_SIZE;
Team enemy_team = (game_board[xf][yf].get_team() == WHITE)?BLACK:WHITE;
if (game_board[xt][yt].get_type() == NO_TYPE)
{
if (game_board[xf][yf].get_type() == PAWN && space_to == en_passant)
{
return CAPTURE;
}
else
{
return FREE;
}
}
if (game_board[xt][yt].get_team() == enemy_team)
{
return CAPTURE;
}
return BLOCKED;
}

View File

@ -14,13 +14,35 @@ public:
Board(std::string board_fen);
void load_FEN(std::string board_fen);
std::string make_FEN();
void draw_board(SDL_Surface* dest_surface);
Team get_active_color();
Piece get_piece(int x, int y);
Piece get_piece(int space);
bool get_able_to_castle(Team team, Type board_half);
int get_en_passant();
int get_half_turn_count();
int get_full_turn_count();
void set_selected_space(int x, int y);
void set_selected_space(int space);
void get_selected_space(int* x, int* y);
int get_selected_space();
std::vector<int> get_moves_for_space(int x, int y);
std::vector<int> get_moves_for_space(int space);
bool is_check(Team team);
private:
Piece game_board[BOARD_SIZE][BOARD_SIZE];
Team active_color;
bool able_to_castle[2][2];
std::vector<int> en_passant;
int en_passant;
int half_turn_count, full_turn_count;
int selected_space;
MoveType can_move(int space_from, int space_to);
};

70
game.cpp Normal file
View File

@ -0,0 +1,70 @@
#include "game.hpp"
#include <chrono>
uint64_t time_milli() {
using namespace std::chrono;
return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
}
Game::Game(SDL_Window* window, SDL_Surface* surface)
{
this->window = window;
this->surface = surface;
running = false;
}
Game::Game(SDL_Window* window, SDL_Surface* surface, std::string board_fen)
{
this->window = window;
this->surface = surface;
board = Board(board_fen);
running = false;
}
void Game::run()
{
uint64_t last_update_time = time_milli(), delta_time;
uint64_t time_quantum = 1000/60;
running = true;
while(running)
{
delta_time = time_milli()-last_update_time;
while(delta_time > time_quantum && running)
{
tick();
delta_time -= time_quantum;
last_update_time += time_quantum;
}
draw();
}
}
void Game::tick()
{
SDL_Event e;
while(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)
{
int x,y;
SDL_GetMouseState(&x, &y);
x/=SPRITE_SIZE;
y/=SPRITE_SIZE;
board.set_selected_space(x,y);
}
}
}
void Game::draw()
{
board.draw_board(surface);
SDL_UpdateWindowSurface(window);
}

24
game.hpp Normal file
View File

@ -0,0 +1,24 @@
#pragma once
#include <SDL2/SDL.h>
#include "board.hpp"
class Game
{
public:
Game(SDL_Window* window, SDL_Surface* surface);
Game(SDL_Window* window, SDL_Surface* surface, std::string board_fen);
void run();
private:
void tick();
void draw();
SDL_Window* window;
SDL_Surface* surface;
Board board;
bool running;
};

View File

@ -1,57 +1,43 @@
#include <SDL2/SDL.h>
#include "app_consts.hpp"
#include "board.hpp"
#include "sprites.hpp"
#include "game.hpp"
int main(int argc, char** argv)
{
//Sourced from lazyfoo's SDL2 tutorials, not really any special code here, just standard init process for the window
//The window we'll be rendering to
SDL_Window* window = NULL;
//The surface contained by the window
SDL_Surface* screenSurface = NULL;
//Initialize SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
}
else
{
//Create window
window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
window = SDL_CreateWindow("Schrodinger's Chess", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if(window == NULL)
{
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
}
else
{
//Get window surface
screenSurface = SDL_GetWindowSurface(window);
//Fill the surface white
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0x00, 0x00, 0x00));
Board board;
Game game(window, screenSurface);
board.draw_board(screenSurface);
//Update the surface
SDL_UpdateWindowSurface(window);
//Wait two seconds
SDL_Delay(5000);
game.run();
}
}
Sprite::close();
//Destroy window
SDL_DestroyWindow(window);
//Quit SDL subsystems
SDL_Quit();
return 0;

View File

@ -19,8 +19,8 @@ int Sprite::get(Team team, Type type, Visibility vis, SDL_Surface* dest_surface)
return -1;
}
SDL_Rect dest_rect{0,0,45,45};
SDL_Rect src_rect{type*45,team*45+vis*90,45,45};
SDL_Rect dest_rect{0,0,SPRITE_SIZE,SPRITE_SIZE};
SDL_Rect src_rect{type*SPRITE_SIZE,team*SPRITE_SIZE+vis*2*SPRITE_SIZE,SPRITE_SIZE,SPRITE_SIZE};
SDL_BlitSurface(sheet, &src_rect, dest_surface, &dest_rect);
return 0;