Added threading, optimizations
This commit is contained in:
@@ -559,7 +559,7 @@ std::vector<int> Board::get_moves_for_space(int x, int y, bool check_for_check)
|
||||
|
||||
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}};
|
||||
int king_offsets[8][2] = {{-1,-1},{0,-1},{1,-1},{-1,0},{1,0},{-1,1},{0,1},{1,1}};
|
||||
for(int i = 0; i < 8; i++)
|
||||
{
|
||||
int x1 = x+king_offsets[i][0];
|
||||
@@ -794,13 +794,26 @@ void Board::do_move(int space_from, int space_to, bool holding_k)
|
||||
|
||||
if (castling)
|
||||
{
|
||||
game_board[xt][yt] = Piece();
|
||||
game_board[xf][yf] = Piece();
|
||||
if (xt == 7)
|
||||
{
|
||||
xt = 6;
|
||||
xf = 5;
|
||||
}
|
||||
else
|
||||
{
|
||||
xt = 2;
|
||||
xf = 3;
|
||||
}
|
||||
game_board[xf][yf] = target_piece;
|
||||
game_board[xt][yt] = cur_piece;
|
||||
}
|
||||
else
|
||||
{
|
||||
game_board[xf][yf] = Piece();
|
||||
game_board[xt][yt] = cur_piece;
|
||||
}
|
||||
game_board[xt][yt] = cur_piece;
|
||||
|
||||
if(is_check(enemy_team))
|
||||
{
|
||||
@@ -813,6 +826,144 @@ void Board::do_move(int space_from, int space_to, bool holding_k)
|
||||
selected_space = -1;
|
||||
}
|
||||
|
||||
int Board::get_attackers_for_space(int space_to)
|
||||
{
|
||||
int xt=space_to%BOARD_SIZE,yt=space_to/BOARD_SIZE;
|
||||
|
||||
if (space_to < 0 || space_to > BOARD_SIZE*BOARD_SIZE || game_board[xt][yt].get_team() == NO_TEAM)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int num_attackers = 0;
|
||||
|
||||
Team enemy_team = (game_board[xt][yt].get_team() == WHITE)?BLACK:WHITE;
|
||||
|
||||
int forward_direction = (game_board[xt][yt].get_team() == WHITE)?-1:1;
|
||||
|
||||
if (game_board[xt][yt].get_type() == PAWN && en_passant == xt+(yt-forward_direction)*BOARD_SIZE)
|
||||
{
|
||||
int en_passent_offsets[2][2] = {{-1,0},{1,0}};
|
||||
|
||||
for(int i = 0; i < 2; i++)
|
||||
{
|
||||
int xf = xt+en_passent_offsets[i][0], yf = yt+en_passent_offsets[i][1];
|
||||
|
||||
if (xf < 0 || xf >= BOARD_SIZE || yf < 0 || yf >= BOARD_SIZE)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (game_board[xf][yf].get_team() != enemy_team || game_board[xf][yf].get_type() != PAWN)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
num_attackers++;
|
||||
}
|
||||
}
|
||||
|
||||
int pawn_offsets[2][2] = {{-1, -1*forward_direction},{1, -1*forward_direction}};
|
||||
for(int i = 0; i < 2; i++)
|
||||
{
|
||||
int xf = xt+pawn_offsets[i][0], yf = yt+pawn_offsets[i][1];
|
||||
|
||||
if (xf < 0 || xf >= BOARD_SIZE || yf < 0 || yf >= BOARD_SIZE)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (game_board[xf][yf].get_team() != enemy_team || game_board[xf][yf].get_type() != PAWN)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
num_attackers++;
|
||||
}
|
||||
|
||||
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 xf = xt+king_offsets[i][0], yf = yt+king_offsets[i][1];
|
||||
|
||||
if (xf < 0 || xf >= BOARD_SIZE || yf < 0 || yf >= BOARD_SIZE)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (game_board[xf][yf].get_team() != enemy_team || (game_board[xf][yf].get_type() != KING && game_board[xf][yf].get_vis() != HIDDEN))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
num_attackers++;
|
||||
}
|
||||
|
||||
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 xf = xt+knight_offsets[i][0], yf = yt+knight_offsets[i][1];
|
||||
|
||||
if (xf < 0 || xf >= BOARD_SIZE || yf < 0 || yf >= BOARD_SIZE)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (game_board[xf][yf].get_team() != enemy_team || game_board[xf][yf].get_type() != KNIGHT)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
num_attackers++;
|
||||
}
|
||||
|
||||
int queen_bishop_offsets[28][2] = {{1,1},{2,2},{3,3},{4,4},{5,5},{6,6},{7,7},
|
||||
{-1,1},{-2,2},{-3,3},{-4,4},{-5,5},{-6,6},{-7,7},
|
||||
{1,-1},{2,-2},{3,-3},{4,-4},{5,-5},{6,-6},{7,-7},
|
||||
{-1,-1},{-2,-2},{-3,-3},{-4,-4},{-5,-5},{-6,-6},{-7,-7}};
|
||||
for(int i = 0; i < 28; i++)
|
||||
{
|
||||
int xf = xt+queen_bishop_offsets[i][0], yf = yt+queen_bishop_offsets[i][1];
|
||||
|
||||
if (xf < 0 || xf >= BOARD_SIZE || yf < 0 || yf >= BOARD_SIZE)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (game_board[xf][yf].get_team() != enemy_team || (game_board[xf][yf].get_type() != QUEEN && game_board[xf][yf].get_type() != BISHOP && game_board[xf][yf].get_vis() != SHOWN))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
num_attackers++;
|
||||
}
|
||||
|
||||
int queen_rook_offsets[28][2] = {{1,0},{2,0},{3,0},{4,0},{5,0},{6,0},{7,0},
|
||||
{0,1},{0,2},{0,3},{0,4},{0,5},{0,6},{0,7},
|
||||
{-1,0},{-2,0},{-3,0},{-4,0},{-5,0},{-6,0},{-7,0},
|
||||
{0,-1},{0,-2},{0,-3},{0,-4},{0,-5},{0,-6},{0,-7}};
|
||||
|
||||
for(int i = 0; i < 28; i++)
|
||||
{
|
||||
int xf = xt+queen_rook_offsets[i][0], yf = yt+queen_rook_offsets[i][1];
|
||||
|
||||
if (xf < 0 || xf >= BOARD_SIZE || yf < 0 || yf >= BOARD_SIZE)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (game_board[xf][yf].get_team() != enemy_team || (game_board[xf][yf].get_type() != QUEEN && game_board[xf][yf].get_type() != ROOK && game_board[xf][yf].get_vis() != SHOWN))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
num_attackers++;
|
||||
}
|
||||
|
||||
return num_attackers;
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -862,7 +1013,7 @@ MoveType Board::can_move(int space_from, int space_to)
|
||||
|
||||
bool Board::does_move_solve_check(int space_from, int space_to)
|
||||
{
|
||||
Board test_board(make_FEN());
|
||||
Board test_board(*this);
|
||||
test_board.do_move(space_from, space_to, false);
|
||||
return !test_board.is_check(game_board[space_from%BOARD_SIZE][space_from/BOARD_SIZE].get_team());
|
||||
}
|
||||
Reference in New Issue
Block a user