vector<Position> -> unordered_set<Position>

master
Tait Hoyem 5 years ago
parent 597eea5810
commit f14ba6346a

@ -16,26 +16,4 @@ int main(){
}
cout << endl;
}
cout << "poly X" << endl;
cout << "(1," << poly_knight_offset_x(1) << ")" << endl;
cout << "(2," << poly_knight_offset_x(2) << ")" << endl;
cout << "(3," << poly_knight_offset_x(3) << ")" << endl;
cout << "(4," << poly_knight_offset_x(4) << ")" << endl;
cout << "(5," << poly_knight_offset_x(5) << ")" << endl;
cout << "(6," << poly_knight_offset_x(6) << ")" << endl;
cout << "(7," << poly_knight_offset_x(7) << ")" << endl;
cout << "(8," << poly_knight_offset_x(8) << ")" << endl;
cout << "(9," << poly_knight_offset_x(9) << ")" << endl;
cout << "poly Y" << endl;
cout << "(1," << poly_knight_offset_y(1) << ")" << endl;
cout << "(2," << poly_knight_offset_y(2) << ")" << endl;
cout << "(3," << poly_knight_offset_y(3) << ")" << endl;
cout << "(4," << poly_knight_offset_y(4) << ")" << endl;
cout << "(5," << poly_knight_offset_y(5) << ")" << endl;
cout << "(6," << poly_knight_offset_y(6) << ")" << endl;
cout << "(7," << poly_knight_offset_y(7) << ")" << endl;
cout << "(8," << poly_knight_offset_y(8) << ")" << endl;
cout << "(9," << poly_knight_offset_y(9) << ")" << endl;
}

