diff options
| author | Charlie Blake | 2018-03-21 11:03:00 +0000 |
|---|---|---|
| committer | GitHub | 2018-03-21 11:03:00 +0000 |
| commit | 58118e89351e4c82fb1f54ebb021a1b0f9ba3ff8 (patch) | |
| tree | 559310dd2c48ed7db6af82233c5bf185d234ee30 /src | |
| parent | Merge pull request #43 from thecharlesblake/shortest-solution (diff) | |
| parent | Update benchmark spider scaling for correct rules (diff) | |
| download | metasol-58118e89351e4c82fb1f54ebb021a1b0f9ba3ff8.tar.gz metasol-58118e89351e4c82fb1f54ebb021a1b0f9ba3ff8.zip | |
Merge pull request #44 from thecharlesblake/spider-fix
Spider fix
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/game/game_state.cpp | 16 | ||||
| -rw-r--r-- | src/main/game/game_state.h | 1 | ||||
| -rw-r--r-- | src/main/game/game_state.legal_moves.cpp | 54 | ||||
| -rw-r--r-- | src/main/game/sol_rules.cpp | 2 | ||||
| -rw-r--r-- | src/main/game/sol_rules.h | 2 | ||||
| -rw-r--r-- | src/main/input-output/input/json-parsing/rules_parser.cpp | 45 | ||||
| -rw-r--r-- | src/main/input-output/input/sol_preset_types.cpp | 18 | ||||
| -rw-r--r-- | src/test/resources/spider/ComplexSolvable.json | 8 | ||||
| -rw-r--r-- | src/test/resources/spider/ComplexUnsolvable.json | 14 | ||||
| -rw-r--r-- | src/test/test_helper.cpp | 2 |
10 files changed, 127 insertions, 35 deletions
diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 6067c15..8a62939 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -231,7 +231,7 @@ void game_state::make_move(const move m) { // Handles special stock-to-tableau-piles move if (rules.stock_deal_t == sdt::TABLEAU_PILES && m.from == stock) { assert(m.from == stock); - assert(m.count == 255 ); + assert(m.to == 255 ); for (pile_ref tab_pr = original_tableau_piles.front(); tab_pr < pile_ref(original_tableau_piles.front() + m.count); @@ -269,7 +269,6 @@ 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) { @@ -281,11 +280,13 @@ void game_state::undo_move(const move m) { } // If this is not a built-pile move else if (m.count == 1 || m.is_dominance()) { + assert(m.to < piles.size()); place_card(m.from, take_card(m.to)); #ifndef NO_AUTO_FOUNDATIONS update_auto_foundation_moves(m.from); #endif } else { + assert(m.to < piles.size()); assert(m.count <= rules.max_rank); // Adds the cards to the 'from' pile for (auto pile_idx = m.count; pile_idx-- > 0;) { @@ -304,7 +305,10 @@ void game_state::undo_move(const move m) { void game_state::place_card(pile_ref pr, card c) { piles[pr].place(c); #ifndef NO_PILE_SYMMETRY - eval_pile_order(pr, true); + // If the stock deals to the tableau piles, there is no pile symmetry + if (rules.stock_size == 0 || rules.stock_deal_t != sdt::TABLEAU_PILES) { + eval_pile_order(pr, true); + } #endif } @@ -312,7 +316,10 @@ void game_state::place_card(pile_ref pr, card c) { card game_state::take_card(pile_ref pr) { card c = piles[pr].take(); #ifndef NO_PILE_SYMMETRY - eval_pile_order(pr, false); + // If the stock deals to the tableau piles, there is no pile symmetry + if (rules.stock_size == 0 || rules.stock_deal_t != sdt::TABLEAU_PILES) { + eval_pile_order(pr, false); + } #endif return c; } @@ -444,6 +451,7 @@ bool game_state::is_solved() const { solved = piles[hole].size() == rules.max_rank * 4 * (rules.two_decks ? 2 : 1); } else { + assert(rules.foundations); for (auto f : foundations) { if (piles[f].size() != rules.max_rank) { solved = false; diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index aeabc10..409fee9 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -84,6 +84,7 @@ private: void add_empty_built_group_moves(std::vector<move>&, pile_ref, pile_ref, card) const; bool dominance_blocks_foundation_move(pile_ref); + bool is_ordered_pile(pile_ref) const; void update_auto_foundation_moves(pile_ref); bool is_valid_auto_foundation_move(pile_ref) const; diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp index 99431e3..f0502d1 100644 --- a/src/main/game/game_state.legal_moves.cpp +++ b/src/main/game/game_state.legal_moves.cpp @@ -21,9 +21,19 @@ vector<game_state::move> game_state::get_legal_moves( // The next legal moves vector<move> moves; - // If we have a stock that can deal to the tableau piles, add this move + // 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()) { - moves.emplace_back(get_stock_tableau_move()); + // Only does this if all the tableau piles are empty + bool no_tableau_empty = true; + for (auto tab_ref : tableau_piles) { + if (piles[tab_ref].empty()) { + no_tableau_empty = false; + break; + } + } + if (no_tableau_empty) { + moves.emplace_back(get_stock_tableau_move()); + } } // Cycles through each pile which we may be able to remove a card from @@ -99,6 +109,22 @@ vector<game_state::move> game_state::get_legal_moves( get_built_group_moves(moves); } + // If only complete built piles can be moved to the foundations, + if (rules.foundations_comp_piles) { + for (pile_ref tab_pr : tableau_piles) { + if (is_ordered_pile(tab_pr)) { + + for (pile_ref found_pr : foundations) { + if (piles[found_pr].empty()) { + moves.emplace_back(tab_pr, found_pr, rules.max_rank); + break; + } + } + } + } + } + + return moves; } @@ -153,7 +179,7 @@ bool game_state::is_valid_tableau_move(const pile_ref rem_ref, bool game_state::is_valid_foundations_move(const pile_ref rem_ref, const pile_ref add_ref) const { - if (rem_ref == add_ref) return false; + if (rules.foundations_comp_piles || rem_ref == add_ref) return false; card rem_c = piles[rem_ref].top_card(); @@ -188,7 +214,7 @@ bool game_state::is_valid_hole_move(const pile_ref rem_ref) const { /////////////////////// void game_state::get_built_group_moves(vector<move>& moves) const { - assert(rules.build_pol != pol::NO_BUILD); + assert(rules.built_group_pol != pol::NO_BUILD); // Cycles through each pile to see if it contains a built group of size > 1 for (auto rem_ref : tableau_piles) { @@ -237,13 +263,13 @@ pile::size_type game_state::get_built_group_height(pile_ref ref) const { card c = piles[ref][group_card_idx]; // If the build policy is same suit, makes sure group has same suit - if (rules.build_pol == sol_rules::build_policy::SAME_SUIT + if (rules.built_group_pol == pol::SAME_SUIT && c.get_suit() != prev_c.get_suit()) { built_group = false; } // If the build policy is red black, makes sure group alternates // in correct phase - else if (rules.build_pol == sol_rules::build_policy::RED_BLACK + else if (rules.built_group_pol == pol::RED_BLACK && c.get_colour() == prev_c.get_colour()) { built_group = false; } @@ -271,11 +297,11 @@ const { } // Checks to see if the suits are correct - switch (rules.build_pol) { - case sol_rules::build_policy::SAME_SUIT: + switch (rules.built_group_pol) { + case pol::SAME_SUIT: return bg_low.get_suit() == add_card.get_suit(); - case sol_rules::build_policy::RED_BLACK: { + case pol::RED_BLACK: { bool same_suit = bg_low.get_suit() == add_card.get_suit(); bool even_diff = (add_card.get_rank() - bg_low.get_rank()) % 2 == 0; return (same_suit && even_diff) || (!same_suit && !even_diff); @@ -344,6 +370,16 @@ optional<game_state::move> game_state::get_dominance_move() const { return none; } +bool game_state::is_ordered_pile(pile_ref pr) const { + if (piles[pr].size() != rules.max_rank) return false; + + card::suit_t s = piles[pr][0].get_suit(); + for (card::rank_t r = 0; r < rules.max_rank; r++) { + if (piles[pr][r] != card(s, card::rank_t(r + 1))) return false; + } + return true; +} + // For games where the foundations can be removed from, this dominance blocks // the foundations being removed from if the card that were to be removed would // go 'automatically' up diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index 7311e06..592437d 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -15,11 +15,13 @@ sol_rules::sol_rules() : build_pol(pol::ANY_SUIT), spaces_pol(s_pol::ANY), move_built_group(false), + built_group_pol(pol::ANY_SUIT), two_decks(false), max_rank(13), hole(false), foundations(false), foundations_init_card(false), + foundations_comp_piles(false), diagonal_deal(false), cells(0), stock_size(0), diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index 8ebb73d..98474fc 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -32,12 +32,14 @@ struct sol_rules { build_policy build_pol; spaces_policy spaces_pol; bool move_built_group; + build_policy built_group_pol; bool two_decks; uint8_t max_rank; bool hole; bool foundations; bool foundations_init_card; bool foundations_removable; + bool foundations_comp_piles; bool diagonal_deal; uint8_t cells; uint8_t stock_size; 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 395f2e7..6fa89bc 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -56,7 +56,6 @@ sol_rules rules_parser::get_default() { } void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { - if (!d.IsObject()) { json_helper::json_parse_err("JSON doc must be object"); } @@ -109,7 +108,7 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } else { - json_helper::json_parse_err("[tableau piles][spaces policy] must be an string"); + json_helper::json_parse_err("[tableau piles][spaces policy] must be a string"); } } @@ -121,6 +120,28 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } + if (d["tableau piles"].HasMember("move built group policy")) { + if (d["tableau piles"]["move built group policy"].IsString()) { + string mbgp_str = d["tableau piles"]["move built group policy"].GetString(); + + if (mbgp_str == "same-as-build") { + sr.built_group_pol = sr.build_pol; + } else if (mbgp_str == "any-suit") { + sr.built_group_pol = pol::ANY_SUIT; + } else if (mbgp_str == "red-black") { + sr.built_group_pol = pol::RED_BLACK; + } else if (mbgp_str == "same-suit") { + sr.built_group_pol = pol::SAME_SUIT; + } else if (mbgp_str == "no-build") { + sr.built_group_pol = pol::NO_BUILD; + } else { + json_helper::json_parse_err("[tableau piles][move built group policy] is invalid"); + } + } else { + json_helper::json_parse_err("[tableau piles][move built group policy] must be a string"); + } + } + if (d["tableau piles"].HasMember("diagonal deal")) { if (d["tableau piles"]["diagonal deal"].IsBool()) { sr.diagonal_deal = d["tableau piles"]["diagonal deal"].GetBool(); @@ -169,10 +190,6 @@ 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(); @@ -189,6 +206,14 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } + if (d.HasMember("foundations complete piles")) { + if (d["foundations complete piles"].IsBool()) { + sr.foundations_comp_piles = d["foundations complete piles"].GetBool(); + } else { + json_helper::json_parse_err("[foundations complete piles] must be a boolean"); + } + } + if (d.HasMember("cells")) { if (d["cells"].IsInt()) { sr.cells = static_cast<uint8_t>(d["cells"].GetInt()); @@ -232,4 +257,12 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { json_helper::json_parse_err("[reserve stacked] must be a boolean"); } } + + int solution_types = 0; + if (sr.hole) solution_types++; + if (sr.foundations) solution_types++; + if (solution_types != 1) { + json_helper::json_parse_err("one and only one of [hole] and [foundations] " + "must be true"); + } } diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index de0fd41..1fa6009 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -25,7 +25,8 @@ std::map<string, string> sol_preset_types::mp = { "build policy": "any-suit", "spaces policy": "any", "diagonal deal": false, - "move built group": false + "move built group": false, + "move built group policy": "same-as-build" }, "max rank": 13, "two decks": false, @@ -33,6 +34,7 @@ std::map<string, string> sol_preset_types::mp = { "foundations": true, "foundations initial card": false, "foundations removable": false, + "foundations complete piles": false, "cells": 0, "stock size": 0, "stock deal type": "waste", @@ -318,10 +320,14 @@ std::map<string, string> sol_preset_types::mp = { R"( { "tableau piles": { - "count": 10 + "count": 10, + "build policy": "any-suit", + "move built group": true, + "move built group policy": "same-suit" }, + "foundations complete piles": true, "two decks": true, - "stock size": 60, + "stock size": 50, "stock deal type": "tableau piles" } )" @@ -333,8 +339,12 @@ std::map<string, string> sol_preset_types::mp = { R"( { "tableau piles": { - "count": 4 + "count": 4, + "build policy": "any-suit", + "move built group": true, + "move built group policy": "same-suit" }, + "foundations complete piles": true, "two decks": true, "max rank": 3, "stock size": 12, diff --git a/src/test/resources/spider/ComplexSolvable.json b/src/test/resources/spider/ComplexSolvable.json index 3591805..c7d96e2 100644 --- a/src/test/resources/spider/ComplexSolvable.json +++ b/src/test/resources/spider/ComplexSolvable.json @@ -3,11 +3,11 @@ ["2D","AD","3D"], ["AC","2C","3C"], ["AS","2S","3S"], - ["AH","2H","3H"] + ["3H","2H","AH"] ], "stock": [ - "2C","2D","2H","2S", + "2C","3D","3H","3S", "AC","AD","AH","AS", - "3C","3D","3H","3S" + "3C","2D","2H","2S" ] -} +}
\ No newline at end of file diff --git a/src/test/resources/spider/ComplexUnsolvable.json b/src/test/resources/spider/ComplexUnsolvable.json index 8fce03d..2379c9a 100644 --- a/src/test/resources/spider/ComplexUnsolvable.json +++ b/src/test/resources/spider/ComplexUnsolvable.json @@ -1,13 +1,13 @@ { "tableau piles": [ ["2D","AD","3D"], - ["AC","AC","3C"], - ["AS","AS","3S"], - ["AH","AH","3H"] + ["AC","2C","3C"], + ["AS","2S","3S"], + ["3H","2H","AH"] ], "stock": [ - "2C","2D","2H","2S", - "AC","2D","2H","2S", - "3C","3D","3H","3S" + "3C","3D","3H","3S", + "2C","2D","AH","AS", + "AC","AD","2H","2S" ] -} +}
\ No newline at end of file diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index da618d8..5dadeb3 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -23,7 +23,7 @@ bool test_helper::is_solvable(const std::string& input_file, const std::string& game_state gs(rules, in_doc); solver sol(gs); - return sol.run(); + return sol.run() == solver::sol_state::solved; } void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy, |
