From 076a6b129e6b35779ea946a5738357e9348c7f7e Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sun, 12 Nov 2017 22:41:27 +0000 Subject: Refactor game state --- src/main/game/game_state.cpp | 80 +++++++++++++++++++++++++------------------- src/main/game/game_state.h | 5 ++- src/main/game/pile.cpp | 9 +++-- src/main/game/pile.h | 3 +- src/main/game/sol_rules.cpp | 4 +-- 5 files changed, 56 insertions(+), 45 deletions(-) (limited to 'src/main') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 4080f3c..3336020 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #include #include @@ -140,7 +139,7 @@ vector game_state::shuffled_deck(int seed, int max_rank = 13) { return deck; } -vector game_state::get_next_legal_states() const { +const 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; @@ -150,62 +149,48 @@ vector game_state::get_next_legal_states() const { // states game_state mut_state = *this; - for (vector::size_type i = 0; i < tableau_piles.size(); i++) { - can_remove.push_back(&mut_state.tableau_piles[i]); - if (rules.build_ord != ord::NO_BUILD) { - can_add.push_back(&mut_state.tableau_piles[i]); + 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(&mut_state.reserve); - } - if (rules.foundations) { - for (vector::size_type i = 0; i < foundations.size(); i++) { - can_remove.push_back(&mut_state.foundations[i]); - can_add.push_back(&mut_state.foundations[i]); + if (p->get_build_order() != ord::NO_BUILD) { + can_add.push_back(p); } } - for (vector::size_type i = 0; i < cells.size(); i++) { - can_remove.push_back(&mut_state.cells[i]); - can_add.push_back(&mut_state.cells[i]); - } - if (rules.stock_size > 0) { - can_remove.push_back(&mut_state.waste); - } - if (rules.hole) { - can_add.push_back(&mut_state.hole); - } // The next legal states vector 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(mut_state); - 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(&mut_state.stock, &mut_state.waste); - game_state s(mut_state); - next.push_back(s); - move(&mut_state.waste, &mut_state.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,6 +202,31 @@ bool game_state::is_solved() const { 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 game_state::get_regular_pile_refs() { + vector piles; + auto to_ptr = [](pile& p){return &p;}; + + if (rules.foundations) { + transform(begin(foundations), end(foundations), back_inserter(piles), + to_ptr); + } + 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"); diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 6b2af7d..12dcda9 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -22,7 +22,7 @@ public: // Do the same from a 'rules' object game_state(int seed, const sol_rules&); - std::vector get_next_legal_states() const; + const std::vector get_next_legal_states() const; bool is_solved() const; std::ostream& print(std::ostream&) const; @@ -33,6 +33,9 @@ public: private: static std::vector shuffled_deck(int, int); + const game_state move(game_state&, pile*, pile*) const; + std::vector get_regular_pile_refs(); + void print_header(std::ostream&, const char*) const; void print_piles(std::ostream&, const std::vector&) const; void print_pile(std::ostream&, const pile&) const; diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp index 5b05661..bebd1f6 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -114,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::size_type i) { return pile_vec[i]; } @@ -136,11 +140,6 @@ void pile::clear() { pile_vec.clear(); } - -void move(pile *a, pile *b) { - b->place(a->take()); -} - bool operator==(const pile& a, const pile& b) { return a.pile_vec == b.pile_vec; } diff --git a/src/main/game/pile.h b/src/main/game/pile.h index 1297063..6482e0c 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -28,6 +28,7 @@ public: 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(); @@ -50,7 +51,5 @@ private: std::vector pile_vec; }; -void move(pile*, pile*); - #endif //SOLVITAIRE_PILE_H diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index fd32c72..003528c 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -99,9 +99,9 @@ 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; + max_rank = 2; reserve_size = 2; stock_size = 2; break; -- cgit v1.2.1