From f56486a83a03ab58fd3f455258de4caebe239322 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 2 Jan 2018 11:29:16 +0000 Subject: Add empty pile logic for bakers dozen --- src/main/game/game_state.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/main/game/game_state.cpp') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index caabb18..319bf5d 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -20,6 +20,7 @@ using namespace boost; typedef sol_rules::build_order ord; typedef sol_rules::build_policy pol; +typedef sol_rules::spaces_policy s_pol; // A private constructor used by both of the public ones. Initializes all of the // piles and pile refs specified by the rules @@ -28,7 +29,8 @@ game_state::game_state(const sol_rules& s_rules) : // Creates the tableau piles for (uint8_t i = 0; i < rules.tableau_pile_count; i++) { - piles.push_back(pile::tableau_factory(rules.build_ord, rules.build_pol)); + piles.push_back(pile::tableau_factory(rules.build_ord, rules.build_pol, + rules.spaces_pol)); tableau_piles.push_back(static_cast(piles.size() - 1)); } -- cgit v1.2.1 From 77a7aeba7854d15b140cc6dc3c69b15ed5f3b936 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 12:30:14 +0000 Subject: Major refactor, putting pile logic into game_state Much of the logic that was in the pile class was beginning to seem out of place. A lot of the rules have fairly arbitrary interactons *between* piles, which weren't best represented in the pile classes themselves, which were getting unweildy and inefficient. Hence, much of this logic has been moved to the game state class. --- src/main/game/game_state.cpp | 246 ++++++++++++++++++++++++++++++------------- 1 file changed, 172 insertions(+), 74 deletions(-) (limited to 'src/main/game/game_state.cpp') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 319bf5d..14867f8 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -22,62 +22,53 @@ typedef sol_rules::build_order ord; typedef sol_rules::build_policy pol; typedef sol_rules::spaces_policy s_pol; + +////////////////// +// CONSTRUCTORS // +////////////////// + // 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) { + rules(s_rules), + stock(PILE_REF_MAX), + waste(PILE_REF_MAX), + hole(PILE_REF_MAX) { - // Creates the tableau piles - for (uint8_t i = 0; i < rules.tableau_pile_count; i++) { - piles.push_back(pile::tableau_factory(rules.build_ord, rules.build_pol, - rules.spaces_pol)); - tableau_piles.push_back(static_cast(piles.size() - 1)); + // If there is a hole, creates pile + if (rules.hole) { + piles.emplace_back(); + hole = static_cast(piles.size() - 1); } // If there are foundation piles, creates the relevant pile vectors if (rules.foundations) { for (uint8_t i = 0; i < 4; i++) { - piles.push_back(pile::foundation_factory(pol(i))); + piles.emplace_back(); foundations.push_back(static_cast(piles.size() - 1)); } } - // If there are cell piles, creates the relevant cell vectors - if (rules.cells > 0) { - for (uint8_t i = 0; i < rules.cells; i++) { - piles.push_back(pile::cell_factory()); - cells.push_back(static_cast(piles.size() - 1)); - } - } - - // If there is a reserve, creates a reserve pile - if (rules.reserve_size > 0) { - piles.push_back(pile::reserve_factory()); - reserve = static_cast(piles.size() - 1); - } - // If there is a stock & waste, creates piles if (rules.stock_size > 0) { - piles.push_back(pile::stock_factory()); + piles.emplace_back(); stock = static_cast(piles.size() - 1); - piles.push_back(pile::waste_factory()); + piles.emplace_back(); waste = static_cast(piles.size() - 1); } - if (rules.hole) { - piles.push_back(pile::hole_factory(s_rules.max_rank)); - hole = static_cast(piles.size() - 1); + // If there are cell piles, creates the relevant cell vectors + if (rules.cells > 0) { + for (uint8_t i = 0; i < rules.cells; i++) { + piles.emplace_back(); + cells.push_back(static_cast(piles.size() - 1)); + } } - for (pile_ref pr = 0; pr < piles.size(); pr++) { - if (rules.stock_size > 0 && pr == waste) continue; - - if (piles[pr].can_remove()) { - removable_piles.emplace_back(pr); - } - if (piles[pr].get_build_order() != ord::NO_BUILD) { - addable_piles.emplace_back(pr); - } + // Creates the tableau piles + for (uint8_t i = 0; i < rules.tableau_pile_count; i++) { + piles.emplace_back(); + tableau_piles.push_back(static_cast(piles.size() - 1)); } } @@ -96,7 +87,7 @@ game_state::game_state(const sol_rules& s_rules, int seed) : // If there is a hole, moves the ace of spades to it if (rules.hole) { deck.erase(find(begin(deck), end(deck), card("AS"))); - piles[hole].place(card("AS")); + piles[hole].place("AS"); } // If there is a stock, deals to it and set up a waste pile too @@ -107,14 +98,6 @@ game_state::game_state(const sol_rules& s_rules, int seed) : } } - // If there is a reserve, deals to it - if (rules.reserve_size > 0) { - for (unsigned int i = 0; i < rules.reserve_size; i++) { - piles[reserve].place(deck.back()); - deck.pop_back(); - } - } - // Deals to the tableau piles (row-by-row) for (int t = 0; !deck.empty(); t++) { card c = deck.back(); @@ -147,16 +130,16 @@ vector game_state::gen_shuffled_deck(int seed, int max_rank = 13) { card::suit_t s; switch ((*i) / max_rank) { case 0 : s = card::suit_t::Clubs; - break; + break; case 1 : s = card::suit_t::Diamonds; - break; + break; case 2 : s = card::suit_t::Hearts; - break; + break; case 3 : s = card::suit_t::Spades; - break; + break; default : assert(false); - s = card::suit_t::Clubs; - break; + s = card::suit_t::Clubs; + break; } deck.emplace_back(card(s, r)); @@ -165,6 +148,11 @@ vector game_state::gen_shuffled_deck(int seed, int max_rank = 13) { return deck; } + +///////////////////////// +// MODIFYING FUNCTIONS // +///////////////////////// + void game_state::make_move(const move m) { piles[m.second].place(piles[m.first].take()); } @@ -173,33 +161,140 @@ void game_state::undo_move(const move m) { piles[m.first].place(piles[m.second].take()); } + +/////////////////////////// +// LEGAL MOVE GENERATION // +/////////////////////////// + vector game_state::get_legal_moves() const { - // The next legal states + // The next legal moves vector moves; - // Dealing from the stock to the waste - if (rules.stock_size > 0 && !piles[stock].empty()) { - 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, the waste, or an empty pile + if (rem_ref == hole + || rem_ref == waste + || piles[rem_ref].empty()) continue; + + // Stock cards can only be moved to the waste + if (stock != PILE_REF_MAX && rem_ref == stock) { + moves.emplace_back(stock, waste); + continue; + } - // Moving from and to the 'can remove' and 'can add' piles - // Note traversal order. Solver evaluates backwards, so we reverse traversal - // order of removable piles, but not addable. This moves from tableau piles - // to foundations first if possible - for (auto rem_it = removable_piles.rbegin(); rem_it != removable_piles.rend(); ++rem_it) { - if (piles[*rem_it].empty()) continue; + // 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); + } + } - for (const pile_ref add_pile : addable_piles) { - if (add_pile != *rem_it && - piles[add_pile].can_place(piles[*rem_it].top_card())) { - moves.emplace_back(*rem_it, add_pile); + // 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); } } + + // Any card can be added to the waste + if (waste != PILE_REF_MAX && waste != rem_ref) { + moves.emplace_back(rem_ref, waste); + } + + // 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 + if (hole != PILE_REF_MAX && is_valid_hole_move(rem_ref)) { + moves.emplace_back(rem_ref, hole); + } } return moves; } +// 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 { + if (rem_ref == add_ref) return false; + + else if (rules.build_pol == pol::NO_BUILD) return false; + + else if (piles[add_ref].empty()) { + return rules.spaces_pol == s_pol::ANY; + } + + card rem_c = piles[rem_ref].top_card(); + card add_c = piles[add_ref].top_card(); + + // Checks violation of same suit policy + if (rules.build_pol == pol::SAME_SUIT + && rem_c.get_suit() != add_c.get_suit()) { + return false; + } + + // Checks violation of red-black suit policy + else if (rules.build_pol == pol::RED_BLACK + && rem_c.get_colour() == add_c.get_colour()) { + return false; + } + + // Checks rank + else if (rules.build_ord == ord::DESCENDING + && rem_c.get_rank() + 1 != add_c.get_rank()) { + return false; + } else if (rules.build_ord == ord::ASCENDING + && rem_c.get_rank() - 1 != add_c.get_rank()) { + return false; + } else { + return true; + } +} + +bool game_state::is_valid_foundations_move(const pile_ref rem_ref, + const pile_ref add_ref) const { + if (rem_ref == add_ref) return false; + + card rem_c = piles[rem_ref].top_card(); + + // Checks violation of same suit policy + if (rem_c.get_suit() != card::to_suit(add_ref - foundations.front())) { + return false; + } + + // Checks rank + else { + card::rank_t add_rank = piles[add_ref].empty() + ? card::rank_t(0) + : piles[add_ref].top_card().get_rank(); + return rem_c.get_rank() - 1 == add_rank; + } +} + +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(); + return rank + 1 == hole_rank + || rank - 1 == hole_rank + || (rank == rules.max_rank && hole_rank == 1) + || (rank == 1 && hole_rank == rules.max_rank); +} + + +//////////////////////// +// INSPECT GAME STATE // +//////////////////////// + bool game_state::is_solved() const { for (auto f : foundations) { if (piles[f].size() != rules.max_rank) { @@ -214,6 +309,11 @@ const vector& game_state::get_data() const { return piles; } + +////////////// +// PRINTING // +////////////// + ostream& game_state::print(ostream& stream) const { if (rules.foundations) { print_header(stream, "Foundations"); @@ -223,10 +323,6 @@ ostream& game_state::print(ostream& stream) const { print_header(stream, "Cells"); print_piles(stream, cells); } - if (rules.reserve_size > 0) { - print_header(stream, "Reserve"); - print_pile(stream, reserve); - } if (rules.tableau_pile_count > 0) { print_header(stream, "Tableau Piles"); print_piles(stream, tableau_piles); @@ -307,14 +403,19 @@ void game_state::print_top_of_pile(ostream& stream, const pile_ref pile_r) const 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; + && a.foundations == b.foundations + && a.hole == b.hole; } ostream& operator <<(ostream& stream, const game_state& gs) { return gs.print(stream); } + +//////////////////// +// HASH FUNCTIONS // +//////////////////// + size_t hash_value(game_state const& gs) { size_t seed = 0; @@ -328,9 +429,6 @@ size_t hash_value(game_state const& gs) { hash_combine(seed, gs.tableau_piles); } - if (gs.rules.reserve_size > 0) { - hash_combine(seed, gs.piles[gs.reserve]); - } if (gs.rules.stock_size > 0) { hash_combine(seed, gs.piles[gs.stock]); hash_combine(seed, gs.piles[gs.waste]); -- cgit v1.2.1 From c70a063f407534ac3d8997b9f1bc58d23c9360b8 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 13:03:26 +0000 Subject: Tidy up card class --- src/main/game/game_state.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/game/game_state.cpp') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 14867f8..b45cc11 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -87,7 +87,7 @@ game_state::game_state(const sol_rules& s_rules, int seed) : // If there is a hole, moves the ace of spades to it if (rules.hole) { deck.erase(find(begin(deck), end(deck), card("AS"))); - piles[hole].place("AS"); + piles[hole].place(card("AS")); } // If there is a stock, deals to it and set up a waste pile too -- cgit v1.2.1 From 7f4712b292632becfc83bc0e887271b25fefd21a Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 13:05:31 +0000 Subject: Refactor io dir structure --- src/main/game/game_state.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/game/game_state.cpp') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index b45cc11..78a5253 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -12,7 +12,7 @@ #include #include "game_state.h" -#include "../input-output/deal_parser.h" +#include "../input-output/input/json-parsing/deal_parser.h" using namespace std; using namespace rapidjson; -- cgit v1.2.1 From 4208d2570e74ab54d1878c72110bd380e7b197f2 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 15:21:47 +0000 Subject: Move game state printing into dedicated class --- src/main/game/game_state.cpp | 106 ++++--------------------------------------- 1 file changed, 10 insertions(+), 96 deletions(-) (limited to 'src/main/game/game_state.cpp') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 78a5253..1a7310d 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -13,6 +13,7 @@ #include "game_state.h" #include "../input-output/input/json-parsing/deal_parser.h" +#include "../input-output/output/state_printer.h" using namespace std; using namespace rapidjson; @@ -309,108 +310,12 @@ const vector& game_state::get_data() const { return piles; } - -////////////// -// PRINTING // -////////////// - -ostream& game_state::print(ostream& stream) const { - if (rules.foundations) { - print_header(stream, "Foundations"); - print_top_of_piles(stream, foundations); - } - if (rules.cells) { - print_header(stream, "Cells"); - print_piles(stream, cells); - } - if (rules.tableau_pile_count > 0) { - print_header(stream, "Tableau Piles"); - print_piles(stream, tableau_piles); - } - if (rules.stock_size > 0) { - print_header(stream, "Stock | Waste"); - print_piles(stream, {stock, waste}); - } - if (rules.hole) { - print_header(stream, "Hole Card"); - print_top_of_pile(stream, hole); - } - return stream << "==================================="; -} - -void game_state::print_header(ostream& stream, const char* header) const { - stream << "--- " << header << " "; - size_t pad = 20 - strlen(header); - for (size_t i = 0; i < pad; i++) { - stream << '-'; - } - stream << "\n"; -} - -void game_state::print_piles(ostream& stream, const vector& pile_rs) const { - bool empty_row = false; - vector::size_type row_idx = 0; - - // Loops through the rows in each pile, starting from the bottom, until - // one is empty - while (!empty_row) { - // Loops through the current row to determine if it is empty - empty_row = true; - for (const pile_ref pile_r : pile_rs) { - if (piles[pile_r].size() > row_idx) { - empty_row = false; - break; - } - } - if (!empty_row || row_idx == 0) { - // Loops through the current (non-empty) row, and outputs the values - for (const pile_ref pile_r : pile_rs) { - if (piles[pile_r].size() > row_idx) { - stream << piles[pile_r][row_idx]; - } else if (row_idx == 0) { - stream << "[]"; - } - stream << "\t"; - } - - stream << "\n"; - row_idx++; - } - } -} - -void game_state::print_pile(ostream& stream, const pile_ref pile_r) const { - print_piles(stream, {pile_r}); -} - -void game_state::print_top_of_piles(ostream& stream, const vector& vp) const { - vector top(vp); - - for (pile_ref p : top) { - if (piles[p].empty()) { - stream << "[]"; - } else { - stream << piles[p].top_card(); - } - stream << "\t"; - } - stream << "\n"; -} - -void game_state::print_top_of_pile(ostream& stream, const pile_ref pile_r) const { - print_top_of_piles(stream, {pile_r}); -} - 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); -} - //////////////////// // HASH FUNCTIONS // @@ -443,3 +348,12 @@ size_t hash_value(game_state const& gs) { size_t hash_value(vector const& vp) { return hash_range(begin(vp), end(vp)); } + + +/////////// +// PRINT // +/////////// + +ostream& operator<< (ostream& str, const game_state& gs) { + return state_printer::print(str, gs); +} -- cgit v1.2.1 From 965de2889e866e481749f73fe638bd160a793da4 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 15:41:34 +0000 Subject: Move game_state legal move gen into dedicated file --- src/main/game/game_state.cpp | 130 ------------------------------------------- 1 file changed, 130 deletions(-) (limited to 'src/main/game/game_state.cpp') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 1a7310d..11e74ff 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -82,7 +82,6 @@ game_state::game_state(const sol_rules& s_rules, const Document& doc) : // Constructs an initial game state from a seed game_state::game_state(const sol_rules& s_rules, int seed) : game_state(s_rules) { - vector deck = gen_shuffled_deck(seed, rules.max_rank); // If there is a hole, moves the ace of spades to it @@ -163,135 +162,6 @@ void game_state::undo_move(const move m) { } -/////////////////////////// -// LEGAL MOVE GENERATION // -/////////////////////////// - -vector game_state::get_legal_moves() const { - // The next legal moves - vector moves; - - // 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 - || rem_ref == waste - || piles[rem_ref].empty()) continue; - - // Stock cards can only be moved to the waste - if (stock != PILE_REF_MAX && rem_ref == stock) { - moves.emplace_back(stock, waste); - continue; - } - - // 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 - for (auto add_ref : cells) { - if (add_ref != rem_ref && piles[add_ref].empty()) { - moves.emplace_back(rem_ref, add_ref); - } - } - - // Any card can be added to the waste - if (waste != PILE_REF_MAX && waste != rem_ref) { - moves.emplace_back(rem_ref, waste); - } - - // 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 - if (hole != PILE_REF_MAX && is_valid_hole_move(rem_ref)) { - moves.emplace_back(rem_ref, hole); - } - } - - return moves; -} - -// 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 { - if (rem_ref == add_ref) return false; - - else if (rules.build_pol == pol::NO_BUILD) return false; - - else if (piles[add_ref].empty()) { - return rules.spaces_pol == s_pol::ANY; - } - - card rem_c = piles[rem_ref].top_card(); - card add_c = piles[add_ref].top_card(); - - // Checks violation of same suit policy - if (rules.build_pol == pol::SAME_SUIT - && rem_c.get_suit() != add_c.get_suit()) { - return false; - } - - // Checks violation of red-black suit policy - else if (rules.build_pol == pol::RED_BLACK - && rem_c.get_colour() == add_c.get_colour()) { - return false; - } - - // Checks rank - else if (rules.build_ord == ord::DESCENDING - && rem_c.get_rank() + 1 != add_c.get_rank()) { - return false; - } else if (rules.build_ord == ord::ASCENDING - && rem_c.get_rank() - 1 != add_c.get_rank()) { - return false; - } else { - return true; - } -} - -bool game_state::is_valid_foundations_move(const pile_ref rem_ref, - const pile_ref add_ref) const { - if (rem_ref == add_ref) return false; - - card rem_c = piles[rem_ref].top_card(); - - // Checks violation of same suit policy - if (rem_c.get_suit() != card::to_suit(add_ref - foundations.front())) { - return false; - } - - // Checks rank - else { - card::rank_t add_rank = piles[add_ref].empty() - ? card::rank_t(0) - : piles[add_ref].top_card().get_rank(); - return rem_c.get_rank() - 1 == add_rank; - } -} - -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(); - return rank + 1 == hole_rank - || rank - 1 == hole_rank - || (rank == rules.max_rank && hole_rank == 1) - || (rank == 1 && hole_rank == rules.max_rank); -} - - //////////////////////// // INSPECT GAME STATE // //////////////////////// -- cgit v1.2.1 From 03178af360824a22d9b6487705f40ee1ca32acce Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 11:47:01 +0000 Subject: Update random deal gen with foundations init cards --- src/main/game/game_state.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'src/main/game/game_state.cpp') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 11e74ff..666b970 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -90,6 +90,16 @@ game_state::game_state(const sol_rules& s_rules, int seed) : piles[hole].place(card("AS")); } + // If the foundations begin filled, then fills them + if (rules.foundations_init_card) { + for (uint8_t f_idx = 0; f_idx < 4; f_idx++) { + card c = card(card::to_suit(f_idx), 1); + + deck.erase(find(begin(deck), end(deck), c)); + piles[foundations[f_idx]].place(c); + } + } + // If there is a stock, deals to it and set up a waste pile too if (rules.stock_size > 0) { for (unsigned int i = 0; i < rules.stock_size; i++) { @@ -167,13 +177,16 @@ void game_state::undo_move(const move m) { //////////////////////// bool game_state::is_solved() const { - for (auto f : foundations) { - if (piles[f].size() != rules.max_rank) { - return false; + if (rules.hole) { + return piles[hole].size() == rules.max_rank * 4; + } else { + for (auto f : foundations) { + if (piles[f].size() != rules.max_rank) { + return false; + } } + return true; } - - return !rules.hole || piles[hole].size() == rules.max_rank * 4; } const vector& game_state::get_data() const { -- cgit v1.2.1 From 4161e60c9d332826129efddd5dabc60e3393057d Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 15:47:00 +0000 Subject: Create game state setup for flower garden --- src/main/game/game_state.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'src/main/game/game_state.cpp') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 666b970..841e213 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -50,6 +50,14 @@ game_state::game_state(const sol_rules& s_rules) : } } + // If there are cell piles, creates the relevant cell vectors + if (rules.cells > 0) { + for (uint8_t i = 0; i < rules.cells; i++) { + piles.emplace_back(); + cells.push_back(static_cast(piles.size() - 1)); + } + } + // If there is a stock & waste, creates piles if (rules.stock_size > 0) { piles.emplace_back(); @@ -58,11 +66,11 @@ game_state::game_state(const sol_rules& s_rules) : waste = static_cast(piles.size() - 1); } - // If there are cell piles, creates the relevant cell vectors - if (rules.cells > 0) { - for (uint8_t i = 0; i < rules.cells; i++) { + // If there is a reserve, creates piles + if (rules.reserve_size > 0) { + for (uint8_t i = 0; i < rules.reserve_size; i++) { piles.emplace_back(); - cells.push_back(static_cast(piles.size() - 1)); + reserve.push_back(static_cast(piles.size() - 1)); } } @@ -108,6 +116,14 @@ game_state::game_state(const sol_rules& s_rules, int seed) : } } + // If there is a reserve, deals to it + if (rules.reserve_size > 0) { + for (unsigned int i = 0; i < rules.reserve_size; i++) { + piles[reserve[i]].place(deck.back()); + deck.pop_back(); + } + } + // Deals to the tableau piles (row-by-row) for (int t = 0; !deck.empty(); t++) { card c = deck.back(); -- cgit v1.2.1 From 6ff8eb79720d76110f2477c07ab95eac167d0389 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 15:56:18 +0000 Subject: Add game state setup for stacked reserve (canfield) --- src/main/game/game_state.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/main/game/game_state.cpp') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 841e213..8da5bac 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -68,7 +68,9 @@ game_state::game_state(const sol_rules& s_rules) : // If there is a reserve, creates piles if (rules.reserve_size > 0) { - for (uint8_t i = 0; i < rules.reserve_size; i++) { + uint8_t pile_count = rules.reserve_stacked ? + uint8_t(1) : rules.reserve_size; + for (uint8_t i = 0; i < pile_count; i++) { piles.emplace_back(); reserve.push_back(static_cast(piles.size() - 1)); } @@ -116,10 +118,13 @@ game_state::game_state(const sol_rules& s_rules, int seed) : } } - // If there is a reserve, deals to it + // If there is a reserve, deals to it. + // We treat a regular reserve like multiple single-card piles, + // but a stacked reserve as a single multiple-card pile. if (rules.reserve_size > 0) { for (unsigned int i = 0; i < rules.reserve_size; i++) { - piles[reserve[i]].place(deck.back()); + int idx = rules.reserve_stacked ? 0 : i; + piles[reserve[idx]].place(deck.back()); deck.pop_back(); } } -- cgit v1.2.1 From 1ffa7dcf0c8df73d5b2affe8066c1cbeb324538e Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 18:03:29 +0000 Subject: Add diagonal deal to game setup (somerset) --- src/main/game/game_state.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'src/main/game/game_state.cpp') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 8da5bac..83b9672 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -132,11 +132,26 @@ game_state::game_state(const sol_rules& s_rules, int seed) : // Deals to the tableau piles (row-by-row) for (int t = 0; !deck.empty(); t++) { card c = deck.back(); - deck.pop_back(); // Add the randomly generated card to the tableau piles - pile_ref tableau_pile = tableau_piles[t % tableau_piles.size()]; - piles[tableau_pile].place(c); + auto p = t % tableau_piles.size(); + + // If we are doing a diagonal deal, each row should have one fewer card. + // Leftover cards are dealt normally in full rows. + auto row_idx = t / tableau_piles.size(); + if (rules.diagonal_deal && row_idx < tableau_piles.size()) { + p = tableau_piles.size()-p-1; + pile_ref tableau_pile = tableau_piles[p]; + + if (p >= row_idx) { + piles[tableau_pile].place(c); + deck.pop_back(); + } + } else { + pile_ref tableau_pile = tableau_piles[p]; + piles[tableau_pile].place(c); + deck.pop_back(); + } } } -- cgit v1.2.1 From cb9217a4675d11c300bc704aadf3af7a1809b68c Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 8 Jan 2018 19:01:07 +0000 Subject: Add game setup for built pile (AS) --- src/main/game/game_state.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) (limited to 'src/main/game/game_state.cpp') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 83b9672..ee02889 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -194,17 +194,48 @@ vector game_state::gen_shuffled_deck(int seed, int max_rank = 13) { return deck; } +game_state::move::move(pile_ref f, pile_ref t, pile::size_type i) + : from(f), to(t), count(i) { + assert(i >= 1); +} + ///////////////////////// // MODIFYING FUNCTIONS // ///////////////////////// void game_state::make_move(const move m) { - piles[m.second].place(piles[m.first].take()); + // If this is not a built-pile move + if (m.count == 1) { + piles[m.to].place(piles[m.from].take()); + } else { + // Adds the cards to the 'to' pile + for (auto pile_idx = m.count; pile_idx-- > 0;) { + piles[m.to].place(piles[m.from][pile_idx]); + } + + // Removes the cards from the 'from' pile + for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) { + piles[m.from].take(); + } + } } void game_state::undo_move(const move m) { - piles[m.first].place(piles[m.second].take()); + // If this is not a built-pile move + if (m.count == 1) { + piles[m.from].place(piles[m.to].take()); + } else { + // Adds the cards to the 'from' pile + for (auto pile_idx = m.count; pile_idx-- > 0;) { + piles[m.from].place(piles[m.to][pile_idx]); + } + + // Removes the cards from the 'to' pile + for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) { + piles[m.to].take(); + } + } } @@ -236,6 +267,17 @@ bool operator==(const game_state& a, const game_state& b) { } +////////////////////////////////// +// STATIC VARIABLES AND MEMBERS // +////////////////////////////////// + +game_state::pile_ref game_state::PILE_REF_MAX = 255; + +game_state::move game_state::null_move() { + return {PILE_REF_MAX, 0, PILE_REF_MAX}; +} + + //////////////////// // HASH FUNCTIONS // //////////////////// -- cgit v1.2.1 From b2edd1a2352ac4ded59f4c91579ab80eeba4db96 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 9 Jan 2018 08:54:12 +0000 Subject: Add game state setup for stock-tableau & 2 deck --- src/main/game/game_state.cpp | 53 +++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 15 deletions(-) (limited to 'src/main/game/game_state.cpp') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index ee02889..76e6488 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -14,14 +14,14 @@ #include "game_state.h" #include "../input-output/input/json-parsing/deal_parser.h" #include "../input-output/output/state_printer.h" +#include "sol_rules.h" using namespace std; using namespace rapidjson; using namespace boost; -typedef sol_rules::build_order ord; typedef sol_rules::build_policy pol; -typedef sol_rules::spaces_policy s_pol; +typedef sol_rules::stock_deal_type sdt; ////////////////// @@ -44,7 +44,7 @@ game_state::game_state(const sol_rules& s_rules) : // If there are foundation piles, creates the relevant pile vectors if (rules.foundations) { - for (uint8_t i = 0; i < 4; i++) { + for (uint8_t i = 0; i < 4*(rules.two_decks ? 2:1); i++) { piles.emplace_back(); foundations.push_back(static_cast(piles.size() - 1)); } @@ -58,12 +58,16 @@ game_state::game_state(const sol_rules& s_rules) : } } - // If there is a stock & waste, creates piles + // If there is a stock, creates a pile if (rules.stock_size > 0) { piles.emplace_back(); stock = static_cast(piles.size() - 1); - piles.emplace_back(); - waste = static_cast(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(piles.size() - 1); + } } // If there is a reserve, creates piles @@ -92,7 +96,7 @@ game_state::game_state(const sol_rules& s_rules, const Document& doc) : // Constructs an initial game state from a seed game_state::game_state(const sol_rules& s_rules, int seed) : game_state(s_rules) { - vector deck = gen_shuffled_deck(seed, rules.max_rank); + vector deck = gen_shuffled_deck(seed, rules.max_rank, rules.two_decks); // If there is a hole, moves the ace of spades to it if (rules.hole) { @@ -102,8 +106,8 @@ game_state::game_state(const sol_rules& s_rules, int seed) : // If the foundations begin filled, then fills them if (rules.foundations_init_card) { - for (uint8_t f_idx = 0; f_idx < 4; f_idx++) { - card c = card(card::to_suit(f_idx), 1); + for (uint8_t f_idx = 0; f_idx < 4*(rules.two_decks ? 2:1); f_idx++) { + card c = card(card::to_suit(f_idx % uint8_t(4)), 1); deck.erase(find(begin(deck), end(deck), c)); piles[foundations[f_idx]].place(c); @@ -156,14 +160,16 @@ game_state::game_state(const sol_rules& s_rules, int seed) : } // Generates a randomly ordered vector of cards -vector game_state::gen_shuffled_deck(int seed, int max_rank = 13) { +vector game_state::gen_shuffled_deck(int seed, int max_rank, + bool two_decks) { vector values; vector v_ptrs; for (int i = 0; i < max_rank * 4; i++) { values.emplace_back(i); + if (two_decks) values.emplace_back(i); } - for (int i = 0; i < max_rank * 4; i++) { - v_ptrs.emplace_back(&values[i]); + for (auto& v : values) { + v_ptrs.emplace_back(&v); } // Randomly shuffle the pointers @@ -205,8 +211,16 @@ game_state::move::move(pile_ref f, pile_ref t, pile::size_type i) ///////////////////////// 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 = tableau_piles.front(); + tab_pr < tableau_piles.front() + m.count; + tab_pr++) { + piles[tab_pr].place(piles[stock].take()); + } + } // If this is not a built-pile move - if (m.count == 1) { + else if (m.count == 1) { piles[m.to].place(piles[m.from].take()); } else { // Adds the cards to the 'to' pile @@ -222,8 +236,16 @@ void game_state::make_move(const move m) { } void game_state::undo_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 = tableau_piles.front() + m.count; + tab_pr-- > tableau_piles.front(); + ) { + piles[stock].place(piles[tab_pr].take()); + } + } // If this is not a built-pile move - if (m.count == 1) { + else if (m.count == 1) { piles[m.from].place(piles[m.to].take()); } else { // Adds the cards to the 'from' pile @@ -245,7 +267,8 @@ void game_state::undo_move(const move m) { bool game_state::is_solved() const { if (rules.hole) { - return piles[hole].size() == rules.max_rank * 4; + return piles[hole].size() + == rules.max_rank * 4 * (rules.two_decks ? 2 : 1); } else { for (auto f : foundations) { if (piles[f].size() != rules.max_rank) { -- cgit v1.2.1