diff options
| author | Charlie Blake | 2018-04-04 16:02:31 +0100 |
|---|---|---|
| committer | GitHub | 2018-04-04 16:02:31 +0100 |
| commit | 9bdacbca71f402507ba2a09929a9cadeaadcfdd0 (patch) | |
| tree | 2f6a6473993d6df448f16253950f03c9f5c5031b /src/main | |
| parent | Merge pull request #47 from thecharlesblake/re-add-schema (diff) | |
| parent | Add legal move gen tests and refactor (diff) | |
| download | metasol-9bdacbca71f402507ba2a09929a9cadeaadcfdd0.tar.gz metasol-9bdacbca71f402507ba2a09929a9cadeaadcfdd0.zip | |
Merge pull request #48 from thecharlesblake/wider-testing
Wider testing
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/game/game_state.h | 3 | ||||
| -rw-r--r-- | src/main/game/game_state.legal_moves.cpp | 12 | ||||
| -rw-r--r-- | src/main/game/pile.cpp | 4 | ||||
| -rw-r--r-- | src/main/game/pile.h | 4 | ||||
| -rw-r--r-- | src/main/input-output/input/json-parsing/deal_parser.cpp | 24 | ||||
| -rw-r--r-- | src/main/input-output/input/json-parsing/deal_parser.h | 3 | ||||
| -rw-r--r-- | src/main/input-output/input/json-parsing/rules_parser.cpp | 25 | ||||
| -rw-r--r-- | src/main/input-output/input/json-parsing/rules_parser.h | 2 | ||||
| -rw-r--r-- | src/main/solver/solver.cpp | 11 |
9 files changed, 49 insertions, 39 deletions
diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 409fee9..a49db2c 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -30,6 +30,7 @@ public: struct move { move(pile_ref, pile_ref, pile::size_type = 1); bool is_dominance() const; + friend bool operator==(const move&, const move&); const static pile::size_type dominance_flag; pile_ref from; pile_ref to; pile::size_type count; @@ -51,7 +52,7 @@ public: std::vector<move> get_legal_moves( #ifndef NO_CARD_JUST_MOVED - move + move = move(255, 255, 255) #endif ); diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp index f0502d1..c137b07 100644 --- a/src/main/game/game_state.legal_moves.cpp +++ b/src/main/game/game_state.legal_moves.cpp @@ -22,8 +22,10 @@ vector<game_state::move> game_state::get_legal_moves( vector<move> moves; // If we have a stock that can deal to the tableau piles, adds this move - if (rules.stock_deal_t == sdt::TABLEAU_PILES && !piles[stock].empty()) { - // Only does this if all the tableau piles are empty + if (rules.stock_size > 0 + && rules.stock_deal_t == sdt::TABLEAU_PILES + && !piles[stock].empty()) { + // Only does this if none of the tableau piles are empty bool no_tableau_empty = true; for (auto tab_ref : tableau_piles) { if (piles[tab_ref].empty()) { @@ -394,3 +396,9 @@ bool game_state::dominance_blocks_foundation_move(pile_ref target_pile) { return blocked; } + +bool operator==(const game_state::move& a, const game_state::move& b) { + return (a.from == b.from ) + && (a.to == b.to ) + && (a.count == b.count); +} diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp index 382fa50..3136681 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -53,6 +53,10 @@ bool operator==(const pile& a, const pile& b) { return a.pile_vec == b.pile_vec; } +bool operator!=(const pile& a, const pile& b) { + return a.pile_vec != b.pile_vec; +} + bool operator<(const pile& a, const pile& b) { if (a.size() == b.size()) { return a.pile_vec < b.pile_vec; diff --git a/src/main/game/pile.h b/src/main/game/pile.h index 654f5be..d86ea37 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -33,15 +33,17 @@ public: card operator[] (size_type) const; friend bool operator==(const pile&, const pile&); + friend bool operator!=(const pile&, const pile&); friend bool operator<(const pile&, const pile&); friend bool operator>(const pile&, const pile&); friend bool operator<=(const pile&, const pile&); friend bool operator>=(const pile&, const pile&); -private: + // Modify void place(card); card take(); +private: // Underlying vector std::vector<card> pile_vec; }; 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 17a8ae4..f841c8d 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -48,11 +48,18 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { fill_foundations(gs); } - apply_deal_schema(doc); // Throws an error if it fails + Document d; + d.Parse(deal_schema_json().c_str()); + assert(!d.HasParseError()); + SchemaDocument sd(d); + SchemaValidator validator(sd); + if(!doc.Accept(validator)) { + throw runtime_error(json_helper::schema_err_str(validator)); + } } -void deal_parser::apply_deal_schema(const rapidjson::Document &doc) { - const char* schema_json = R"( +string deal_parser::deal_schema_json() { + string schema_json = R"( { "$schema": "http://json-schema.org/draft-04/schema#", "description": "JSON Schema representing a solitaire game state to solve", @@ -75,16 +82,7 @@ void deal_parser::apply_deal_schema(const rapidjson::Document &doc) { }, "additionalProperties": false } )"; - - Document d; - d.Parse(schema_json); - assert(!d.HasParseError()); - SchemaDocument schema(d); - SchemaValidator validator(schema); - - if(!doc.Accept(validator)) { - throw runtime_error(json_helper::schema_err_str(validator)); - } + return schema_json; } void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& doc) { diff --git a/src/main/input-output/input/json-parsing/deal_parser.h b/src/main/input-output/input/json-parsing/deal_parser.h index bfb7246..7aa3afe 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.h +++ b/src/main/input-output/input/json-parsing/deal_parser.h @@ -16,14 +16,13 @@ class game_state; class deal_parser { public: static void parse(game_state&, const rapidjson::Document&); -private: static void parse_tableau_piles(game_state&, const rapidjson::Document&); static void parse_hole(game_state&, const rapidjson::Document&); static void parse_cells(game_state&, const rapidjson::Document&); static void parse_stock(game_state&, const rapidjson::Document&); static void parse_reserve(game_state&, const rapidjson::Document&); static void fill_foundations(game_state&); - static void apply_deal_schema(const rapidjson::Document &); + static std::string deal_schema_json(); }; #endif //SOLVITAIRE_DEAL_PARSER_H 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 800f90e..3c4bee3 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -272,11 +272,19 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { "must be true"); } - apply_rules_schema(d); // Throws an error if it fails + rules_schema_json(); // Throws an error if it fails + Document vd; + vd.Parse(rules_schema_json().c_str()); + assert(!vd.HasParseError()); + SchemaDocument sd(vd); + SchemaValidator validator(sd); + if(!d.Accept(validator)) { + throw runtime_error(json_helper::schema_err_str(validator)); + } } -void rules_parser::apply_rules_schema(const rapidjson::Document &doc) { - const char* schema_json = R"( +string rules_parser::rules_schema_json() { + string schema_json = R"( { "$schema": "http://json-schema.org/draft-04/schema#", "description": "JSON Schema representing a generic solitaire game", @@ -309,14 +317,5 @@ void rules_parser::apply_rules_schema(const rapidjson::Document &doc) { }, "additionalProperties": false } )"; - - Document d; - d.Parse(schema_json); - assert(!d.HasParseError()); - SchemaDocument schema(d); - SchemaValidator validator(schema); - - if(!doc.Accept(validator)) { - throw runtime_error(json_helper::schema_err_str(validator)); - } + return schema_json; } diff --git a/src/main/input-output/input/json-parsing/rules_parser.h b/src/main/input-output/input/json-parsing/rules_parser.h index 497caaf..fdfe018 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.h +++ b/src/main/input-output/input/json-parsing/rules_parser.h @@ -11,11 +11,11 @@ class rules_parser { public: static const sol_rules from_file(std::string); static const sol_rules from_preset(std::string); + static std::string rules_schema_json(); private: static sol_rules get_default(); static void modify_sol_rules(sol_rules&, rapidjson::Document&); - static void apply_rules_schema(const rapidjson::Document&); }; diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 474800c..2fca763 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -61,14 +61,14 @@ solver::sol_state solver::run_with_cutoff( if (cutoff_depth && depth >= *cutoff_depth) { cutoff_triggered = true; - states_exhausted = revert_to_last_node_with_children();//TODO- + states_exhausted = revert_to_last_node_with_children(); } else { // If there is a dominance move available, adds it to the search tree // and repeats. Doesn't cache the state. optional<game_state::move> dominance_move = state.get_dominance_move(); if (dominance_move) { // Adds the dominance move as a child of the current search node - add_child(*dominance_move);//TODO+ + add_child(*dominance_move); } else { // Caches the current state bool is_new_state = cache.insert(state); @@ -78,14 +78,14 @@ solver::sol_state solver::run_with_cutoff( // If there are none, reverts to the last node with children if (next_moves.empty()) { - states_exhausted = revert_to_last_node_with_children();//TODO- + states_exhausted = revert_to_last_node_with_children(); } else { - add_children(next_moves);//TODO+ + add_children(next_moves); } } // If the state is not a new one, reverts to the last node with children else { - states_exhausted = revert_to_last_node_with_children();//TODO- + states_exhausted = revert_to_last_node_with_children(); } } } @@ -175,7 +175,6 @@ void solver::print_solution() const { if (states_searched > 1) { while (!n->children.empty()) { - // TODO print move as well state_copy.make_move(n->move); cout << state_copy << "\n"; n = &n->children.back(); |
