diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/game_state.cpp | 66 | ||||
| -rw-r--r-- | src/main/game_state.h | 1 | ||||
| -rw-r--r-- | src/main/sol_rules.cpp | 4 | ||||
| -rw-r--r-- | src/main/sol_rules.h | 10 |
4 files changed, 53 insertions, 28 deletions
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<card>::size_type i = 0; i < deck.size(); i++) { + vector<card>::size_type remainingCards = deck.size() - deckCardsUsed; + for (vector<card>::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> 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> 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<card>::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<pile> tableau_piles; std::vector<pile> 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&); |
