@ -13,13 +13,6 @@ inline Position _pair_to_pos_unsafe(int x, int y){
return static_cast < Position > ( std : : abs ( y - 7 ) * 8 + x ) ;
}
void _push_if_valid_pos ( int pos , std : : unordered_set < int > * pns ) {
if ( is_valid_position ( pos ) ) {
pns - > insert ( pos ) ;
}
}
// This function returns true if the color of the piece on tile (x,y) is the Color c
bool _xy_is_color ( int pos , std : : array < PieceType , 120 > * board , Color c ) {
return c = = Color : : WHITE ? is_white ( ( * board ) [ pos ] ) : is_black ( ( * board ) [ pos ] ) ;
@ -30,7 +23,7 @@ Color _rev_color(Color c){
}
// This function will set the boolean guarding it to false if it is blocked, thus stopping it from running.
void _add_if_not_blocked ( int pos , std : : unordered_set < int > * pns , std : : array < PieceType , 120 > * board , Color color_of_piece , Color color_of_opposite , bool * is_not_blocked ) {
void _add_if_not_blocked ( int pos , int from , std : : unordered_set < int > * pns , std : : array < PieceType , 120 > * board , Color color_of_piece , Color color_of_opposite , bool * is_not_blocked ) {
if ( * is_not_blocked ) {
if ( ! is_valid_position ( pos ) ) {
* is_not_blocked = false ;
@ -38,45 +31,41 @@ void _add_if_not_blocked(int pos, std::unordered_set<int> *pns, std::array<Piece
if ( _xy_is_color ( pos , board , color_of_piece ) ) {
* is_not_blocked = false ;
} else if ( _xy_is_color ( pos , board , color_of_opposite ) ) {
pns - > insert ( pos) ;
pns - > insert ( make_move( from , pos) ) ;
* is_not_blocked = false ;
} else {
pns - > insert ( pos) ;
pns - > insert ( make_move( from , pos) ) ;
}
}
}
}
// This function is for non-ray types only, as it ignores the 'ray rules', and just jumps over stuff (e.g. knight), or only moves one space generally (e.g. king)
void _add_if_not_blocked ( int pos , std : : unordered_set < int > * pns , std : : array < PieceType , 120 > * board , Color color_of_piece , Color color_of_opposite ) {
if ( _xy_is_color ( pos , board , color_of_piece ) ) {
void _add_if_not_blocked ( int pos , int from , std : : unordered_set < int > * pns , std : : array < PieceType , 120 > * board , Color color_of_piece , Color color_of_opposite ) {
if ( ! is_valid_position ( pos ) | |
_xy_is_color ( pos , board , color_of_piece ) ) {
return ;
} else {
pns - > insert ( pos) ;
pns - > insert ( make_move( from , pos) ) ;
}
}
// This is a specialized function for the pawn's diagonal takes.
// It will only to pns if there is a piece of opposite color on it.
void _pawn_diag_add_if_not_blocked ( int pos , std : : unordered_set < int > * pns , std : : array < PieceType , 120 > * board , Color color_of_piece , Color color_of_opposite , int en_passant ) {
void _pawn_diag_add_if_not_blocked ( int pos , int from , std : : unordered_set < int > * pns , std : : array < PieceType , 120 > * board , Color color_of_piece , Color color_of_opposite , int en_passant ) {
if ( is_valid_position ( pos ) & & ( _xy_is_color ( pos , board , color_of_opposite ) | |
pos = = en_passant ) ) {
pns - > insert ( pos) ;
pns - > insert ( make_move( from , pos) ) ;
}
}
// This is a specialized functions for the pawn's inability to take going forward.
// Notice the lack of insertion where there usually is when (x,y) is a different color.
void _pawn_add_if_not_blocked ( int pos , std : : unordered_set < int > * pns , std : : array < PieceType , 120 > * board , Color color_of_piece , Color color_of_opposite , bool * is_not_blocked ) {
void _pawn_add_if_not_blocked ( int pos , int from , std : : unordered_set < int > * pns , std : : array < PieceType , 120 > * board , Color color_of_piece , Color color_of_opposite , bool * is_not_blocked ) {
if ( * is_not_blocked ) {
if ( is_valid_position ( pos ) ) {
if ( ( * board ) [ pos ] ! = PieceType : : NONE | |
( * board ) [ pos ] = = PieceType : : INV ) {
* is_not_blocked = false ;
} else {
if ( _xy_is_color ( pos , board , color_of_piece ) ) {
* is_not_blocked = false ;
} else if ( _xy_is_color ( pos , board , color_of_opposite ) ) {
* is_not_blocked = false ;
} else {
pns - > insert ( pos ) ;
}
pns - > insert ( make_move ( from , pos ) ) ;
}
}
}
@ -86,7 +75,7 @@ void _get_all_moves_rook(int pos, std::unordered_set<int> *pns, std::array<Piece
for ( int rk_off : ROOK_PIECE_OFFSETS ) {
bool * not_blocked = new bool ( true ) ;
for ( int offset = 1 ; offset < 8 ; offset + + ) {
_add_if_not_blocked ( pos + ( rk_off * offset ) , p ns, board , pc , rc , not_blocked ) ;
_add_if_not_blocked ( pos + ( rk_off * offset ) , p os, p ns, board , pc , rc , not_blocked ) ;
}
}
@ -108,7 +97,7 @@ void _get_all_moves_bishop(int pos, std::unordered_set<int> *pns, std::array<Pie
for ( int bs_off : BISHOP_PIECE_OFFSETS ) {
bool * not_blocked = new bool ( true ) ;
for ( int offset = 1 ; offset < 8 ; offset + + ) {
_add_if_not_blocked ( pos + ( bs_off * offset ) , p ns, board , pc , rc , not_blocked ) ;
_add_if_not_blocked ( pos + ( bs_off * offset ) , p os, p ns, board , pc , rc , not_blocked ) ;
}
}
/*
@ -132,7 +121,7 @@ void _get_all_moves_bishop(int pos, std::unordered_set<int> *pns, std::array<Pie
void _get_all_moves_knight ( int pos , std : : unordered_set < int > * pns , std : : array < PieceType , 120 > * board , Color pc , Color rc ) {
for ( int kn_off : KNIGHT_PIECE_OFFSETS ) {
bool * not_blocked = new bool ( true ) ;
_add_if_not_blocked ( pos + kn_off , p ns, board , pc , rc ) ;
_add_if_not_blocked ( pos + kn_off , p os, p ns, board , pc , rc ) ;
}
/*
for ( int xo = 1 ; xo < = 2 ; xo + + ) {
@ -148,7 +137,7 @@ void _get_all_moves_knight(int pos, std::unordered_set<int> *pns, std::array<Pie
void _get_all_moves_king ( int pos , std : : unordered_set < int > * pns , std : : array < PieceType , 120 > * board , Color pc , Color rc ) {
for ( int kn_off : KING_PIECE_OFFSETS ) {
bool * not_blocked = new bool ( true ) ;
_add_if_not_blocked ( pos + kn_off , p ns, board , pc , rc ) ;
_add_if_not_blocked ( pos + kn_off , p os, p ns, board , pc , rc ) ;
}
@ -167,17 +156,17 @@ void _get_all_moves_king(int pos, std::unordered_set<int> *pns, std::array<Piece
void _get_all_moves_pawn ( int pos , std : : unordered_set < int > * pns , std : : array < PieceType , 120 > * board , Color pc , Color rc , int en_passant ) {
// if it's white use different offsets, and pawn starting rank
int offset2 = pc = = Color : : WHITE ? 20 : - 20 ;
int offset1 = pc = = Color : : WHITE ? 10 : - 10 ;
int offset2 = pc = = Color : : WHITE ? - 20 : 20 ;
int offset1 = pc = = Color : : WHITE ? - 10 : 10 ;
int default_pawn_rank = pc = = Color : : WHITE ? Rank : : RANK2 : Rank : : RANK7 ;
bool * free_to_double_move = new bool ( true ) ;
_pawn_add_if_not_blocked ( pos + offset1 , p ns, board , pc , rc , free_to_double_move ) ;
_pawn_add_if_not_blocked ( pos + offset1 , p os, p ns, board , pc , rc , free_to_double_move ) ;
if ( get_rank ( pos ) = = default_pawn_rank ) { // If on second/seventh rank
_pawn_add_if_not_blocked ( pos + offset2 , p ns, board , pc , rc , free_to_double_move ) ;
_pawn_add_if_not_blocked ( pos + offset2 , p os, p ns, board , pc , rc , free_to_double_move ) ;
}
// pos+offset1 is 1 rank up (or down) depending on color.
// Adding, or removing one will shift it over by one square, hence diagnoals.
_pawn_diag_add_if_not_blocked ( pos + offset1 + 1 , p ns, board , pc , rc , en_passant ) ;
_pawn_diag_add_if_not_blocked ( pos + offset1 - 1 , p ns, board , pc , rc , en_passant ) ;
_pawn_diag_add_if_not_blocked ( pos + offset1 + 1 , p os, p ns, board , pc , rc , en_passant ) ;
_pawn_diag_add_if_not_blocked ( pos + offset1 - 1 , p os, p ns, board , pc , rc , en_passant ) ;
}