aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorthecharlesblake2017-10-28 14:03:54 +0100
committerthecharlesblake2017-10-28 14:03:54 +0100
commit9c018b08ed775eda03e4507e33a20742659683ea (patch)
treef40910ff140d7e28aa4426de78a53ae27901a72a /src/main
parentAdd Spanish Paitence to cmd parse and refactor (diff)
downloadmetasol-9c018b08ed775eda03e4507e33a20742659683ea.tar.gz
metasol-9c018b08ed775eda03e4507e33a20742659683ea.zip

Refactor game generation to make more generic

Diffstat (limited to 'src/main')
-rw-r--r--src/main/card.cpp4
-rw-r--r--src/main/card.h1
-rw-r--r--src/main/game_state.cpp44
-rw-r--r--src/main/game_state.h4
4 files changed, 36 insertions, 17 deletions
diff --git a/src/main/card.cpp b/src/main/card.cpp
index 0b7c0b8..0510304 100644
--- a/src/main/card.cpp
+++ b/src/main/card.cpp
@@ -51,6 +51,10 @@ card::card(const char* c) : rank(-1), suit(-1) {
}
}
+bool operator==(const card& l, const card& r) {
+ return l.get_rank() == r.get_rank() && l.get_suit() == r.get_suit();
+}
+
int card::get_rank() const {
return rank;
}
diff --git a/src/main/card.h b/src/main/card.h
index 13507f0..1569a5b 100644
--- a/src/main/card.h
+++ b/src/main/card.h
@@ -21,6 +21,7 @@ private:
};
std::ostream & operator<<(std::ostream &, card const &);
+bool operator==(const card&, const card&);
#endif //SOLITAIRE_PIP_CARD_H
diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp
index ef9db97..28f6d3b 100644
--- a/src/main/game_state.cpp
+++ b/src/main/game_state.cpp
@@ -18,7 +18,7 @@ using namespace std;
// Construct an initial game state from a JSON doc
game_state::game_state(const Document& doc, string sol_type)
: max_rank(1) {
- simple = sol_type == "simple-black-hole";
+ max_rank = sol_type == "simple-black-hole" ? 7 : 13;
// Construct tableau piles
assert(doc.HasMember("tableau piles"));
@@ -46,15 +46,27 @@ game_state::game_state(const Document& doc, string sol_type)
// Construct an initial game state from a seed
game_state::game_state(int seed, string sol_type) {
- simple = sol_type == "simple-black-hole";
- max_rank = simple ? 7 : 13;
+ max_rank = sol_type == "simple-black-hole" ? 7 : 13;
- int start = 1; // Ignore the first (hole) card
- int end = max_rank * 4;
+ vector<card> deck = shuffled_deck(seed, max_rank);
- // Create a vector of pointers to ints from [start, end)
+ for (vector<card>::size_type i = 0; i < deck.size(); i++) {
+ // Don't add the Ace of Spades to the hole card
+ card c = deck[i];
+ if (c == "AS") continue;
+
+ // Add the randomly generated card to the tableau piles
+ if (i % 3 == 0) tableau_piles.push_back(vector<card>());
+ tableau_piles[i / 3].push_back(deck[i]);
+ }
+
+ hole_card = "AS";
+}
+
+// Generate a randomly ordered vector of cards
+vector<card> game_state::shuffled_deck(int seed, int max_rank = 13) {
vector<int*> v;
- for (int i = start; i < end; i++) {
+ for (int i = 0; i < max_rank * 4; i++) {
v.push_back(new int(i));
}
@@ -62,23 +74,23 @@ game_state::game_state(int seed, string sol_type) {
auto rng = std::default_random_engine(seed);
std::shuffle(std::begin(v), std::end(v), rng);
- for (vector<int*>::size_type i = 0; i < v.size(); i++) {
- int r = ((*v[i]) % max_rank);
- int s = (*v[i]) / max_rank;
+ vector<card> deck;
+ for (int *i : v) {
+ int r = ((*i) % max_rank) + 1;
+ int s = (*i) / max_rank;
- // Add the randomly generated card to the tableau piles
- if (i % 3 == 0) tableau_piles.push_back(vector<card>());
- tableau_piles[i / 3].push_back(card(r + 1, s));
+ deck.push_back(card(r, s));
}
- hole_card = "AS";
-
// release memory
- for (int i = start; i < max_rank; i++) {
+ for (int i = 0; i < max_rank * 4; i++) {
delete v[i];
}
+
+ return deck;
}
+
vector<game_state> game_state::get_next_legal_states() const {
vector<game_state> next;
diff --git a/src/main/game_state.h b/src/main/game_state.h
index ff92247..88e8937 100644
--- a/src/main/game_state.h
+++ b/src/main/game_state.h
@@ -17,6 +17,7 @@ class game_state {
public:
game_state(const rapidjson::Document&, std::string);
game_state(int seed, std::string);
+
std::vector<game_state> get_next_legal_states() const;
bool is_solved() const;
std::ostream& print(std::ostream&) const;
@@ -24,9 +25,10 @@ public:
friend std::ostream& operator<< (std::ostream&, const game_state&);
private:
+ static std::vector<card> shuffled_deck(int, int);
+
void move_to_hole(int);
bool adjacent(const card& a, const card& b) const;
- bool simple;
std::vector<std::vector<card> > tableau_piles;
int max_rank;