Minor refactoring in preperation for bitshift moves

master
Tait Hoyem 5 years ago
parent 9fcf3b8a98
commit f80d961dea

@ -2,14 +2,20 @@
#include <unordered_set>
#include <iostream>
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 x, int y, std::unordered_set<Position> *pns){
if (is_valid_position(x, y)){
pns->insert(pair_to_pos(x, y));
pns->insert(_pair_to_pos_unsafe(x, y));
}
}
// This function returns true if the color of the piece on tile (x,y) is the Color c
bool _xy_is_color(int x, int y, std::array<PieceType, 64> board, Color c){
return c==Color::WHITE ? is_white(board[pair_to_pos(x, y)]) : is_black(board[pair_to_pos(x, y)]);
return c==Color::WHITE ? is_white(board[_pair_to_pos_unsafe(x, y)]) : is_black(board[_pair_to_pos_unsafe(x, y)]);
}
Color _rev_color(Color c){
@ -22,10 +28,10 @@ void _add_if_not_blocked(int x, int y, std::unordered_set<Position> *pns, std::a
if (_xy_is_color(x, y, board, color_of_piece)){
*is_not_blocked = false;
} else if (_xy_is_color(x, y, board, color_of_opposite)){
pns->insert(pair_to_pos(x, y));
pns->insert(_pair_to_pos_unsafe(x, y));
*is_not_blocked = false;
} else {
pns->insert(pair_to_pos(x, y));
pns->insert(_pair_to_pos_unsafe(x, y));
}
}
}
@ -35,7 +41,7 @@ void _add_if_not_blocked(int x, int y, std::unordered_set<Position> *pns, std::a
if (_xy_is_color(x, y, board, color_of_piece)){
return;
} else {
pns->insert(pair_to_pos(x, y));
pns->insert(_pair_to_pos_unsafe(x, y));
}
}
}
@ -44,8 +50,8 @@ void _add_if_not_blocked(int x, int y, std::unordered_set<Position> *pns, std::a
// It will only to pns if there is a piece of opposite color on it.
void _pawn_diag_add_if_not_blocked(int x, int y, std::unordered_set<Position> *pns, std::array<PieceType, 64> board, Color color_of_piece, Color color_of_opposite, Position en_passant){
if (is_valid_position(x, y) && (_xy_is_color(x, y, board, color_of_opposite) ||
pair_to_pos(x, y) == en_passant)){
pns->insert(pair_to_pos(x, y));
_pair_to_pos_unsafe(x, y) == en_passant)){
pns->insert(_pair_to_pos_unsafe(x, y));
}
}
// This is a specialized functions for the pawn's inability to take going forward.
@ -57,7 +63,7 @@ void _pawn_add_if_not_blocked(int x, int y, std::unordered_set<Position> *pns, s
} else if (_xy_is_color(x, y, board, color_of_opposite)){
*is_not_blocked = false;
} else {
pns->insert(pair_to_pos(x, y));
pns->insert(_pair_to_pos_unsafe(x, y));
}
}
}

@ -78,6 +78,7 @@ Color get_color(PieceType pt){
if (is_black(pt)) return Color::BLACK;
return Color::NO_COLOR;
}
Color get_color(int x, int y, std::array<PieceType, 64> board){
return get_color(board[pair_to_pos(x, y)]);
}
@ -101,6 +102,62 @@ std::unordered_set<Position> get_possible_moves(Position pn, std::array<PieceTyp
return get_all_moves(pn, board);
}
std::unordered_set<int> get_all_moves_bitwise(Position pn, std::array<PieceType, 64> board, bool recursive, Position en_passant){
}
//TODO: Make faster by running from king squar eonly, instead of running on every piece of opposite team.
void filter_checked_moves(Position pn, PieceType pt, std::array<PieceType, 64> board, std::unordered_set<Position> *pns){
PieceType my_king = is_white(pt)?PieceType::W_KING:PieceType::B_KING;
std::vector<Position> king_poss = get_pos_of(my_king, board);
Position my_king_pos;
if (king_poss.empty()){
return;
} else {
my_king_pos = king_poss[0];
}
int attackers = 0;
for (auto p_pn= pns->begin(); p_pn!=pns->end();){
// Make move
std::array<PieceType, 64> moved_board = dumb_move(pn, *p_pn, board);
// Get all piecetypes of other team
std::array<PieceType, 6> other_team = is_white(pt)?Pieces::BLACK:Pieces::WHITE;
bool checks_king = false;
// go through each piece of other team
for (PieceType other_p : other_team) {
checks_king = false;
// For every place the piecetype is
// NEW CODE
// for (Position psn : get_all_moves(my_king_pos, moved_board, false)){
//
// }
// \NEW CODE
for (Position psn : get_pos_of(other_p, moved_board)){
std::unordered_set<Position> other_moves = get_all_moves(psn, moved_board, false);
// for every position the piece can mvoe to
for (Position cp : other_moves){
if (cp == my_king_pos){
checks_king = true;
attackers++;
break;
}
}
if (checks_king){
break;
}
}
if (checks_king){
break;
}
}
if (checks_king){
p_pn = pns->erase(p_pn);
} else {
++p_pn;
}
}
}
std::unordered_set<Position> get_all_moves(Position pn, std::array<PieceType, 64> board, bool recursive, Position en_passant){
PieceType pt = board[pn];
std::unordered_set<Position> pns;
@ -137,48 +194,8 @@ std::unordered_set<Position> get_all_moves(Position pn, std::array<PieceType, 64
default:
break;
}
// This code removes spots that are illegal due to putting your own king in check.
// TODO: rewrite this in a function.
// TODO: make it less like vomit. and more like english.
if (recursive){
PieceType my_king = is_white(pt)?PieceType::W_KING:PieceType::B_KING;
std::vector<Position> king_poss = get_pos_of(my_king, board);
Position my_king_pos;
if (king_poss.empty()){
return pns;
} else {
my_king_pos = king_poss[0];
}
int attackers = 0;
for (auto p_pn= pns.begin(); p_pn!=pns.end();){
std::array<PieceType, 64> moved_board = dumb_move(pn, *p_pn, board);
std::array<PieceType, 6> other_team = is_white(pt)?Pieces::BLACK:Pieces::WHITE;
bool checks_king = false;
for (PieceType other_p : other_team) {
checks_king = false;
for (Position psn : get_pos_of(other_p, moved_board)){
std::unordered_set<Position> other_moves = get_all_moves(psn, moved_board, false);
for (Position cp : other_moves){
if (cp == my_king_pos){
checks_king = true;
attackers++;
break;
}
}
if (checks_king){
break;
}
}
if (checks_king){
break;
}
}
if (checks_king){
p_pn = pns.erase(p_pn);
} else {
++p_pn;
}
}
filter_checked_moves(pn, pt, board, &pns);
}
return pns;
}

Loading…
Cancel
Save