From 213a17d3628e8af86cad1288579b651e09cab3cc Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 7 Nov 2017 10:03:06 +0000 Subject: Add Canfield cmdln arg and enum --- src/main/sol_rules.cpp | 4 +++- src/main/sol_rules.h | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'src/main') diff --git a/src/main/sol_rules.cpp b/src/main/sol_rules.cpp index b98e96d..7010af1 100644 --- a/src/main/sol_rules.cpp +++ b/src/main/sol_rules.cpp @@ -21,7 +21,9 @@ vector sol_rules::valid_sol_strs = { "spanish-patience", "simple-spanish-patience", "free-cell", - "simple-free-cell" + "simple-free-cell", + "canfield", + "simple-canfield" }; bool sol_rules::is_suit(pol bp) { diff --git a/src/main/sol_rules.h b/src/main/sol_rules.h index 5699ef1..26eae2e 100644 --- a/src/main/sol_rules.h +++ b/src/main/sol_rules.h @@ -16,7 +16,9 @@ public: SPANISH_PATIENCE, SIMPLE_SPANISH_PATIENCE, FREE_CELL, - SIMPLE_FREE_CELL + SIMPLE_FREE_CELL, + CANFIELD, + SIMPLE_CANFIELD }; enum class build_order { NO_BUILD, -- cgit v1.2.1 From 31d05fec2003736e20d05a16c8abdebcd5d5fbf3 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 7 Nov 2017 10:19:41 +0000 Subject: Add Canfield game rules --- src/main/sol_rules.cpp | 16 ++++++++++++++++ src/main/sol_rules.h | 6 +++++- 2 files changed, 21 insertions(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/sol_rules.cpp b/src/main/sol_rules.cpp index 7010af1..b3b5457 100644 --- a/src/main/sol_rules.cpp +++ b/src/main/sol_rules.cpp @@ -46,10 +46,13 @@ sol_rules::sol_rules(std::string sol_type) { // Default game values build_ord = build_order::DESCENDING; + build_pol = build_policy::ANY_SUIT; max_rank = 13; hole = false; foundations = true; cells = 0; + reserve_size = 0; + stock_size = 0; // Game-specific values switch(vs) { @@ -83,6 +86,19 @@ sol_rules::sol_rules(std::string sol_type) { max_rank = 3; cells = 1; break; + case valid_sol::CANFIELD: + tableau_pile_count = 4; + build_pol = build_policy::RED_BLACK; + reserve_size = 14; + stock_size = 34; + break; + case valid_sol::SIMPLE_CANFIELD: + tableau_pile_count = 3; + build_pol = build_policy::RED_BLACK; + max_rank = 4; + reserve_size = 3; + stock_size = 9; + break; default: assert(false); } diff --git a/src/main/sol_rules.h b/src/main/sol_rules.h index 26eae2e..aadda69 100644 --- a/src/main/sol_rules.h +++ b/src/main/sol_rules.h @@ -33,7 +33,8 @@ public: HEARTS, SPADES, ANY_SUIT, - SAME_SUIT + SAME_SUIT, + RED_BLACK }; static std::vector valid_sol_strs; @@ -44,10 +45,13 @@ public: int tableau_pile_count; build_order build_ord; + build_policy build_pol; int max_rank; bool hole; bool foundations; int cells; + int reserve_size; + int stock_size; private: static valid_sol valid_sol_enum(const std::string&); -- cgit v1.2.1 From d3bc9ce404a32029d6602549b13c9b97e5963105 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 7 Nov 2017 10:38:00 +0000 Subject: Amend random deal gen w/ stock,waste,reserve --- src/main/game_state.cpp | 34 +++++++++++++++++++++++++++++++--- src/main/game_state.h | 5 ++++- 2 files changed, 35 insertions(+), 4 deletions(-) (limited to 'src/main') diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp index 05bfb4f..80f3cce 100644 --- a/src/main/game_state.cpp +++ b/src/main/game_state.cpp @@ -51,19 +51,45 @@ 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), - hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank) { + : rules(s_rules) { + vector 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"); } + 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 (int i = 0; i < rules.stock_size; i++) { + stock.place(deck[i + deckCardsUsed]); + deckCardsUsed++; + } + } + + // If there is a reserve, deal to it + if (rules.reserve_size > 0) { + reserve = pile(true, ord::NO_BUILD, pol::ANY_SUIT, false); + + for (int i = 0; i < rules.reserve_size; i++) { + reserve.place(deck[i + deckCardsUsed]); + deckCardsUsed++; + } + } + + // Deal to the tableau piles (row-by-row) for (vector::size_type i = 0; i < deck.size(); i++) { - card c = deck[i]; + card c = deck[i + deckCardsUsed]; + deckCardsUsed++; // For the first row we must create all the tableau pile vectors if (i / rules.tableau_pile_count == 0) { @@ -75,6 +101,8 @@ game_state::game_state(int seed, const sol_rules& s_rules) tableau_piles[i % rules.tableau_pile_count].place(c); } + assert(deckCardsUsed == deck.size()); + // If there are foundation piles, create the relevant pile vectors if (rules.foundations) { for (int i = 0; i < 4; i++) { diff --git a/src/main/game_state.h b/src/main/game_state.h index 97a1bec..c58e506 100644 --- a/src/main/game_state.h +++ b/src/main/game_state.h @@ -43,8 +43,11 @@ private: std::vector tableau_piles; std::vector cells; std::vector foundations; - sol_rules rules; + pile waste; + pile reserve; + pile stock; pile hole; + sol_rules rules; }; -- cgit v1.2.1 From dc01c77d6615d7b74bd3657142ea3e9604279a52 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 7 Nov 2017 10:44:06 +0000 Subject: Add Canfield valid next state gen --- src/main/game_state.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/main') diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp index 80f3cce..a783158 100644 --- a/src/main/game_state.cpp +++ b/src/main/game_state.cpp @@ -159,6 +159,9 @@ vector game_state::get_next_legal_states() { can_add.push_back(&tableau_piles[i]); } } + if (rules.reserve_size > 0) { + can_remove.push_back(&reserve); + } if (rules.foundations) { for (vector::size_type i = 0; i < foundations.size(); i++) { can_remove.push_back(&foundations[i]); @@ -176,6 +179,7 @@ vector game_state::get_next_legal_states() { // The next legal states vector next; + // Moving from and to the 'can remove' and 'can add' piles for (pile *rem_pile : can_remove) { if (rem_pile->empty()) continue; @@ -191,6 +195,12 @@ vector game_state::get_next_legal_states() { } } + // Dealing from the stock to the waste + move(&stock, &waste); + game_state s = *this; + next.push_back(s); + move(&waste, &stock); + return next; } -- cgit v1.2.1 From d9c6e2e3be58544bf5d5117bc0511787e0535311 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 7 Nov 2017 10:52:21 +0000 Subject: Add game state printing and fix bugs --- src/main/game_state.cpp | 66 +++++++++++++++++++++++++++++++++---------------- src/main/game_state.h | 1 + src/main/sol_rules.cpp | 4 +-- src/main/sol_rules.h | 10 ++++---- 4 files changed, 53 insertions(+), 28 deletions(-) (limited to 'src/main') diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp index a783158..2d6ae2b 100644 --- a/src/main/game_state.cpp +++ b/src/main/game_state.cpp @@ -22,8 +22,8 @@ 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"), - hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank) { + : hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank), + rules("simple-black-hole") { // Construct tableau piles assert(doc.HasMember("tableau piles")); const Value& json_tab_piles = doc["tableau piles"]; @@ -62,34 +62,34 @@ game_state::game_state(int seed, const sol_rules& s_rules) hole.place("AS"); } - int deckCardsUsed = 0; + 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 (int i = 0; i < rules.stock_size; i++) { + for (unsigned int i = 0; i < rules.stock_size; i++) { stock.place(deck[i + deckCardsUsed]); - deckCardsUsed++; } + 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 (int i = 0; i < rules.reserve_size; i++) { + for (unsigned int i = 0; i < rules.reserve_size; i++) { reserve.place(deck[i + deckCardsUsed]); - deckCardsUsed++; } + deckCardsUsed += rules.reserve_size; } // Deal to the tableau piles (row-by-row) - for (vector::size_type i = 0; i < deck.size(); i++) { + vector::size_type remainingCards = deck.size() - deckCardsUsed; + for (vector::size_type i = 0; i < remainingCards; i++) { card c = deck[i + deckCardsUsed]; - deckCardsUsed++; // For the first row we must create all the tableau pile vectors if (i / rules.tableau_pile_count == 0) { @@ -100,7 +100,7 @@ game_state::game_state(int seed, const sol_rules& s_rules) // Add the randomly generated card to the tableau piles tableau_piles[i % rules.tableau_pile_count].place(c); } - + deckCardsUsed += remainingCards; assert(deckCardsUsed == deck.size()); // If there are foundation piles, create the relevant pile vectors @@ -113,7 +113,7 @@ game_state::game_state(int seed, const sol_rules& s_rules) // If there are cell piles, create the relevant cell vectors if (rules.cells > 0) { - for (int i = 0; i < rules.cells; i++) { + for (unsigned int i = 0; i < rules.cells; i++) { pile p(true, ord::SINGLE_CARD, pol::ANY_SUIT, false); cells.push_back(p); } @@ -172,6 +172,9 @@ vector game_state::get_next_legal_states() { 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); } @@ -196,25 +199,26 @@ vector game_state::get_next_legal_states() { } // Dealing from the stock to the waste - move(&stock, &waste); - game_state s = *this; - next.push_back(s); - move(&waste, &stock); + if (!stock.empty()) { + move(&stock, &waste); + game_state s = *this; + next.push_back(s); + move(&waste, &stock); + } return next; } bool game_state::is_solved() const { - for (auto p : tableau_piles) { - if (!p.empty()) { + for (auto p : foundations) { + if (p.size() != rules.max_rank) { return false; } } - for (auto p : cells) { - if (!p.empty()) { - return false; - } + + if (rules.hole && hole.size() != rules.max_rank * 4) { + return false; } return true; } @@ -228,10 +232,20 @@ ostream& game_state::print(ostream& stream) const { print_header(stream, "Cells"); print_cells(stream); } + 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_tableau_piles(stream); } + if (rules.stock_size > 0) { + print_header(stream, "Stock"); + print_pile(stream, stock); + print_header(stream, "Waste"); + print_pile(stream, waste); + } if (rules.hole) { print_header(stream, "Hole Card"); print_hole(stream); @@ -266,6 +280,16 @@ void game_state::print_cells(ostream& stream) const { 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 { bool empty_row = false; vector::size_type row_idx = 0; diff --git a/src/main/game_state.h b/src/main/game_state.h index c58e506..424c678 100644 --- a/src/main/game_state.h +++ b/src/main/game_state.h @@ -39,6 +39,7 @@ private: void print_cells(std::ostream&) const; void print_tableau_piles(std::ostream&) const; void print_hole(std::ostream&) const; + void print_pile(std::ostream&, const pile&) const; std::vector tableau_piles; std::vector cells; diff --git a/src/main/sol_rules.cpp b/src/main/sol_rules.cpp index b3b5457..f47cf44 100644 --- a/src/main/sol_rules.cpp +++ b/src/main/sol_rules.cpp @@ -95,9 +95,9 @@ sol_rules::sol_rules(std::string sol_type) { case valid_sol::SIMPLE_CANFIELD: tableau_pile_count = 3; build_pol = build_policy::RED_BLACK; - max_rank = 4; + max_rank = 3; reserve_size = 3; - stock_size = 9; + stock_size = 6; break; default: assert(false); diff --git a/src/main/sol_rules.h b/src/main/sol_rules.h index aadda69..44b83bb 100644 --- a/src/main/sol_rules.h +++ b/src/main/sol_rules.h @@ -43,15 +43,15 @@ public: sol_rules(std::string); - int tableau_pile_count; + unsigned int tableau_pile_count; build_order build_ord; build_policy build_pol; - int max_rank; + unsigned int max_rank; bool hole; bool foundations; - int cells; - int reserve_size; - int stock_size; + unsigned int cells; + unsigned int reserve_size; + unsigned int stock_size; private: static valid_sol valid_sol_enum(const std::string&); -- cgit v1.2.1