aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorCharlie Blake2018-02-15 12:48:35 +0000
committerGitHub2018-02-15 12:48:35 +0000
commit9e865ef56ff2b90695343e2e5b7be6e05b2c56f4 (patch)
tree7cd620ee74084d74d849b44fae2db3897763cadb /src/main
parentMerge pull request #37 from thecharlesblake/docs (diff)
parentAdd simple auto-found + works w/ removable founds (diff)
downloadmetasol-9e865ef56ff2b90695343e2e5b7be6e05b2c56f4.tar.gz
metasol-9e865ef56ff2b90695343e2e5b7be6e05b2c56f4.zip

Merge pull request #38 from thecharlesblake/foundation-dominance

Foundation dominance

Diffstat (limited to 'src/main')
-rw-r--r--src/main/game/card.cpp24
-rw-r--r--src/main/game/card.h18
-rw-r--r--src/main/game/game_state.cpp80
-rw-r--r--src/main/game/game_state.h14
-rw-r--r--src/main/game/game_state.legal_moves.cpp79
-rw-r--r--src/main/game/pile.cpp2
-rw-r--r--src/main/game/pile.h1
-rw-r--r--src/main/game/sol_rules.h1
-rw-r--r--src/main/input-output/input/json-parsing/rules_parser.cpp8
-rw-r--r--src/main/input-output/input/sol_preset_types.cpp25
-rw-r--r--src/main/main.cpp6
-rw-r--r--src/main/solver/solver.cpp173
-rw-r--r--src/main/solver/solver.h19
13 files changed, 350 insertions, 100 deletions
diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp
index 271bf73..a19760b 100644
--- a/src/main/game/card.cpp
+++ b/src/main/game/card.cpp
@@ -11,12 +11,20 @@
using namespace std;
using namespace boost;
+const card::suit_t card::suit::Clubs = 0;
+const card::suit_t card::suit::Hearts = 1;
+const card::suit_t card::suit::Spades = 2;
+const card::suit_t card::suit::Diamonds = 3;
+
+const card::colour_t card::colour::Black = 0;
+const card::colour_t card::colour::Red = 1;
+
// A card can either be created using two int values (rank & suit), or
// using a string (e.g. "AS")
-card::card(suit_t s, rank_t r) : suit(s), rank(r) {}
+card::card(suit_t s, rank_t r) : card_suit(s), card_rank(r) {}
-card::card(const char* c) : suit(suit_from_str(c)), rank(rank_from_str(c)) {}
+card::card(const char* c) : card_suit(suit_from_str(c)), card_rank(rank_from_str(c)) {}
card::card() : card(0, 0) {}
@@ -44,11 +52,11 @@ card::rank_t card::rank_from_str(const char* c) {
}
card::suit_t card::get_suit() const {
- return suit;
+ return card_suit;
}
card::colour_t card::get_colour() const {
- switch (suit) {
+ switch (card_suit) {
case card::suit::Spades :
case card::suit::Clubs : return card::colour::Black;
default : return card::colour::Red;
@@ -56,19 +64,19 @@ card::colour_t card::get_colour() const {
}
card::rank_t card::get_rank() const {
- return rank;
+ return card_rank;
}
std::string card::to_string() const {
string s;
- switch(rank) {
+ switch(card_rank) {
case 1 : s += "A"; break;
case 11: s += "J"; break;
case 12: s += "Q"; break;
case 13: s += "K"; break;
- default: s += std::to_string(rank);
+ default: s += std::to_string(card_rank);
}
- switch(suit) {
+ switch(card_suit) {
case card::suit::Spades : s += "S"; break;
case card::suit::Hearts : s += "H"; break;
case card::suit::Clubs : s += "C"; break;
diff --git a/src/main/game/card.h b/src/main/game/card.h
index 74f513d..8d88df8 100644
--- a/src/main/game/card.h
+++ b/src/main/game/card.h
@@ -16,15 +16,15 @@ public:
typedef uint8_t colour_t;
struct suit {
- static const suit_t Clubs = 0;
- static const suit_t Hearts = 1;
- static const suit_t Spades = 2;
- static const suit_t Diamonds = 3;
+ static const suit_t Clubs;
+ static const suit_t Hearts;
+ static const suit_t Spades;
+ static const suit_t Diamonds;
};
struct colour {
- static const colour_t Black = 0;
- static const colour_t Red = 1;
+ static const colour_t Black;
+ static const colour_t Red;
};
const static card divider;
@@ -32,7 +32,7 @@ public:
// A card can either be created using two int values (rank & suit), or
// using a string (e.g. "AS")
card(suit_t, rank_t);
- explicit card(const char*);
+ card(const char*);
card();
suit_t get_suit() const;
@@ -45,8 +45,8 @@ private:
static suit_t suit_from_str(const char* c);
static rank_t rank_from_str(const char* c);
- suit_t suit : 4;
- rank_t rank : 4;
+ suit_t card_suit : 4;
+ rank_t card_rank : 4;
};
bool operator==(const card&, const card&);
diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp
index a6186ff..322bfcc 100644
--- a/src/main/game/game_state.cpp
+++ b/src/main/game/game_state.cpp
@@ -14,6 +14,7 @@
#include "game_state.h"
#include "../input-output/input/json-parsing/deal_parser.h"
#include "../input-output/output/state_printer.h"
+#include "../input-output/output/log_helper.h"
using namespace std;
using namespace rapidjson;
@@ -28,7 +29,10 @@ typedef sol_rules::stock_deal_type sdt;
// A private constructor used by both of the public ones. Initializes all of the
// piles and pile refs specified by the rules
-game_state::game_state(const sol_rules& s_rules) : rules(s_rules) {
+game_state::game_state(const sol_rules& s_rules) : rules(s_rules),
+ stock(255),
+ waste(255),
+ hole(255) {
// If there is a hole, creates pile
if (rules.hole) {
piles.emplace_back();
@@ -40,6 +44,7 @@ game_state::game_state(const sol_rules& s_rules) : rules(s_rules) {
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));
+ auto_foundation_moves.push_back(true);
}
}
@@ -205,11 +210,18 @@ 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 //
@@ -222,15 +234,20 @@ void game_state::make_move(const move m) {
// Handles special stock-to-tableau-piles move
if (rules.stock_deal_t == sdt::TABLEAU_PILES && m.from == stock) {
for (pile_ref tab_pr = original_tableau_piles.front();
- tab_pr < original_tableau_piles.front() + m.count;
+ tab_pr < pile_ref(original_tableau_piles.front() + m.count);
tab_pr++) {
place_card(tab_pr, take_card(stock));
}
}
// If this is not a built-pile move
- else if (m.count == 1) {
+ else if (m.count == 1 || m.is_dominance()) {
place_card(m.to, take_card(m.from));
- } else {
+#ifndef NO_AUTO_FOUNDATIONS
+ update_auto_foundation_moves(m.to);
+#endif
+ }
+ // If this is a built-pile move
+ else {
assert(m.count <= rules.max_rank);
// Adds the cards to the 'to' pile
for (auto pile_idx = m.count; pile_idx-- > 0;) {
@@ -257,8 +274,11 @@ void game_state::undo_move(const move m) {
}
}
// If this is not a built-pile move
- else if (m.count == 1) {
+ else if (m.count == 1 || m.is_dominance()) {
place_card(m.from, take_card(m.to));
+#ifndef NO_AUTO_FOUNDATIONS
+ update_auto_foundation_moves(m.from);
+#endif
} else {
assert(m.count <= rules.max_rank);
// Adds the cards to the 'from' pile
@@ -361,6 +381,52 @@ void game_state::eval_pile_order(list<pile_ref>& pile_lst, pile_ref changed_pr,
#endif
}
+// Decides which 'auto foundation' booleans should be 'true' currently
+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()
+ || target_pile > foundations.back()) {
+ return;
+ }
+
+ // Turns the 'auto' boolean for the target foundation false
+
+ 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 {
+ if (rules.build_pol == pol::ANY_SUIT || rules.build_pol == pol::RED_BLACK) {
+ card::suit_t target_suit(target_pile - foundations.front());
+ card::rank_t target_rank = piles[target_pile].empty() ? card::rank_t(0)
+ : piles[target_pile].top_card().get_rank();
+ card target_card = card(target_suit, target_rank);
+
+ for (pile_ref pr : foundations) {
+ if (pr == target_pile) continue;
+
+ card::suit_t foundation_suit(pr - foundations.front());
+ card::rank_t foundation_rank = piles[pr].empty() ? card::rank_t(0)
+ : piles[pr].top_card().get_rank();
+ card foundation_card = card(foundation_suit, foundation_rank);
+ int rank_diff = int(target_rank)
+ - int(foundation_card.get_rank());
+
+ if (rules.build_pol == pol::RED_BLACK
+ && foundation_card.get_colour() == target_card.get_colour()) {
+ if (rank_diff >= 3) {
+ return false;
+ }
+ } else if (rank_diff >= 2) {
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
////////////////////////
// INSPECT GAME STATE //
@@ -408,6 +474,10 @@ bool game_state::is_solved() const {
return solved;
}
+const std::vector<pile>& game_state::get_data() const {
+ return piles;
+}
+
///////////
// PRINT //
diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h
index 9b0d7b5..b403bc7 100644
--- a/src/main/game/game_state.h
+++ b/src/main/game/game_state.h
@@ -11,6 +11,7 @@
#include <rapidjson/document.h>
#include <boost/functional/hash.hpp>
+#include <boost/optional/optional.hpp>
#include "card.h"
#include "pile.h"
@@ -28,6 +29,9 @@ public:
typedef uint8_t pile_ref;
struct move {
move(pile_ref, pile_ref, pile::size_type = 1);
+ bool is_dominance() const;
+
+ const static pile::size_type dominance_flag;
pile_ref from; pile_ref to; pile::size_type count;
};
@@ -45,10 +49,12 @@ public:
void place_card(pile_ref, card);
card take_card(pile_ref);
- std::vector<move> get_legal_moves() const;
+ std::vector<move> get_legal_moves();
// 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&);
@@ -73,6 +79,10 @@ private:
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);
+
+ void update_auto_foundation_moves(pile_ref);
+ bool is_valid_auto_foundation_move(pile_ref) const;
// References to piles
const sol_rules rules;
@@ -90,6 +100,8 @@ private:
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;
};
diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp
index 3043587..c1a87e4 100644
--- a/src/main/game/game_state.legal_moves.cpp
+++ b/src/main/game/game_state.legal_moves.cpp
@@ -2,15 +2,18 @@
// Created by thecharlesblake on 1/3/18.
//
+#include <boost/optional.hpp>
+
#include "game_state.h"
using namespace std;
+using namespace boost;
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() const {
+vector<game_state::move> game_state::get_legal_moves() {
// The next legal moves
vector<move> moves;
@@ -22,7 +25,23 @@ vector<game_state::move> game_state::get_legal_moves() const {
// 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, the waste, or an empty pile
- if (rem_ref == hole || piles[rem_ref].empty()) continue;
+ if ((rules.hole && rem_ref == hole) || piles[rem_ref].empty()) continue;
+
+ // 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 (!rules.foundations_removable
+#ifndef NO_AUTO_FOUNDATIONS
+ || dominance_blocks_foundation_move(rem_ref)
+#endif
+ ) {
+ continue;
+ }
+ }
// Stock cards can only be moved to the waste (assuming there is a waste)
if (rules.stock_size > 0 && rem_ref == stock) {
@@ -271,3 +290,59 @@ void game_state::add_empty_built_group_moves(vector<move>& moves,
);
} while (piles[rem_ref][card_idx++] != bg_high);
}
+
+/////////////////////
+// Dominance Moves //
+/////////////////////
+
+// Returns a dominance move if one is available
+optional<game_state::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;
+
+ // 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++) {
+ // Don't move foundation cards, hole or stock cards to the foundations
+ if ((pr >= foundations.front() && pr <= foundations.back())
+ || (rules.hole && pr == hole)
+ || (rules.stock_size > 0 && pr == stock)
+ || (piles[pr].empty())) {
+ continue;
+ }
+
+ card c = piles[pr].top_card();
+ 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
+ card::rank_t target_rank = piles[target_foundation].empty()
+ ? card::rank_t(1) :
+ piles[target_foundation].top_card().get_rank()
+ + card::rank_t(1);
+ if (target_rank == c.get_rank() &&
+ auto_foundation_moves[c.get_suit()]) {
+ return move(pr, target_foundation, move::dominance_flag);
+ }
+ }
+#endif
+
+ return none;
+}
+
+// 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) {
+ assert(!piles[target_pile].empty());
+
+ card target_card = piles[target_pile].top_card();
+ piles[target_pile].take();
+
+ bool blocked = is_valid_auto_foundation_move(target_pile);
+ piles[target_pile].place(target_card);
+
+ return blocked;
+}
diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp
index 367f7a0..382fa50 100644
--- a/src/main/game/pile.cpp
+++ b/src/main/game/pile.cpp
@@ -9,6 +9,8 @@
using namespace std;
+const pile::size_type pile::max_size_type = 255;
+
pile::pile(std::vector<card> pv) : pile_vec(pv) {
}
diff --git a/src/main/game/pile.h b/src/main/game/pile.h
index 7257738..654f5be 100644
--- a/src/main/game/pile.h
+++ b/src/main/game/pile.h
@@ -17,6 +17,7 @@ class pile {
friend class cached_game_state;
public:
typedef uint8_t size_type;
+ const static size_type max_size_type;
pile() = default;
pile(std::vector<card>);
diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h
index 067e107..8ebb73d 100644
--- a/src/main/game/sol_rules.h
+++ b/src/main/game/sol_rules.h
@@ -37,6 +37,7 @@ struct sol_rules {
bool hole;
bool foundations;
bool foundations_init_card;
+ bool foundations_removable;
bool diagonal_deal;
uint8_t cells;
uint8_t stock_size;
diff --git a/src/main/input-output/input/json-parsing/rules_parser.cpp b/src/main/input-output/input/json-parsing/rules_parser.cpp
index 7b9de84..395f2e7 100644
--- a/src/main/input-output/input/json-parsing/rules_parser.cpp
+++ b/src/main/input-output/input/json-parsing/rules_parser.cpp
@@ -181,6 +181,14 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
}
}
+ if (d.HasMember("foundations removable")) {
+ if (d["foundations removable"].IsBool()) {
+ sr.foundations_removable = d["foundations removable"].GetBool();
+ } else {
+ json_helper::json_parse_err("[foundations removable] must be a boolean");
+ }
+ }
+
if (d.HasMember("cells")) {
if (d["cells"].IsInt()) {
sr.cells = static_cast<uint8_t>(d["cells"].GetInt());
diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp
index 8f0293f..de0fd41 100644
--- a/src/main/input-output/input/sol_preset_types.cpp
+++ b/src/main/input-output/input/sol_preset_types.cpp
@@ -32,6 +32,7 @@ std::map<string, string> sol_preset_types::mp = {
"hole": false,
"foundations": true,
"foundations initial card": false,
+ "foundations removable": false,
"cells": 0,
"stock size": 0,
"stock deal type": "waste",
@@ -48,7 +49,8 @@ std::map<string, string> sol_preset_types::mp = {
{
"tableau piles": {
"count": 13
- }
+ },
+ "foundations removable": true
}
)"
@@ -61,7 +63,8 @@ std::map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 3
},
- "max rank": 2
+ "max rank": 2,
+ "foundations removable": true
}
)"
@@ -102,7 +105,8 @@ std::map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 13,
"spaces policy": "no-build"
- }
+ },
+ "foundations removable": true
}
)"
@@ -116,7 +120,8 @@ std::map<string, string> sol_preset_types::mp = {
"count": 3,
"spaces policy": "no-build"
},
- "max rank": 3
+ "max rank": 3,
+ "foundations removable": true
}
)"
@@ -191,7 +196,8 @@ std::map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 6
},
- "reserve size": 16
+ "reserve size": 16,
+ "foundations removable": true
}
)"
@@ -205,7 +211,8 @@ std::map<string, string> sol_preset_types::mp = {
"count": 3
},
"max rank": 3,
- "reserve size": 3
+ "reserve size": 3,
+ "foundations removable": true
}
)"
@@ -252,7 +259,8 @@ std::map<string, string> sol_preset_types::mp = {
"count": 10,
"build policy": "red-black",
"diagonal deal": true
- }
+ },
+ "foundations removable": true
}
)"
@@ -267,7 +275,8 @@ std::map<string, string> sol_preset_types::mp = {
"build policy": "red-black",
"diagonal deal": true
},
- "max rank": 2
+ "max rank": 2,
+ "foundations removable": true
}
)"
diff --git a/src/main/main.cpp b/src/main/main.cpp
index 78a3551..975e6c8 100644
--- a/src/main/main.cpp
+++ b/src/main/main.cpp
@@ -2,6 +2,8 @@
#include <boost/optional.hpp>
#include <rapidjson/document.h>
+#include <gperftools/profiler.h>
+
#include "input-output/input/command_line_helper.h"
#include "input-output/input/json-parsing/json_helper.h"
#include "input-output/input/json-parsing/rules_parser.h"
@@ -92,10 +94,12 @@ void solve_input_files(const vector<string> input_files, const sol_rules& rules)
void solve_game(const game_state& gs) {
solver solv(gs);
bool solution = solv.run();
+ ProfilerStop();
if (solution) {
solv.print_solution();
} else {
- cout << "Deal:\n" << gs << "\nNo Possible Solution\n";
+ cout << "Deal:\n" << gs << "\nNo Possible Solution\nStates Searched: "
+ << solv.get_states_searched() << "\n";
}
}
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
index 6dddfb4..e55a4e3 100644
--- a/src/main/solver/solver.cpp
+++ b/src/main/solver/solver.cpp
@@ -6,8 +6,7 @@
#include <ostream>
#include <iostream>
#include <algorithm>
-
-#include <gperftools/profiler.h>
+#include <list>
#include "solver.h"
#include "../input-output/output/log_helper.h"
@@ -16,86 +15,136 @@ using namespace std;
using namespace boost;
solver::solver(const game_state& gs)
- : cache(gs), initial_state(gs), states_searched(0) {}
+ : cache(gs)
+ , init_state(gs)
+ , state(gs)
+ , states_searched(1)
+ , root(nullptr, game_state::move(0, 0))
+ , current_node(&root) {
+}
-solver::node::node(const game_state::move m,
- vector<game_state::move> uc)
- : move(m), unsearched_children(std::move(uc)) {
+solver::node::node(node* p, const game_state::move m)
+ : parent(p), move(m), children() {
}
bool solver::run() {
- //ProfilerStart("solvitaire");
- game_state state = initial_state;
-
- states_searched++;
- LOG_DEBUG (state);
- frontier.emplace_back(game_state::move(0,0), state.get_legal_moves());
- cache.insert(state);
-
- while (!frontier.empty()) {
- node& current = frontier.back();
-
- // If we have a solution, returns true
- if (state.is_solved()) {
- ProfilerStop();
- return true;
+ bool states_exhausted = false;
+ while(!state.is_solved() && !states_exhausted) {
+#ifndef NDEBUG
+ if (current_node->move.is_dominance()) {
+ LOG_DEBUG("(dominance move)");
}
-
- // If the current node has no unsearched children
- if (current.unsearched_children.empty()) {
- // Undoes the move that led to this state
- // (unless it's the null first move)
- if (frontier.size() > 1) {
- state.undo_move(current.move);
- assert(cache.contains(state));
- LOG_DEBUG (state);
- }
- // Returns to the previous state
- frontier.pop_back();
+ LOG_DEBUG(state);
+#endif
+
+ // 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();
+ if (dominance_move) {
+ // Adds the dominance move as a child of the current search node
+ add_child(*dominance_move);
} else {
- game_state::move next_move = current.unsearched_children.back();
- current.unsearched_children.pop_back();
-
- // Applies the first possible move in this state
- state.make_move(next_move);
-
- // Insert the state into the global cache
+ // Caches the current state
bool is_new_state = cache.insert(state);
if (is_new_state) {
- states_searched++;
- LOG_DEBUG (state);
- // If the state is new, creates a new node in the frontier to
- // represent the move just made, and those that are now possible
- frontier.emplace_back(next_move, state.get_legal_moves());
- } else {
- // If we've seen the state before, undoes the move
- state.undo_move(next_move);
- assert(cache.contains(state));
+ // Gets the legal moves in the current state
+ vector<game_state::move> next_moves = state.get_legal_moves();
+
+ // If there are none, reverts to the last node with children
+ if (state.get_legal_moves().empty()) {
+ states_exhausted = revert_to_last_node_with_children();
+ } else {
+ add_children(next_moves);
+ }
+ }
+ // If the state is not a new one, reverts to the last node with children
+ else {
+ states_exhausted = revert_to_last_node_with_children();
}
}
+
+ // Sets the current node to one of its children
+ assert(states_exhausted == current_node->children.empty());
+ if (!states_exhausted) {
+ current_node = &current_node->children.back();
+ state.make_move(current_node->move);
+ }
+
+ states_searched++;
+ }
+
+ return state.is_solved();
+}
+
+void solver::add_children(std::vector<game_state::move>& moves) {
+ for (auto move : moves) {
+ add_child(move);
}
+}
- ProfilerStop();
- return false;
+void solver::add_child(game_state::move move) {
+ current_node->children.emplace_back(current_node, move);
+}
+
+// Called when the current node's children have been exhausted. Travels back up
+// the search tree until it finds a node which still has children. Returns true
+// unless all children have been exhausted.
+bool solver::revert_to_last_node_with_children() {
+ if (!current_node->parent)
+ return true;
+
+ state.undo_move(current_node->move);
+
+#ifndef NDEBUG
+ // 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()) {
+ assert(cache.contains(state));
+ LOG_DEBUG("(undo move)");
+ } else {
+ LOG_DEBUG("(undo dominance move)");
+ }
+ LOG_DEBUG(state);
+#endif
+
+ // Reverts the current node to its parent and removes it
+ current_node = current_node->parent;
+ current_node->children.pop_back();
+
+ // If the current node now has no children, repeat
+ if (current_node->children.empty()) {
+ return revert_to_last_node_with_children();
+ } else {
+ return false;
+ }
}
void solver::print_solution() const {
std::flush(clog);
std::flush(cout);
- game_state state = initial_state;
+
+ const node* n = &root.children.back();
+ game_state state_copy = init_state;
cout << "Solution:\n";
- cout << state << "\n";
- // Ignores first move which is
- bool first = true;
- for (auto f_node : frontier) {
- if (first) {
- first = false;
- continue;
+ cout << state_copy << "\n";
+
+ if (states_searched > 1) {
+ while (!n->children.empty()) {
+ // TODO print move as well
+ state_copy.make_move(n->move);
+ cout << state_copy << "\n";
+ n = &n->children.back();
}
- // TODO print move as well
- state.make_move(f_node.move);
- cout << state << "\n";
}
cout << "States Searched: " << states_searched << "\n";
}
+
+int solver::get_states_searched() const {
+ return states_searched;
+}
+
+const solver::node& solver::get_search_tree() const {
+ return root;
+}
diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h
index 526bd53..9488dda 100644
--- a/src/main/solver/solver.h
+++ b/src/main/solver/solver.h
@@ -6,6 +6,7 @@
#define SOLVITAIRE_SOLVER_H
#include <string>
+#include <list>
#include <vector>
#include "../game/global_cache.h"
@@ -16,20 +17,30 @@ public:
global_cache cache;
struct node {
- explicit node(game_state::move, std::vector<game_state::move>);
+ node(node*, game_state::move);
+ node* parent;
const game_state::move move;
- std::vector<game_state::move> unsearched_children;
+ std::list<node> children;
};
explicit solver(const game_state&);
bool run();
void print_solution() const;
+ int get_states_searched() const;
+ const node& get_search_tree() const;
private:
- const game_state initial_state;
- std::vector<node> frontier;
+ bool revert_to_last_node_with_children();
+ void add_children(std::vector<game_state::move>&);
+ void add_child(game_state::move);
+
+ const game_state init_state;
+ game_state state;
int states_searched;
+
+ node root;
+ node* current_node;
};