aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharlie Blake2017-11-11 11:27:28 +0000
committerGitHub2017-11-11 11:27:28 +0000
commit832b319d482a1fb29e5a673b16ea11ab5edb8d68 (patch)
treef29c4843e60bd06c3d04f7abb398c08da22136c7
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

-rw-r--r--src/main/game_state.cpp88
-rw-r--r--src/main/game_state.h6
-rw-r--r--src/main/sol_rules.cpp20
-rw-r--r--src/main/sol_rules.h16
4 files changed, 110 insertions, 20 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;
diff --git a/src/main/game_state.h b/src/main/game_state.h
index 97a1bec..424c678 100644
--- a/src/main/game_state.h
+++ b/src/main/game_state.h
@@ -39,12 +39,16 @@ 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;
std::vector<pile> foundations;
- sol_rules rules;
+ pile waste;
+ pile reserve;
+ pile stock;
pile hole;
+ sol_rules rules;
};
diff --git a/src/main/sol_rules.cpp b/src/main/sol_rules.cpp
index b98e96d..f47cf44 100644
--- a/src/main/sol_rules.cpp
+++ b/src/main/sol_rules.cpp
@@ -21,7 +21,9 @@ vector<string> sol_rules::valid_sol_strs = {
"spanish-patience",
"simple-spanish-patience",
"free-cell",
- "simple-free-cell"
+ "simple-free-cell",
+ "canfield",
+ "simple-canfield"
};
bool sol_rules::is_suit(pol bp) {
@@ -44,10 +46,13 @@ sol_rules::sol_rules(std::string sol_type) {
// Default game values
build_ord = build_order::DESCENDING;
+ build_pol = build_policy::ANY_SUIT;
max_rank = 13;
hole = false;
foundations = true;
cells = 0;
+ reserve_size = 0;
+ stock_size = 0;
// Game-specific values
switch(vs) {
@@ -81,6 +86,19 @@ sol_rules::sol_rules(std::string sol_type) {
max_rank = 3;
cells = 1;
break;
+ case valid_sol::CANFIELD:
+ tableau_pile_count = 4;
+ build_pol = build_policy::RED_BLACK;
+ reserve_size = 14;
+ stock_size = 34;
+ break;
+ case valid_sol::SIMPLE_CANFIELD:
+ tableau_pile_count = 3;
+ build_pol = build_policy::RED_BLACK;
+ max_rank = 3;
+ reserve_size = 3;
+ stock_size = 6;
+ break;
default:
assert(false);
}
diff --git a/src/main/sol_rules.h b/src/main/sol_rules.h
index 5699ef1..44b83bb 100644
--- a/src/main/sol_rules.h
+++ b/src/main/sol_rules.h
@@ -16,7 +16,9 @@ public:
SPANISH_PATIENCE,
SIMPLE_SPANISH_PATIENCE,
FREE_CELL,
- SIMPLE_FREE_CELL
+ SIMPLE_FREE_CELL,
+ CANFIELD,
+ SIMPLE_CANFIELD
};
enum class build_order {
NO_BUILD,
@@ -31,7 +33,8 @@ public:
HEARTS,
SPADES,
ANY_SUIT,
- SAME_SUIT
+ SAME_SUIT,
+ RED_BLACK
};
static std::vector<std::string> valid_sol_strs;
@@ -40,12 +43,15 @@ public:
sol_rules(std::string);
- int tableau_pile_count;
+ unsigned int tableau_pile_count;
build_order build_ord;
- int max_rank;
+ build_policy build_pol;
+ unsigned int max_rank;
bool hole;
bool foundations;
- int cells;
+ unsigned int cells;
+ unsigned int reserve_size;
+ unsigned int stock_size;
private:
static valid_sol valid_sol_enum(const std::string&);