@ -48,19 +48,83 @@ bool is_valid_position(int x, int y){
y < 8 && y >= 0);
}
std::vector<Position> get_possible_movers(Position pn, std::array<PieceType, 64> board){
std::vector<Position> pns = {Position::A1};
std::unordered_set<Position> get_possible_movers(Position pn, std::array<PieceType, 64> board){
std::unordered_set<Position> pns = {Position::A1};
return pns;
}
std::vector<Position> get_possible_moves(Position pn, std::array<PieceType, 64> board){
std::vector<Position> pns = {Position::A1};
std::unordered_set<Position> get_possible_moves(Position pn, std::array<PieceType, 64> board){
std::unordered_set<Position> pns = {Position::A1};
return pns;
}
std::vector<Position> get_all_moves(Position pn, std::array<PieceType, 64> board){
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));
}
}
void _get_all_moves_rook(int x, int y, std::unordered_set<Position> *pns){
for (int offset=1; offset<8; offset++){
int xpoff = x+offset;
int ypoff = y+offset;
int xnoff = x-offset;
int ynoff = y-offset;
_push_if_valid_pos(xpoff, y, pns);
_push_if_valid_pos(x, ypoff, pns);
_push_if_valid_pos(xnoff, y, pns);
_push_if_valid_pos(x, ynoff, pns);
}
}
void _get_all_moves_bishop(int x, int y, std::unordered_set<Position> *pns){
for (int offset=1; offset<8; offset++){
int xpoff = x+offset;
int ypoff = y+offset;
int xnoff = x-offset;
int ynoff = y-offset;
_push_if_valid_pos(xpoff, ypoff, pns);
_push_if_valid_pos(xpoff, ynoff, pns);
_push_if_valid_pos(xnoff, ypoff, pns);
_push_if_valid_pos(xnoff, ynoff, pns);
}
}
void _get_all_moves_knight(int x, int y, std::unordered_set<Position> *pns){
for (int xo=1;xo<=2;xo++){
int yo=(xo==1)?2:1;
_push_if_valid_pos(x+xo, y+yo, pns);
_push_if_valid_pos(x-xo, y+yo, pns);
_push_if_valid_pos(x+xo, y-yo, pns);
_push_if_valid_pos(x-xo, y-yo, pns);
}
}
void _get_all_moves_king(int x, int y, std::unordered_set<Position> *pns){
_push_if_valid_pos(x+1, y+1, pns);
_push_if_valid_pos(x+1, y-1, pns);
_push_if_valid_pos(x-1, y+1, pns);
_push_if_valid_pos(x-1, y-1, pns);
_push_if_valid_pos(x, y+1, pns);
_push_if_valid_pos(x, y-1, pns);
_push_if_valid_pos(x+1, y, pns);
_push_if_valid_pos(x-1, y, pns);
}
void _get_all_moves_w_pawn(int x, int y, std::unordered_set<Position> *pns){
_push_if_valid_pos(x, y+1, pns);
_push_if_valid_pos(x, y+2, pns);
_push_if_valid_pos(x+1, y+1, pns);
_push_if_valid_pos(x-1, y+1, pns);
}
void _get_all_moves_b_pawn(int x, int y, std::unordered_set<Position> *pns){
_push_if_valid_pos(x, y-1, pns);
_push_if_valid_pos(x, y-2, pns);
_push_if_valid_pos(x-1, y-1, pns);
_push_if_valid_pos(x+1, y-1, pns);
}
std::unordered_set<Position> get_all_moves(Position pn, std::array<PieceType, 64> board){
PieceType pt = board[pn];
std::vector<Position> pns;
std::unordered_set<Position> pns;
int x = pos_to_pair(pn).first;
int y = pos_to_pair(pn).second;
std::vector<int> kg_dx = {-1,0,1,-1,0,1,-1,0,1};
@ -70,75 +134,49 @@ std::vector<Position> get_all_moves(Position pn, std::array<PieceType, 64> board
std::vector<int> Wpa_dx = {0,0,-1,0,1,0};
std::vector<int> Wpa_dy = {2,1,0,0,0,-1};
switch(pt){
case PieceType::B_QUEEN:
case PieceType::W_QUEEN:
_get_all_moves_rook(x, y, &pns);
_get_all_moves_bishop(x, y, &pns);
break;
case PieceType::B_ROOK:
case PieceType::W_ROOK:
for (int j = 7; j >= 0; j--){
if (j != y){
pns.push_back(pair_to_pos(std::make_pair(x,j)));
}
for (int i = 0; i < 8; i++){
if (j == y){
pns.push_back(pair_to_pos(std::make_pair(i,y)));
}
}
}
break;
_get_all_moves_rook(x, y, &pns);
break;
case PieceType::B_BISHOP:
case PieceType::W_BISHOP:
for (int r = 7; r >= 0; r--){
for (int f = 0; f < 8; f++){
for (int i=0; i<8; i++){
if ((f-i == x && r-i == y) ||
(f+i == x && r+i == y) ||
(f-i == x && r+i == y) ||
(f+i == x && r-i == y)){
pns.push_back(pair_to_pos(std::make_pair(f, r)));
break;
}
}
}
}
break;
_get_all_moves_bishop(x, y, &pns);
break;
case PieceType::B_KNIGHT:
case PieceType::W_KNIGHT:
for (int xo=1;xo<=2;xo++){
int yo=(xo==1)?2:1;
if (is_valid_position(x+xo, y+yo)){
pns.push_back(pair_to_pos(std::make_pair(x+xo, y+yo)));
}
if (is_valid_position(x-xo, y-yo)){
pns.push_back(pair_to_pos(std::make_pair(x-xo, y-yo)));
}
if (is_valid_position(x-xo, y+yo)){
pns.push_back(pair_to_pos(std::make_pair(x-xo, y+yo)));
}
if (is_valid_position(x+xo, y-yo)){
pns.push_back(pair_to_pos(std::make_pair(x+xo, y-yo)));
}
}
break;
_get_all_moves_knight(x, y, &pns);
break;
case PieceType::B_KING:
case PieceType::W_KING:
for (int j = 7; j >= 0; j--){
for (int i = 0; i < 8; i++){
for (int k = 0; k < 9; k++){
if (std::make_pair(x+kg_dx[k],y+kg_dy[k]) == std::make_pair(i,j))
pns.push_back(pair_to_pos(std::make_pair(i,j)));
}
}
}
_get_all_moves_king(x, y, &pns);
// for (int j = 7; j >= 0; j--){
// for (int i = 0; i < 8; i++){
// for (int k = 0; k < 9; k++){
// if (std::make_pair(x+kg_dx[k],y+kg_dy[k]) == std::make_pair(i,j))
// pns.push_back(pair_to_pos(std::make_pair(i,j)));
// }
// }
// }
break;
case PieceType::B_PAWN:
for (int j = 7; j >= 0; j--){
for (int i = 0; i < 8; i++){
for (int k = 0; k < 6; k++){
if (std::make_pair(x+Bpa_dx[k],y+Bpa_dy[k]) == std::make_pair(i,j))
pns.push_back(pair_to_pos(std::make_pair(i,j)));
}
}
}
_get_all_moves_b_pawn(x, y, &pns);
// for (int j = 7; j >= 0; j--){
// for (int i = 0; i < 8; i++){
// for (int k = 0; k < 6; k++){
// if (std::make_pair(x+Bpa_dx[k],y+Bpa_dy[k]) == std::make_pair(i,j))
// pns.push_back(pair_to_pos(std::make_pair(i,j)));
// }
// }
// }
break;
case PieceType::W_PAWN:
_get_all_moves_w_pawn(x, y, &pns);
/*
for (int j = 7; j >= 0; j--){
for (int i = 0; i < 8; i++){
for (int k = 0; k < 6; k++){
@ -146,7 +184,7 @@ std::vector<Position> get_all_moves(Position pn, std::array<PieceType, 64> board
pns.push_back(pair_to_pos(std::make_pair(i,j)));
}
}
}
}*/
break;
default:
break;

@ -1,4 +1,5 @@
#include "constants.h"
#include <unordered_set>
#include <utility>
#include <vector>
#include <math.h>
@ -26,14 +27,14 @@ bool is_valid_position(int x, int y);
// Get all positions of pieces which can move to this square
// This may require helper functions for each individual peice.
// TODO rename to something less stupid.
std::vector<Position> get_possible_movers(Position pn, std::array<PieceType, 64> pt);
std::unordered_set<Position> get_possible_movers(Position pn, std::array<PieceType, 64> pt);
// Get all possible moved for piece in Position pn.
// This may require helper functions for each individual piece.
std::vector<Position> get_possible_moves(Position pn, std::array<PieceType, 64> pt);
std::unordered_set<Position> get_possible_moves(Position pn, std::array<PieceType, 64> pt);
// Get all moved for piece in Position pn.
std::vector<Position> get_all_moves(Position pn, std::array<PieceType, 64> pt);
std::unordered_set<Position> get_all_moves(Position pn, std::array<PieceType, 64> pt);
// Dumb function to do board moves.
// Does NOT check for valid moves. Only moves PieceType of Pos1 to Pos2, then replaces Pos1 with Piecetype::NONE

@ -1,3 +1,4 @@
#include <unordered_set>
#include <constants.h>
const Position B_QUEEN_POS = E4;
@ -12,12 +13,12 @@ const std::array<PieceType, 64> B_QUEEN_BOARD = {
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE
};
const std::vector<Position> B_QUEEN_ALL_MOVES = {
const std::unordered_set<Position> B_QUEEN_ALL_MOVES = {
A8, E8,
B7, E7, H7,
C6, E6, G6,
D5, E5, F5,
A4, B4, C4, D4, E4, F4, G4, H4,
A4, B4, C4, D4, F4, G4, H4,
D3, E3, F3,
C2, E2, G2,
B1, E1, H1
@ -35,12 +36,11 @@ const std::array<PieceType, 64> B_BISHOP_BOARD = {
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE
};
const std::vector<Position> B_BISHOP_ALL_MOVES = {
const std::unordered_set<Position> B_BISHOP_ALL_MOVES = {
H8,
A7, G7,
B6, F6,
C5, E5,
D4,
C3, E3,
B2, F2,
A1, G1
@ -58,10 +58,9 @@ const std::array<PieceType, 64> B_KNIGHT_BOARD = {
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE
};
const std::vector<Position> B_KNIGHT_ALL_MOVES = {
const std::unordered_set<Position> B_KNIGHT_ALL_MOVES = {
C7, E7,
B6, F6,
D5,
B4, F4,
C3, E3
};
@ -78,9 +77,9 @@ const std::array<PieceType, 64> B_ROOK_BOARD = {
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE
};
const std::vector<Position> B_ROOK_ALL_MOVES = {
const std::unordered_set<Position> B_ROOK_ALL_MOVES = {
E8, E7, E6,
A5, B5, C5, D5, E5, F5, G5, H5,
A5, B5, C5, D5, F5, G5, H5,
E4, E3, E2, E1
};
@ -97,9 +96,9 @@ const std::array<PieceType, 64> B_KING_BOARD = {
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE
};
const std::vector<Position> B_KING_ALL_MOVES = {
const std::unordered_set<Position> B_KING_ALL_MOVES = {
B5, C5, D5,
B4, C4, D4,
B4, D4,
B3, C3, D3
};
@ -116,8 +115,7 @@ const std::array<PieceType, 64> B_PAWN_BOARD = {
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE
};
const std::vector<Position> B_PAWN_ALL_MOVES = {
F4,
const std::unordered_set<Position> B_PAWN_ALL_MOVES = {
E3, F3, G3,
F2
};
@ -132,9 +130,8 @@ const std::array<PieceType, 64> B_KNIGHT_SIDE1_BOARD = {
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE
};
const std::vector<Position> B_KNIGHT_SIDE1_ALL_MOVES = {
const std::unordered_set<Position> B_KNIGHT_SIDE1_ALL_MOVES = {
D8,
B7,
D6,
A5, C5
};
@ -149,8 +146,8 @@ const std::array<PieceType, 64> B_KING_SIDE1_BOARD = {
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE
};
const std::vector<Position> B_KING_SIDE1_ALL_MOVES = {
A8,B8,
const std::unordered_set<Position> B_KING_SIDE1_ALL_MOVES = {
B8,
A7,B7
};
@ -165,7 +162,7 @@ const std::array<PieceType, 64> B_PAWN_SIDE1_BOARD = {
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE
};
const std::vector<Position> B_PAWN_SIDE1_ALL_MOVES = {
const std::unordered_set<Position> B_PAWN_SIDE1_ALL_MOVES = {
A8,
A7, B7,
A6

@ -1,3 +1,4 @@
#include <unordered_set>
#include <constants.h>
const Position W_QUEEN_POS = E4;
@ -22,12 +23,12 @@ const std::array<PieceType, 64> W_QUEEN_BOARD_BLOCKED = {
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE
};
const std::vector<Position> W_QUEEN_ALL_MOVES = {
const std::unordered_set<Position> W_QUEEN_ALL_MOVES = {
A8, E8,
B7, E7, H7,
C6, E6, G6,
D5, E5, F5,
A4, B4, C4, D4, E4, F4, G4, H4,
A4, B4, C4, D4, F4, G4, H4,
D3, E3, F3,
C2, E2, G2,
B1, E1, H1
@ -45,12 +46,11 @@ const std::array<PieceType, 64> W_BISHOP_BOARD = {
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE
};
const std::vector<Position> W_BISHOP_ALL_MOVES = {
const std::unordered_set<Position> W_BISHOP_ALL_MOVES = {
H8,
A7, G7,
B6, F6,
C5, E5,
D4,
C3, E3,
B2, F2,
A1, G1
@ -79,17 +79,15 @@ const std::array<PieceType, 64> W_KNIGHT_SIDE1_BOARD = {
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE
};
const std::vector<Position> W_KNIGHT_SIDE1_ALL_MOVES = {
const std::unordered_set<Position> W_KNIGHT_SIDE1_ALL_MOVES = {
D8,
B7,
D6,
A5, C5
};
const std::vector<Position> W_KNIGHT_ALL_MOVES = {
const std::unordered_set<Position> W_KNIGHT_ALL_MOVES = {
C7, E7,
B6, F6,
D5,
B4, F4,
C3, E3
};
@ -106,9 +104,9 @@ const std::array<PieceType, 64> W_ROOK_BOARD = {
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE
};
const std::vector<Position> W_ROOK_ALL_MOVES = {
const std::unordered_set<Position> W_ROOK_ALL_MOVES = {
E8, E7, E6,
A5, B5, C5, D5, E5, F5, G5, H5,
A5, B5, C5, D5, F5, G5, H5,
E4, E3, E2, E1
};
@ -137,14 +135,14 @@ const std::array<PieceType, 64> W_KING_SIDE1_BOARD = {
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE
};
const std::vector<Position> W_KING_SIDE1_ALL_MOVES = {
A8,B8,
const std::unordered_set<Position> W_KING_SIDE1_ALL_MOVES = {
B8,
A7,B7
};
const std::vector<Position> W_KING_ALL_MOVES = {
const std::unordered_set<Position> W_KING_ALL_MOVES = {
B5, C5, D5,
B4, C4, D4,
B4, D4,
B3, C3, D3
};
@ -161,10 +159,9 @@ const std::array<PieceType, 64> W_PAWN_BOARD = {
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE
};
const std::vector<Position> W_PAWN_ALL_MOVES = {
const std::unordered_set<Position> W_PAWN_ALL_MOVES = {
F6,
E5, F5, G5,
F4
E5, F5, G5
};
const Position W_PAWN_SIDE1_POS = A1;
@ -178,6 +175,4 @@ const std::array<PieceType, 64> W_PAWN_SIDE1_BOARD = {
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE
};
const std::vector<Position> W_PAWN_SIDE1_ALL_MOVES = {
A8
};
const std::unordered_set<Position> W_PAWN_SIDE1_ALL_MOVES;

@ -43,8 +43,9 @@ namespace Catch {
};
// This overrides vectors of positions. I want it to print a board with the positions that are selected so we can see a representation of what positions are selected.
template<>
struct StringMaker<std::vector<Position>> {
static std::string convert(std::vector<Position> const& poss){
struct StringMaker<std::unordered_set<Position>> {
static std::string convert(std::unordered_set<Position> const& uo_poss){
std::vector<Position> poss(uo_poss.begin(), uo_poss.end());
std::stringstream ss;
std::string files = " A B C D E F G H";
ss << "{ {" << std::endl;
@ -144,13 +145,13 @@ TEST_CASE("Test that invalid position ints return false", "[is_valid_position]")
}
TEST_CASE("Test what pieces may move where functon", "[get_possible_movers]"){
std::vector<Position> H1_possible_movers = {Position::H2, Position::G1};
std::unordered_set<Position> H1_possible_movers = {Position::H2, Position::G1};
CHECK(get_possible_movers(Position::H3, DEFAULT_BOARD) == H1_possible_movers);
}
TEST_CASE("Test where this piece may move to", "[get_possible_moves]"){
std::vector<Position> white_right_knight_possible_moves = {Position::H3, Position::F3};
std::vector<Position> black_A_pawn_possible_moves = {Position::A6,Position::A5};
std::unordered_set<Position> white_right_knight_possible_moves = {Position::H3, Position::F3};
std::unordered_set<Position> black_A_pawn_possible_moves = {Position::A6,Position::A5};
CHECK(get_possible_moves(Position::G1, DEFAULT_BOARD) == white_right_knight_possible_moves);
CHECK(get_possible_moves(Position::A7, DEFAULT_BOARD) == black_A_pawn_possible_moves);
}
@ -162,7 +163,6 @@ TEST_CASE("Test all possible and impossible moves for black pieces", "[get_all_m
CHECK(get_all_moves(B_BISHOP_POS, B_BISHOP_BOARD) == B_BISHOP_ALL_MOVES);
CHECK(get_all_moves(B_KNIGHT_POS, B_KNIGHT_BOARD) == B_KNIGHT_ALL_MOVES);
CHECK(get_all_moves(B_PAWN_POS, B_PAWN_BOARD) == B_PAWN_ALL_MOVES);
}
TEST_CASE("Test all possible and impossible moves for whtie pieces", "[get_all_moves][white]"){
CHECK(get_all_moves(W_KING_POS, W_KING_BOARD) == W_KING_ALL_MOVES);

Loading…
Cancel
Save