diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/game/card.cpp (renamed from src/main/card.cpp) | 8 | ||||
| -rw-r--r-- | src/main/game/card.h (renamed from src/main/card.h) | 4 | ||||
| -rw-r--r-- | src/main/game/game_state.cpp (renamed from src/main/game_state.cpp) | 253 | ||||
| -rw-r--r-- | src/main/game/game_state.h (renamed from src/main/game_state.h) | 19 | ||||
| -rw-r--r-- | src/main/game/pile.cpp (renamed from src/main/pile.cpp) | 89 | ||||
| -rw-r--r-- | src/main/game/pile.h (renamed from src/main/pile.h) | 44 | ||||
| -rw-r--r-- | src/main/game/sol_rules.cpp (renamed from src/main/sol_rules.cpp) | 18 | ||||
| -rw-r--r-- | src/main/game/sol_rules.h (renamed from src/main/sol_rules.h) | 2 | ||||
| -rw-r--r-- | src/main/input-output/command_line_helper.cpp (renamed from src/main/command_line_helper.cpp) | 39 | ||||
| -rw-r--r-- | src/main/input-output/command_line_helper.h (renamed from src/main/command_line_helper.h) | 2 | ||||
| -rw-r--r-- | src/main/input-output/deal_parser.cpp (renamed from src/main/deal_parser.cpp) | 0 | ||||
| -rw-r--r-- | src/main/input-output/deal_parser.h (renamed from src/main/deal_parser.h) | 2 | ||||
| -rw-r--r-- | src/main/input-output/log_helper.h | 19 | ||||
| -rw-r--r-- | src/main/log_helper.h | 19 | ||||
| -rw-r--r-- | src/main/main.cpp | 71 | ||||
| -rw-r--r-- | src/main/solver.cpp | 82 | ||||
| -rw-r--r-- | src/main/solver/solver.cpp | 85 | ||||
| -rw-r--r-- | src/main/solver/solver.h (renamed from src/main/solver.h) | 26 |
18 files changed, 429 insertions, 353 deletions
diff --git a/src/main/card.cpp b/src/main/game/card.cpp index 14ba92d..9f7dbdf 100644 --- a/src/main/card.cpp +++ b/src/main/game/card.cpp @@ -3,12 +3,14 @@ // #include <sstream> -#include <ctype.h> #include "card.h" using namespace std; +// A card can either be created using two int values (rank & suit), or +// using a string (e.g. "AS") + card::card(int r, int s) : rank(r), suit(s) {} card::card(const char* c) : rank(-1), suit(-1) { @@ -26,7 +28,7 @@ card::card(const char* c) : rank(-1), suit(-1) { case 'k': rank = 13; letter = true; break; - default: rank = atoi(c); + default: rank = stoi(c); } char s; @@ -36,7 +38,7 @@ card::card(const char* c) : rank(-1), suit(-1) { s = c[1]; } - s = tolower(s); + s = static_cast<char>(tolower(s)); if (s == 'c') { suit = 0; diff --git a/src/main/card.h b/src/main/game/card.h index ef6ae58..1cac344 100644 --- a/src/main/card.h +++ b/src/main/game/card.h @@ -6,11 +6,15 @@ #define SOLITAIRE_PIP_CARD_H #include <string> +#include <map> class card { public: + // A card can either be created using two int values (rank & suit), or + // using a string (e.g. "AS") card(int, int); card(const char*); + int get_rank() const; char get_suit() const; int get_suit_val() const; diff --git a/src/main/game_state.cpp b/src/main/game/game_state.cpp index 2d6ae2b..3336020 100644 --- a/src/main/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -4,7 +4,6 @@ #include <vector> #include <ostream> -#include <numeric> #include <random> #include <algorithm> #include <functional> @@ -12,7 +11,6 @@ #include <rapidjson/document.h> #include "game_state.h" -#include "pile.h" using namespace rapidjson; using namespace std; @@ -22,7 +20,10 @@ typedef sol_rules::build_policy pol; // Construct an initial game state from a JSON doc game_state::game_state(const Document& doc) - : hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank), + : reserve(true, ord::NO_BUILD, pol::ANY_SUIT, false), + stock(true, ord::NO_BUILD, pol::ANY_SUIT, false), + waste(true, ord::NO_BUILD, pol::ANY_SUIT, false), + hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank), rules("simple-black-hole") { // Construct tableau piles assert(doc.HasMember("tableau piles")); @@ -50,165 +51,146 @@ game_state::game_state(const Document& doc) } // Construct an initial game state from a seed -game_state::game_state(int seed, const sol_rules& s_rules) - : rules(s_rules) { +game_state::game_state(int seed, const sol_rules& s_rules) : + reserve(pile::reserve_factory()), + stock(pile::stock_factory()), + waste(pile::waste_factory()), + hole(pile::hole_factory(s_rules.max_rank)), + rules(s_rules) { vector<card> deck = shuffled_deck(seed, rules.max_rank); // If there is a hole, move the ace of spades to it if (rules.hole) { deck.erase(find(begin(deck), end(deck), card("AS"))); - hole = pile(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank); hole.place("AS"); } - unsigned int deckCardsUsed = 0; - // If there is a stock, deal to it and set up a waste pile too if (rules.stock_size > 0) { - stock = pile(true, ord::NO_BUILD, pol::ANY_SUIT, false); - waste = pile(true, ord::NO_BUILD, pol::ANY_SUIT, false); - for (unsigned int i = 0; i < rules.stock_size; i++) { - stock.place(deck[i + deckCardsUsed]); + stock.place(deck.back()); + deck.pop_back(); } - deckCardsUsed += rules.stock_size; } // If there is a reserve, deal to it if (rules.reserve_size > 0) { - reserve = pile(true, ord::NO_BUILD, pol::ANY_SUIT, false); - for (unsigned int i = 0; i < rules.reserve_size; i++) { - reserve.place(deck[i + deckCardsUsed]); + reserve.place(deck.back()); + deck.pop_back(); } - deckCardsUsed += rules.reserve_size; } - // Deal to the tableau piles (row-by-row) - vector<card>::size_type remainingCards = deck.size() - deckCardsUsed; - for (vector<card>::size_type i = 0; i < remainingCards; i++) { - card c = deck[i + deckCardsUsed]; + unsigned int t = 0; + while (!deck.empty()) { + card c = deck.back(); + deck.pop_back(); // For the first row we must create all the tableau pile vectors - if (i / rules.tableau_pile_count == 0) { - pile p(true, rules.build_ord, pol::ANY_SUIT, false); - tableau_piles.push_back(p); + if (t / rules.tableau_pile_count == 0) { + tableau_piles.push_back(pile::tableau_factory(rules.build_ord)); } // Add the randomly generated card to the tableau piles - tableau_piles[i % rules.tableau_pile_count].place(c); + tableau_piles[t % rules.tableau_pile_count].place(c); + t++; } - deckCardsUsed += remainingCards; - assert(deckCardsUsed == deck.size()); // If there are foundation piles, create the relevant pile vectors if (rules.foundations) { for (int i = 0; i < 4; i++) { - pile p(true, ord::ASCENDING, pol(i), false); - foundations.push_back(p); + foundations.push_back(pile::foundation_factory(pol(i))); } } // If there are cell piles, create the relevant cell vectors if (rules.cells > 0) { for (unsigned int i = 0; i < rules.cells; i++) { - pile p(true, ord::SINGLE_CARD, pol::ANY_SUIT, false); - cells.push_back(p); + cells.push_back(pile::cell_factory()); } } } // Generate a randomly ordered vector of cards vector<card> game_state::shuffled_deck(int seed, int max_rank = 13) { - vector<int*> v; + vector<int> values; + vector<int*> v_ptrs; for (int i = 0; i < max_rank * 4; i++) { - v.push_back(new int(i)); + values.push_back(i); + } + for (int i = 0; i < max_rank * 4; i++) { + v_ptrs.push_back(&values[i]); } // Randomly shuffle the pointers - auto rng = std::default_random_engine(seed); - std::shuffle(std::begin(v), std::end(v), rng); + auto rng = default_random_engine(seed); + shuffle(begin(v_ptrs), end(v_ptrs), rng); vector<card> deck; - for (int *i : v) { + for (int *i : v_ptrs) { int r = ((*i) % max_rank) + 1; int s = (*i) / max_rank; - deck.push_back(card(r, s)); - } - - // release memory - for (int i = 0; i < max_rank * 4; i++) { - delete v[i]; + deck.emplace_back(card(r, s)); } return deck; } -vector<game_state> game_state::get_next_legal_states() { +const vector<game_state> game_state::get_next_legal_states() const { // Generates vectors of references to all the piles from which // cards can be removed, and to which they can be added vector<pile*> can_remove; vector<pile*> can_add; - for (vector<pile>::size_type i = 0; i < tableau_piles.size(); i++) { - can_remove.push_back(&tableau_piles[i]); - if (rules.build_ord != ord::NO_BUILD) { - can_add.push_back(&tableau_piles[i]); + // A mutable version of the current state, used to create the next legal + // states + game_state mut_state = *this; + + for (pile* p : mut_state.get_regular_pile_refs()) { + if (p->can_remove()) { + can_remove.push_back(p); } - } - if (rules.reserve_size > 0) { - can_remove.push_back(&reserve); - } - if (rules.foundations) { - for (vector<pile>::size_type i = 0; i < foundations.size(); i++) { - can_remove.push_back(&foundations[i]); - can_add.push_back(&foundations[i]); + if (p->get_build_order() != ord::NO_BUILD) { + can_add.push_back(p); } } - for (vector<pile>::size_type i = 0; i < cells.size(); i++) { - can_remove.push_back(&cells[i]); - can_add.push_back(&cells[i]); - } - if (rules.stock_size > 0) { - can_remove.push_back(&waste); - } - if (rules.hole) { - can_add.push_back(&hole); - } // The next legal states vector<game_state> next; // Moving from and to the 'can remove' and 'can add' piles - for (pile *rem_pile : can_remove) { + for (pile* rem_pile : can_remove) { if (rem_pile->empty()) continue; - for (pile *add_pile : can_add) { - if (add_pile != rem_pile + for (pile* add_pile : can_add) { + if (&add_pile != &rem_pile && add_pile->can_place(rem_pile->top_card())) { - move(rem_pile, add_pile); // remove - game_state s = *this; - next.push_back(s); - move(add_pile, rem_pile); // restore + game_state new_gs = move(mut_state, rem_pile, add_pile); + next.push_back(new_gs); } } } // Dealing from the stock to the waste if (!stock.empty()) { - move(&stock, &waste); - game_state s = *this; - next.push_back(s); - move(&waste, &stock); + game_state new_gs = move(mut_state, &mut_state.stock, &mut_state.waste); + next.push_back(new_gs); } return next; } +const game_state game_state::move(game_state& gs, pile* from, pile* to) const { + to->place(from->take()); + const game_state new_gs(gs); + from->place(to->take()); + return new_gs; +} + bool game_state::is_solved() const { for (auto p : foundations) { @@ -217,20 +199,42 @@ bool game_state::is_solved() const { } } - if (rules.hole && hole.size() != rules.max_rank * 4) { - return false; + return !rules.hole || hole.size() == rules.max_rank * 4; +} + +// Not the stock, because cards from it can't be moved to the rest of the piles! +vector<pile*> game_state::get_regular_pile_refs() { + vector<pile*> piles; + auto to_ptr = [](pile& p){return &p;}; + + if (rules.foundations) { + transform(begin(foundations), end(foundations), back_inserter(piles), + to_ptr); } - return true; + if (rules.cells) { + transform(begin(cells), end(cells), back_inserter(piles), to_ptr); + } + transform(begin(tableau_piles), end(tableau_piles), back_inserter(piles), to_ptr); + if (rules.reserve_size > 0) { + piles.push_back(&reserve); + } + if (rules.stock_size > 0) { + piles.push_back(&waste); + } + if (rules.hole) { + piles.push_back(&hole); + } + return piles; } ostream& game_state::print(ostream& stream) const { if (rules.foundations) { print_header(stream, "Foundations"); - print_foundations(stream); + print_top_of_piles(stream, foundations); } if (rules.cells) { print_header(stream, "Cells"); - print_cells(stream); + print_piles(stream, cells); } if (rules.reserve_size > 0) { print_header(stream, "Reserve"); @@ -238,19 +242,17 @@ ostream& game_state::print(ostream& stream) const { } if (rules.tableau_pile_count > 0) { print_header(stream, "Tableau Piles"); - print_tableau_piles(stream); + print_piles(stream, tableau_piles); } if (rules.stock_size > 0) { - print_header(stream, "Stock"); - print_pile(stream, stock); - print_header(stream, "Waste"); - print_pile(stream, waste); + print_header(stream, "Stock | Waste"); + print_piles(stream, {stock, waste}); } if (rules.hole) { print_header(stream, "Hole Card"); - print_hole(stream); + print_top_of_pile(stream, hole); } - return stream << "===================================\n"; + return stream << "==================================="; } void game_state::print_header(ostream& stream, const char* header) const { @@ -262,56 +264,57 @@ void game_state::print_header(ostream& stream, const char* header) const { stream << "\n"; } -void game_state::print_foundations(ostream& stream) const { - stream << foundations[0] << "\t" - << foundations[1] << "\t" - << foundations[2] << "\t" - << foundations[3] << "\n"; -} - -void game_state::print_cells(ostream& stream) const { - for (pile cell : cells) { - if (cells.empty()) { - stream << "[]" << "\t"; - } else { - stream << cells[0] << "\t"; - } - } - stream << "\n"; -} - -void game_state::print_pile(ostream& stream, const pile &p) const { - if (p.empty()) { - stream << "[]\n"; - } else { - for (unsigned int i = 0; i < p.size(); i++) { - stream << p[i] << "\n"; - } - } -} - -void game_state::print_tableau_piles(ostream& stream) const { +void game_state::print_piles(ostream& stream, + const vector<pile>& pile_vec) const { bool empty_row = false; vector<card>::size_type row_idx = 0; + // Loops through the rows in each pile, starting from the bottom, until + // one is empty while (!empty_row) { + // Loops throught the current row to determine if it is empty empty_row = true; - - for (pile tableau_pile : tableau_piles) { + for (const pile& tableau_pile : pile_vec) { if (tableau_pile.size() > row_idx) { empty_row = false; - stream << tableau_pile[row_idx]; + break; } - stream << "\t"; } + if (!empty_row || row_idx == 0) { + // Loops through the current (non-empty) row, and outputs the values + for (const pile& tableau_pile : pile_vec) { + if (tableau_pile.size() > row_idx) { + stream << tableau_pile[row_idx]; + } else if (row_idx == 0) { + stream << "[]"; + } + stream << "\t"; + } - stream << "\n"; - row_idx++; + stream << "\n"; + row_idx++; + } + } +} + +void game_state::print_pile(ostream& stream, const pile &p) const { + print_piles(stream, {p}); +} + +void game_state::print_top_of_piles(ostream& stream, const vector<pile>& vp) const { + vector<pile> top(vp); + for (pile& p : top) { + if (!p.empty()) { + card t = p.top_card(); + p.clear(); + p.place(t); + } } + print_piles(stream, top); } -void game_state::print_hole(ostream& stream) const { - stream << hole.top_card() << "\n"; +void game_state::print_top_of_pile(ostream& stream, const pile& p) const { + print_top_of_piles(stream, {p}); } bool operator==(const game_state& a, const game_state& b) { diff --git a/src/main/game_state.h b/src/main/game/game_state.h index 424c678..12dcda9 100644 --- a/src/main/game_state.h +++ b/src/main/game/game_state.h @@ -22,8 +22,7 @@ public: // Do the same from a 'rules' object game_state(int seed, const sol_rules&); - std::vector<game_state> get_next_legal_states(); - // NOTE: will only be evaluated after get_next_legal_states() is called + const std::vector<game_state> get_next_legal_states() const; bool is_solved() const; std::ostream& print(std::ostream&) const; @@ -34,19 +33,21 @@ public: private: static std::vector<card> shuffled_deck(int, int); + const game_state move(game_state&, pile*, pile*) const; + std::vector<pile*> get_regular_pile_refs(); + void print_header(std::ostream&, const char*) const; - void print_foundations(std::ostream&) const; - void print_cells(std::ostream&) const; - void print_tableau_piles(std::ostream&) const; - void print_hole(std::ostream&) const; + void print_piles(std::ostream&, const std::vector<pile>&) const; void print_pile(std::ostream&, const pile&) const; + void print_top_of_piles(std::ostream&, const std::vector<pile>&) const; + void print_top_of_pile(std::ostream&, const pile&) const; - std::vector<pile> tableau_piles; - std::vector<pile> cells; std::vector<pile> foundations; - pile waste; + std::vector<pile> cells; + std::vector<pile> tableau_piles; pile reserve; pile stock; + pile waste; pile hole; sol_rules rules; }; diff --git a/src/main/pile.cpp b/src/main/game/pile.cpp index 344e56c..bebd1f6 100644 --- a/src/main/pile.cpp +++ b/src/main/game/pile.cpp @@ -9,28 +9,47 @@ using namespace std; -pile::pile() { - set_max_rank(13); -} +typedef sol_rules::build_order ord; +typedef sol_rules::build_policy pol; + +//////////// +// Static // +//////////// -pile::pile(bool r, sol_rules::build_order bo, sol_rules::build_policy bp, bool l, - int mr) { - set_remove(r); - set_build_order(bo); - set_build_policy(bp); - set_loops(l); - set_max_rank(mr); +pile pile::foundation_factory(pol p) { + return pile(true, ord::ASCENDING, p, false); +} +pile pile::cell_factory() { + return pile(true, ord::SINGLE_CARD, pol::ANY_SUIT, false); +} +pile pile::tableau_factory(ord o) { + return pile(true, o, pol::ANY_SUIT, false); +} +pile pile::reserve_factory() { + return pile(true, ord::NO_BUILD, pol::ANY_SUIT, false); } +pile pile::stock_factory() { + return pile(true, ord::NO_BUILD, pol::ANY_SUIT, false); +} +pile pile::waste_factory() { + return pile(true, ord::NO_BUILD, pol::ANY_SUIT, false); +} +pile pile::hole_factory(int max_rank) { + return pile(false, ord::BOTH, pol::ANY_SUIT, true, max_rank); +} + +//////////////// +// Non-static // +//////////////// -void pile::set_remove(bool r) {remove = r;} -void pile::set_loops(bool l) {build_order_loops = l;} -void pile::set_max_rank(int mr) {max_rank = mr;} -void pile::set_build_order(sol_rules::build_order bo) {build_order = bo;} -void pile::set_build_policy(sol_rules::build_policy bp) {build_policy = bp;} +pile::pile(bool r, ord bo, pol bp, bool l, int mr) : + remove(r), + build_order(bo), + build_policy(bp), + build_order_loops(l), + max_rank(mr) {} bool pile::can_place(const card& c) const { - typedef sol_rules::build_order ord; - typedef sol_rules::build_policy pol; if (build_order == ord::NO_BUILD) { return false; @@ -77,20 +96,12 @@ bool pile::one_lt(int a, int b) const { return (a == b - 1) || (build_order_loops && a == max_rank && b == 1); } -void pile::place(const card& c) { - pile_vec.push_back(c); -} + card pile::top_card() const { return pile_vec.back(); } -card pile::take() { - card c = top_card(); - pile_vec.pop_back(); - return c; -} - bool pile::can_remove() const { return remove; } @@ -103,6 +114,10 @@ unsigned long pile::size() const { return pile_vec.size(); } +ord pile::get_build_order() const { + return build_order; +} + card& pile::operator[] (vector<card>::size_type i) { return pile_vec[i]; } @@ -111,18 +126,20 @@ card pile::operator[] (vector<card>::size_type i) const { } -void move(pile *a, pile *b) { - b->place(a->take()); +void pile::place(const card& c) { + pile_vec.push_back(c); } -bool operator==(const pile& a, const pile& b) { - return a.pile_vec == b.pile_vec; +card pile::take() { + card c = top_card(); + pile_vec.pop_back(); + return c; } -ostream & operator<<(ostream & stream, pile const & p) { - if (p.empty()) { - return stream << "[]"; - } else { - return stream << p.top_card(); - } +void pile::clear() { + pile_vec.clear(); +} + +bool operator==(const pile& a, const pile& b) { + return a.pile_vec == b.pile_vec; } diff --git a/src/main/pile.h b/src/main/game/pile.h index 4bc6e71..6482e0c 100644 --- a/src/main/pile.h +++ b/src/main/game/pile.h @@ -12,38 +12,44 @@ class pile { public: - pile(); - pile(bool, sol_rules::build_order, sol_rules::build_policy, bool, int m = 13); + static pile foundation_factory(sol_rules::build_policy); + static pile cell_factory(); + static pile tableau_factory(sol_rules::build_order); + static pile reserve_factory(); + static pile stock_factory(); + static pile waste_factory(); + static pile hole_factory(int max_rank = 13); - void set_remove(bool); - void set_loops(bool); - void set_max_rank(int); - void set_build_order(sol_rules::build_order); - void set_build_policy(sol_rules::build_policy); + pile(bool, sol_rules::build_order, sol_rules::build_policy, bool, + int max_rank = 13); bool can_place(const card&) const; - void place(const card&); - card take(); - card top_card() const; bool can_remove() const; + card top_card() const; bool empty() const; unsigned long size() const; + sol_rules::build_order get_build_order() const; + + void place(const card&); + card take(); + void clear(); + card& operator[] (std::vector<card>::size_type); card operator[] (std::vector<card>::size_type) const; friend bool operator==(const pile&, const pile&); - friend std::ostream & operator<<(std::ostream &, pile const &); + private: - std::vector<card> pile_vec; - bool remove; - bool build_order_loops; - int max_rank; - sol_rules::build_order build_order; - sol_rules::build_policy build_policy; bool one_lt(int, int) const; -}; -void move(pile*, pile*); + const bool remove; + const sol_rules::build_order build_order; + const sol_rules::build_policy build_policy; + const bool build_order_loops; + const int max_rank; + + std::vector<card> pile_vec; +}; #endif //SOLVITAIRE_PILE_H diff --git a/src/main/sol_rules.cpp b/src/main/game/sol_rules.cpp index f47cf44..003528c 100644 --- a/src/main/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -11,7 +11,9 @@ using namespace std; -// Static +//////////// +// Static // +//////////// typedef sol_rules::build_policy pol; @@ -39,12 +41,16 @@ int sol_rules::suit_val(pol bp) { } } -// Non-static +//////////////// +// Non-static // +//////////////// sol_rules::sol_rules(std::string sol_type) { valid_sol vs = valid_sol_enum(sol_type); // Default game values + + // tableau_pile_count = no obvious default build_ord = build_order::DESCENDING; build_pol = build_policy::ANY_SUIT; max_rank = 13; @@ -93,11 +99,11 @@ sol_rules::sol_rules(std::string sol_type) { stock_size = 34; break; case valid_sol::SIMPLE_CANFIELD: - tableau_pile_count = 3; + tableau_pile_count = 2; build_pol = build_policy::RED_BLACK; - max_rank = 3; - reserve_size = 3; - stock_size = 6; + max_rank = 2; + reserve_size = 2; + stock_size = 2; break; default: assert(false); diff --git a/src/main/sol_rules.h b/src/main/game/sol_rules.h index 44b83bb..b73097d 100644 --- a/src/main/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -41,7 +41,7 @@ public: static bool is_suit(build_policy); static int suit_val(build_policy); - sol_rules(std::string); + explicit sol_rules(std::string); unsigned int tableau_pile_count; build_order build_ord; diff --git a/src/main/command_line_helper.cpp b/src/main/input-output/command_line_helper.cpp index 71eaf43..e61c546 100644 --- a/src/main/command_line_helper.cpp +++ b/src/main/input-output/command_line_helper.cpp @@ -4,19 +4,19 @@ #include <tuple> #include <vector> -#include <iostream> #include <boost/program_options.hpp> #include "command_line_helper.h" -#include "sol_rules.h" +#include "game/sol_rules.h" +#include "input-output/log_helper.h" using namespace std; namespace po = boost::program_options; command_line_helper::command_line_helper() - : main_options("options"), random_deal(-1) { + : main_options("options") { main_options.add_options() ("help", "produce help message") @@ -35,19 +35,24 @@ command_line_helper::command_line_helper() p.add("input-files", -1); } +// Returns true if we can continue solving the supplied solitaire(s) bool command_line_helper::parse(int argc, const char* argv[]) { po::variables_map vm; + // Attempts to parse cmdln options. Catches basic errors try { store(po::command_line_parser(argc, argv).options(cmdline_options) .positional(p).run(), vm); } catch(po::error& e) { - cerr << "Error: " << e.what() << "\n"; + LOG_ERROR ("Error: " << e.what()); return false; } + // Updates the variables map which the user's options are stored in notify(vm); - help = vm.count("help"); + // Converts the variables map into flags and values + + help = (vm.count("help") != 0); if (vm.count("input-files")) { input_files = vm["input-files"].as<vector<string>>(); @@ -59,6 +64,8 @@ bool command_line_helper::parse(int argc, const char* argv[]) { if (vm.count("random")) { random_deal = vm["random"].as<int>(); + } else { + random_deal = -1; } // Handle logic error scenarios @@ -79,6 +86,8 @@ bool command_line_helper::assess_errors() { bool valid_sol_type = assess_sol_type(); if (!valid_sol_type) return false; + // The user must either supply input files or a random seed + if (random_deal != -1 && !input_files.empty()) { print_rand_plus_input_err(); return false; @@ -91,14 +100,16 @@ bool command_line_helper::assess_errors() { return true; } +// Checks if the supplied solitaire type is in the list of valid solitaires bool command_line_helper::assess_sol_type() { if (find(sol_rules::valid_sol_strs.begin(), sol_rules::valid_sol_strs.end(), solitaire_type) != sol_rules::valid_sol_strs.end()) { return true; + } else { - cerr << "Error: Solitaire type is not valid: " << solitaire_type - << "\nValid solitaire types are: "; + LOG_ERROR ("Error: Solitaire type is not valid: " << solitaire_type + << "\nValid solitaire types are: "); copy(begin(sol_rules::valid_sol_strs), end(sol_rules::valid_sol_strs), ostream_iterator<string>(cerr, ", ")); cerr << "\n"; @@ -108,24 +119,24 @@ bool command_line_helper::assess_sol_type() { } void command_line_helper::print_help() { - cerr << "Usage: solvitaire [options] input-file1 input-file2 ...\n" - << main_options << "\n"; + LOG_ERROR ("Usage: solvitaire [options] input-file1 input-file2 ...\n" + << main_options); } void command_line_helper::print_no_input_error() { - cerr << "Error: User must supply input file(s) or the '--random' " - "option\n"; + LOG_ERROR ("Error: User must supply input file(s) or the '--random' " + "option"); print_help(); } void command_line_helper::print_no_sol_type_error() { - cerr << "Error: User must supply a solitaire type\n"; + LOG_ERROR ("Error: User must supply a solitaire type"); print_help(); } void command_line_helper::print_rand_plus_input_err() { - cerr << "Error: User must supply input file(s) or the '--random' option, " - "not both\n"; + LOG_ERROR ("Error: User must supply input file(s) or the '--random' option, " + "not both"); print_help(); } diff --git a/src/main/command_line_helper.h b/src/main/input-output/command_line_helper.h index 8cc67cb..bb2f698 100644 --- a/src/main/command_line_helper.h +++ b/src/main/input-output/command_line_helper.h @@ -10,11 +10,13 @@ class command_line_helper { public: command_line_helper(); + bool parse(int argc, const char* argv[]); const std::vector<std::string> get_input_files(); const std::string get_solitaire_type(); int get_random_deal(); bool get_help(); + private: bool assess_errors(); bool assess_sol_type(); diff --git a/src/main/deal_parser.cpp b/src/main/input-output/deal_parser.cpp index 8a9cef1..8a9cef1 100644 --- a/src/main/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp diff --git a/src/main/deal_parser.h b/src/main/input-output/deal_parser.h index 99523f6..60eb8fb 100644 --- a/src/main/deal_parser.h +++ b/src/main/input-output/deal_parser.h @@ -11,7 +11,7 @@ #include <rapidjson/document.h> #include <rapidjson/schema.h> -#include "card.h" +#include "game/card.h" class deal_parser { public: diff --git a/src/main/input-output/log_helper.h b/src/main/input-output/log_helper.h new file mode 100644 index 0000000..71cc5f4 --- /dev/null +++ b/src/main/input-output/log_helper.h @@ -0,0 +1,19 @@ +// +// Created by thecharlesblake on 10/16/17. +// + +#ifndef SOLVITAIRE_LOG_HELPER_H +#define SOLVITAIRE_LOG_HELPER_H + +#include <iostream> + +#if defined NDEBUG + #define LOG_DEBUG(msg) +#else + #define LOG_DEBUG(msg) clog << "[debug]\n" << msg << "\n" +#endif +#define LOG_INFO(msg) clog << "[info] " << msg << "\n" +#define LOG_WARNING clog(msg) << "[warning] " << msg << "\n" +#define LOG_ERROR(msg) cerr << "[ERROR] " << msg << "\n" + +#endif //SOLVITAIRE_LOG_HELPER_H diff --git a/src/main/log_helper.h b/src/main/log_helper.h deleted file mode 100644 index 889a5ae..0000000 --- a/src/main/log_helper.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by thecharlesblake on 10/16/17. -// - -#ifndef SOLVITAIRE_LOG_HELPER_H -#define SOLVITAIRE_LOG_HELPER_H - -#include <iostream> - -#if defined NDEBUG - #define LOG_DEBUG -#else - #define LOG_DEBUG(msg) clog << "[debug]\n" << msg -#endif -#define LOG_INFO(msg) clog << "[info]\n" << msg -#define LOG_WARNING(msg) clog << "[warning]\n" << msg -#define LOG_ERROR(msg) cerr << "[error]\n" << msg - -#endif //SOLVITAIRE_LOG_HELPER_H diff --git a/src/main/main.cpp b/src/main/main.cpp index 5e11537..d748c80 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -1,19 +1,22 @@ -#include <iostream> - #include <boost/program_options.hpp> +#include <boost/optional.hpp> #include <rapidjson/document.h> -#include "command_line_helper.h" -#include "deal_parser.h" -#include "game_state.h" -#include "solver.h" +#include "input-output/command_line_helper.h" +#include "input-output/deal_parser.h" +#include "game/game_state.h" +#include "solver/solver.h" +#include "input-output/log_helper.h" using namespace rapidjson; using namespace std; +using namespace boost; namespace po = boost::program_options; +void solve_random_game(int, const sol_rules&); +void solve_input_files(const vector<string>, const sol_rules); void solve_game(const game_state&, const sol_rules&); int main(int argc, const char* argv[]) { @@ -26,36 +29,50 @@ int main(int argc, const char* argv[]) { // Generates the rules of the solitaire from the game type const sol_rules rules(clh.get_solitaire_type()); - int seed = clh.get_random_deal(); - if (seed >= 0) { - cout << "Attempting to solve with seed: " << seed << "...\n"; - game_state gs(seed, rules); - solve_game(gs, rules); + // Retrieves the input files to be solved + const vector<string> input_files = clh.get_input_files(); + + // If there are no input files, solve a random deal based on the + // supplied seed + if (input_files.empty()) { + solve_random_game(clh.get_random_deal(), rules); } + // Otherwise, solve the input files + else { + solve_input_files(input_files, rules); + } + + return 0; +} - for (auto input_json : clh.get_input_files()) { - try { - // Attempts to read the user's json into a document - Document doc; - deal_parser::parse(doc, input_json); +void solve_random_game(int seed, const sol_rules& rules) { + LOG_INFO ("Attempting to solve with seed: " << seed << "..."); + game_state gs(seed, rules); + solve_game(gs, rules); +} - // Creates a game state object from the json, plus a solver - game_state gs(doc); +void solve_input_files(const vector<string> input_files, const sol_rules rules) { + for (const auto &input_json : input_files) { + // Attempts to read the user's json into a document + Document doc; + deal_parser::parse(doc, input_json); - cout << "Attempting to solve " << input_json << "...\n"; - solve_game(gs, rules); + // Creates a game state object from the json, plus a solver + game_state gs(doc); - } catch (runtime_error& e) { - cerr << "Error: " << e.what() << "\n"; - } + LOG_INFO ("Attempting to solve " << input_json << "..."); + solve_game(gs, rules); } - - return 0; } void solve_game(const game_state& gs, const sol_rules& rules) { solver sol(gs, rules); - sol.run(); - cout << sol; + optional<solver::node> solution = sol.run(); + if (solution) { + cout << "Solution:\n" << *solution << "States Searched: " + << sol.get_states_searched() << "\n"; + } else { + cout << "Deal:\n" << sol.get_root() << "\nNo Possible Solution\n"; + } } diff --git a/src/main/solver.cpp b/src/main/solver.cpp deleted file mode 100644 index 1db92f9..0000000 --- a/src/main/solver.cpp +++ /dev/null @@ -1,82 +0,0 @@ -// -// Created by thecharlesblake on 10/24/17. -// - -#include <string> -#include <ostream> -#include <iostream> -#include <algorithm> - -#include "solver.h" -#include "log_helper.h" -#include "game_state.h" -#include <boost/log/trivial.hpp> -using namespace std; - -solver::solver(const game_state gs, const sol_rules& sr) - : root(gs), rules(sr), solution(gs), solution_found(false), - states_searched(0) {} - -solver::node::node(const game_state gs) : state(gs) {} - -bool solver::run() { - been_run = true; - - // Iteratively run a depth-first search. Nodes in the frontier are yet to - // be explored - vector<node> frontier; - frontier.push_back(root); - while (!frontier.empty()) { - // Pop the element at the top of the stack - node current = frontier.back(); - frontier.pop_back(); - - LOG_DEBUG(current.state); - - // Create new nodes for each of the children - vector<game_state> new_children = current.state.get_next_legal_states(); - states_searched++; - - // NOTE: get_next_legal_states() makes the is_solved() function work - if (current.state.is_solved()) { - solution = current; - solution_found = true; - break; - } - - for (auto it = new_children.rbegin(); it != new_children.rend(); it++) { - // If we have seen the state before, ignore it (loop detection)) - if (find(begin(current.history), end(current.history), *it) - != end(current.history)) { - continue; - } - - node n = current; - n.history.push_back(n.state); - n.state = *it; - - frontier.push_back(n); - } - } - - return solution_found; -} - -std::ostream& operator<< (std::ostream& stream, const solver& sol) { - - if (sol.solution_found) { - stream << "Solution:\n"; - for (auto state : sol.solution.history) { - stream << state << "\n"; - } - stream << sol.solution.state << "\nSolved!\n"; - - } else if (sol.been_run) { - stream << "Deal:\n" << sol.root.state << "\n" - << "No Possible Solution\n"; - } else { - stream << "Solver has not been run yet\n"; - } - stream << "States Searched: " << sol.states_searched << "\n"; - return stream; -}
\ No newline at end of file diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp new file mode 100644 index 0000000..f412d03 --- /dev/null +++ b/src/main/solver/solver.cpp @@ -0,0 +1,85 @@ +// +// Created by thecharlesblake on 10/24/17. +// + +#include <string> +#include <ostream> +#include <iostream> +#include <algorithm> + +#include "solver.h" +#include "input-output/log_helper.h" +#include "game/game_state.h" +#include <boost/log/trivial.hpp> + +using namespace std; +using namespace boost; + +solver::solver(const game_state gs, const sol_rules& sr) + : root(NULL, gs), rules(sr), solution(root), states_searched(0) {} + +solver::node::node(const node* parent, const game_state& gs) : + history(solver::node::gen_history(parent)), state(gs) {} + +const vector<game_state> solver::node::gen_history(const node* parent) { + vector<game_state> hist; + if (parent != NULL) { + hist = std::vector<game_state>(parent->history); + hist.push_back(parent->state); + } else { + hist = vector<game_state>(); + } + return hist; +} + +const optional<solver::node> solver::run() { + // Iteratively run a depth-first search. Nodes in the frontier are yet to + // be explored + vector<node> frontier; + frontier.push_back(root); + + while (!frontier.empty()) { + // Pop the element at the top of the stack + const node current = frontier.back(); + frontier.pop_back(); + + LOG_DEBUG (current.state); + + // Create new nodes for each of the children + vector<game_state> new_children = current.state.get_next_legal_states(); + states_searched++; + + for (auto it = new_children.rbegin(); it != new_children.rend(); it++) { + // If we have seen the state before, ignore it (loop detection)) + if (find(begin(current.history), end(current.history), *it) + != end(current.history)) { + continue; + } + + const node n(¤t, *it); + if (n.state.is_solved()) { + return n; + } else { + frontier.push_back(n); + } + } + } + + return none; +} + +const solver::node solver::get_root() const { + return root; +} + +int solver::get_states_searched() const { + return states_searched; +} + +std::ostream& operator<< (std::ostream& stream, const solver::node& solution) { + for (auto state : solution.history) { + stream << state << "\n"; + } + stream << solution.state << "\n"; + return stream; +}
\ No newline at end of file diff --git a/src/main/solver.h b/src/main/solver/solver.h index 4215989..91c93d6 100644 --- a/src/main/solver.h +++ b/src/main/solver/solver.h @@ -10,28 +10,32 @@ #include <boost/optional.hpp> -#include "game_state.h" -#include "sol_rules.h" +#include "game/game_state.h" +#include "game/sol_rules.h" class solver { public: - solver(const game_state, const sol_rules&); - bool run(); - - friend std::ostream& operator<< (std::ostream&, const solver&); - -private: struct node { - node(const game_state); + node(const node*, const game_state&); std::vector<game_state> history; game_state state; + + static const std::vector<game_state> gen_history(const node* parent); }; + solver(const game_state, const sol_rules&); + + const node get_root() const; + int get_states_searched() const; + + const boost::optional<node> run(); + + friend std::ostream& operator<< (std::ostream&, const solver::node&); + +private: const node root; const sol_rules rules; - bool been_run; node solution; - bool solution_found; int states_searched; }; |
