aboutsummaryrefslogtreecommitdiff
path: root/src/main/game_state.cpp
diff options
context:
space:
mode:
authorCharlie Blake2017-11-11 11:27:28 +0000
committerGitHub2017-11-11 11:27:28 +0000
commit832b319d482a1fb29e5a673b16ea11ab5edb8d68 (patch)
treef29c4843e60bd06c3d04f7abb398c08da22136c7 /src/main/game_state.cpp
parentMerge pull request #18 from thecharlesblake/docs (diff)
parentAdd game state printing and fix bugs (diff)
downloadmetasol-832b319d482a1fb29e5a673b16ea11ab5edb8d68.tar.gz
metasol-832b319d482a1fb29e5a673b16ea11ab5edb8d68.zip

Merge pull request #19 from thecharlesblake/canfield

Canfield

Diffstat (limited to 'src/main/game_state.cpp')
-rw-r--r--src/main/game_state.cpp88
1 files changed, 75 insertions, 13 deletions
diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp
index 05bfb4f..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"];
@@ -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<card> 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)
- for (vector<card>::size_type i = 0; i < deck.size(); i++) {
- card c = deck[i];
+ vector<card>::size_type remainingCards = deck.size() - deckCardsUsed;
+ for (vector<card>::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) {
@@ -74,6 +100,8 @@ 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
if (rules.foundations) {
@@ -85,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);
}
@@ -131,6 +159,9 @@ vector<game_state> 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<pile>::size_type i = 0; i < foundations.size(); i++) {
can_remove.push_back(&foundations[i]);
@@ -141,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);
}
@@ -148,6 +182,7 @@ vector<game_state> game_state::get_next_legal_states() {
// The next legal states
vector<game_state> next;
+ // Moving from and to the 'can remove' and 'can add' piles
for (pile *rem_pile : can_remove) {
if (rem_pile->empty()) continue;
@@ -163,20 +198,27 @@ vector<game_state> game_state::get_next_legal_states() {
}
}
+ // 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 : 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;
}
@@ -190,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);
@@ -228,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;