aboutsummaryrefslogtreecommitdiff
path: root/src/main/game
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/game')
-rw-r--r--src/main/game/card.cpp4
-rw-r--r--src/main/game/game_state.cpp90
-rw-r--r--src/main/game/game_state.h6
-rw-r--r--src/main/game/game_state.legal_moves.cpp10
-rw-r--r--src/main/game/pile.cpp1
5 files changed, 64 insertions, 47 deletions
diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp
index 4d9342d..80ebef6 100644
--- a/src/main/game/card.cpp
+++ b/src/main/game/card.cpp
@@ -35,7 +35,9 @@ card::rank_t card::rank_from_str(const char* c) {
case 'j': return 11;
case 'q': return 12;
case 'k': return 13;
- default: return static_cast<rank_t>(stoi(c));
+ default:
+ assert(stoi(c) >= 1 && stoi(c) <= 13);
+ return static_cast<rank_t>(stoi(c));
}
}
diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp
index 76e6488..f8f9e1e 100644
--- a/src/main/game/game_state.cpp
+++ b/src/main/game/game_state.cpp
@@ -31,10 +31,7 @@ typedef sol_rules::stock_deal_type sdt;
// 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),
- stock(PILE_REF_MAX),
- waste(PILE_REF_MAX),
- hole(PILE_REF_MAX) {
+ rules(s_rules) {
// If there is a hole, creates pile
if (rules.hole) {
@@ -157,13 +154,20 @@ game_state::game_state(const sol_rules& s_rules, int seed) :
deck.pop_back();
}
}
+
+ // The size of all piles must equal the deck size
+ int piles_sz = 0;
+ for (auto& p : piles) piles_sz += p.size();
+ if (piles_sz != rules.max_rank * (rules.two_decks ? 8:4)) {
+ throw runtime_error("Error: incorrect number of cards in starting piles");
+ }
}
// Generates a randomly ordered vector of cards
-vector<card> game_state::gen_shuffled_deck(int seed, int max_rank,
+vector<card> game_state::gen_shuffled_deck(int seed, card::rank_t max_rank,
bool two_decks) {
- vector<int> values;
- vector<int*> v_ptrs;
+ vector<uint8_t> values;
+ vector<uint8_t*> v_ptrs;
for (int i = 0; i < max_rank * 4; i++) {
values.emplace_back(i);
if (two_decks) values.emplace_back(i);
@@ -177,26 +181,13 @@ vector<card> game_state::gen_shuffled_deck(int seed, int max_rank,
shuffle(begin(v_ptrs), end(v_ptrs), rng);
vector<card> deck;
- for (int *i : v_ptrs) {
+ for (uint8_t *i : v_ptrs) {
auto r = static_cast<card::rank_t>(((*i) % max_rank) + 1);
- card::suit_t s;
- switch ((*i) / max_rank) {
- case 0 : s = card::suit_t::Clubs;
- break;
- case 1 : s = card::suit_t::Diamonds;
- break;
- case 2 : s = card::suit_t::Hearts;
- break;
- case 3 : s = card::suit_t::Spades;
- break;
- default : assert(false);
- s = card::suit_t::Clubs;
- break;
- }
-
+ card::suit_t s = card::to_suit((*i) / max_rank);
deck.emplace_back(card(s, r));
}
+ assert(deck.size() == pile::size_type(max_rank * (two_decks ? 8 : 4)));
return deck;
}
@@ -211,6 +202,9 @@ game_state::move::move(pile_ref f, pile_ref t, pile::size_type i)
/////////////////////////
void game_state::make_move(const move m) {
+ assert(m.from < piles.size());
+ assert(m.to < piles.size());
+
// Handles special stock-to-tableau-piles move
if (rules.stock_deal_t == sdt::TABLEAU_PILES && m.from == stock) {
for (pile_ref tab_pr = tableau_piles.front();
@@ -223,6 +217,7 @@ void game_state::make_move(const move m) {
else if (m.count == 1) {
piles[m.to].place(piles[m.from].take());
} else {
+ assert(m.count <= rules.max_rank);
// Adds the cards to the 'to' pile
for (auto pile_idx = m.count; pile_idx-- > 0;) {
piles[m.to].place(piles[m.from][pile_idx]);
@@ -236,6 +231,9 @@ void game_state::make_move(const move m) {
}
void game_state::undo_move(const move m) {
+ assert(m.from < piles.size());
+ assert(m.to < piles.size());
+
// Handles special stock-to-tableau-piles move
if (rules.stock_deal_t == sdt::TABLEAU_PILES && m.from == stock) {
for (pile_ref tab_pr = tableau_piles.front() + m.count;
@@ -248,6 +246,7 @@ void game_state::undo_move(const move m) {
else if (m.count == 1) {
piles[m.from].place(piles[m.to].take());
} else {
+ assert(m.count <= rules.max_rank);
// Adds the cards to the 'from' pile
for (auto pile_idx = m.count; pile_idx-- > 0;) {
piles[m.from].place(piles[m.to][pile_idx]);
@@ -266,17 +265,45 @@ void game_state::undo_move(const move m) {
////////////////////////
bool game_state::is_solved() const {
+ bool solved = true;
if (rules.hole) {
- return piles[hole].size()
+ solved = piles[hole].size()
== rules.max_rank * 4 * (rules.two_decks ? 2 : 1);
} else {
for (auto f : foundations) {
if (piles[f].size() != rules.max_rank) {
- return false;
+ solved = false;
}
}
- return true;
}
+
+ // Runs some alternative checks in debug mode to make sure the game state
+ // is consistent
+#ifndef NDEBUG
+ bool rest_empty = true;
+ for (pile_ref pr = 0; pr < piles.size(); pr++) {
+ // All piles other than the hole must be empty
+ if (rules.hole) {
+ if (pr != hole && !piles[pr].empty()) {
+ rest_empty = false;
+ break;
+ }
+ }
+ // All piles other than the foundations must be empty
+ else {
+ bool non_foundation_ref =
+ pr < foundations.front()
+ || pr >= foundations.front() + foundations.size();
+ if (non_foundation_ref && !piles[pr].empty()) {
+ rest_empty = false;
+ break;
+ }
+ }
+ }
+ assert(solved == rest_empty);
+#endif
+
+ return solved;
}
const vector<pile>& game_state::get_data() const {
@@ -290,17 +317,6 @@ bool operator==(const game_state& a, const game_state& b) {
}
-//////////////////////////////////
-// STATIC VARIABLES AND MEMBERS //
-//////////////////////////////////
-
-game_state::pile_ref game_state::PILE_REF_MAX = 255;
-
-game_state::move game_state::null_move() {
- return {PILE_REF_MAX, 0, PILE_REF_MAX};
-}
-
-
////////////////////
// HASH FUNCTIONS //
////////////////////
diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h
index 8f7bf57..323646f 100644
--- a/src/main/game/game_state.h
+++ b/src/main/game/game_state.h
@@ -46,17 +46,13 @@ public:
friend bool operator==(const game_state&, const game_state&);
- // Static pile ref members/vars
- static pile_ref PILE_REF_MAX;
- static move null_move();
-
friend std::size_t hash_value(game_state const&);
friend std::size_t hash_value(std::vector<pile> const&);
friend std::ostream& operator<< (std::ostream&, const game_state&);
private:
- static std::vector<card> gen_shuffled_deck(int, int, bool);
+ static std::vector<card> gen_shuffled_deck(int, card::rank_t, bool);
// Private constructor
explicit game_state(const sol_rules&);
diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp
index 5bc7762..7dfc87c 100644
--- a/src/main/game/game_state.legal_moves.cpp
+++ b/src/main/game/game_state.legal_moves.cpp
@@ -25,8 +25,8 @@ vector<game_state::move> game_state::get_legal_moves() const {
if (rem_ref == hole || piles[rem_ref].empty()) continue;
// Stock cards can only be moved to the waste (assuming there is a waste)
- if (stock != PILE_REF_MAX && rem_ref == stock) {
- if (waste != PILE_REF_MAX) {
+ if (rules.stock_size > 0 && rem_ref == stock) {
+ if (rules.stock_deal_t == sdt::WASTE) {
moves.emplace_back(stock, waste);
}
continue;
@@ -56,7 +56,7 @@ vector<game_state::move> game_state::get_legal_moves() const {
}
// Does the same for the hole
- if (hole != PILE_REF_MAX && is_valid_hole_move(rem_ref)) {
+ if (rules.hole && is_valid_hole_move(rem_ref)) {
moves.emplace_back(rem_ref, hole);
}
@@ -79,7 +79,9 @@ game_state::move game_state::get_stock_tableau_move() const {
piles[stock].size() >= tableau_piles.size()
? pile::size_type(tableau_piles.size())
: piles[stock].size();
- return {stock, PILE_REF_MAX, stock_moves};
+
+ assert(stock_moves > 0 && stock_moves <= piles[stock].size());
+ return {stock, 0, stock_moves};
}
// If a tableau pile is empty and the space policy is
diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp
index 264bb49..82ae344 100644
--- a/src/main/game/pile.cpp
+++ b/src/main/game/pile.cpp
@@ -38,6 +38,7 @@ void pile::place(const card c) {
}
card pile::take() {
+ assert(!empty());
card c = top_card();
pile_vec.pop_back();
return c;