Pawn promotions addad.

master
Tait Hoyem 5 years ago
parent 14b15087d2
commit 5b7e16c081

@ -51,21 +51,39 @@ void _add_if_not_blocked(int pos, int from, std::vector<int> *pns, std::array<Pi
// 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, int from, std::vector<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->push_back(make_move(from, pos));
void _pawn_diag_add_if_not_blocked(int pos, int from, std::vector<int> *pns, std::array<PieceType, 120> *board, Color color_of_piece, Color color_of_opposite, int en_passant, int promoting){
if (is_valid_position(pos)){
if (_xy_is_color(pos, board, color_of_opposite)){
if (promoting){
if (color_of_piece == Color::WHITE){
pns->push_back(make_move(from, pos, PieceType::NONE, PieceType::W_KNIGHT));
pns->push_back(make_move(from, pos, PieceType::NONE, PieceType::W_BISHOP));
pns->push_back(make_move(from, pos, PieceType::NONE, PieceType::W_ROOK));
pns->push_back(make_move(from, pos, PieceType::NONE, PieceType::W_QUEEN));
} else {
pns->push_back(make_move(from, pos, PieceType::NONE, PieceType::B_KNIGHT));
pns->push_back(make_move(from, pos, PieceType::NONE, PieceType::B_BISHOP));
pns->push_back(make_move(from, pos, PieceType::NONE, PieceType::B_ROOK));
pns->push_back(make_move(from, pos, PieceType::NONE, PieceType::B_QUEEN));
}
} else {
pns->push_back(make_move(from, pos));
}
} else if (pos == en_passant){
pns->push_back(make_move(from, pos, PieceType::NONE, PieceType::NONE, 1));
}
}
}
// This is a specialized functions for the pawn's inability to take going forward.
// Notice the lack of push_backion where there usually is when (x,y) is a different color.
void _pawn_add_if_not_blocked(int pos, int from, std::vector<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::vector<int> *pns, std::array<PieceType, 120> *board, Color color_of_piece, Color color_of_opposite, bool *is_not_blocked, bool double_move){
if (*is_not_blocked){
if ((*board)[pos] != PieceType::NONE ||
(*board)[pos] == PieceType::INV){
*is_not_blocked = false;
} else {
pns->push_back(make_move(from, pos));
double_move?pns->push_back(make_move(from, pos, PieceType::NONE, PieceType::NONE, PieceType::NONE, 1)):pns->push_back(make_move(from, pos));
}
}
}
@ -159,14 +177,16 @@ void _get_all_moves_pawn(int pos, std::vector<int> *pns, std::array<PieceType, 1
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;
int promotion_rank = pc==Color::WHITE?Rank::RANK7:Rank::RANK2;
int is_promoting_if_forward = get_rank(pos) == promotion_rank?1:0;
bool *free_to_double_move = new bool(true);
_pawn_add_if_not_blocked(pos+offset1, pos, pns, board, pc, rc, free_to_double_move);
_pawn_add_if_not_blocked(pos+offset1, pos, pns, board, pc, rc, free_to_double_move, false);
if (get_rank(pos) == default_pawn_rank){ // If on second/seventh rank
_pawn_add_if_not_blocked(pos+offset2, pos, pns, board, pc, rc, free_to_double_move);
_pawn_add_if_not_blocked(pos+offset2, pos, pns, board, pc, rc, free_to_double_move, true);
}
// 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, pos, pns, board, pc, rc, en_passant);
_pawn_diag_add_if_not_blocked(pos+offset1-1, pos, pns, board, pc, rc, en_passant);
_pawn_diag_add_if_not_blocked(pos+offset1+1, pos, pns, board, pc, rc, en_passant, is_promoting_if_forward);
_pawn_diag_add_if_not_blocked(pos+offset1-1, pos, pns, board, pc, rc, en_passant, is_promoting_if_forward);
}

@ -72,22 +72,22 @@ inline int make_move(int from){
return from;
}
inline int make_move(int from, int to){
return from + (to << 7);
return from | (to << 7);
}
inline int make_move(int from, int to, PieceType captured){
return from + (to << 7) + ((int) captured << 14);
return from | (to << 7) | ((int) captured << 14);
}
inline int make_move(int from, int to, PieceType captured, PieceType promotion){
return from + (to << 7) + ((int) captured << 14) + ((int) promotion << 18);
return from | (to << 7) | ((int) captured << 14) | ((int) promotion << 18);
}
inline int make_move(int from, int to, PieceType captured, PieceType promotion, int en_passant){
return from + (to << 7) + ((int) captured << 14) + ((int) promotion << 18) + (en_passant << 22);
return from | (to << 7) | ((int) captured << 14) | ((int) promotion << 18) | (en_passant << 22);
}
inline int make_move(int from, int to, PieceType captured, PieceType promotion, int en_passant, int pawn_start){
return from + (to << 7) + ((int) captured << 14) + ((int) promotion << 18) + (en_passant << 22) + (pawn_start << 23);
return from | (to << 7) | ((int) captured << 14) | ((int) promotion << 18) | (en_passant << 22) | (pawn_start << 23);
}
inline int make_move(int from, int to, PieceType captured, PieceType promotion, int en_passant, int pawn_start, int castle_move){
return from + (to << 7) + ((int) captured << 14) + ((int) promotion << 18) + (en_passant << 22) + (pawn_start << 23) + (castle_move << 24);
return from | (to << 7) | ((int) captured << 14) | ((int) promotion << 18) | (en_passant << 22) | (pawn_start << 23) | (castle_move << 24);
}

@ -1,6 +1,12 @@
defualt: all.out
bitwise: bitwise.out
clean:
rm all.out
distclean:
rm *.o
rm *.out
catch_main.o:
g++ -w -c -o catch_main.o catch_main.cpp

@ -52,7 +52,7 @@ namespace Catch {
ss << "{ {" << std::endl;
ss << files << std::endl;
for (int i = 2; i < 10; ++i){
ss << i-1 << "|";
ss << 9-(i-2) << "|";
for (int j = 1; j < 9; ++j){
int index = (i*10)+j;
// This part loops through all positions in the list and checks if it contains the current index.
@ -67,7 +67,7 @@ namespace Catch {
// (if) part_of_poss ?(then) do this :(else) do that.
part_of_poss ? ss << "* " : ss << " ";
}
ss << "|" << i-1;
ss << "|" << 8-(i-2);
ss << std::endl;
}
ss << files << std::endl;

@ -113,6 +113,18 @@ TEST_CASE("Test that moves that put king in check are not returned", "[get_all_m
}
TEST_CASE("Tests for en pessant squares.", "[get_all_moves]"){
CHECK(get_to_squares(get_all_moves(EN_PASSANT_TEST_POS, EN_PASSANT_TEST_BOARD, false, EN_PASSANT_SQUARE)) == EN_PASSANT_TEST_MOVES);
auto en_pass_moves = get_all_moves(EN_PASSANT_TEST_POS, EN_PASSANT_TEST_BOARD, true, EN_PASSANT_SQUARE);
std::vector<int> en_passant_flags = {0, 1};
CHECK(get_to_squares(en_pass_moves) == EN_PASSANT_TEST_MOVES);
CHECK(get_en_passant_flags(en_pass_moves) == en_passant_flags);
CHECK(get_to_squares(get_all_moves(NO_EN_PASSANT_TEST_POS, NO_EN_PASSANT_TEST_BOARD)) == NO_EN_PASSANT_TEST_MOVES);
}
TEST_CASE("Test for pawn promotions.", "[get_all_moves]"){
auto prom_moves = get_all_moves(PROM_PAWN_POS, PROM_PAWN_BOARD);
CHECK(get_to_squares(prom_moves) == PROM_PAWN_MOVES);
CHECK(get_promoted_pieces(prom_moves) == PROM_PAWN_PROMS);
auto bprom_moves = get_all_moves(PROM_BPAWN_POS, PROM_BPAWN_BOARD);
CHECK(get_to_squares(bprom_moves) == PROM_BPAWN_MOVES);
CHECK(get_promoted_pieces(bprom_moves) == PROM_BPAWN_PROMS);
}

@ -170,6 +170,56 @@ const std::array<PieceType, 120> EN_PASSANT_TEST_BOARD = {
const std::vector<int> EN_PASSANT_TEST_MOVES = {
D6, E6
};
// Test pawn promotion
const int PROM_PAWN_POS = G7;
const std::array<PieceType, 120> PROM_PAWN_BOARD = {
INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
INV, B_ROOK, B_KNIGHT, B_BISHOP, B_QUEEN, B_KING, B_BISHOP, B_KNIGHT, B_ROOK,INV,
INV, B_PAWN, B_PAWN, B_PAWN, NONE, B_PAWN, B_PAWN, W_PAWN, B_PAWN,INV,
INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,INV,
INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,INV,
INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,INV,
INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,INV,
INV, W_PAWN, W_PAWN, W_PAWN, NONE, W_PAWN, W_PAWN, W_PAWN, W_PAWN,INV,
INV, W_ROOK, W_KNIGHT, W_BISHOP, W_QUEEN, W_KING, W_BISHOP, W_KNIGHT, W_ROOK, INV,
INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
INV, INV, INV, INV, INV, INV, INV, INV, INV, INV
};
const std::vector<int> PROM_PAWN_MOVES = {
H8, H8, H8, H8,
F8, F8, F8, F8
};
const std::vector<int> PROM_PAWN_PROMS = {
W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN,
W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN
};
// Black pawn promotion
const int PROM_BPAWN_POS = B2;
const std::array<PieceType, 120> PROM_BPAWN_BOARD = {
INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
INV, B_ROOK, B_KNIGHT, B_BISHOP, B_QUEEN, B_KING, B_BISHOP, B_KNIGHT, B_ROOK,INV,
INV, B_PAWN, B_PAWN, B_PAWN, NONE, B_PAWN, B_PAWN, B_PAWN, B_PAWN,INV,
INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,INV,
INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,INV,
INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,INV,
INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,INV,
INV, W_PAWN, B_PAWN, W_PAWN, NONE, W_PAWN, W_PAWN, W_PAWN, W_PAWN,INV,
INV, W_ROOK, W_KNIGHT, W_BISHOP, W_QUEEN, W_KING, W_BISHOP, W_KNIGHT, W_ROOK, INV,
INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
INV, INV, INV, INV, INV, INV, INV, INV, INV, INV
};
const std::vector<int> PROM_BPAWN_MOVES = {
C1, C1, C1, C1,
A1, A1, A1, A1
};
const std::vector<int> PROM_BPAWN_PROMS = {
B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN,
B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN
};
const int NO_EN_PASSANT_TEST_POS = D5;
const std::array<PieceType, 120> NO_EN_PASSANT_TEST_BOARD = {
@ -187,3 +237,27 @@ const std::array<PieceType, 120> NO_EN_PASSANT_TEST_BOARD = {
INV, INV, INV, INV, INV, INV, INV, INV, INV, INV
};
const std::vector<int> NO_EN_PASSANT_TEST_MOVES = {D6};
// Castling tests
const int CASTLING_POS = E1;
const std::array<PieceType, 120> CASTLING_BOARD = {
INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
INV, B_ROOK, NONE, NONE, NONE, B_KING, NONE, NONE, B_ROOK, INV,
INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV,
INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV,
INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV,
INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV,
INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV,
INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV,
INV, W_ROOK, NONE, NONE, NONE, W_KING, NONE, NONE, W_ROOK, INV,
INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
INV, INV, INV, INV, INV, INV, INV, INV, INV, INV
};
// Should NOT inclde D7
const std::vector<int> CASTLING_MOVES = {
D1, D2, E2, F2, F1,
C1, G1
};

Loading…
Cancel
Save