aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharlie Blake2018-01-10 18:16:34 +0000
committerGitHub2018-01-10 18:16:34 +0000
commit2a62143fb9edb929f410b1b9b3aed16a16ceba39 (patch)
treed5d7839783cebb097af9f071751d693f47e3af6d /src
parentMerge pull request #28 from thecharlesblake/all-canonical-games (diff)
parentAdd asserts to program and fix bugs they raise (diff)
downloadmetasol-2a62143fb9edb929f410b1b9b3aed16a16ceba39.tar.gz
metasol-2a62143fb9edb929f410b1b9b3aed16a16ceba39.zip

Merge pull request #29 from thecharlesblake/refactor-improve-debug

Refactor improve debug

Diffstat (limited to 'src')
-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.cpp17
-rw-r--r--src/main/game/pile.cpp1
-rw-r--r--src/main/game/sol_rules.h5
-rw-r--r--src/main/input-output/input/json-parsing/deal_parser.cpp31
-rw-r--r--src/main/input-output/input/json-parsing/json_helper.cpp9
-rw-r--r--src/main/input-output/input/json-parsing/json_helper.h1
-rw-r--r--src/main/input-output/input/json-parsing/rules_parser.cpp28
-rw-r--r--src/main/input-output/output/log_helper.h4
-rw-r--r--src/main/solver/solver.cpp8
12 files changed, 106 insertions, 98 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 cbc052a..7dfc87c 100644
--- a/src/main/game/game_state.legal_moves.cpp
+++ b/src/main/game/game_state.legal_moves.cpp
@@ -6,7 +6,6 @@
using namespace std;
-typedef sol_rules::build_order ord;
typedef sol_rules::build_policy pol;
typedef sol_rules::spaces_policy s_pol;
typedef sol_rules::stock_deal_type sdt;
@@ -26,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;
@@ -57,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);
}
@@ -80,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
@@ -112,11 +113,7 @@ bool game_state::is_valid_tableau_move(const pile_ref rem_ref,
}
// Checks rank
- else if (rules.build_ord == ord::DESCENDING
- && rem_c.get_rank() + 1 != add_c.get_rank()) {
- return false;
- } else if (rules.build_ord == ord::ASCENDING
- && rem_c.get_rank() - 1 != add_c.get_rank()) {
+ else if (rem_c.get_rank() + 1 != add_c.get_rank()) {
return false;
} else {
return true;
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;
diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h
index 87266ee..812e82c 100644
--- a/src/main/game/sol_rules.h
+++ b/src/main/game/sol_rules.h
@@ -13,10 +13,6 @@
struct sol_rules {
// Tableau policies
- enum class build_order {
- DESCENDING,
- ASCENDING
- };
enum class build_policy {
NO_BUILD,
SAME_SUIT,
@@ -33,7 +29,6 @@ struct sol_rules {
};
uint8_t tableau_pile_count;
- build_order build_ord;
build_policy build_pol;
spaces_policy spaces_pol;
bool move_built_group;
diff --git a/src/main/input-output/input/json-parsing/deal_parser.cpp b/src/main/input-output/input/json-parsing/deal_parser.cpp
index 6f0694d..b2a4c9d 100644
--- a/src/main/input-output/input/json-parsing/deal_parser.cpp
+++ b/src/main/input-output/input/json-parsing/deal_parser.cpp
@@ -9,7 +9,6 @@
using namespace std;
using namespace rapidjson;
-typedef sol_rules::build_order ord;
typedef sol_rules::build_policy pol;
void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) {
@@ -49,7 +48,7 @@ void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document&
const Value& json_tab_piles = doc["tableau piles"];
assert(json_tab_piles.IsArray());
- if (gs.tableau_piles.size() != gs.rules.tableau_pile_count) {
+ if (json_tab_piles.Size() != gs.rules.tableau_pile_count ) {
json_helper::json_parse_err("Incorrect number of tableau piles");
}
@@ -57,6 +56,7 @@ void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document&
p.second != end(gs.tableau_piles);
++p.first, ++p.second) {
+ assert(p.first->IsArray());
for (auto& json_card : p.first->GetArray()) {
assert(json_card.IsString());
gs.piles[*p.second].place(card(json_card.GetString()));
@@ -81,7 +81,7 @@ void deal_parser::parse_cells(game_state &gs, const Document& doc) {
const auto json_cell_arr = json_cells.GetArray();
if (json_cell_arr.Empty()) return;
- else if (gs.rules.cells != json_cell_arr.Size()) {
+ else if (json_cell_arr.Size() != gs.rules.cells) {
json_helper::json_parse_err("Incorrect number of cells");
}
@@ -101,11 +101,12 @@ void deal_parser::parse_stock(game_state &gs, const Document& doc) {
const Value &json_stock = doc["stock"];
assert(json_stock.IsArray());
- if (gs.rules.stock_size != json_stock.Size()) {
+ if (json_stock.Size() != gs.rules.stock_size) {
json_helper::json_parse_err("Incorrect stock size");
}
for (const Value& json_card : json_stock.GetArray()) {
+ assert(json_card.IsString());
gs.piles[gs.stock].place(card(json_card.GetString()));
}
}
@@ -116,21 +117,14 @@ void deal_parser::parse_reserve(game_state &gs, const Document& doc) {
const Value& json_reserve_piles = doc["reserve"];
assert(json_reserve_piles.IsArray());
+ const auto& json_card_arr = json_reserve_piles.GetArray();
+
// We treat a regular reserve like multiple single-card piles,
// but a stacked reserve as a single multiple-card pile
- if (gs.rules.reserve_stacked) {
- if (json_reserve_piles.Size() != gs.rules.reserve_size
- || json_reserve_piles.Size() != gs.rules.reserve_size) {
- json_helper::json_parse_err("Incorrect reserve size");
- }
- } else {
- if (gs.reserve.size() != gs.rules.reserve_size
- || json_reserve_piles.Size() != gs.rules.reserve_size) {
- json_helper::json_parse_err("Incorrect reserve size");
- }
+ if (json_reserve_piles.Size() != gs.rules.reserve_size) {
+ json_helper::json_parse_err("Incorrect reserve size");
}
- const auto& json_card_arr = json_reserve_piles.GetArray();
for (game_state::pile_ref i = 0; i < json_card_arr.Size(); i++) {
assert(json_card_arr[i].IsString());
game_state::pile_ref pr = gs.reserve[0];
@@ -140,9 +134,12 @@ void deal_parser::parse_reserve(game_state &gs, const Document& doc) {
}
void deal_parser::fill_foundations(game_state &gs) {
- for (uint8_t f_idx = 0; f_idx < 4; f_idx++) {
+ auto foundations_count = uint8_t(4 * (gs.rules.two_decks ? 2:1));
+ assert(gs.foundations.size() == foundations_count);
+
+ for (uint8_t f_idx = 0; f_idx < foundations_count; f_idx++) {
gs.piles[gs.foundations[f_idx]].place(
- card(card::to_suit(f_idx), 1)
+ card(card::to_suit(f_idx % uint8_t(4)), 1)
);
}
}
diff --git a/src/main/input-output/input/json-parsing/json_helper.cpp b/src/main/input-output/input/json-parsing/json_helper.cpp
index 89f4564..f53da6c 100644
--- a/src/main/input-output/input/json-parsing/json_helper.cpp
+++ b/src/main/input-output/input/json-parsing/json_helper.cpp
@@ -6,6 +6,7 @@
#include <sstream>
#include "json_helper.h"
+#include "../../output/log_helper.h"
using namespace std;
using namespace rapidjson;
@@ -31,5 +32,11 @@ Document json_helper::get_file_json(const string& filename) {
}
void json_helper::json_parse_err(const string& msg) {
- throw runtime_error("Error in JSON doc: " + msg);
+ string err_msg = "Error in JSON doc: " + msg;
+ LOG_DEBUG(err_msg);
+ throw runtime_error(err_msg);
+}
+
+void json_helper::json_parse_warning(const string& msg) {
+ LOG_WARNING("Error in JSON doc: " + msg);
}
diff --git a/src/main/input-output/input/json-parsing/json_helper.h b/src/main/input-output/input/json-parsing/json_helper.h
index 5ab24bc..c9c971c 100644
--- a/src/main/input-output/input/json-parsing/json_helper.h
+++ b/src/main/input-output/input/json-parsing/json_helper.h
@@ -12,6 +12,7 @@
class json_helper {
public:
static rapidjson::Document get_file_json(const std::string&);
+ static void json_parse_warning(const std::string& msg);
static void json_parse_err(const std::string& msg);
};
diff --git a/src/main/input-output/input/json-parsing/rules_parser.cpp b/src/main/input-output/input/json-parsing/rules_parser.cpp
index 948eb39..9154ffd 100644
--- a/src/main/input-output/input/json-parsing/rules_parser.cpp
+++ b/src/main/input-output/input/json-parsing/rules_parser.cpp
@@ -16,7 +16,6 @@ using namespace rapidjson;
// Static //
////////////
-typedef sol_rules::build_order ord;
typedef sol_rules::build_policy pol;
typedef sol_rules::spaces_policy s_pol;
typedef sol_rules::stock_deal_type sdt;
@@ -67,25 +66,11 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
if (d["tableau piles"].HasMember("count")) {
if (d["tableau piles"]["count"].IsInt()) {
sr.tableau_pile_count = static_cast<uint8_t>(d["tableau piles"]["count"].GetInt());
- } else {
- json_helper::json_parse_err("[tableau piles][count] must be an integer");
- }
- }
-
- if (d["tableau piles"].HasMember("build order")) {
- if (d["tableau piles"]["build order"].IsString()) {
- string bo_str = d["tableau piles"]["build order"].GetString();
-
- if (bo_str == "ascending") {
- sr.build_ord = ord::ASCENDING;
- } else if (bo_str == "descending") {
- sr.build_ord = ord::DESCENDING;
- } else {
- json_helper::json_parse_err("[tableau piles][build order] is invalid");
+ if (sr.tableau_pile_count > 52) {
+ json_helper::json_parse_warning("[tableau piles][count] may be too high");
}
-
} else {
- json_helper::json_parse_err("[tableau piles][build order] must be an string");
+ json_helper::json_parse_err("[tableau piles][count] must be an integer");
}
}
@@ -151,6 +136,9 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
if (d.HasMember("max rank")) {
if (d["max rank"].IsInt()) {
sr.max_rank = static_cast<card::rank_t>(d["max rank"].GetInt());
+ if (sr.max_rank > 13) {
+ json_helper::json_parse_err("[max rank] must be a valid rank");
+ }
} else {
json_helper::json_parse_err("[max rank] must be an integer");
}
@@ -180,6 +168,10 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
}
}
+ if (!sr.hole && !sr.foundations) {
+ json_helper::json_parse_err("one of [hole] and [foundations] must be true");
+ }
+
if (d.HasMember("foundations initial card")) {
if (d["foundations initial card"].IsBool()) {
sr.foundations_init_card = d["foundations initial card"].GetBool();
diff --git a/src/main/input-output/output/log_helper.h b/src/main/input-output/output/log_helper.h
index 71cc5f4..7762250 100644
--- a/src/main/input-output/output/log_helper.h
+++ b/src/main/input-output/output/log_helper.h
@@ -13,7 +13,7 @@
#define LOG_DEBUG(msg) clog << "[debug]\n" << msg << "\n"
#endif
#define LOG_INFO(msg) clog << "[info] " << msg << "\n"
-#define LOG_WARNING clog(msg) << "[warning] " << msg << "\n"
-#define LOG_ERROR(msg) cerr << "[ERROR] " << msg << "\n"
+#define LOG_WARNING(msg) clog << "[warning] " << msg << "\n"
+#define LOG_ERROR(msg) cerr << "[error] " << msg << "\n"
#endif //SOLVITAIRE_LOG_HELPER_H
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
index aaaf530..bd19351 100644
--- a/src/main/solver/solver.cpp
+++ b/src/main/solver/solver.cpp
@@ -26,7 +26,8 @@ bool solver::run() {
states_searched++;
LOG_DEBUG (state);
- frontier.emplace_back(game_state::null_move(), state.get_legal_moves());
+ frontier.emplace_back(game_state::move(0,0), state.get_legal_moves());
+ global_cache.insert(state.get_data());
while (!frontier.empty()) {
node& current = frontier.back();
@@ -40,11 +41,13 @@ bool solver::run() {
// (unless it's the null first move)
if (frontier.size() > 1) {
state.undo_move(current.move);
+ assert(global_cache.count(state.get_data()) == 1);
+ LOG_DEBUG (state);
}
// Returns to the previous state
frontier.pop_back();
} else {
- game_state::move next_move = current.unsearched_children[current.unsearched_children.size()-1];
+ game_state::move next_move = current.unsearched_children.back();
current.unsearched_children.pop_back();
// Applies the first possible move in this state
@@ -61,6 +64,7 @@ bool solver::run() {
} else {
// If we've seen the state before, undoes the move
state.undo_move(next_move);
+ assert(global_cache.count(state.get_data()) == 1);
}
}
}