aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/evaluation/benchmark.cpp2
-rw-r--r--src/main/game/game_state.h111
-rw-r--r--src/main/game/global_cache.cpp8
-rw-r--r--src/main/game/global_cache.h4
-rw-r--r--src/main/game/move.cpp22
-rw-r--r--src/main/game/move.h20
-rw-r--r--src/main/game/pile.h1
-rw-r--r--src/main/game/search-state/game_state.cpp (renamed from src/main/game/game_state.cpp)100
-rw-r--r--src/main/game/search-state/game_state.h119
-rw-r--r--src/main/game/search-state/game_state.legal_moves.cpp (renamed from src/main/game/game_state.legal_moves.cpp)114
-rw-r--r--src/main/input-output/input/json-parsing/deal_parser.cpp6
-rw-r--r--src/main/input-output/output/state_printer.cpp14
-rw-r--r--src/main/input-output/output/state_printer.h8
-rw-r--r--src/main/solver/solver.cpp41
-rw-r--r--src/main/solver/solver.h10
-rw-r--r--src/test/test_helper.cpp10
-rw-r--r--src/test/unit_tests/foundations_dominance_test.cpp2
-rw-r--r--src/test/unit_tests/global_cache_test.cpp2
-rw-r--r--src/test/unit_tests/legal_move_gen_test.cpp123
19 files changed, 384 insertions, 333 deletions
diff --git a/src/main/evaluation/benchmark.cpp b/src/main/evaluation/benchmark.cpp
index 0712032..42fff9d 100644
--- a/src/main/evaluation/benchmark.cpp
+++ b/src/main/evaluation/benchmark.cpp
@@ -7,7 +7,7 @@
#include <numeric>
#include "benchmark.h"
-#include "../game/game_state.h"
+#include "../game/search-state/game_state.h"
#include "../solver/solver.h"
using namespace std;
diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h
deleted file mode 100644
index 1e527ea..0000000
--- a/src/main/game/game_state.h
+++ /dev/null
@@ -1,111 +0,0 @@
-//
-// Created by thecharlesblake on 10/24/17.
-//
-
-#ifndef SOLVITAIRE_GAME_STATE_H
-#define SOLVITAIRE_GAME_STATE_H
-
-#include <vector>
-#include <list>
-#include <string>
-
-#include <boost/functional/hash.hpp>
-#include <boost/optional/optional.hpp>
-
-#include "../../../lib/rapidjson/document.h"
-#include "card.h"
-#include "pile.h"
-#include "sol_rules.h"
-
-class game_state {
- friend class deal_parser;
- friend class state_printer;
- friend class predicate;
- friend class hasher;
- friend class global_cache;
- friend class cached_game_state;
-public:
- // Defines types
- typedef uint8_t pile_ref;
- struct move {
- move(pile_ref, pile_ref, pile::size_type = 1);
- bool is_dominance() const;
- friend bool operator==(const move&, const move&);
-
- const static pile::size_type dominance_flag;
- pile_ref from; pile_ref to; pile::size_type count;
- };
-
- // Constructors
- // Creates a game state representation from a JSON doc
- game_state(const sol_rules&, const rapidjson::Document&);
- // Does the same from a seed
- game_state(const sol_rules&, int seed);
- // Does the same but with an initialiser list (useful for testing)
- game_state(const sol_rules&, std::initializer_list<pile>);
-
- // Alters state
- void make_move(move);
- void undo_move(move);
- void place_card(pile_ref, card);
- card take_card(pile_ref);
-
- std::vector<move> get_legal_moves(move = move(255, 255, 255));
-
- // Inspects state
- bool is_solved() const;
- const std::vector<pile>& get_data() const;
- boost::optional<move> get_dominance_move() const;
-
- friend std::ostream& operator<< (std::ostream&, const game_state&);
-
-private:
- static std::vector<card> gen_shuffled_deck(int, card::rank_t, bool);
-
- // Private constructor
- explicit game_state(const sol_rules&);
-
- // Used to maintain pile order invariant
- void eval_pile_order(pile_ref, bool);
- void eval_pile_order(std::list<pile_ref>&, pile_ref, bool);
-
- // Used in get_legal_moves()
- move get_stock_tableau_move() const;
- bool is_valid_tableau_move(pile_ref, pile_ref) const;
- bool is_valid_foundations_move(pile_ref, pile_ref) const;
- bool is_valid_hole_move(pile_ref) const;
- void get_built_group_moves(std::vector<move>&) const;
- pile::size_type get_built_group_height(pile_ref) const;
- bool valid_built_group_move(card, card, card) const;
- void add_built_group_move(std::vector<move>&, pile_ref, pile_ref) const;
- void add_empty_built_group_moves(std::vector<move>&, pile_ref, pile_ref,
- card) const;
- bool dominance_blocks_foundation_move(pile_ref);
- bool is_ordered_pile(pile_ref) const;
-
- void update_auto_foundation_moves(pile_ref);
- bool is_valid_auto_foundation_move(pile_ref) const;
-
- // References to piles
- const sol_rules rules;
- std::list<pile_ref> tableau_piles;
- std::list<pile_ref> cells;
- pile_ref stock;
- pile_ref waste;
- std::list<pile_ref> reserve;
- std::vector<pile_ref> foundations;
- pile_ref hole;
-
- // Stores the original pile refs of the commutative piles so that they can
- // be printed in the order the user expects
- std::vector<pile_ref> original_tableau_piles;
- std::vector<pile_ref> original_cells;
- std::vector<pile_ref> original_reserve;
-
- std::vector<bool> auto_foundation_moves;
-
- // The core piles
- std::vector<pile> piles;
-};
-
-#endif //SOLVITAIRE_GAME_STATE_H
diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp
index f2320ef..3620ddb 100644
--- a/src/main/game/global_cache.cpp
+++ b/src/main/game/global_cache.cpp
@@ -26,7 +26,7 @@ cached_game_state::cached_game_state(const game_state& gs) {
add_card(gs.piles[gs.hole].top_card(), gs);
}
- for (game_state::pile_ref pr : gs.cells) {
+ for (pile::ref pr : gs.cells) {
add_pile(pr, gs);
}
if (gs.rules.cells > 0) {
@@ -43,21 +43,21 @@ cached_game_state::cached_game_state(const game_state& gs) {
}
}
- for (game_state::pile_ref pr : gs.reserve) {
+ for (pile::ref pr : gs.reserve) {
add_pile(pr, gs);
}
if (gs.rules.reserve_size > 0) {
add_card_divider();
}
- for (game_state::pile_ref pr : gs.tableau_piles) {
+ for (pile::ref pr : gs.tableau_piles) {
add_pile(pr, gs);
add_card_divider();
}
}
-void cached_game_state::add_pile(game_state::pile_ref pr, const game_state& gs) {
+void cached_game_state::add_pile(pile::ref pr, const game_state& gs) {
for (card c : gs.piles[pr].pile_vec) {
add_card(c, gs);
}
diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h
index b3164ea..9541100 100644
--- a/src/main/game/global_cache.h
+++ b/src/main/game/global_cache.h
@@ -11,14 +11,14 @@
#include <boost/pool/pool_alloc.hpp>
#include "sol_rules.h"
-#include "game_state.h"
+#include "search-state/game_state.h"
struct cached_game_state {
typedef std::vector<card> state_data;
typedef state_data::size_type size_type;
explicit cached_game_state(const game_state&);
- void add_pile(game_state::pile_ref, const game_state&);
+ void add_pile(pile::ref, const game_state&);
void add_card(card, const game_state&);
void add_card_divider();
diff --git a/src/main/game/move.cpp b/src/main/game/move.cpp
new file mode 100644
index 0000000..053b3ba
--- /dev/null
+++ b/src/main/game/move.cpp
@@ -0,0 +1,22 @@
+//
+// Created by thecharlesblake on 07/06/18.
+//
+
+#include "move.h"
+
+const pile::size_type move::dominance_flag = pile::max_size_type;
+
+move::move(pile::ref f, pile::ref t, pile::size_type i) : from(f), to(t), count(i) {
+ assert(i >= 1);
+}
+
+// Returns true is the move has been flagged as a special 'dominance' move
+bool move::is_dominance() const {
+ return count == dominance_flag;
+}
+
+bool operator==(const move& a, const move& b) {
+ return (a.from == b.from )
+ && (a.to == b.to )
+ && (a.count == b.count);
+} \ No newline at end of file
diff --git a/src/main/game/move.h b/src/main/game/move.h
new file mode 100644
index 0000000..81a6116
--- /dev/null
+++ b/src/main/game/move.h
@@ -0,0 +1,20 @@
+//
+// Created by thecharlesblake on 07/06/18.
+//
+
+#ifndef SOLVITAIRE_MOVE_H
+#define SOLVITAIRE_MOVE_H
+
+#include "pile.h"
+
+struct move {
+ move(pile::ref , pile::ref, pile::size_type = 1);
+ bool is_dominance() const;
+ friend bool operator==(const move&, const move&);
+
+ const static pile::size_type dominance_flag;
+ pile::ref from; pile::ref to; pile::size_type count;
+};
+
+
+#endif //SOLVITAIRE_MOVE_H
diff --git a/src/main/game/pile.h b/src/main/game/pile.h
index d86ea37..5ab2404 100644
--- a/src/main/game/pile.h
+++ b/src/main/game/pile.h
@@ -18,6 +18,7 @@ class pile {
public:
typedef uint8_t size_type;
const static size_type max_size_type;
+ typedef uint8_t ref;
pile() = default;
pile(std::vector<card>);
diff --git a/src/main/game/game_state.cpp b/src/main/game/search-state/game_state.cpp
index 2e79f41..bd533f1 100644
--- a/src/main/game/game_state.cpp
+++ b/src/main/game/search-state/game_state.cpp
@@ -9,16 +9,27 @@
#include <functional>
#include <boost/functional/hash.hpp>
+#include <boost/optional.hpp>
#include "game_state.h"
-#include "../../../lib/rapidjson/document.h"
-#include "../input-output/input/json-parsing/deal_parser.h"
-#include "../input-output/output/state_printer.h"
-#include "../input-output/output/log_helper.h"
+#include "document.h"
+#include "../../input-output/input/json-parsing/deal_parser.h"
+#include "../../input-output/output/state_printer.h"
+#include "../../input-output/output/log_helper.h"
+#include "../move.h"
-using namespace std;
using namespace rapidjson;
-using namespace boost;
+using std::vector;
+using std::list;
+using std::find;
+using std::begin;
+using std::end;
+using std::rbegin;
+using std::rend;
+using std::runtime_error;
+using std::default_random_engine;
+using std::max;
+using std::ostream;
typedef sol_rules::build_policy pol;
typedef sol_rules::stock_deal_type sdt;
@@ -36,14 +47,14 @@ game_state::game_state(const sol_rules& s_rules) : rules(s_rules),
// If there is a hole, creates pile
if (rules.hole) {
piles.emplace_back();
- hole = static_cast<pile_ref>(piles.size() - 1);
+ hole = static_cast<pile::ref>(piles.size() - 1);
}
// If there are foundation piles, creates the relevant pile vectors
if (rules.foundations) {
for (uint8_t i = 0; i < 4*(rules.two_decks ? 2:1); i++) {
piles.emplace_back();
- foundations.push_back(static_cast<pile_ref>(piles.size() - 1));
+ foundations.push_back(static_cast<pile::ref>(piles.size() - 1));
auto_foundation_moves.push_back(true);
}
}
@@ -52,20 +63,20 @@ game_state::game_state(const sol_rules& s_rules) : rules(s_rules),
if (rules.cells > 0) {
for (uint8_t i = 0; i < rules.cells; i++) {
piles.emplace_back();
- cells.push_back(static_cast<pile_ref>(piles.size() - 1));
- original_cells.push_back(static_cast<pile_ref>(piles.size() - 1));
+ cells.push_back(static_cast<pile::ref>(piles.size() - 1));
+ original_cells.push_back(static_cast<pile::ref>(piles.size() - 1));
}
}
// If there is a stock, creates a pile
if (rules.stock_size > 0) {
piles.emplace_back();
- stock = static_cast<pile_ref>(piles.size() - 1);
+ stock = static_cast<pile::ref>(piles.size() - 1);
// If the stock deals to a waste, create the waste
if (rules.stock_deal_t == sdt::WASTE) {
piles.emplace_back();
- waste = static_cast<pile_ref>(piles.size() - 1);
+ waste = static_cast<pile::ref>(piles.size() - 1);
}
}
@@ -75,16 +86,16 @@ game_state::game_state(const sol_rules& s_rules) : rules(s_rules),
uint8_t(1) : rules.reserve_size;
for (uint8_t i = 0; i < pile_count; i++) {
piles.emplace_back();
- reserve.push_back(static_cast<pile_ref>(piles.size() - 1));
- original_reserve.push_back(static_cast<pile_ref>(piles.size() - 1));
+ reserve.push_back(static_cast<pile::ref>(piles.size() - 1));
+ original_reserve.push_back(static_cast<pile::ref>(piles.size() - 1));
}
}
// Creates the tableau piles
for (uint8_t i = 0; i < rules.tableau_pile_count; i++) {
piles.emplace_back();
- tableau_piles.push_back(static_cast<pile_ref>(piles.size() - 1));
- original_tableau_piles.push_back(static_cast<pile_ref>(piles.size() - 1));
+ tableau_piles.push_back(static_cast<pile::ref>(piles.size() - 1));
+ original_tableau_piles.push_back(static_cast<pile::ref>(piles.size() - 1));
}
}
@@ -149,14 +160,14 @@ game_state::game_state(const sol_rules& s_rules, int seed)
auto row_idx = t / original_tableau_piles.size();
if (rules.diagonal_deal && row_idx < original_tableau_piles.size()) {
p = original_tableau_piles.size()-p-1;
- pile_ref tableau_pile = original_tableau_piles[p];
+ pile::ref tableau_pile = original_tableau_piles[p];
if (p >= row_idx) {
place_card(tableau_pile, c);
deck.pop_back();
}
} else {
- pile_ref tableau_pile = original_tableau_piles[p];
+ pile::ref tableau_pile = original_tableau_piles[p];
place_card(tableau_pile, c);
deck.pop_back();
}
@@ -173,7 +184,7 @@ game_state::game_state(const sol_rules& s_rules, int seed)
game_state::game_state(const sol_rules& s_rules,
std::initializer_list<pile> il)
: game_state(s_rules) {
- pile_ref pr = 0;
+ pile::ref pr = 0;
for (const pile& p : il) {
for (const card c : p.pile_vec) {
place_card(pr, c);
@@ -210,18 +221,6 @@ vector<card> game_state::gen_shuffled_deck(int seed, card::rank_t max_rank,
return deck;
}
-const pile::size_type game_state::move::dominance_flag = pile::max_size_type;
-
-game_state::move::move(pile_ref f, pile_ref t, pile::size_type i)
- : from(f), to(t), count(i) {
- assert(i >= 1);
-}
-
-// Returns true is the move has been flagged as a special 'dominance' move
-bool game_state::move::is_dominance() const {
- return count == dominance_flag;
-}
-
/////////////////////////
// MODIFYING FUNCTIONS //
@@ -235,13 +234,13 @@ void game_state::make_move(const move m) {
assert(m.from == stock);
assert(m.to == 255);
- for (pile_ref tab_pr = original_tableau_piles.front();
- tab_pr < pile_ref(original_tableau_piles.front() + m.count);
+ for (pile::ref tab_pr = original_tableau_piles.front();
+ tab_pr < pile::ref(original_tableau_piles.front() + m.count);
tab_pr++) {
place_card(tab_pr, take_card(stock));
}
}
- // Handles stock-to-waste moves
+ // Handles stock-to-waste moves
else {
if (piles[stock].empty()) {
// Re-deal waste back to stock
@@ -257,7 +256,7 @@ void game_state::make_move(const move m) {
}
}
- // If this is not a built-pile move
+ // If this is not a built-pile move
else if (m.count == 1 || m.is_dominance()) {
assert(m.from < piles.size());
assert(m.to < piles.size());
@@ -268,7 +267,7 @@ void game_state::make_move(const move m) {
#endif
}
- // If this is a built-pile move
+ // If this is a built-pile move
else {
assert(m.from < piles.size() );
assert(m.to < piles.size() );
@@ -293,13 +292,13 @@ void game_state::undo_move(const move m) {
if (m.from == stock) {
// Handles special stock-to-tableau-piles move
if (rules.stock_deal_t == sdt::TABLEAU_PILES) {
- for (pile_ref tab_pr = original_tableau_piles.front() + m.count;
+ for (pile::ref tab_pr = original_tableau_piles.front() + m.count;
tab_pr-- > original_tableau_piles.front();
) {
place_card(stock, take_card(tab_pr));
}
}
- // Handles stock-to-waste moves
+ // Handles stock-to-waste moves
else {
if (piles[waste].empty()) {
// Re-deal waste back to stock
@@ -314,7 +313,7 @@ void game_state::undo_move(const move m) {
}
}
- // If this is not a built-pile move
+ // If this is not a built-pile move
else if (m.count == 1 || m.is_dominance()) {
assert(m.to < piles.size());
place_card(m.from, take_card(m.to));
@@ -323,7 +322,7 @@ void game_state::undo_move(const move m) {
#endif
}
- // If this is a built-pile move
+ // If this is a built-pile move
else {
assert(m.to < piles.size());
assert(m.count <= rules.max_rank);
@@ -341,7 +340,7 @@ void game_state::undo_move(const move m) {
// Places a card on a pile and if it is on a tableau, cell or reserve pile,
// reorders the pile refs so that the largest pile is first
-void game_state::place_card(pile_ref pr, card c) {
+void game_state::place_card(pile::ref pr, card c) {
piles[pr].place(c);
#ifndef NO_PILE_SYMMETRY
// If the stock deals to the tableau piles, there is no pile symmetry
@@ -352,7 +351,7 @@ void game_state::place_card(pile_ref pr, card c) {
}
// Same as above but for taking cards
-card game_state::take_card(pile_ref pr) {
+card game_state::take_card(pile::ref pr) {
card c = piles[pr].take();
#ifndef NO_PILE_SYMMETRY
// If the stock deals to the tableau piles, there is no pile symmetry
@@ -365,7 +364,7 @@ card game_state::take_card(pile_ref pr) {
// Assesses whether the pile ref that was modified was a tableau, cell or
// reserve pile, and if so makes the relevant function call
-void game_state::eval_pile_order(pile_ref pr, bool is_place) {
+void game_state::eval_pile_order(pile::ref pr, bool is_place) {
if (!original_tableau_piles.empty()
&& pr >= original_tableau_piles[0]
&& pr < original_tableau_piles[0] + original_tableau_piles.size()) {
@@ -386,7 +385,7 @@ void game_state::eval_pile_order(pile_ref pr, bool is_place) {
// Finds the pile ref in the list and evaluates whether it should be moved to
// maintain the "pile order"
-void game_state::eval_pile_order(list<pile_ref>& pile_lst, pile_ref changed_pr,
+void game_state::eval_pile_order(list<pile::ref>& pile_lst, pile::ref changed_pr,
bool is_place) {
// Finds the first pile that is larger/smaller than the ref pile
// (the comp pile).
@@ -434,7 +433,7 @@ void game_state::eval_pile_order(list<pile_ref>& pile_lst, pile_ref changed_pr,
}
// Decides which 'auto foundation' booleans should be 'true' currently
-void game_state::update_auto_foundation_moves(pile_ref target_pile) {
+void game_state::update_auto_foundation_moves(pile::ref target_pile) {
// If there are no foundations, or this is not a foundation move, do nothing
if (!rules.foundations
|| target_pile < foundations.front()
@@ -444,12 +443,12 @@ void game_state::update_auto_foundation_moves(pile_ref target_pile) {
// Turns the 'auto' boolean for the target foundation false
- for (pile_ref pr : foundations) {
+ for (pile::ref pr : foundations) {
auto_foundation_moves[pr] = is_valid_auto_foundation_move(pr);
}
}
-bool game_state::is_valid_auto_foundation_move(pile_ref target_pile) const {
+bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const {
#ifndef AUTO_FOUNDATIONS_STREAMLINER
if (rules.build_pol == pol::NO_BUILD || rules.build_pol == pol::SAME_SUIT)
#endif
@@ -466,7 +465,7 @@ bool game_state::is_valid_auto_foundation_move(pile_ref target_pile) const {
// same colour and the other colour
int same_rank_diff = 0;
int other_rank_diff = 0;
- for (pile_ref pr : foundations) {
+ for (pile::ref pr : foundations) {
if (pr == target_pile) continue;
card::suit_t s(pr - foundations.front());
@@ -505,7 +504,7 @@ bool game_state::is_solved() const {
bool solved = true;
if (rules.hole) {
solved = piles[hole].size()
- == rules.max_rank * 4 * (rules.two_decks ? 2 : 1);
+ == rules.max_rank * 4 * (rules.two_decks ? 2 : 1);
} else {
assert(rules.foundations);
for (auto f : foundations) {
@@ -519,7 +518,7 @@ bool game_state::is_solved() const {
// is consistent
#ifndef NDEBUG
bool rest_empty = true;
- for (pile_ref pr = 0; pr < piles.size(); pr++) {
+ for (pile::ref pr = 0; pr < piles.size(); pr++) {
// All piles other than the hole must be empty
if (rules.hole) {
if (pr != hole && !piles[pr].empty()) {
@@ -556,3 +555,4 @@ const std::vector<pile>& game_state::get_data() const {
ostream& operator<< (ostream& str, const game_state& gs) {
return state_printer::print(str, gs);
}
+
diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h
new file mode 100644
index 0000000..c46f25b
--- /dev/null
+++ b/src/main/game/search-state/game_state.h
@@ -0,0 +1,119 @@
+//
+// Created by thecharlesblake on 10/24/17.
+//
+
+#ifndef SOLVITAIRE_GAME_STATE_H
+#define SOLVITAIRE_GAME_STATE_H
+
+#include <vector>
+#include <list>
+#include <string>
+
+#include <boost/functional/hash.hpp>
+#include <boost/optional/optional.hpp>
+
+#include "document.h"
+#include "../card.h"
+#include "../pile.h"
+#include "../sol_rules.h"
+#include "../move.h"
+
+class game_state {
+ friend class hasher;
+ friend class global_cache;
+ friend class cached_game_state;
+ friend class deal_parser;
+ friend class state_printer;
+public:
+ /* Constructors */
+
+ // Creates a game state representation from a JSON doc
+ game_state(const sol_rules&, const rapidjson::Document&);
+ // Does the same from a seed
+ game_state(const sol_rules&, int seed);
+ // Does the same but with an initialiser list (useful for testing)
+ game_state(const sol_rules&, std::initializer_list<pile>);
+
+ /* Altering state */
+
+ void make_move(move);
+ void undo_move(move);
+ void place_card(pile::ref, card);
+ card take_card(pile::ref);
+
+ /* Legal move generation */
+
+ std::vector<move> get_legal_moves(move = move(255, 255, 255));
+ boost::optional<move> get_dominance_move() const;
+
+ /* State inspection */
+
+ bool is_solved() const;
+ const std::vector<pile>& get_data() const;
+
+ /* Printing */
+
+ friend std::ostream& operator<< (std::ostream&, const game_state&);
+
+private:
+ /* Constructors (& helper function) */
+
+ explicit game_state(const sol_rules&);
+ static std::vector<card> gen_shuffled_deck(int, card::rank_t, bool);
+
+ /* Pile order logic */
+
+ void eval_pile_order(pile::ref, bool);
+ void eval_pile_order(std::list<pile::ref>&, pile::ref, bool);
+
+ /* Legal move generation */
+
+ move get_stock_tableau_move() const;
+ bool is_valid_tableau_move(pile::ref, pile::ref) const;
+ bool is_valid_foundations_move(pile::ref, pile::ref) const;
+ bool is_valid_hole_move(pile::ref) const;
+ void get_built_group_moves(std::vector<move>&) const;
+ pile::size_type get_built_group_height(pile::ref) const;
+ bool valid_built_group_move(card, card, card) const;
+ void add_built_group_move(std::vector<move>&, pile::ref, pile::ref) const;
+ void add_empty_built_group_moves(std::vector<move>&, pile::ref, pile::ref,
+ card) const;
+ bool dominance_blocks_foundation_move(pile::ref);
+ bool is_ordered_pile(pile::ref) const;
+
+ /* Auto-foundation moves */
+
+ void update_auto_foundation_moves(pile::ref);
+ bool is_valid_auto_foundation_move(pile::ref) const;
+
+
+ /* Game rules */
+
+ const sol_rules rules;
+
+ /* Pile references */
+
+ std::list<pile::ref> tableau_piles;
+ std::list<pile::ref> cells;
+ pile::ref stock;
+ pile::ref waste;
+ std::list<pile::ref> reserve;
+ std::vector<pile::ref> foundations;
+ pile::ref hole;
+
+ /* Pile references of starting/original layout */
+
+ std::vector<pile::ref> original_tableau_piles;
+ std::vector<pile::ref> original_cells;
+ std::vector<pile::ref> original_reserve;
+
+ /* Auto-foundation moves state */
+
+ std::vector<bool> auto_foundation_moves;
+
+ /* Core piles */
+
+ std::vector<pile> piles;
+};
+
+#endif //SOLVITAIRE_GAME_STATE_H
diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/search-state/game_state.legal_moves.cpp
index 5a49ec9..85b9382 100644
--- a/src/main/game/game_state.legal_moves.cpp
+++ b/src/main/game/search-state/game_state.legal_moves.cpp
@@ -1,32 +1,34 @@
//
-// Created by thecharlesblake on 1/3/18.
+// Created by thecharlesblake on 07/06/18.
//
-#include <boost/optional.hpp>
-
#include "game_state.h"
-using namespace std;
-using namespace boost;
+#include <boost/optional/optional.hpp>
+
+using std::vector;
+using std::min;
+using namespace rapidjson;
typedef sol_rules::build_policy pol;
typedef sol_rules::spaces_policy s_pol;
typedef sol_rules::stock_deal_type sdt;
-vector<game_state::move> game_state::get_legal_moves(move parent_move) {
- // The next legal moves
+
+vector<move> game_state::get_legal_moves(move parent_move) {
+// The next legal moves
vector<move> moves;
- // If we have a stock that can deal to the tableau piles, adds this move
+// If we have a stock that can deal to the tableau piles, adds this move
if (rules.stock_size > 0
&& rules.stock_deal_t == sdt::TABLEAU_PILES
&& !piles[stock].empty()) {
-
+
moves.emplace_back(get_stock_tableau_move());
}
-
- // If the stock is empty and we are allowed to re-deal, move the waste
- // back onto the stock
+
+// If the stock is empty and we are allowed to re-deal, move the waste
+// back onto the stock
if (rules.stock_size > 0
&& rules.stock_redeal
&& rules.stock_deal_t == sdt::WASTE
@@ -35,10 +37,10 @@ vector<game_state::move> game_state::get_legal_moves(move parent_move) {
moves.emplace_back(stock, waste);
}
- // Cycles through each pile which we may be able to remove a card from
- for (pile_ref rem_ref = 0; rem_ref < piles.size(); rem_ref++) {
- // Never removes a card from the hole, an empty pile, or the pile we
- // just moved a card to
+// Cycles through each pile which we may be able to remove a card from
+ for (pile::ref rem_ref = 0; rem_ref < piles.size(); rem_ref++) {
+// Never removes a card from the hole, an empty pile, or the pile we
+// just moved a card to
if ((rules.hole && rem_ref == hole)
|| piles[rem_ref].empty()
|| (rem_ref == parent_move.to
@@ -46,15 +48,15 @@ vector<game_state::move> game_state::get_legal_moves(move parent_move) {
&& parent_move.from != stock)
) continue;
- // If we have a foundations card as the rem_ref
+// If we have a foundations card as the rem_ref
if (rules.foundations
&& rem_ref >= foundations.front()
&& rem_ref <= foundations.back()) {
- // If the foundations are removable, checks to see if a dominance
- // prevents us from removing from it. Otherwise, skips the pile ref
+// If the foundations are removable, checks to see if a dominance
+// prevents us from removing from it. Otherwise, skips the pile ref
if (!rules.foundations_removable
-#ifndef NO_AUTO_FOUNDATIONS
+ #ifndef NO_AUTO_FOUNDATIONS
|| dominance_blocks_foundation_move(rem_ref)
#endif
) {
@@ -62,7 +64,7 @@ vector<game_state::move> game_state::get_legal_moves(move parent_move) {
}
}
- // Stock cards can only be moved to the waste (assuming there is a waste)
+// Stock cards can only be moved to the waste (assuming there is a waste)
if (rules.stock_size > 0 && rem_ref == stock) {
if (rules.stock_deal_t == sdt::WASTE) {
uint8_t cards_to_move =
@@ -72,48 +74,48 @@ vector<game_state::move> game_state::get_legal_moves(move parent_move) {
continue;
}
- // For each tableau pile, check if moving from the rem pile is a valid
- // tableau move according to the rules
+// For each tableau pile, check if moving from the rem pile is a valid
+// tableau move according to the rules
for (auto add_ref : tableau_piles) {
if (is_valid_tableau_move(rem_ref, add_ref)) {
moves.emplace_back(rem_ref, add_ref);
}
}
- // Any card can be moved to a cell, so long as the cell isn't full
+// Any card can be moved to a cell, so long as the cell isn't full
for (auto add_ref : cells) {
if (add_ref != rem_ref && piles[add_ref].empty()) {
moves.emplace_back(rem_ref, add_ref);
}
}
- // For each foundation pile, check if moving from the rem pile is a valid
- // move according to the rules
+// For each foundation pile, check if moving from the rem pile is a valid
+// move according to the rules
for (auto add_ref : foundations) {
if (is_valid_foundations_move(rem_ref, add_ref)) {
moves.emplace_back(rem_ref, add_ref);
}
}
- // Does the same for the hole
+// Does the same for the hole
if (rules.hole && is_valid_hole_move(rem_ref)) {
moves.emplace_back(rem_ref, hole);
}
- // Note that the stock and the reserve aren't mentioned here, as
- // they cannot be added to
+// Note that the stock and the reserve aren't mentioned here, as
+// they cannot be added to
}
if (rules.move_built_group) {
get_built_group_moves(moves);
}
- // If only complete built piles can be moved to the foundations,
+// If only complete built piles can be moved to the foundations,
if (rules.foundations_comp_piles) {
- for (pile_ref tab_pr : tableau_piles) {
+ for (pile::ref tab_pr : tableau_piles) {
if (is_ordered_pile(tab_pr)) {
- for (pile_ref found_pr : foundations) {
+ for (pile::ref found_pr : foundations) {
if (piles[found_pr].empty()) {
moves.emplace_back(tab_pr, found_pr, rules.max_rank);
break;
@@ -130,7 +132,7 @@ vector<game_state::move> game_state::get_legal_moves(move parent_move) {
// This is a special kind of move which the solver handles differently. Hence
// the arbitrary second param. We must supply the number of stock cards
// that we will deal, so that the move can be undone in backtracking
-game_state::move game_state::get_stock_tableau_move() const {
+move game_state::get_stock_tableau_move() const {
pile::size_type stock_moves =
piles[stock].size() >= tableau_piles.size()
? pile::size_type(tableau_piles.size())
@@ -143,8 +145,8 @@ game_state::move game_state::get_stock_tableau_move() const {
// If a tableau pile is empty and the space policy is
// no-build, do not add. Otherwise any card with the right
// suit/rank can be moved to a tableau
-bool game_state::is_valid_tableau_move(const pile_ref rem_ref,
- const pile_ref add_ref) const {
+bool game_state::is_valid_tableau_move(const pile::ref rem_ref,
+ const pile::ref add_ref) const {
if (rem_ref == add_ref) {
return false;
} else if (rules.build_pol == pol::NO_BUILD) {
@@ -183,8 +185,8 @@ bool game_state::is_valid_tableau_move(const pile_ref rem_ref,
}
}
-bool game_state::is_valid_foundations_move(const pile_ref rem_ref,
- const pile_ref add_ref) const {
+bool game_state::is_valid_foundations_move(const pile::ref rem_ref,
+ const pile::ref add_ref) const {
if (rules.foundations_comp_piles || rem_ref == add_ref) return false;
card rem_c = piles[rem_ref].top_card();
@@ -204,7 +206,7 @@ bool game_state::is_valid_foundations_move(const pile_ref rem_ref,
}
}
-bool game_state::is_valid_hole_move(const pile_ref rem_ref) const {
+bool game_state::is_valid_hole_move(const pile::ref rem_ref) const {
if (rem_ref == hole) return false;
card::rank_t rank = piles[rem_ref].top_card().get_rank();
card::rank_t hole_rank = piles[hole].top_card().get_rank();
@@ -240,8 +242,8 @@ void game_state::get_built_group_moves(vector<move>& moves) const {
// If the pile is empty, and we can move to empty piles, does so
if (piles[add_ref].empty()) {
if (rules.spaces_pol == s_pol::ANY ||
- (rules.spaces_pol == s_pol::KINGS
- && bg_high.get_rank() == 13)) {
+ (rules.spaces_pol == s_pol::KINGS
+ && bg_high.get_rank() == 13)) {
add_empty_built_group_moves(moves, rem_ref, add_ref, bg_high);
}
}
@@ -259,7 +261,7 @@ void game_state::get_built_group_moves(vector<move>& moves) const {
}
// Finds the size of the built group at the top of a pile
-pile::size_type game_state::get_built_group_height(pile_ref ref) const {
+pile::size_type game_state::get_built_group_height(pile::ref ref) const {
// The index of the currently examined card, the current card, previous card
// and whether we currently have a valid built group.
@@ -318,21 +320,21 @@ const {
}
}
-void game_state::add_built_group_move(vector<move>& moves, pile_ref rem_ref,
- pile_ref add_ref) const {
+void game_state::add_built_group_move(vector<move>& moves, pile::ref rem_ref,
+ pile::ref add_ref) const {
moves.emplace_back(rem_ref, add_ref, 2);
}
void game_state::add_empty_built_group_moves(vector<move>& moves,
- pile_ref rem_ref,
- pile_ref add_ref,
+ pile::ref rem_ref,
+ pile::ref add_ref,
card bg_high) const {
// Loops through each possible built group move to an empty pile and adds it
// to the list
pile::size_type card_idx = 1;
do {
moves.emplace_back(
- rem_ref, add_ref, static_cast<pile_ref>(card_idx + 1)
+ rem_ref, add_ref, static_cast<pile::ref>(card_idx + 1)
);
} while (piles[rem_ref][card_idx++] != bg_high);
}
@@ -342,15 +344,15 @@ void game_state::add_empty_built_group_moves(vector<move>& moves,
/////////////////////
// Returns a dominance move if one is available
-optional<game_state::move> game_state::get_dominance_move() const {
+boost::optional<move> game_state::get_dominance_move() const {
#ifndef NO_AUTO_FOUNDATIONS
// If there are 2 decks or no foundations, return
if (!rules.foundations || rules.two_decks)
- return none;
+ return boost::none;
// Cycles through the piles and sees if any cards can be automatically moved
// to the foundations
- for (pile_ref pr = 0; pr < piles.size(); pr++) {
+ for (pile::ref pr = 0; pr < piles.size(); pr++) {
// Don't move foundation cards, hole or stock cards to the foundations
if ((pr >= foundations.front() && pr <= foundations.back())
|| (rules.hole && pr == hole)
@@ -360,7 +362,7 @@ optional<game_state::move> game_state::get_dominance_move() const {
}
card c = piles[pr].top_card();
- pile_ref target_foundation = foundations[c.get_suit()];
+ pile::ref target_foundation = foundations[c.get_suit()];
// If the card is the right rank and the auto-move boolean is true, then
// returns the move
@@ -375,10 +377,10 @@ optional<game_state::move> game_state::get_dominance_move() const {
}
#endif
- return none;
+ return boost::none;
}
-bool game_state::is_ordered_pile(pile_ref pr) const {
+bool game_state::is_ordered_pile(pile::ref pr) const {
if (piles[pr].size() != rules.max_rank) return false;
card::suit_t s = piles[pr][0].get_suit();
@@ -391,7 +393,7 @@ bool game_state::is_ordered_pile(pile_ref pr) const {
// For games where the foundations can be removed from, this dominance blocks
// the foundations being removed from if the card that were to be removed would
// go 'automatically' up
-bool game_state::dominance_blocks_foundation_move(pile_ref target_pile) {
+bool game_state::dominance_blocks_foundation_move(pile::ref target_pile) {
assert(!piles[target_pile].empty());
card target_card = piles[target_pile].top_card();
@@ -402,9 +404,3 @@ bool game_state::dominance_blocks_foundation_move(pile_ref target_pile) {
return blocked;
}
-
-bool operator==(const game_state::move& a, const game_state::move& b) {
- return (a.from == b.from )
- && (a.to == b.to )
- && (a.count == b.count);
-}
diff --git a/src/main/input-output/input/json-parsing/deal_parser.cpp b/src/main/input-output/input/json-parsing/deal_parser.cpp
index f841c8d..29ac3b1 100644
--- a/src/main/input-output/input/json-parsing/deal_parser.cpp
+++ b/src/main/input-output/input/json-parsing/deal_parser.cpp
@@ -3,7 +3,7 @@
//
#include "deal_parser.h"
-#include "../../../game/game_state.h"
+#include "../../../game/search-state/game_state.h"
#include "json_helper.h"
using namespace std;
@@ -166,9 +166,9 @@ void deal_parser::parse_reserve(game_state &gs, const Document& doc) {
json_helper::json_parse_err("Incorrect reserve size");
}
- for (game_state::pile_ref i = 0; i < json_card_arr.Size(); i++) {
+ for (pile::ref i = 0; i < json_card_arr.Size(); i++) {
assert(json_card_arr[i].IsString());
- game_state::pile_ref pr = gs.original_reserve[0];
+ pile::ref pr = gs.original_reserve[0];
if (!gs.rules.reserve_stacked) pr += i;
gs.place_card(pr, card(json_card_arr[i].GetString()));
}
diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp
index 0fdacae..9d13129 100644
--- a/src/main/input-output/output/state_printer.cpp
+++ b/src/main/input-output/output/state_printer.cpp
@@ -55,7 +55,7 @@ void state_printer::print_header(ostream& stream, const char* header) {
}
void state_printer::print_piles(ostream& stream,
- const vector<game_state::pile_ref>& pile_rs,
+ const vector<pile::ref>& pile_rs,
const game_state& gs) {
bool empty_row = false;
pile::size_type row_idx = 0;
@@ -65,7 +65,7 @@ void state_printer::print_piles(ostream& stream,
while (!empty_row) {
// Loops through the current row to determine if it is empty
empty_row = true;
- for (const game_state::pile_ref pile_r : pile_rs) {
+ for (const pile::ref pile_r : pile_rs) {
if (gs.piles[pile_r].size() > row_idx) {
empty_row = false;
break;
@@ -73,7 +73,7 @@ void state_printer::print_piles(ostream& stream,
}
if (!empty_row || row_idx == 0) {
// Loops through the current (non-empty) row, and outputs the values
- for (const game_state::pile_ref pile_r : pile_rs) {
+ for (const pile::ref pile_r : pile_rs) {
const pile& p = gs.piles[pile_r];
if (p.size() > row_idx) {
print_card(stream, p[p.size() - 1 - row_idx]);
@@ -90,11 +90,11 @@ void state_printer::print_piles(ostream& stream,
}
void state_printer::print_top_of_piles(ostream& stream,
- const vector<game_state::pile_ref>& vp,
+ const vector<pile::ref>& vp,
const game_state& gs) {
- vector<game_state::pile_ref> top(vp);
+ vector<pile::ref> top(vp);
- for (game_state::pile_ref p : top) {
+ for (pile::ref p : top) {
if (gs.piles[p].empty()) {
stream << "[]";
} else {
@@ -106,7 +106,7 @@ void state_printer::print_top_of_piles(ostream& stream,
}
void state_printer::print_top_of_pile(ostream& stream,
- const game_state::pile_ref pile_r,
+ const pile::ref pile_r,
const game_state& gs) {
print_top_of_piles(stream, {pile_r}, gs);
}
diff --git a/src/main/input-output/output/state_printer.h b/src/main/input-output/output/state_printer.h
index ae63182..2b2090b 100644
--- a/src/main/input-output/output/state_printer.h
+++ b/src/main/input-output/output/state_printer.h
@@ -7,7 +7,7 @@
#include <ostream>
-#include "../../game/game_state.h"
+#include "../../game/search-state/game_state.h"
class state_printer {
public:
@@ -16,12 +16,12 @@ public:
private:
static void print_header(std::ostream&, const char*);
static void print_piles(std::ostream&,
- const std::vector<game_state::pile_ref>&,
+ const std::vector<pile::ref>&,
const game_state&);
static void print_top_of_piles(std::ostream&,
- const std::vector<game_state::pile_ref>&,
+ const std::vector<pile::ref>&,
const game_state&);
- static void print_top_of_pile(std::ostream&, game_state::pile_ref,
+ static void print_top_of_pile(std::ostream&, pile::ref,
const game_state&);
static void print_card(std::ostream&, card);
};
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
index 51cb574..b104da3 100644
--- a/src/main/solver/solver.cpp
+++ b/src/main/solver/solver.cpp
@@ -9,10 +9,13 @@
#include <list>
#include "solver.h"
+#include "../game/move.h"
#include "../input-output/output/log_helper.h"
-using namespace std;
-using namespace boost;
+using std::atomic;
+using std::vector;
+using std::cout;
+using std::clog;
solver::solver(const game_state& gs)
: cache(gs)
@@ -23,7 +26,7 @@ solver::solver(const game_state& gs)
, backtracks(0)
, dominance_moves(0)
, depth(0)
- , root(nullptr, game_state::move(255, 255, 255))
+ , root(nullptr, move(255, 255, 255))
, current_node(&root) {
}
@@ -36,11 +39,11 @@ solver::~solver() {
}
}
-solver::node::node(node* p, const game_state::move m)
- : parent(p), move(m), children() {
+solver::node::node(node* p, const move m)
+ : parent(p), mv(m), children() {
}
-solver::sol_state solver::run(optional<atomic<bool> &> terminate_solver) {
+solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver) {
bool states_exhausted = false;
while(!(state.is_solved() || states_exhausted)) {
@@ -50,7 +53,7 @@ solver::sol_state solver::run(optional<atomic<bool> &> terminate_solver) {
}
#ifndef NDEBUG
- if (current_node->move.is_dominance()) {
+ if (current_node->mv.is_dominance()) {
LOG_DEBUG("(dominance move)");
}
LOG_DEBUG(state);
@@ -58,7 +61,7 @@ solver::sol_state solver::run(optional<atomic<bool> &> terminate_solver) {
// If there is a dominance move available, adds it to the search tree
// and repeats. Doesn't cache the state.
- optional<game_state::move> dominance_move = state.get_dominance_move();
+ boost::optional<move> dominance_move = state.get_dominance_move();
if (dominance_move) {
// Adds the dominance move as a child of the current search node
add_child(*dominance_move);
@@ -67,7 +70,7 @@ solver::sol_state solver::run(optional<atomic<bool> &> terminate_solver) {
bool is_new_state = cache.insert(state);
if (is_new_state) {
// Gets the legal moves in the current state
- vector<game_state::move> next_moves = get_next_moves();
+ vector<move> next_moves = get_next_moves();
// If there are none, reverts to the last node with children
if (next_moves.empty()) {
@@ -87,9 +90,9 @@ solver::sol_state solver::run(optional<atomic<bool> &> terminate_solver) {
assert(states_exhausted == current_node->children.empty());
if (!states_exhausted) {
current_node = &current_node->children.back();
- state.make_move(current_node->move);
+ state.make_move(current_node->mv);
depth++;
- if (current_node->move.is_dominance()) dominance_moves++;
+ if (current_node->mv.is_dominance()) dominance_moves++;
}
states_searched++;
@@ -104,19 +107,19 @@ solver::sol_state solver::run(optional<atomic<bool> &> terminate_solver) {
}
}
-void solver::add_children(std::vector<game_state::move>& moves) {
+void solver::add_children(std::vector<move>& moves) {
current_node->children.reserve(moves.size());
for (auto move : moves) {
add_child(move);
}
}
-vector<game_state::move> solver::get_next_moves() {
- return state.get_legal_moves(current_node->move);
+vector<move> solver::get_next_moves() {
+ return state.get_legal_moves(current_node->mv);
}
-void solver::add_child(game_state::move move) {
- current_node->children.emplace_back(current_node, move);
+void solver::add_child(move m) {
+ current_node->children.emplace_back(current_node, m);
}
// Called when the current node's children have been exhausted. Travels back up
@@ -126,7 +129,7 @@ bool solver::revert_to_last_node_with_children() {
if (!current_node->parent)
return true;
- state.undo_move(current_node->move);
+ state.undo_move(current_node->mv);
depth--;
backtracks++;
@@ -134,7 +137,7 @@ bool solver::revert_to_last_node_with_children() {
// Checks that the state after the undo is in the cache
// (as long as the move wasn't a dominance move)
- if (!current_node->move.is_dominance()) {
+ if (!current_node->mv.is_dominance()) {
assert(cache.contains(state));
LOG_DEBUG("(undo move)");
} else {
@@ -167,7 +170,7 @@ void solver::print_solution() const {
if (states_searched > 1) {
while (!n->children.empty()) {
- state_copy.make_move(n->move);
+ state_copy.make_move(n->mv);
cout << state_copy << "\n";
n = &n->children.back();
}
diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h
index 77e8bde..21b8d6e 100644
--- a/src/main/solver/solver.h
+++ b/src/main/solver/solver.h
@@ -18,9 +18,9 @@ public:
global_cache cache;
struct node {
- node(node*, game_state::move);
+ node(node*, move);
node* parent;
- const game_state::move move;
+ const move mv;
std::vector<node> children;
};
@@ -36,9 +36,9 @@ public:
private:
bool revert_to_last_node_with_children();
- void add_children(std::vector<game_state::move>&);
- void add_child(game_state::move);
- std::vector<game_state::move> get_next_moves();
+ void add_children(std::vector<move>&);
+ void add_child(move);
+ std::vector<move> get_next_moves();
const game_state init_state;
game_state state;
diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp
index 9d928c6..6f2a68d 100644
--- a/src/test/test_helper.cpp
+++ b/src/test/test_helper.cpp
@@ -5,7 +5,7 @@
#include <gtest/gtest.h>
#include "test_helper.h"
-#include "../main/game/game_state.h"
+#include "../main/game/search-state/game_state.h"
#include "../main/solver/solver.h"
#include "../main/input-output/input/json-parsing/json_helper.h"
#include "../main/input-output/input/json-parsing/rules_parser.h"
@@ -50,10 +50,10 @@ void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy,
solver::node const *n = &sol.get_search_tree();
for (card c : cards) {
n = &n->children.back();
- ASSERT_TRUE(n->move.is_dominance());
- ASSERT_TRUE(gs.get_data()[n->move.from].top_card() == c);
- ASSERT_TRUE(n->move.to >= 0 && n->move.to <= 4);
- gs.make_move(n->move);
+ ASSERT_TRUE(n->mv.is_dominance());
+ ASSERT_TRUE(gs.get_data()[n->mv.from].top_card() == c);
+ ASSERT_TRUE(n->mv.to >= 0 && n->mv.to <= 4);
+ gs.make_move(n->mv);
}
ASSERT_TRUE(n->children.empty());
}
diff --git a/src/test/unit_tests/foundations_dominance_test.cpp b/src/test/unit_tests/foundations_dominance_test.cpp
index 4a7d2aa..8fdc285 100644
--- a/src/test/unit_tests/foundations_dominance_test.cpp
+++ b/src/test/unit_tests/foundations_dominance_test.cpp
@@ -5,7 +5,7 @@
#include <gtest/gtest.h>
#include "../test_helper.h"
-#include "../../main/game/game_state.h"
+#include "../../main/game/search-state/game_state.h"
#include "../../main/game/global_cache.h"
typedef sol_rules::build_policy pol;
diff --git a/src/test/unit_tests/global_cache_test.cpp b/src/test/unit_tests/global_cache_test.cpp
index 97490ba..6b0accd 100644
--- a/src/test/unit_tests/global_cache_test.cpp
+++ b/src/test/unit_tests/global_cache_test.cpp
@@ -5,7 +5,7 @@
#include <gtest/gtest.h>
#include "../test_helper.h"
-#include "../../main/game/game_state.h"
+#include "../../main/game/search-state/game_state.h"
#include "../../main/game/global_cache.h"
typedef sol_rules::build_policy pol;
diff --git a/src/test/unit_tests/legal_move_gen_test.cpp b/src/test/unit_tests/legal_move_gen_test.cpp
index 4f4b7ef..84755f6 100644
--- a/src/test/unit_tests/legal_move_gen_test.cpp
+++ b/src/test/unit_tests/legal_move_gen_test.cpp
@@ -5,19 +5,20 @@
#include <gtest/gtest.h>
#include "../test_helper.h"
-#include "../../main/game/game_state.h"
+#include "../../main/game/search-state/game_state.h"
#include "../../main/game/sol_rules.h"
typedef sol_rules::build_policy pol;
typedef sol_rules::spaces_policy s_pol;
typedef sol_rules::stock_deal_type sdt;
-typedef game_state::move mv;
-using namespace std;
+using std::vector;
+using std::ostream;
+using std::initializer_list;
-bool moves_eq(vector<mv>&, vector<mv>&);
-ostream& operator <<(ostream&, const mv&);
-ostream& operator <<(ostream&, const vector<mv>&);
+bool moves_eq(vector<move>&, vector<move>&);
+ostream& operator <<(ostream&, const move&);
+ostream& operator <<(ostream&, const vector<move>&);
TEST(LegalMoveGen, BuildPolAnySuit) {
sol_rules sr;
@@ -31,13 +32,13 @@ TEST(LegalMoveGen, BuildPolAnySuit) {
{"2S"},
{"AC"}
});
- vector<mv> actual_moves = gs.get_legal_moves();
+ vector<move> actual_moves = gs.get_legal_moves();
- vector<mv> exp_moves = {
- mv(4,0),
- mv(4,1),
- mv(4,2),
- mv(4,3)
+ vector<move> exp_moves = {
+ move(4,0),
+ move(4,1),
+ move(4,2),
+ move(4,3)
};
ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves;
@@ -55,11 +56,11 @@ TEST(LegalMoveGen, BuildPolRedBlack) {
{"2D"},
{"AC"}
});
- vector<mv> actual_moves = gs.get_legal_moves();
+ vector<move> actual_moves = gs.get_legal_moves();
- vector<mv> exp_moves = {
- mv(4,1),
- mv(4,3)
+ vector<move> exp_moves = {
+ move(4,1),
+ move(4,3)
};
ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves;
@@ -77,10 +78,10 @@ TEST(LegalMoveGen, BuildPolSameSuit) {
{"2D"},
{"AC"}
});
- vector<mv> actual_moves = gs.get_legal_moves();
+ vector<move> actual_moves = gs.get_legal_moves();
- vector<mv> exp_moves = {
- mv(4,0)
+ vector<move> exp_moves = {
+ move(4,0)
};
ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves;
@@ -98,9 +99,9 @@ TEST(LegalMoveGen, BuildPolNoBuild) {
{"2D"},
{"AC"}
});
- vector<mv> actual_moves = gs.get_legal_moves();
+ vector<move> actual_moves = gs.get_legal_moves();
- vector<mv> exp_moves = {
+ vector<move> exp_moves = {
};
ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves;
@@ -115,10 +116,10 @@ TEST(LegalMoveGen, SpacesPolAny) {
{},
{"AC"}
});
- vector<mv> actual_moves = gs.get_legal_moves();
+ vector<move> actual_moves = gs.get_legal_moves();
- vector<mv> exp_moves = {
- mv(1, 0)
+ vector<move> exp_moves = {
+ move(1, 0)
};
ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves;
@@ -134,10 +135,10 @@ TEST(LegalMoveGen, SpacesPolKings) {
{"AC"},
{"KD"}
});
- vector<mv> actual_moves = gs.get_legal_moves();
+ vector<move> actual_moves = gs.get_legal_moves();
- vector<mv> exp_moves = {
- mv(2, 0)
+ vector<move> exp_moves = {
+ move(2, 0)
};
ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves;
@@ -152,9 +153,9 @@ TEST(LegalMoveGen, SpacesPolNoBuild) {
{},
{"AC"}
});
- vector<mv> actual_moves = gs.get_legal_moves();
+ vector<move> actual_moves = gs.get_legal_moves();
- vector<mv> exp_moves = {
+ vector<move> exp_moves = {
};
ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves;
@@ -169,11 +170,11 @@ TEST(LegalMoveGen, MoveBuiltGroupTrue) {
{},
{"2C", "AC"}
});
- vector<mv> actual_moves = gs.get_legal_moves();
+ vector<move> actual_moves = gs.get_legal_moves();
- vector<mv> exp_moves = {
- mv(1, 0, 1),
- mv(1, 0, 2)
+ vector<move> exp_moves = {
+ move(1, 0, 1),
+ move(1, 0, 2)
};
ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves;
@@ -188,10 +189,10 @@ TEST(LegalMoveGen, MoveBuiltGroupFalse) {
{},
{"AC", "2C"}
});
- vector<mv> actual_moves = gs.get_legal_moves();
+ vector<move> actual_moves = gs.get_legal_moves();
- vector<mv> exp_moves = {
- mv(1, 0, 1)
+ vector<move> exp_moves = {
+ move(1, 0, 1)
};
ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves;
@@ -209,13 +210,13 @@ TEST(LegalMoveGen, Foundations) {
{"AS"},
{"AD"},
});
- vector<mv> actual_moves = gs.get_legal_moves();
+ vector<move> actual_moves = gs.get_legal_moves();
- vector<mv> exp_moves = {
- mv(4, 0),
- mv(5, 1),
- mv(6, 2),
- mv(7, 3)
+ vector<move> exp_moves = {
+ move(4, 0),
+ move(5, 1),
+ move(6, 2),
+ move(7, 3)
};
ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves;
@@ -234,10 +235,10 @@ TEST(LegalMoveGen, FoundationsRemovable) {
{},
{}
});
- vector<mv> actual_moves = gs.get_legal_moves();
+ vector<move> actual_moves = gs.get_legal_moves();
- vector<mv> exp_moves = {
- mv(0, 4)
+ vector<move> exp_moves = {
+ move(0, 4)
};
ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves;
@@ -252,12 +253,12 @@ TEST(LegalMoveGen, Cells) {
{"3D"}, {}, // The cells
{"4H"}
});
- vector<mv> actual_moves = gs.get_legal_moves();
+ vector<move> actual_moves = gs.get_legal_moves();
- vector<mv> exp_moves = {
- mv(0, 1),
- mv(0, 2),
- mv(2, 1)
+ vector<move> exp_moves = {
+ move(0, 1),
+ move(0, 2),
+ move(2, 1)
};
ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves;
@@ -276,10 +277,10 @@ TEST(LegalMoveGen, StockDealToWaste) {
{}, // The waste
{}, {}, {} // The tableau piles
});
- vector<mv> actual_moves = gs.get_legal_moves();
+ vector<move> actual_moves = gs.get_legal_moves();
- vector<mv> exp_moves = {
- mv(4, 5)
+ vector<move> exp_moves = {
+ move(4, 5)
};
ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves;
@@ -297,10 +298,10 @@ TEST(LegalMoveGen, StockDealToTableau) {
{"AC", "AD"}, // The stock
{"3H"}, {"5D"}, {"7C"} // The tableau piles
});
- vector<mv> actual_moves = gs.get_legal_moves();
+ vector<move> actual_moves = gs.get_legal_moves();
- vector<mv> exp_moves = {
- mv(4, 255, 2) // Special stock-to-tableau syntax
+ vector<move> exp_moves = {
+ move(4, 255, 2) // Special stock-to-tableau syntax
};
ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves;
@@ -308,10 +309,10 @@ TEST(LegalMoveGen, StockDealToTableau) {
-bool moves_eq(vector<mv>& exp_moves, vector<mv>& actual_moves) {
+bool moves_eq(vector<move>& exp_moves, vector<move>& actual_moves) {
if (exp_moves.size() != actual_moves.size()) return false;
- for (mv m : actual_moves) {
+ for (move m : actual_moves) {
auto it = find(begin(exp_moves), end(exp_moves), m);
bool found = it != end(exp_moves);
@@ -322,15 +323,15 @@ bool moves_eq(vector<mv>& exp_moves, vector<mv>& actual_moves) {
return true;
}
-ostream& operator <<(ostream& os, const mv& m) {
+ostream& operator <<(ostream& os, const move& m) {
return os << "move:(" << int(m.from)
<< "," << int(m.to)
<< "," << int(m.count)
<< ")";
}
-ostream& operator <<(ostream& os, const vector<mv>& moves) {
- for (const mv m : moves) {
+ostream& operator <<(ostream& os, const vector<move>& moves) {
+ for (const move m : moves) {
os << m << ",\n";
}
return os;