aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorCharlie Blake2017-11-07 10:00:41 +0000
committerGitHub2017-11-07 10:00:41 +0000
commit45a4fa8ddeaa707e63e0dc88fcb7e561c5ecf9d9 (patch)
tree2b0301e9198a6d56afb60055ef6aeb68b466e047 /src/main
parentMerge pull request #16 from thecharlesblake/docs (diff)
parentFixup cell/foundation mixup and printing errs (diff)
downloadmetasol-45a4fa8ddeaa707e63e0dc88fcb7e561c5ecf9d9.tar.gz
metasol-45a4fa8ddeaa707e63e0dc88fcb7e561c5ecf9d9.zip

Merge pull request #17 from thecharlesblake/free-cell

Free cell

Diffstat (limited to 'src/main')
-rw-r--r--src/main/game_state.cpp34
-rw-r--r--src/main/game_state.h2
-rw-r--r--src/main/pile.cpp2
-rw-r--r--src/main/sol_rules.cpp29
-rw-r--r--src/main/sol_rules.h8
5 files changed, 63 insertions, 12 deletions
diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp
index 29ccdc3..05bfb4f 100644
--- a/src/main/game_state.cpp
+++ b/src/main/game_state.cpp
@@ -55,7 +55,7 @@ game_state::game_state(int seed, const sol_rules& s_rules)
hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank) {
vector<card> deck = shuffled_deck(seed, rules.max_rank);
- // If there is a hole, move the ace to spades to it
+ // 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.place("AS");
@@ -82,6 +82,14 @@ game_state::game_state(int seed, const sol_rules& s_rules)
foundations.push_back(p);
}
}
+
+ // If there are cell piles, create the relevant cell vectors
+ if (rules.cells > 0) {
+ for (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
@@ -129,6 +137,10 @@ vector<game_state> game_state::get_next_legal_states() {
can_add.push_back(&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]);
+ }
if (rules.hole) {
can_add.push_back(&hole);
}
@@ -161,6 +173,11 @@ bool game_state::is_solved() const {
return false;
}
}
+ for (auto p : cells) {
+ if (!p.empty()) {
+ return false;
+ }
+ }
return true;
}
@@ -169,6 +186,10 @@ ostream& game_state::print(ostream& stream) const {
print_header(stream, "Foundations");
print_foundations(stream);
}
+ if (rules.cells) {
+ print_header(stream, "Cells");
+ print_cells(stream);
+ }
if (rules.tableau_pile_count > 0) {
print_header(stream, "Tableau Piles");
print_tableau_piles(stream);
@@ -196,6 +217,17 @@ void game_state::print_foundations(ostream& stream) const {
<< 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_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 311c97f..97a1bec 100644
--- a/src/main/game_state.h
+++ b/src/main/game_state.h
@@ -36,10 +36,12 @@ private:
void print_header(std::ostream&, const char*) const;
void print_foundations(std::ostream&) const;
+ void print_cells(std::ostream&) const;
void print_tableau_piles(std::ostream&) const;
void print_hole(std::ostream&) const;
std::vector<pile> tableau_piles;
+ std::vector<pile> cells;
std::vector<pile> foundations;
sol_rules rules;
pile hole;
diff --git a/src/main/pile.cpp b/src/main/pile.cpp
index cceda50..344e56c 100644
--- a/src/main/pile.cpp
+++ b/src/main/pile.cpp
@@ -34,6 +34,8 @@ bool pile::can_place(const card& c) const {
if (build_order == ord::NO_BUILD) {
return false;
+ } else if (build_order == ord::SINGLE_CARD) {
+ return empty();
}
if (empty()) {
diff --git a/src/main/sol_rules.cpp b/src/main/sol_rules.cpp
index eded5eb..b98e96d 100644
--- a/src/main/sol_rules.cpp
+++ b/src/main/sol_rules.cpp
@@ -19,7 +19,9 @@ vector<string> sol_rules::valid_sol_strs = {
"black-hole",
"simple-black-hole",
"spanish-patience",
- "simple-spanish-patience"
+ "simple-spanish-patience",
+ "free-cell",
+ "simple-free-cell"
};
bool sol_rules::is_suit(pol bp) {
@@ -40,14 +42,21 @@ int sol_rules::suit_val(pol bp) {
sol_rules::sol_rules(std::string sol_type) {
valid_sol vs = valid_sol_enum(sol_type);
+ // Default game values
+ build_ord = build_order::DESCENDING;
+ max_rank = 13;
+ hole = false;
+ foundations = true;
+ cells = 0;
+ // Game-specific values
switch(vs) {
case valid_sol::BLACK_HOLE:
tableau_pile_count = 17;
build_ord = build_order::NO_BUILD;
- max_rank = 13;
hole = true;
foundations = false;
+ cells = 0;
break;
case valid_sol::SIMPLE_BLACK_HOLE:
tableau_pile_count = 9;
@@ -58,17 +67,19 @@ sol_rules::sol_rules(std::string sol_type) {
break;
case valid_sol::SPANISH_PATIENCE:
tableau_pile_count = 13;
- build_ord = build_order::DESCENDING;
- max_rank = 13;
- hole = false;
- foundations = true;
break;
case valid_sol::SIMPLE_SPANISH_PATIENCE:
tableau_pile_count = 5;
- build_ord = build_order::DESCENDING;
max_rank = 5;
- hole = false;
- foundations = true;
+ break;
+ case valid_sol::FREE_CELL:
+ tableau_pile_count = 8;
+ cells = 4;
+ break;
+ case valid_sol::SIMPLE_FREE_CELL:
+ tableau_pile_count = 3;
+ max_rank = 3;
+ cells = 1;
break;
default:
assert(false);
diff --git a/src/main/sol_rules.h b/src/main/sol_rules.h
index 671e8b7..5699ef1 100644
--- a/src/main/sol_rules.h
+++ b/src/main/sol_rules.h
@@ -14,13 +14,16 @@ public:
BLACK_HOLE,
SIMPLE_BLACK_HOLE,
SPANISH_PATIENCE,
- SIMPLE_SPANISH_PATIENCE
+ SIMPLE_SPANISH_PATIENCE,
+ FREE_CELL,
+ SIMPLE_FREE_CELL
};
enum class build_order {
NO_BUILD,
DESCENDING,
ASCENDING,
- BOTH
+ BOTH,
+ SINGLE_CARD
};
enum class build_policy {
CLUBS,
@@ -42,6 +45,7 @@ public:
int max_rank;
bool hole;
bool foundations;
+ int cells;
private:
static valid_sol valid_sol_enum(const std::string&);