aboutsummaryrefslogtreecommitdiff
path: root/src/main/game
diff options
context:
space:
mode:
authorCharlie Blake2018-06-08 10:31:22 +0100
committerCharlie Blake2018-06-08 10:31:22 +0100
commit819a1064c5142a9b2f04ff8149d34fc915c18322 (patch)
tree6d0c8763ad79df10e6e6bcd30a0a974326e54e3a /src/main/game
parentRemove reduced state build and logic (diff)
downloadmetasol-819a1064c5142a9b2f04ff8149d34fc915c18322.tar.gz
metasol-819a1064c5142a9b2f04ff8149d34fc915c18322.zip

Refactor legal moves gen and moves logic

Diffstat (limited to 'src/main/game')
-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
9 files changed, 273 insertions, 226 deletions
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);
-}