aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorthecharlesblake2017-11-12 09:54:57 +0000
committerthecharlesblake2017-11-12 09:54:57 +0000
commitaf1fa0716bbf21ffe6b03694351be85b7457dc2c (patch)
tree2314ca55c53973775e3990c472976bf06ec5a0be /src/main
parentSimplify pile class & make const where possible (diff)
downloadmetasol-af1fa0716bbf21ffe6b03694351be85b7457dc2c.tar.gz
metasol-af1fa0716bbf21ffe6b03694351be85b7457dc2c.zip

Init piles in game_state & make get states const

Diffstat (limited to 'src/main')
-rw-r--r--src/main/game/game_state.cpp51
-rw-r--r--src/main/game/game_state.h4
2 files changed, 30 insertions, 25 deletions
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<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");
}
@@ -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<card> game_state::shuffled_deck(int seed, int max_rank = 13) {
return deck;
}
-vector<game_state> game_state::get_next_legal_states() {
+vector<game_state> 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<pile*> can_remove;
vector<pile*> can_add;
+ // A mutable version of the current state, used to create the next legal
+ // states
+ game_state mut_state = *this;
+
for (vector<pile>::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<pile>::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<pile>::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> 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> 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;
diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h
index 424c678..e856056 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<game_state> get_next_legal_states();
+ std::vector<game_state> get_next_legal_states() const;
// NOTE: will only be evaluated after get_next_legal_states() is called
bool is_solved() const;
@@ -44,9 +44,9 @@ private:
std::vector<pile> tableau_piles;
std::vector<pile> cells;
std::vector<pile> foundations;
- pile waste;
pile reserve;
pile stock;
+ pile waste;
pile hole;
sol_rules rules;
};