Define, add tests for and write partial impl. of get_all_moves

master
Justin Pilon 5 years ago
parent e40f10adde
commit 2575a55b2f

@ -1,3 +1,14 @@
#CC=g++-4.2
#
#a.out: main.o SerialComm.o
# g++ main.o SerialComm.o -lboost_system -lboost_filesystem -lboost_serialization -lpthread -std=c++11
#
#main.o: main.cpp
# g++ -c -std=c++11 main.cpp
#
#SerialComm.o: SerialComm.cpp SerialComm.hpp
# g++ -c -std=c++11 SerialComm.cpp
default: build
build:

Before

Width:  |  Height:  |  Size: 9.5 KiB

After

Width:  |  Height:  |  Size: 9.5 KiB

@ -1,4 +1,5 @@
#include "functions.h"
#include <sstream>
#include <stdexcept>
#include <cmath>
#include <iostream>
@ -22,10 +23,12 @@ Position pair_to_pos(std::pair<int, int> pr){
throw std::invalid_argument("Cannot use any pairs with values > 7 or < 0.");
}
int int_val = std::abs(pr.second - 7)*8 + pr.first;
if (int_val > 0 && int_val < 64) {
if (int_val >= 0 && int_val < 64) {
return static_cast<Position>(int_val);
} else {
throw std::invalid_argument("Something went terribly wrong. x and y < 8 && x and y > 0 but abs(y-7)*8 + x < 0 or >= 64");
std::stringstream ss;
ss << "Something went terribly wrong. x and y < 8 && x and y >= 0 but abs(y-7)*8 + x < 0 or >= 64. It equalled: " << int_val;
throw std::invalid_argument(ss.str());
}
}
std::vector<Position> get_possible_movers(Position pn, std::array<PieceType, 64> board){
@ -38,6 +41,30 @@ std::vector<Position> get_possible_moves(Position pn, std::array<PieceType, 64>
return pns;
}
std::vector<Position> get_all_moves(Position pn, std::array<PieceType, 64> board){
PieceType pt = board[pn];
std::vector<Position> pns;
int x = pos_to_pair(pn).first;
int y = pos_to_pair(pn).second;
switch(pt){
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)));
}
}
}
default:
break;
}
return pns;
}
std::array<PieceType, 64> dumb_move(Position from, Position to, std::array<PieceType, 64> board){
PieceType piece = board[from];
board[to] = piece;

@ -18,6 +18,9 @@ std::vector<Position> get_possible_movers(Position pn, std::array<PieceType, 64>
// This may require helper functions for each individual piece.
std::vector<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);
// 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
std::array<PieceType, 64> dumb_move(Position from, Position to, std::array<PieceType, 64> board);

@ -1,4 +1,6 @@
CC=g++-4.2
defualt: build
build:
g++ -std=c++11 -I../src/ -o tests.out ../src/functions.cpp tests_main.cpp
g++ -std=c++11 -Wall -I../src/ -o tests.out ../src/functions.cpp tests_main.cpp

@ -40,8 +40,49 @@ namespace Catch {
return ss.str();
}
};
// 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){
std::stringstream ss;
std::string files = " A B C D E F G H";
ss << "{ {" << std::endl;
ss << files << std::endl;
for (int i = 0; i < 8; ++i){
ss << 8-i << "|";
for (int j = 0; j < 8; ++j){
int index = (i*8)+j;
bool part_of_poss = false;
for (Position p : poss){
if (index == p) {
part_of_poss = true;
break;
}
}
part_of_poss ? ss << "* " : ss << " ";
}
ss << "|" << 8-i;
ss << std::endl;
}
ss << files << std::endl;
ss << "}" << std::endl;
ss << "," << std::endl;
ss << "{ ";
for (int pi = 0; pi < poss.size(); ++pi){
pi == poss.size()-1 ? ss << poss[pi] << "(" << POSITION_STRING[poss[pi]] << ") }" << std::endl : ss << poss[pi] << "(" << POSITION_STRING[poss[pi]] << "), ";
}
ss << "}" << std::endl;
return ss.str();
}
};
}
const std::array<PieceType, 64> TEST_MOVES = {
W_QUEEN, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
NONE, NONE, B_KNIGHT
};
const std::array<PieceType, 64> DUMB_MOVE_1 = {
B_ROOK, B_KNIGHT, B_BISHOP, B_QUEEN, B_KING, B_BISHOP, B_KNIGHT, B_ROOK,
B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN,
@ -74,13 +115,49 @@ TEST_CASE("Test convert method to go from a board position to an x and y", "[pos
}
TEST_CASE("Test what pieces may move where functon", "[get_possible_movers]"){
std::vector<Position> H1_possible_movers = {Position::G1, Position::G2};
std::vector<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> black_right_knight_possible_moves = {Position::H3, Position::F3};
std::vector<Position> white_a_pawn_possible_moves = {Position::A6};
CHECK(get_possible_moves(Position::G1, DEFAULT_BOARD) == black_right_knight_possible_moves);
CHECK(get_possible_moves(Position::A7, DEFAULT_BOARD) == white_a_pawn_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};
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);
}
TEST_CASE("Test all possible and impossible moves", "[get_all_moves]"){
std::vector<Position> white_right_knight_all_moves = {Position::F3, Position::H3, Position::E2, Position::G1};
std::vector<Position> black_A_pawn_all_moves = {Position::A7, Position::A6, Position::B6, Position::A5};
std::vector<Position> black_F_bishop_all_moves = {Position:F8, Position::E7, Position::G7, Position::D6, Position::H6, Position::C5, Position::B4, Position::A3};
std::vector<Position> black_queen_all_moves = {Position::A8, Position::B8, Position::C8, Position::D8, Position::E8, Position::F8, Position::G8, Position::H8, Position::C7, Position::D7, Position::E7, Position::B6, Position::D6, Position::F6, Position::A5, Position::D5, Position::G5, Position::D4, Position::H4, Position::D3, Position::D2, Position::D1};
std::vector<Position> white_A_rook_all_moves = {Position::A8, Position::A7, Position::A6, Position::A5, Position::A4, Position::A3, Position::A2, Position::A1, Position::B1, Position::C1, Position::D1, Position::E1, Position::F1, Position::G1, Position::H1};
std::vector<Position> white_king_all_moves = {Position::D2, Position::E2, Position::F2, Position::D1, Position::E1, Position::F1};
std::vector<Position> black_king_all_moves = {Position::D8, Position::E8, Position::F8, Position::D7, Position::E7, Position::F7};
std::vector<Position> white_F_bishop_all_moves = {Position::A6, Position::B5, Position::C4, Position::D3, Position::H3, Position::E2, Position::G2, Position::F1};
std::vector<Position> white_queen_all_moves = {Position::D8, Position::D7, Position::D6, Position::D5, Position::H5, Position::A4, Position::D4, Position::G4, Position::B3, Position::D3, Position::F3, Position::C2, Position::D2, Position::E2, Position::A1, Position::B1, Position::C1, Position::D1, Position::E1, Position::F1, Position::G1, Position::H1};
std::vector<Position> left_black_knight_all_moves = {Position::B8, Position::D7, Position::A6, Position::C6};
std::vector<Position> black_H_rook_all_moves = {Position::A8, Position::B8, Position::C8, Position::D8, Position::E8, Position::F8, Position::G8, Position::H8, Position::H7, Position::H6, Position::H5, Position::H4, Position::H3, Position::H2, Position::H1};
CHECK(get_all_moves(Position::H8, DEFAULT_BOARD) == black_H_rook_all_moves);
CHECK(get_all_moves(Position::G1, DEFAULT_BOARD) == white_right_knight_all_moves);
CHECK(get_all_moves(Position::A7, DEFAULT_BOARD) == black_A_pawn_all_moves);
CHECK(get_all_moves(Position::F8, DEFAULT_BOARD) == black_F_bishop_all_moves);
CHECK(get_all_moves(Position::D8, DEFAULT_BOARD) == black_queen_all_moves);
CHECK(get_all_moves(Position::A1, DEFAULT_BOARD) == white_A_rook_all_moves);
CHECK(get_all_moves(Position::E1, DEFAULT_BOARD) == white_king_all_moves);
CHECK(get_all_moves(Position::D1, DEFAULT_BOARD) == white_queen_all_moves);
CHECK(get_all_moves(Position::B8, DEFAULT_BOARD) == left_black_knight_all_moves);
}

Loading…
Cancel
Save