aboutsummaryrefslogtreecommitdiff
path: root/src/main/game
diff options
context:
space:
mode:
authorthecharlesblake2018-01-18 13:51:09 +0000
committerthecharlesblake2018-01-18 13:51:09 +0000
commit91442e15dd15f8a4db7455f52255bd46273b0a40 (patch)
treeb2e1374c28dde347f05a107771d76c923c2c8bee /src/main/game
parentUpdate legal moves code to use new suit rep. (diff)
downloadmetasol-91442e15dd15f8a4db7455f52255bd46273b0a40.tar.gz
metasol-91442e15dd15f8a4db7455f52255bd46273b0a40.zip

Update game state with static pile refs & rules

Diffstat (limited to 'src/main/game')
-rw-r--r--src/main/game/game_state.cpp57
-rw-r--r--src/main/game/game_state.h35
2 files changed, 60 insertions, 32 deletions
diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp
index e2bfc24..8075f50 100644
--- a/src/main/game/game_state.cpp
+++ b/src/main/game/game_state.cpp
@@ -14,7 +14,6 @@
#include "game_state.h"
#include "../input-output/input/json-parsing/deal_parser.h"
#include "../input-output/output/state_printer.h"
-#include "sol_rules.h"
using namespace std;
using namespace rapidjson;
@@ -23,15 +22,15 @@ using namespace boost;
typedef sol_rules::build_policy pol;
typedef sol_rules::stock_deal_type sdt;
-
//////////////////
// CONSTRUCTORS //
//////////////////
// A private constructor used by both of the public ones. Initializes all of the
// piles and pile refs specified by the rules
-game_state::game_state(const sol_rules& s_rules) :
- rules(s_rules) {
+game_state::game_state(const sol_rules& s_rules) {
+ game_state::static_reset();
+ game_state::rules = s_rules;
// If there is a hole, creates pile
if (rules.hole) {
@@ -85,14 +84,14 @@ game_state::game_state(const sol_rules& s_rules) :
}
// Constructs an initial game state from a JSON doc
-game_state::game_state(const sol_rules& s_rules, const Document& doc) :
- game_state(s_rules) {
+game_state::game_state(const sol_rules& s_rules, const Document& doc)
+ : game_state(s_rules) {
deal_parser::parse(*this, doc);
}
// Constructs an initial game state from a seed
-game_state::game_state(const sol_rules& s_rules, int seed) :
- game_state(s_rules) {
+game_state::game_state(const sol_rules& s_rules, int seed)
+ : game_state(s_rules) {
vector<card> deck = gen_shuffled_deck(seed, rules.max_rank, rules.two_decks);
// If there is a hole, moves the ace of spades to it
@@ -104,7 +103,7 @@ game_state::game_state(const sol_rules& s_rules, int seed) :
// If the foundations begin filled, then fills them
if (rules.foundations_init_card) {
for (uint8_t f_idx = 0; f_idx < 4*(rules.two_decks ? 2:1); f_idx++) {
- card c = card(card::to_suit(f_idx % uint8_t(4)), 1);
+ card c = card(f_idx % uint8_t(4), 1);
deck.erase(find(begin(deck), end(deck), c));
piles[foundations[f_idx]].place(c);
@@ -130,6 +129,9 @@ game_state::game_state(const sol_rules& s_rules, int seed) :
}
}
+ // This only occurs during testing
+ if (rules.tableau_pile_count == 0) return;
+
// Deals to the tableau piles (row-by-row)
for (int t = 0; !deck.empty(); t++) {
card c = deck.back();
@@ -163,6 +165,10 @@ game_state::game_state(const sol_rules& s_rules, int seed) :
}
}
+game_state::game_state(std::initializer_list<pile> il) {
+ piles = il;
+}
+
// Generates a randomly ordered vector of cards
vector<card> game_state::gen_shuffled_deck(int seed, card::rank_t max_rank,
bool two_decks) {
@@ -183,7 +189,7 @@ vector<card> game_state::gen_shuffled_deck(int seed, card::rank_t max_rank,
vector<card> deck;
for (uint8_t *i : v_ptrs) {
auto r = static_cast<card::rank_t>(((*i) % max_rank) + 1);
- card::suit_t s = card::to_suit((*i) / max_rank);
+ card::suit_t s = (*i) / max_rank;
deck.emplace_back(card(s, r));
}
@@ -197,6 +203,31 @@ game_state::move::move(pile_ref f, pile_ref t, pile::size_type i)
}
+////////////////////////////
+// SETUP STATIC VARIABLES //
+////////////////////////////
+
+sol_rules game_state::rules;
+std::vector<game_state::pile_ref> game_state::tableau_piles;
+std::vector<game_state::pile_ref> game_state::cells;
+game_state::pile_ref game_state::stock;
+game_state::pile_ref game_state::waste;
+std::vector<game_state::pile_ref> game_state::reserve;
+std::vector<game_state::pile_ref> game_state::foundations;
+game_state::pile_ref game_state::hole;
+
+
+void game_state::static_reset() {
+ tableau_piles.clear();
+ cells.clear();
+ stock = 0;
+ waste = 0;
+ reserve.clear();
+ foundations.clear();
+ hole = 0;
+}
+
+
/////////////////////////
// MODIFYING FUNCTIONS //
/////////////////////////
@@ -310,12 +341,6 @@ const vector<pile>& game_state::get_data() const {
return piles;
}
-bool operator==(const game_state& a, const game_state& b) {
- return a.tableau_piles == b.tableau_piles
- && a.foundations == b.foundations
- && a.hole == b.hole;
-}
-
///////////
// PRINT //
diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h
index 8a41398..d8e35f3 100644
--- a/src/main/game/game_state.h
+++ b/src/main/game/game_state.h
@@ -15,12 +15,13 @@
#include "pile.h"
#include "sol_rules.h"
-class deal_parser;
+//class deal_parser;
-class game_state {
- friend class deal_parser;
+// TODO make class
+struct game_state {
+ //friend class deal_parser;
friend class state_printer;
-public:
+//public:
// Defines types
typedef uint8_t pile_ref;
struct move {
@@ -34,6 +35,9 @@ public:
// Does the same from a 'rules' object
game_state(const sol_rules&, int seed);
+ // For testing
+ game_state(std::initializer_list<pile>);
+
// Alters state
void make_move(move);
void undo_move(move);
@@ -44,13 +48,13 @@ public:
bool is_solved() const;
const std::vector<pile>& get_data() const;
- friend bool operator==(const game_state&, const game_state&);
friend std::ostream& operator<< (std::ostream&, const game_state&);
friend std::size_t hash_value(card const&);
- friend std::size_t hash_value(std::vector<pile> const&);
+ friend std::size_t hash_value(game_state const&);
-private:
+//private:
+ void static_reset();
static std::vector<card> gen_shuffled_deck(int, card::rank_t, bool);
// Private constructor
@@ -69,18 +73,17 @@ private:
card) const;
// References to piles
- sol_rules rules;
- std::vector<pile_ref> tableau_piles;
- std::vector<pile_ref> cells;
- pile_ref stock;
- pile_ref waste;
- std::vector<pile_ref> reserve;
- std::vector<pile_ref> foundations;
- pile_ref hole;
+ static sol_rules rules;
+ static std::vector<pile_ref> tableau_piles;
+ static std::vector<pile_ref> cells;
+ static pile_ref stock;
+ static pile_ref waste;
+ static std::vector<pile_ref> reserve;
+ static std::vector<pile_ref> foundations;
+ static pile_ref hole;
// The core piles
std::vector<pile> piles;
};
-
#endif //SOLVITAIRE_GAME_STATE_H