From 9c018b08ed775eda03e4507e33a20742659683ea Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 28 Oct 2017 14:03:54 +0100 Subject: Refactor game generation to make more generic --- src/main/game_state.cpp | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) (limited to 'src/main/game_state.cpp') diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp index ef9db97..28f6d3b 100644 --- a/src/main/game_state.cpp +++ b/src/main/game_state.cpp @@ -18,7 +18,7 @@ using namespace std; // Construct an initial game state from a JSON doc game_state::game_state(const Document& doc, string sol_type) : max_rank(1) { - simple = sol_type == "simple-black-hole"; + max_rank = sol_type == "simple-black-hole" ? 7 : 13; // Construct tableau piles assert(doc.HasMember("tableau piles")); @@ -46,15 +46,27 @@ game_state::game_state(const Document& doc, string sol_type) // Construct an initial game state from a seed game_state::game_state(int seed, string sol_type) { - simple = sol_type == "simple-black-hole"; - max_rank = simple ? 7 : 13; + max_rank = sol_type == "simple-black-hole" ? 7 : 13; - int start = 1; // Ignore the first (hole) card - int end = max_rank * 4; + vector deck = shuffled_deck(seed, max_rank); - // Create a vector of pointers to ints from [start, end) + for (vector::size_type i = 0; i < deck.size(); i++) { + // Don't add the Ace of Spades to the hole card + card c = deck[i]; + if (c == "AS") continue; + + // Add the randomly generated card to the tableau piles + if (i % 3 == 0) tableau_piles.push_back(vector()); + tableau_piles[i / 3].push_back(deck[i]); + } + + hole_card = "AS"; +} + +// Generate a randomly ordered vector of cards +vector game_state::shuffled_deck(int seed, int max_rank = 13) { vector v; - for (int i = start; i < end; i++) { + for (int i = 0; i < max_rank * 4; i++) { v.push_back(new int(i)); } @@ -62,23 +74,23 @@ game_state::game_state(int seed, string sol_type) { auto rng = std::default_random_engine(seed); std::shuffle(std::begin(v), std::end(v), rng); - for (vector::size_type i = 0; i < v.size(); i++) { - int r = ((*v[i]) % max_rank); - int s = (*v[i]) / max_rank; + vector deck; + for (int *i : v) { + int r = ((*i) % max_rank) + 1; + int s = (*i) / max_rank; - // Add the randomly generated card to the tableau piles - if (i % 3 == 0) tableau_piles.push_back(vector()); - tableau_piles[i / 3].push_back(card(r + 1, s)); + deck.push_back(card(r, s)); } - hole_card = "AS"; - // release memory - for (int i = start; i < max_rank; i++) { + for (int i = 0; i < max_rank * 4; i++) { delete v[i]; } + + return deck; } + vector game_state::get_next_legal_states() const { vector next; -- cgit v1.2.1 From 6a5a840f678aa78659b51ec647dcced4544c4a0b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 28 Oct 2017 15:29:17 +0100 Subject: Modify game state generation to work across sols --- src/main/game_state.cpp | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'src/main/game_state.cpp') diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp index 28f6d3b..9d0349d 100644 --- a/src/main/game_state.cpp +++ b/src/main/game_state.cpp @@ -16,15 +16,13 @@ using namespace rapidjson; using namespace std; // Construct an initial game state from a JSON doc -game_state::game_state(const Document& doc, string sol_type) - : max_rank(1) { - max_rank = sol_type == "simple-black-hole" ? 7 : 13; - +game_state::game_state(const Document& doc) { // Construct tableau piles assert(doc.HasMember("tableau piles")); const Value& json_tab_piles = doc["tableau piles"]; assert(json_tab_piles.IsArray()); + int max_rank = 1; for (auto& json_tab : json_tab_piles.GetArray()) { vector tableau_pile; @@ -45,22 +43,27 @@ game_state::game_state(const Document& doc, string sol_type) } // Construct an initial game state from a seed -game_state::game_state(int seed, string sol_type) { - max_rank = sol_type == "simple-black-hole" ? 7 : 13; - - vector deck = shuffled_deck(seed, max_rank); +game_state::game_state(int seed, const sol_rules& rules) { + vector deck = shuffled_deck(seed, rules.max_rank); + // Deal to the tableau piles (row-by-row) for (vector::size_type i = 0; i < deck.size(); i++) { - // Don't add the Ace of Spades to the hole card card c = deck[i]; - if (c == "AS") continue; + + // If there is a hole card, don't deal the ace of spades + if (rules.hole && c == "AS") { + hole_card = "AS"; + continue; + } + + // For the first row we must create all the tableau pile vectors + if (i / rules.tableau_pile_count == 0) { + tableau_piles.push_back(vector()); + } // Add the randomly generated card to the tableau piles - if (i % 3 == 0) tableau_piles.push_back(vector()); - tableau_piles[i / 3].push_back(deck[i]); + tableau_piles[i % rules.tableau_pile_count].push_back(c); } - - hole_card = "AS"; } // Generate a randomly ordered vector of cards -- cgit v1.2.1 From c2e0bbad760bb95b7967d6df065c46e1aac1270d Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 28 Oct 2017 15:50:45 +0100 Subject: Add generic state printing --- src/main/game_state.cpp | 53 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 11 deletions(-) (limited to 'src/main/game_state.cpp') diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp index 9d0349d..45bea21 100644 --- a/src/main/game_state.cpp +++ b/src/main/game_state.cpp @@ -16,7 +16,7 @@ using namespace rapidjson; using namespace std; // Construct an initial game state from a JSON doc -game_state::game_state(const Document& doc) { +game_state::game_state(const Document& doc) : rules("simple-black-hole") { // Construct tableau piles assert(doc.HasMember("tableau piles")); const Value& json_tab_piles = doc["tableau piles"]; @@ -43,7 +43,7 @@ game_state::game_state(const Document& doc) { } // Construct an initial game state from a seed -game_state::game_state(int seed, const sol_rules& rules) { +game_state::game_state(int seed, const sol_rules& rules) : rules(rules) { vector deck = shuffled_deck(seed, rules.max_rank); // Deal to the tableau piles (row-by-row) @@ -131,8 +131,8 @@ bool game_state::adjacent(const card& a, const card& b) const { return x == y + 1 || x + 1 == y - || (x == 1 && y == max_rank) - || (x == max_rank && y == 1); + || (x == 1 && y == rules.max_rank) + || (x == rules.max_rank && y == 1); } void game_state::move_to_hole(int tab_idx) { @@ -145,6 +145,40 @@ bool game_state::is_solved() const { } ostream& game_state::print(ostream& stream) const { + const char *sep = "------------------------------\n"; + + if (rules.foundations) { + print_header(stream, "Foundations"); + print_foundations(stream); + } + if (rules.tableau_pile_count > 0) { + print_header(stream, "Tableau Piles"); + print_tableau_piles(stream); + } + if (rules.hole) { + print_header(stream, "Hole Card"); + print_hole(stream); + } + return stream << "///////////////////////////////////\n\n"; +} + +void game_state::print_header(ostream& stream, const char* header) const { + stream << "--- " << header << " "; + int pad = 20 - strlen(header); + for (int i = 0; i < pad; i++) { + stream << '-'; + } + 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_tableau_piles(ostream& stream) const { bool empty_row = false; vector::size_type row_idx = 0; @@ -159,17 +193,14 @@ ostream& game_state::print(ostream& stream) const { stream << "\t"; } - if (!empty_row) stream << "|"; - if (row_idx == 0) { - stream << " Hole card: " << hole_card; - } - stream << "\n"; row_idx++; } +}; - return stream; -} +void game_state::print_hole(ostream& stream) const { + stream << hole_card << "\n"; +}; ostream& operator <<(ostream& stream, const game_state& gs) { return gs.print(stream); -- cgit v1.2.1 From c3e6f892599f145c14317e5712263b3e5562b785 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 28 Oct 2017 22:21:03 +0100 Subject: Add generic solving and pile class --- src/main/game_state.cpp | 115 ++++++++++++++++++++++++++++++------------------ 1 file changed, 73 insertions(+), 42 deletions(-) (limited to 'src/main/game_state.cpp') diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp index 45bea21..93ecb02 100644 --- a/src/main/game_state.cpp +++ b/src/main/game_state.cpp @@ -7,16 +7,23 @@ #include #include #include +#include #include #include "game_state.h" +#include "pile.h" using namespace rapidjson; using namespace std; +typedef sol_rules::build_order ord; +typedef sol_rules::build_policy pol; + // Construct an initial game state from a JSON doc -game_state::game_state(const Document& doc) : rules("simple-black-hole") { +game_state::game_state(const Document& doc) + : rules("simple-black-hole"), + hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank) { // Construct tableau piles assert(doc.HasMember("tableau piles")); const Value& json_tab_piles = doc["tableau piles"]; @@ -24,12 +31,12 @@ game_state::game_state(const Document& doc) : rules("simple-black-hole") { int max_rank = 1; for (auto& json_tab : json_tab_piles.GetArray()) { - vector tableau_pile; + pile tableau_pile(true, rules.build_order, pol::ANY_SUIT, false); for (auto& json_card : json_tab.GetArray()) { card c(json_card.GetString()); if (c.get_rank() > max_rank) max_rank = c.get_rank(); - tableau_pile.push_back(c); + tableau_pile.place(c); } tableau_piles.push_back(tableau_pile); @@ -39,11 +46,13 @@ game_state::game_state(const Document& doc) : rules("simple-black-hole") { assert(doc.HasMember("hole card")); const Value& json_h_card = doc["hole card"]; assert(json_h_card.IsString()); - hole_card = json_h_card.GetString(); + hole.place(json_h_card.GetString()); } // Construct an initial game state from a seed -game_state::game_state(int seed, const sol_rules& rules) : rules(rules) { +game_state::game_state(int seed, const sol_rules& rules) + : rules(rules), + hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank) { vector deck = shuffled_deck(seed, rules.max_rank); // Deal to the tableau piles (row-by-row) @@ -52,17 +61,26 @@ game_state::game_state(int seed, const sol_rules& rules) : rules(rules) { // If there is a hole card, don't deal the ace of spades if (rules.hole && c == "AS") { - hole_card = "AS"; + hole.place("AS"); continue; } // For the first row we must create all the tableau pile vectors if (i / rules.tableau_pile_count == 0) { - tableau_piles.push_back(vector()); + pile p(true, rules.build_order, pol::ANY_SUIT, false); + tableau_piles.push_back(p); } // Add the randomly generated card to the tableau piles - tableau_piles[i % rules.tableau_pile_count].push_back(c); + tableau_piles[i % rules.tableau_pile_count].place(c); + } + + // 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); + } } } @@ -93,38 +111,51 @@ vector game_state::shuffled_deck(int seed, int max_rank = 13) { return deck; } - vector 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 can_remove; + vector can_add; + + for (pile p : tableau_piles) { + can_remove.push_back(p); + if (rules.build_order != ord::NO_BUILD) { + can_add.push_back(p); + } + } + if (rules.foundations) { + for (pile p : foundations) { + can_remove.push_back(p); + can_add.push_back(p); + } + } + if (rules.hole) { + can_add.push_back(hole); + } + + // The next legal states vector next; - // Searches through each column for a top card that can be moved to the hole - bool all_tableaux_empty = true; - for (vector>::size_type i = 0; i < tableau_piles.size(); i++) { - vector tableau_pile = tableau_piles[i]; + for (pile &rem_pile : can_remove) { + if (rem_pile.empty()) continue; - // Ignore empty piles - if (tableau_pile.empty()) { - continue; - } else { - all_tableaux_empty = false; - } - card top_card = tableau_pile.back(); - - // If a card at the top of a tableau pile is adjacent to the hole card, - // move it to the hole and add that state - if (adjacent(top_card, hole_card)) { - game_state s = *this; - s.move_to_hole(i); - next.push_back(s); + 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 + } } } - // If there are no cards left in play, deal solved - solved = all_tableaux_empty; - return next; } + + bool game_state::adjacent(const card& a, const card& b) const { int x = a.get_rank(); int y = b.get_rank(); @@ -135,13 +166,13 @@ bool game_state::adjacent(const card& a, const card& b) const { || (x == rules.max_rank && y == 1); } -void game_state::move_to_hole(int tab_idx) { - hole_card = tableau_piles[tab_idx].back(); - tableau_piles[tab_idx].pop_back(); -} - bool game_state::is_solved() const { - return solved; + for (auto p : tableau_piles) { + if (!p.empty()) { + return false; + } + } + return true; } ostream& game_state::print(ostream& stream) const { @@ -172,10 +203,10 @@ void game_state::print_header(ostream& stream, const char* header) const { } void game_state::print_foundations(ostream& stream) const { - stream << foundations[0] << "\t" - << foundations[1] << "\t" - << foundations[2] << "\t" - << foundations[3] << "\n"; + stream << foundations[0].top_card() << "\t" + << foundations[1].top_card() << "\t" + << foundations[2].top_card() << "\t" + << foundations[3].top_card() << "\n"; }; void game_state::print_tableau_piles(ostream& stream) const { @@ -185,7 +216,7 @@ void game_state::print_tableau_piles(ostream& stream) const { while (!empty_row) { empty_row = true; - for (vector tableau_pile : tableau_piles) { + for (pile tableau_pile : tableau_piles) { if (tableau_pile.size() > row_idx) { empty_row = false; stream << tableau_pile[row_idx]; @@ -199,7 +230,7 @@ void game_state::print_tableau_piles(ostream& stream) const { }; void game_state::print_hole(ostream& stream) const { - stream << hole_card << "\n"; + stream << hole.top_card() << "\n"; }; ostream& operator <<(ostream& stream, const game_state& gs) { -- cgit v1.2.1 From 578f9c9f92d05b5907b91f07a4c732839d4dfca5 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 6 Nov 2017 14:14:38 +0000 Subject: Add counting of states searched --- src/main/game_state.cpp | 87 +++++++++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 47 deletions(-) (limited to 'src/main/game_state.cpp') diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp index 93ecb02..29ccdc3 100644 --- a/src/main/game_state.cpp +++ b/src/main/game_state.cpp @@ -31,7 +31,7 @@ game_state::game_state(const Document& doc) int max_rank = 1; for (auto& json_tab : json_tab_piles.GetArray()) { - pile tableau_pile(true, rules.build_order, pol::ANY_SUIT, false); + pile tableau_pile(true, rules.build_ord, pol::ANY_SUIT, false); for (auto& json_card : json_tab.GetArray()) { card c(json_card.GetString()); @@ -50,24 +50,24 @@ game_state::game_state(const Document& doc) } // Construct an initial game state from a seed -game_state::game_state(int seed, const sol_rules& rules) - : rules(rules), +game_state::game_state(int seed, const sol_rules& s_rules) + : rules(s_rules), hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank) { vector deck = shuffled_deck(seed, rules.max_rank); + // If there is a hole, move the ace to spades to it + if (rules.hole) { + deck.erase(find(begin(deck), end(deck), card("AS"))); + hole.place("AS"); + } + // Deal to the tableau piles (row-by-row) for (vector::size_type i = 0; i < deck.size(); i++) { card c = deck[i]; - // If there is a hole card, don't deal the ace of spades - if (rules.hole && c == "AS") { - hole.place("AS"); - continue; - } - // For the first row we must create all the tableau pile vectors if (i / rules.tableau_pile_count == 0) { - pile p(true, rules.build_order, pol::ANY_SUIT, false); + pile p(true, rules.build_ord, pol::ANY_SUIT, false); tableau_piles.push_back(p); } @@ -111,37 +111,37 @@ vector game_state::shuffled_deck(int seed, int max_rank = 13) { return deck; } -vector game_state::get_next_legal_states() const { +vector game_state::get_next_legal_states() { // Generates vectors of references to all the piles from which // cards can be removed, and to which they can be added - vector can_remove; - vector can_add; + vector can_remove; + vector can_add; - for (pile p : tableau_piles) { - can_remove.push_back(p); - if (rules.build_order != ord::NO_BUILD) { - can_add.push_back(p); + for (vector::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]); } } if (rules.foundations) { - for (pile p : foundations) { - can_remove.push_back(p); - can_add.push_back(p); + for (vector::size_type i = 0; i < foundations.size(); i++) { + can_remove.push_back(&foundations[i]); + can_add.push_back(&foundations[i]); } } if (rules.hole) { - can_add.push_back(hole); + can_add.push_back(&hole); } // The next legal states vector next; - for (pile &rem_pile : can_remove) { - if (rem_pile.empty()) continue; + for (pile *rem_pile : can_remove) { + if (rem_pile->empty()) continue; - for (pile &add_pile : can_add) { - if (&add_pile != &rem_pile - && add_pile.can_place(rem_pile.top_card())) { + 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; @@ -155,17 +155,6 @@ vector game_state::get_next_legal_states() const { } - -bool game_state::adjacent(const card& a, const card& b) const { - int x = a.get_rank(); - int y = b.get_rank(); - - return x == y + 1 - || x + 1 == y - || (x == 1 && y == rules.max_rank) - || (x == rules.max_rank && y == 1); -} - bool game_state::is_solved() const { for (auto p : tableau_piles) { if (!p.empty()) { @@ -176,8 +165,6 @@ bool game_state::is_solved() const { } ostream& game_state::print(ostream& stream) const { - const char *sep = "------------------------------\n"; - if (rules.foundations) { print_header(stream, "Foundations"); print_foundations(stream); @@ -190,7 +177,7 @@ ostream& game_state::print(ostream& stream) const { print_header(stream, "Hole Card"); print_hole(stream); } - return stream << "///////////////////////////////////\n\n"; + return stream << "===================================\n"; } void game_state::print_header(ostream& stream, const char* header) const { @@ -203,11 +190,11 @@ void game_state::print_header(ostream& stream, const char* header) const { } void game_state::print_foundations(ostream& stream) const { - stream << foundations[0].top_card() << "\t" - << foundations[1].top_card() << "\t" - << foundations[2].top_card() << "\t" - << foundations[3].top_card() << "\n"; -}; + stream << foundations[0] << "\t" + << foundations[1] << "\t" + << foundations[2] << "\t" + << foundations[3] << "\n"; +} void game_state::print_tableau_piles(ostream& stream) const { bool empty_row = false; @@ -227,11 +214,17 @@ void game_state::print_tableau_piles(ostream& stream) const { stream << "\n"; row_idx++; } -}; +} void game_state::print_hole(ostream& stream) const { stream << hole.top_card() << "\n"; -}; +} + +bool operator==(const game_state& a, const game_state& b) { + return a.tableau_piles == b.tableau_piles + && a.foundations == b.foundations + && a.hole == b.hole; +} ostream& operator <<(ostream& stream, const game_state& gs) { return gs.print(stream); -- cgit v1.2.1