From bba8775ce7c63783303240470d4a4b8404c9498b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 11 Nov 2017 11:36:27 +0000 Subject: Add src file directory structure --- src/main/game/game_state.cpp | 325 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 325 insertions(+) create mode 100644 src/main/game/game_state.cpp (limited to 'src/main/game/game_state.cpp') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp new file mode 100644 index 0000000..2d6ae2b --- /dev/null +++ b/src/main/game/game_state.cpp @@ -0,0 +1,325 @@ +// +// Created by thecharlesblake on 10/24/17. +// + +#include +#include +#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) + : 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"]; + assert(json_tab_piles.IsArray()); + + int max_rank = 1; + for (auto& json_tab : json_tab_piles.GetArray()) { + pile tableau_pile(true, rules.build_ord, 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.place(c); + } + + tableau_piles.push_back(tableau_pile); + } + + // Assign hole card + assert(doc.HasMember("hole card")); + const Value& json_h_card = doc["hole card"]; + assert(json_h_card.IsString()); + hole.place(json_h_card.GetString()); +} + +// Construct an initial game state from a seed +game_state::game_state(int seed, const sol_rules& s_rules) + : 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"); + } + + 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 (unsigned int i = 0; i < rules.stock_size; i++) { + stock.place(deck[i + 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 (unsigned int i = 0; i < rules.reserve_size; i++) { + reserve.place(deck[i + deckCardsUsed]); + } + deckCardsUsed += rules.reserve_size; + } + + + // Deal to the tableau piles (row-by-row) + vector::size_type remainingCards = deck.size() - deckCardsUsed; + for (vector::size_type i = 0; i < remainingCards; i++) { + card c = deck[i + deckCardsUsed]; + + // For the first row we must create all the tableau pile vectors + if (i / rules.tableau_pile_count == 0) { + pile p(true, rules.build_ord, 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].place(c); + } + deckCardsUsed += remainingCards; + assert(deckCardsUsed == deck.size()); + + // 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); + } + } + + // If there are cell piles, create the relevant cell vectors + if (rules.cells > 0) { + for (unsigned int i = 0; i < rules.cells; i++) { + pile p(true, ord::SINGLE_CARD, pol::ANY_SUIT, false); + cells.push_back(p); + } + } +} + +// Generate a randomly ordered vector of cards +vector game_state::shuffled_deck(int seed, int max_rank = 13) { + vector v; + for (int i = 0; i < max_rank * 4; i++) { + v.push_back(new int(i)); + } + + // Randomly shuffle the pointers + auto rng = std::default_random_engine(seed); + std::shuffle(std::begin(v), std::end(v), rng); + + vector deck; + for (int *i : v) { + int r = ((*i) % max_rank) + 1; + int s = (*i) / max_rank; + + deck.push_back(card(r, s)); + } + + // release memory + for (int i = 0; i < max_rank * 4; i++) { + delete v[i]; + } + + return deck; +} + +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; + + 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.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]); + can_add.push_back(&foundations[i]); + } + } + for (vector::size_type i = 0; i < cells.size(); i++) { + 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); + } + + // 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; + + 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 + } + } + } + + // Dealing from the stock to the waste + 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 : foundations) { + if (p.size() != rules.max_rank) { + return false; + } + } + + if (rules.hole && hole.size() != rules.max_rank * 4) { + return false; + } + return true; +} + +ostream& game_state::print(ostream& stream) const { + if (rules.foundations) { + print_header(stream, "Foundations"); + print_foundations(stream); + } + if (rules.cells) { + 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); + } + return stream << "===================================\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_cells(ostream& stream) const { + for (pile cell : cells) { + if (cells.empty()) { + stream << "[]" << "\t"; + } else { + stream << cells[0] << "\t"; + } + } + 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; + + while (!empty_row) { + empty_row = true; + + for (pile tableau_pile : tableau_piles) { + if (tableau_pile.size() > row_idx) { + empty_row = false; + stream << tableau_pile[row_idx]; + } + stream << "\t"; + } + + 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); +} \ No newline at end of file -- cgit v1.2.1 From af1fa0716bbf21ffe6b03694351be85b7457dc2c Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sun, 12 Nov 2017 09:54:57 +0000 Subject: Init piles in game_state & make get states const --- src/main/game/game_state.cpp | 51 ++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 23 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 2d6ae2b..9432239 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -22,7 +22,10 @@ typedef sol_rules::build_policy pol; // Construct an initial game state from a JSON doc game_state::game_state(const Document& doc) - : hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank), + : reserve(true, ord::NO_BUILD, pol::ANY_SUIT, false), + stock(true, ord::NO_BUILD, pol::ANY_SUIT, false), + waste(true, ord::NO_BUILD, pol::ANY_SUIT, false), + hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank), rules("simple-black-hole") { // Construct tableau piles assert(doc.HasMember("tableau piles")); @@ -50,15 +53,18 @@ 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) { +game_state::game_state(int seed, const sol_rules& s_rules) : + reserve(true, ord::NO_BUILD, pol::ANY_SUIT, false), + stock(true, ord::NO_BUILD, pol::ANY_SUIT, false), + waste(true, ord::NO_BUILD, pol::ANY_SUIT, false), + hole(false, ord::BOTH, pol::ANY_SUIT, true, s_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"); } @@ -66,9 +72,6 @@ game_state::game_state(int seed, const sol_rules& s_rules) // 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 (unsigned int i = 0; i < rules.stock_size; i++) { stock.place(deck[i + deckCardsUsed]); } @@ -77,8 +80,6 @@ game_state::game_state(int seed, const sol_rules& s_rules) // If there is a reserve, deal to it if (rules.reserve_size > 0) { - reserve = pile(true, ord::NO_BUILD, pol::ANY_SUIT, false); - for (unsigned int i = 0; i < rules.reserve_size; i++) { reserve.place(deck[i + deckCardsUsed]); } @@ -147,36 +148,40 @@ vector game_state::shuffled_deck(int seed, int max_rank = 13) { return deck; } -vector game_state::get_next_legal_states() { +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; + // A mutable version of the current state, used to create the next legal + // states + game_state mut_state = *this; + for (vector::size_type i = 0; i < tableau_piles.size(); i++) { - can_remove.push_back(&tableau_piles[i]); + can_remove.push_back(&mut_state.tableau_piles[i]); if (rules.build_ord != ord::NO_BUILD) { - can_add.push_back(&tableau_piles[i]); + can_add.push_back(&mut_state.tableau_piles[i]); } } if (rules.reserve_size > 0) { - can_remove.push_back(&reserve); + can_remove.push_back(&mut_state.reserve); } if (rules.foundations) { for (vector::size_type i = 0; i < foundations.size(); i++) { - can_remove.push_back(&foundations[i]); - can_add.push_back(&foundations[i]); + can_remove.push_back(&mut_state.foundations[i]); + can_add.push_back(&mut_state.foundations[i]); } } for (vector::size_type i = 0; i < cells.size(); i++) { - can_remove.push_back(&cells[i]); - can_add.push_back(&cells[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(&waste); + can_remove.push_back(&mut_state.waste); } if (rules.hole) { - can_add.push_back(&hole); + can_add.push_back(&mut_state.hole); } // The next legal states @@ -191,7 +196,7 @@ vector game_state::get_next_legal_states() { && add_pile->can_place(rem_pile->top_card())) { move(rem_pile, add_pile); // remove - game_state s = *this; + game_state s(mut_state); next.push_back(s); move(add_pile, rem_pile); // restore } @@ -200,10 +205,10 @@ vector game_state::get_next_legal_states() { // Dealing from the stock to the waste if (!stock.empty()) { - move(&stock, &waste); - game_state s = *this; + move(&mut_state.stock, &mut_state.waste); + game_state s(mut_state); next.push_back(s); - move(&waste, &stock); + move(&mut_state.waste, &mut_state.stock); } return next; -- cgit v1.2.1 From c651b6894081ba71609249c98f61789ebbe31f0d Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sun, 12 Nov 2017 10:51:09 +0000 Subject: Add factory to pile class to create std pile types --- src/main/game/game_state.cpp | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 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 9432239..9d03155 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -12,7 +12,6 @@ #include #include "game_state.h" -#include "pile.h" using namespace rapidjson; using namespace std; @@ -54,10 +53,10 @@ 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) : - reserve(true, ord::NO_BUILD, pol::ANY_SUIT, false), - stock(true, ord::NO_BUILD, pol::ANY_SUIT, false), - waste(true, ord::NO_BUILD, pol::ANY_SUIT, false), - hole(false, ord::BOTH, pol::ANY_SUIT, true, s_rules.max_rank), + reserve(pile::reserve_factory()), + stock(pile::stock_factory()), + waste(pile::waste_factory()), + hole(pile::hole_factory(s_rules.max_rank)), rules(s_rules) { vector deck = shuffled_deck(seed, rules.max_rank); @@ -94,8 +93,7 @@ game_state::game_state(int seed, const sol_rules& s_rules) : // For the first row we must create all the tableau pile vectors if (i / rules.tableau_pile_count == 0) { - pile p(true, rules.build_ord, pol::ANY_SUIT, false); - tableau_piles.push_back(p); + tableau_piles.push_back(pile::tableau_factory(rules.build_ord)); } // Add the randomly generated card to the tableau piles @@ -107,16 +105,14 @@ game_state::game_state(int seed, const sol_rules& s_rules) : // 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); + foundations.push_back(pile::foundation_factory(pol(i))); } } // If there are cell piles, create the relevant cell vectors if (rules.cells > 0) { for (unsigned int i = 0; i < rules.cells; i++) { - pile p(true, ord::SINGLE_CARD, pol::ANY_SUIT, false); - cells.push_back(p); + cells.push_back(pile::cell_factory()); } } } -- cgit v1.2.1 From 5c5afaa42e5f51d5f6d289be2b7d0b13ee80ca10 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sun, 12 Nov 2017 10:59:04 +0000 Subject: Remove dynamic mem alloc for random deal gen --- src/main/game/game_state.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 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 9d03155..b38c2d8 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -119,26 +119,23 @@ game_state::game_state(int seed, const sol_rules& s_rules) : // Generate a randomly ordered vector of cards vector game_state::shuffled_deck(int seed, int max_rank = 13) { - vector v; + vector values; + vector v_ptrs; for (int i = 0; i < max_rank * 4; i++) { - v.push_back(new int(i)); + values.push_back(i); + v_ptrs.push_back(&values[i]); } // Randomly shuffle the pointers auto rng = std::default_random_engine(seed); - std::shuffle(std::begin(v), std::end(v), rng); + std::shuffle(std::begin(v_ptrs), std::end(v_ptrs), rng); vector deck; - for (int *i : v) { + for (int *i : v_ptrs) { int r = ((*i) % max_rank) + 1; int s = (*i) / max_rank; - deck.push_back(card(r, s)); - } - - // release memory - for (int i = 0; i < max_rank * 4; i++) { - delete v[i]; + deck.emplace_back(card(r, s)); } return deck; -- cgit v1.2.1 From 9779d5b9823de8d013c449630e198f16a5c05bf6 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sun, 12 Nov 2017 11:18:14 +0000 Subject: Fix random deal bug --- src/main/game/game_state.cpp | 6 ++++-- 1 file changed, 4 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 b38c2d8..cd40c53 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -123,12 +123,14 @@ vector game_state::shuffled_deck(int seed, int max_rank = 13) { vector v_ptrs; for (int i = 0; i < max_rank * 4; i++) { values.push_back(i); + } + for (int i = 0; i < max_rank * 4; i++) { v_ptrs.push_back(&values[i]); } // Randomly shuffle the pointers - auto rng = std::default_random_engine(seed); - std::shuffle(std::begin(v_ptrs), std::end(v_ptrs), rng); + auto rng = default_random_engine(seed); + shuffle(begin(v_ptrs), end(v_ptrs), rng); vector deck; for (int *i : v_ptrs) { -- cgit v1.2.1 From 44f14e06c347db7eea68cad9c4ece8972b89ebed Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sun, 12 Nov 2017 11:33:59 +0000 Subject: Clean up deal from shuffled deck --- src/main/game/game_state.cpp | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 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 cd40c53..4caf9d9 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -67,40 +67,37 @@ game_state::game_state(int seed, const sol_rules& s_rules) : hole.place("AS"); } - unsigned int deckCardsUsed = 0; - // If there is a stock, deal to it and set up a waste pile too if (rules.stock_size > 0) { for (unsigned int i = 0; i < rules.stock_size; i++) { - stock.place(deck[i + deckCardsUsed]); + stock.place(deck.back()); + deck.pop_back(); } - deckCardsUsed += rules.stock_size; } // If there is a reserve, deal to it if (rules.reserve_size > 0) { for (unsigned int i = 0; i < rules.reserve_size; i++) { - reserve.place(deck[i + deckCardsUsed]); + reserve.place(deck.back()); + deck.pop_back(); } - deckCardsUsed += rules.reserve_size; } - // Deal to the tableau piles (row-by-row) - vector::size_type remainingCards = deck.size() - deckCardsUsed; - for (vector::size_type i = 0; i < remainingCards; i++) { - card c = deck[i + deckCardsUsed]; + unsigned int t = 0; + while (!deck.empty()) { + card c = deck.back(); + deck.pop_back(); // For the first row we must create all the tableau pile vectors - if (i / rules.tableau_pile_count == 0) { + if (t / rules.tableau_pile_count == 0) { tableau_piles.push_back(pile::tableau_factory(rules.build_ord)); } // Add the randomly generated card to the tableau piles - tableau_piles[i % rules.tableau_pile_count].place(c); + tableau_piles[t % rules.tableau_pile_count].place(c); + t++; } - deckCardsUsed += remainingCards; - assert(deckCardsUsed == deck.size()); // If there are foundation piles, create the relevant pile vectors if (rules.foundations) { -- cgit v1.2.1 From 2b3e92a384c2483d88f48c8fcafe8ffcdfb0f3b3 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sun, 12 Nov 2017 11:41:28 +0000 Subject: Simplify is_solved() --- src/main/game/game_state.cpp | 5 +---- 1 file changed, 1 insertion(+), 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 4caf9d9..8105cb3 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -214,10 +214,7 @@ bool game_state::is_solved() const { } } - if (rules.hole && hole.size() != rules.max_rank * 4) { - return false; - } - return true; + return !rules.hole || hole.size() == rules.max_rank * 4; } ostream& game_state::print(ostream& stream) const { -- cgit v1.2.1 From f6259b5e0ebf20a3da6683d2e5c6c09fe33e1064 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sun, 12 Nov 2017 12:26:34 +0000 Subject: Refactor and improve state printing --- src/main/game/game_state.cpp | 91 ++++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 46 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 8105cb3..4080f3c 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -220,11 +220,11 @@ bool game_state::is_solved() const { ostream& game_state::print(ostream& stream) const { if (rules.foundations) { print_header(stream, "Foundations"); - print_foundations(stream); + print_top_of_piles(stream, foundations); } if (rules.cells) { print_header(stream, "Cells"); - print_cells(stream); + print_piles(stream, cells); } if (rules.reserve_size > 0) { print_header(stream, "Reserve"); @@ -232,19 +232,17 @@ ostream& game_state::print(ostream& stream) const { } if (rules.tableau_pile_count > 0) { print_header(stream, "Tableau Piles"); - print_tableau_piles(stream); + print_piles(stream, tableau_piles); } if (rules.stock_size > 0) { - print_header(stream, "Stock"); - print_pile(stream, stock); - print_header(stream, "Waste"); - print_pile(stream, waste); + print_header(stream, "Stock | Waste"); + print_piles(stream, {stock, waste}); } if (rules.hole) { print_header(stream, "Hole Card"); - print_hole(stream); + print_top_of_pile(stream, hole); } - return stream << "===================================\n"; + return stream << "==================================="; } void game_state::print_header(ostream& stream, const char* header) const { @@ -256,56 +254,57 @@ void game_state::print_header(ostream& stream, const char* header) const { 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_cells(ostream& stream) const { - for (pile cell : cells) { - if (cells.empty()) { - stream << "[]" << "\t"; - } else { - stream << cells[0] << "\t"; - } - } - 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 { +void game_state::print_piles(ostream& stream, + const vector& pile_vec) 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 throught the current row to determine if it is empty empty_row = true; - - for (pile tableau_pile : tableau_piles) { + for (const pile& tableau_pile : pile_vec) { if (tableau_pile.size() > row_idx) { empty_row = false; - stream << tableau_pile[row_idx]; + break; } - stream << "\t"; } + if (!empty_row || row_idx == 0) { + // Loops through the current (non-empty) row, and outputs the values + for (const pile& tableau_pile : pile_vec) { + if (tableau_pile.size() > row_idx) { + stream << tableau_pile[row_idx]; + } else if (row_idx == 0) { + stream << "[]"; + } + stream << "\t"; + } - stream << "\n"; - row_idx++; + stream << "\n"; + row_idx++; + } + } +} + +void game_state::print_pile(ostream& stream, const pile &p) const { + print_piles(stream, {p}); +} + +void game_state::print_top_of_piles(ostream& stream, const vector& vp) const { + vector top(vp); + for (pile& p : top) { + if (!p.empty()) { + card t = p.top_card(); + p.clear(); + p.place(t); + } } + print_piles(stream, top); } -void game_state::print_hole(ostream& stream) const { - stream << hole.top_card() << "\n"; +void game_state::print_top_of_pile(ostream& stream, const pile& p) const { + print_top_of_piles(stream, {p}); } bool operator==(const game_state& a, const game_state& b) { -- cgit v1.2.1 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 +++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 35 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 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"); -- cgit v1.2.1