diff options
| author | Charlie Blake | 2018-07-06 17:27:39 +0100 |
|---|---|---|
| committer | GitHub | 2018-07-06 17:27:39 +0100 |
| commit | 0e685d6e4a343daab945f5fb7d0cf250c077b6ff (patch) | |
| tree | 634eede8d39d3e313c1206cb2b9a6f6205570f8c /src | |
| parent | Merge pull request #74 from thecharlesblake/sol-streamliner-variants (diff) | |
| parent | Refactor k-plus impl to simplify and fix deal1 bug (diff) | |
| download | metasol-0e685d6e4a343daab945f5fb7d0cf250c077b6ff.tar.gz metasol-0e685d6e4a343daab945f5fb7d0cf250c077b6ff.zip | |
Merge pull request #75 from thecharlesblake/refactor-rules-json
Refactor rules json
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/game/move.cpp | 9 | ||||
| -rw-r--r-- | src/main/game/move.h | 7 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.auto_foundation_moves.cpp | 6 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.cpp | 73 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.h | 4 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.legal_moves.cpp | 95 | ||||
| -rw-r--r-- | src/main/game/sol_rules.cpp | 4 | ||||
| -rw-r--r-- | src/main/game/sol_rules.h | 4 | ||||
| -rw-r--r-- | src/main/input-output/input/json-parsing/deal_parser.cpp | 2 | ||||
| -rw-r--r-- | src/main/input-output/input/json-parsing/rules_parser.cpp | 327 | ||||
| -rw-r--r-- | src/main/input-output/input/sol_preset_types.cpp | 392 | ||||
| -rw-r--r-- | src/main/input-output/output/state_printer.cpp | 38 | ||||
| -rw-r--r-- | src/main/input-output/output/state_printer.h | 1 | ||||
| -rw-r--r-- | src/main/solver/solver.cpp | 12 | ||||
| -rw-r--r-- | src/test/test_helper.cpp | 3 | ||||
| -rw-r--r-- | src/test/unit_tests/k_plus_stock_test.cpp | 152 | ||||
| -rw-r--r-- | src/test/unit_tests/legal_move_gen_test.cpp | 6 | ||||
| -rw-r--r-- | src/test/unit_tests/rules_parser_test.cpp | 377 |
18 files changed, 702 insertions, 810 deletions
diff --git a/src/main/game/move.cpp b/src/main/game/move.cpp index 915bac6..528b631 100644 --- a/src/main/game/move.cpp +++ b/src/main/game/move.cpp @@ -4,11 +4,12 @@ #include "move.h" -move::move(mtype ty, pile::ref f, pile::ref t, pile::size_type i, bool reveal_move_) : - type(ty), from(f), to(t), count(i), reveal_move(reveal_move_) { +move::move(mtype ty, pile::ref f, pile::ref t, int8_t count_, bool reveal_move_, bool flip_waste_) : + type(ty), from(f), to(t), count(count_), reveal_move(reveal_move_), flip_waste(flip_waste_) { #ifndef NDEBUG - if (ty == mtype::regular) assert(i == 1); - else if (ty != mtype::stock_k_plus) assert(i >= 1); + if (ty == mtype::regular) assert(count == 1); + if (ty == mtype::stock_k_plus) assert(!reveal_move_); + if (ty != mtype::stock_k_plus) assert(count >= 1 && !flip_waste_); #endif } diff --git a/src/main/game/move.h b/src/main/game/move.h index 5e6892f..27ba279 100644 --- a/src/main/game/move.h +++ b/src/main/game/move.h @@ -10,7 +10,7 @@ struct move { enum class mtype : uint8_t {regular, dominance, built_group, stock_k_plus, stock_to_all_tableau, null}; - explicit move(mtype, pile::ref = 255 , pile::ref = 255, pile::size_type = 1, bool reveal_move_ = false); + explicit move(mtype, pile::ref = 255 , pile::ref = 255, int8_t = 1, bool reveal_move_ = false, bool flip_waste_ = false); friend bool operator==(const move&, const move&); void make_reveal_move(); @@ -18,8 +18,11 @@ struct move { mtype type; pile::ref from; pile::ref to; - pile::size_type count; + // For built piles the size of the pile, or for stock k-plus moves, + // the number of cards to be moved from the stock to the waste before the waste card is played on the foundations + int8_t count; bool reveal_move; + bool flip_waste; }; #endif //SOLVITAIRE_MOVE_H diff --git a/src/main/game/search-state/game_state.auto_foundation_moves.cpp b/src/main/game/search-state/game_state.auto_foundation_moves.cpp index 43f8911..b74948a 100644 --- a/src/main/game/search-state/game_state.auto_foundation_moves.cpp +++ b/src/main/game/search-state/game_state.auto_foundation_moves.cpp @@ -12,7 +12,7 @@ using std::max; bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const { - if (rules.foundations_comp_piles) + if (rules.foundations_only_comp_piles) return false; else if ( stream_opts == sos::AUTO_FOUNDATIONS || stream_opts == sos::BOTH @@ -65,7 +65,7 @@ bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const { boost::optional<move> game_state::get_dominance_move() const { #ifndef NO_AUTO_FOUNDATIONS // If there are 2 decks or no foundations, return - if (!rules.foundations || rules.two_decks) + if (!rules.foundations_present || rules.two_decks) return boost::none; // Cycles through the piles and sees if any cards can be automatically moved @@ -132,7 +132,7 @@ bool game_state::dominance_blocks_foundation_move(pile::ref target_pile) { // Decides which 'auto foundation' booleans should be 'true' currently void game_state::update_auto_foundation_moves(pile::ref target_pile) { // If there are no foundations, or this is not a foundation move, do nothing - if (!rules.foundations + if (!rules.foundations_present || target_pile < foundations.front() || target_pile > foundations.back()) { return; diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index a1d60bf..0720f6e 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -58,7 +58,7 @@ game_state::game_state(const sol_rules& s_rules, streamliner_options stream_opts } // If there are foundation piles, creates the relevant pile vectors - if (rules.foundations) { + if (rules.foundations_present) { for (uint8_t i = 0; i < 4*(rules.two_decks ? 2:1); i++) { piles.emplace_back(); foundations.push_back(static_cast<pile::ref>(piles.size() - 1)); @@ -378,67 +378,78 @@ void game_state::undo_built_group_move(move m) { } void game_state::make_stock_k_plus_move(const move m) { +#ifndef NDEBUG assert(rules.stock_deal_t == sdt::WASTE); - assert(m.count < piles[stock].size() + piles[waste].size()); + assert(m.from == stock); + if (rules.stock_redeal) assert(m.count <= piles[stock].size() && m.count > -piles[waste].size()); assert(!rules.stock_redeal || m.to != waste); -#ifndef NDEBUG auto sz_before = piles[stock].size() + piles[waste].size(); #endif - // if redeal -> stock tableau move implies s+w-=1 - // Index of the card to be moved - auto count = m.count % (piles[stock].size() + piles[waste].size()); + // Transfers count cards from the stock to the waste + if (m.count > 0) { + for (int i = 0; i < m.count; i++) { + place_card(waste, take_card(stock)); + } + } else { + for (int i = 0; i > m.count; i--) { + place_card(stock, take_card(waste)); + } + } - for (int i = 0; i <= count; i++) { - if (i < count) place_card(waste, take_card(stock)); - else place_card(m.to, take_card(stock)); + // Moves the card on top of the waste to the target pile + place_card(m.to, take_card(waste)); - if (rules.stock_redeal) { - // If the stock is empty, flips it - // (invariant: no search state has an empty stock if rules.stock_size > 0 && rules.stock_redeal) - if (piles[stock].empty()) - while (!piles[waste].empty()) - place_card(stock, take_card(waste)); + // Flips the waste back on to the empty stock (if necessary) + if (m.flip_waste) { + assert(rules.stock_redeal && piles[stock].empty()); + while (!piles[waste].empty()) { + place_card(stock, take_card(waste)); } } #ifndef NDEBUG auto sz_after = piles[stock].size() + piles[waste].size(); assert(sz_before == sz_after + 1); -#endif assert(!(rules.stock_size > 0 && rules.stock_redeal && piles[stock].empty() && !piles[waste].empty())); assert(piles[stock].size() <= rules.stock_size); +#endif } void game_state::undo_stock_k_plus_move(move m) { +#ifndef NDEBUG assert(rules.stock_deal_t == sdt::WASTE); - assert(m.count < piles[stock].size() + piles[waste].size() + 1); + assert(m.from == stock); assert(!rules.stock_redeal || m.to != waste); -#ifndef NDEBUG auto sz_after = piles[stock].size() + piles[waste].size(); #endif - auto count = m.count % (piles[stock].size() + piles[waste].size() + 1); - - for (int i = count; i >= 0; i--) { - if (rules.stock_redeal) { - // If the waste is empty, flips it - // (invariant: no search state has an empty stock if rules.stock_size > 0 && rules.stock_redeal) - if (piles[waste].empty()) - while (!piles[stock].empty()) - place_card(waste, take_card(stock)); + if (m.flip_waste) { + assert(rules.stock_redeal && piles[waste].empty()); + while (!piles[stock].empty()) { + place_card(waste, take_card(stock)); } + } - if (i < count) place_card(stock, take_card(waste)); - else place_card(stock, take_card(m.to )); + place_card(waste, take_card(m.to)); + + if (m.count > 0) { + for (int i = 0; i < m.count; i++) { + place_card(stock, take_card(waste)); + } + } else { + for (int i = 0; i > m.count; i--) { + place_card(waste, take_card(stock)); + } } #ifndef NDEBUG auto sz_before = piles[stock].size() + piles[waste].size(); assert(sz_before == sz_after + 1); -#endif + if (rules.stock_redeal) assert(m.count <= piles[stock].size() && m.count > -piles[waste].size()); assert(!(rules.stock_size > 0 && rules.stock_redeal && piles[stock].empty() && !piles[waste].empty())); assert(piles[stock].size() <= rules.stock_size); +#endif } void game_state::make_stock_to_all_tableau_move(move m) { @@ -514,7 +525,7 @@ bool game_state::is_solved() const { solved = piles[hole].size() == rules.max_rank * 4 * (rules.two_decks ? 2 : 1); } else { - assert(rules.foundations); + assert(rules.foundations_present); for (auto f : foundations) { if (piles[f].size() != rules.max_rank) { solved = false; diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index ecb8ace..3f354d1 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -90,11 +90,11 @@ private: bool stock_can_deal_all_tableau() const; move get_stock_to_all_tableau_move() const; - std::set<pile::size_type> generate_stock_moves_to_check() const; + std::set<std::pair<int8_t, bool>> generate_k_plus_moves_to_check() const; void add_stock_to_cell_move(std::vector<move>&, pile::ref) const; void add_stock_to_tableau_moves(std::vector<move>&) const; void add_stock_to_hole_foundation_moves(std::vector<move>&) const; - card stock_card_from_index(pile::size_type) const; + card stock_card_from_count(int8_t) const; void add_foundation_complete_piles_moves(std::vector<move> &) const; bool is_valid_tableau_move(pile::ref, pile::ref) const; diff --git a/src/main/game/search-state/game_state.legal_moves.cpp b/src/main/game/search-state/game_state.legal_moves.cpp index 7dd75d8..f6fb68f 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -119,7 +119,7 @@ vector<move> game_state::get_legal_moves(move parent_move) { } // Tableau / cells / reserve / stock-waste to hole / foundation moves - if (rules.hole || (rules.foundations && !rules.foundations_comp_piles)) { + if (rules.hole || (rules.foundations_present && !rules.foundations_only_comp_piles)) { // Stock if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty()) add_stock_to_hole_foundation_moves(moves); @@ -139,7 +139,7 @@ vector<move> game_state::get_legal_moves(move parent_move) { } } - if (rules.foundations_comp_piles) // i.e. Spider-type winning condition + if (rules.foundations_only_comp_piles) // i.e. Spider-type winning condition add_foundation_complete_piles_moves(moves); if (rules.tableau_pile_count > 0 && rules.face_up != fu::ALL) @@ -171,94 +171,67 @@ move game_state::get_stock_to_all_tableau_move() const { return move(move::mtype::stock_to_all_tableau, 0, 0, stock_moves); } -set<pile::size_type> game_state::generate_stock_moves_to_check() const { - set<pile::size_type> stock_moves_to_check; +set<pair<int8_t, bool>> game_state::generate_k_plus_moves_to_check() const { + set<pair<int8_t, bool>> stock_moves_to_check; + + // If the waste isn't empty, adds the move from the top of the current waste + if (piles[waste].size() != 0) stock_moves_to_check.insert(pair<int8_t, bool>(0, false)); // Adds multiples of deal count (excluding last card) - for (auto i = static_cast<pile::size_type>(rules.stock_deal_count - 1); - i < piles[stock].size() - 1; - i += rules.stock_deal_count) { - stock_moves_to_check.insert(i); - } + for (int8_t count = rules.stock_deal_count; + count < piles[stock].size(); + count += rules.stock_deal_count) + stock_moves_to_check.insert(pair<int8_t, bool>(count, false)); // Adds last card - stock_moves_to_check.insert(static_cast<pile::size_type>(piles[stock].size() - 1)); + bool flip_waste = rules.stock_redeal; + stock_moves_to_check.insert(pair<int8_t, bool>(static_cast<int8_t>(piles[stock].size()), flip_waste)); // If the stock can be redealt, searches through the waste then the stock again - if (rules.stock_redeal) { - auto stock_waste_sz = piles[waste].size() + piles[stock].size(); - for (auto i = piles[stock].size() - 1 + rules.stock_deal_count; - i < piles[stock].size() + stock_waste_sz - 1; - i += rules.stock_deal_count) { - stock_moves_to_check.insert(static_cast<pile::size_type>(i % stock_waste_sz)); - } - } + if (rules.stock_redeal) + for (auto count = static_cast<int8_t>(-piles[waste].size() + rules.stock_deal_count); + count < piles[stock].size(); + count += rules.stock_deal_count) + stock_moves_to_check.insert(pair<int8_t, bool>(count, false)); return stock_moves_to_check; } void game_state::add_stock_to_cell_move(std::vector<move>& moves, pile::ref empty_cell) const { - for (auto i : generate_stock_moves_to_check()) - moves.emplace_back(move::mtype::stock_k_plus, stock, empty_cell, i); - - // If there is no redeal option and the k-plus representation is not in effect, - // adds a regular move from the waste - if (!piles[waste].empty()) - moves.emplace_back(move::mtype::regular, waste, empty_cell); + for (auto k_plus_mv : generate_k_plus_moves_to_check()) + moves.emplace_back(move::mtype::stock_k_plus, stock, empty_cell, k_plus_mv.first, false, k_plus_mv.second); } void game_state::add_stock_to_tableau_moves(std::vector<move>& moves) const { // For each stock move to check, if it can be moved legally to one of the tableau // piles, adds this as a move - for (auto i : generate_stock_moves_to_check()) { - card from = stock_card_from_index(i); + for (auto k_plus_mv : generate_k_plus_moves_to_check()) { + card from = stock_card_from_count(k_plus_mv.first); - for (auto t : tableau_piles) { + for (auto t : tableau_piles) if (is_valid_tableau_move(from, t)) - moves.emplace_back(move::mtype::stock_k_plus, stock, t, i); - } - } - - // If there is no redeal option and the k-plus representation is not in effect, - // adds a regular move from the waste to valid tableau piles - if (!piles[waste].empty()) { - for (auto t : tableau_piles) { - if (is_valid_tableau_move(waste, t)) - moves.emplace_back(move::mtype::regular, waste, t); - } + moves.emplace_back(move::mtype::stock_k_plus, stock, t, k_plus_mv.first, false, k_plus_mv.second); } } void game_state::add_stock_to_hole_foundation_moves(std::vector<move>& moves) const { - for (auto i : generate_stock_moves_to_check()) { - card from = stock_card_from_index(i); + for (auto k_plus_mv : generate_k_plus_moves_to_check()) { + card from = stock_card_from_count(k_plus_mv.first); for (auto f : foundations) if (is_valid_foundations_move(from, f)) - moves.emplace_back(move::mtype::stock_k_plus, stock, f, i); - if (rules.hole && is_valid_hole_move(from)) - moves.emplace_back(move::mtype::stock_k_plus, stock, hole, i); - } - - // If there is no redeal option and the k-plus representation is not in effect, - // adds a regular move from the waste to valid foundation piles - if (!piles[waste].empty()) { - for (auto f : foundations) - if (is_valid_foundations_move(waste, f)) - moves.emplace_back(move::mtype::regular, waste, f); + moves.emplace_back(move::mtype::stock_k_plus, stock, f, k_plus_mv.first, false, k_plus_mv.second); - if (rules.hole && is_valid_hole_move(waste)) - moves.emplace_back(move::mtype::regular, waste, hole); + if (rules.hole && is_valid_hole_move(from)) + moves.emplace_back(move::mtype::stock_k_plus, stock, hole, k_plus_mv.first, false, k_plus_mv.second); } } -card game_state::stock_card_from_index(pile::size_type i) const { - if (i < piles[stock].size()) { - return piles[stock][i]; +card game_state::stock_card_from_count(int8_t count) const { + if (count > 0) { + return piles[stock][count - 1]; } else { - // If we are moving from the waste in a readeal (by looping), finds the correct card - auto _i = static_cast<pile::size_type>(piles[stock].size() + piles[waste].size() - 1 - i); - return piles[waste][_i]; + return piles[waste][-count]; } } @@ -308,7 +281,7 @@ bool game_state::is_next_tableau_card(card a, card b) const { bool game_state::is_valid_foundations_move(const pile::ref rem_ref, const pile::ref add_ref) const { - if (rem_ref == add_ref || rules.foundations_comp_piles) return false; + if (rem_ref == add_ref || rules.foundations_only_comp_piles) return false; return is_valid_foundations_move(piles[rem_ref].top_card(), add_ref); } diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index 4cef7c1..d50d852 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -19,9 +19,9 @@ sol_rules::sol_rules() : two_decks(false), max_rank(13), hole(false), - foundations(false), + foundations_present(false), foundations_init_card(false), - foundations_comp_piles(false), + foundations_only_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 a7b9ae6..8075821 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -41,10 +41,10 @@ struct sol_rules { bool two_decks; uint8_t max_rank; bool hole; - bool foundations; + bool foundations_present; bool foundations_init_card; bool foundations_removable; - bool foundations_comp_piles; + bool foundations_only_comp_piles; bool diagonal_deal; uint8_t cells; uint8_t stock_size; 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 bc31cbc..5a6dff6 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -52,7 +52,7 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { } // If the foundations begin with cards in them, fill them - if (gs.rules.foundations) { + if (gs.rules.foundations_present) { bool supplied_foundations = parse_foundations(gs, doc); if (!supplied_foundations && gs.rules.foundations_init_card) { 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 29e755b..c47ed2b 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -186,22 +186,42 @@ 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"); + if (d.HasMember("foundations")) { + if (d["foundations"].IsObject()) { + if (d["foundations"].HasMember("present")) { + if (d["foundations"]["present"].IsBool()) { + sr.foundations_present = d["foundations"]["present"].GetBool(); + } else { + json_helper::json_parse_err("[foundations][present] must be a boolean"); + } + } + + if (d["foundations"].HasMember("initial card")) { + if (d["foundations"]["initial card"].IsBool()) { + sr.foundations_init_card = d["foundations"]["initial card"].GetBool(); + } else { + json_helper::json_parse_err("[foundations][initial card] must be a boolean"); + } + } + + if (d["foundations"].HasMember("removable")) { + if (d["foundations"]["removable"].IsBool()) { + sr.foundations_removable = d["foundations"]["removable"].GetBool(); + } else { + json_helper::json_parse_err("[foundations][removable] must be a boolean"); + } + } + + if (d["foundations"].HasMember("complete piles")) { + if (d["foundations"]["complete piles"].IsBool()) { + sr.foundations_only_comp_piles = d["foundations"]["complete piles"].GetBool(); + } else { + json_helper::json_parse_err("[foundations][complete piles] must be a boolean"); + } } - } else { - json_helper::json_parse_err("[max rank] must be an integer"); - } - } - if (d.HasMember("two decks")) { - if (d["two decks"].IsBool()) { - sr.two_decks = d["two decks"].GetBool(); } else { - json_helper::json_parse_err("[two decks] must be a boolean"); + json_helper::json_parse_err("[foundations] must be an object"); } } @@ -213,38 +233,6 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } - if (d.HasMember("foundations")) { - if (d["foundations"].IsBool()) { - sr.foundations = d["foundations"].GetBool(); - } else { - json_helper::json_parse_err("[foundations] must be a boolean"); - } - } - - if (d.HasMember("foundations initial card")) { - if (d["foundations initial card"].IsBool()) { - sr.foundations_init_card = d["foundations initial card"].GetBool(); - } else { - json_helper::json_parse_err("[foundations initial card] must be a boolean"); - } - } - - if (d.HasMember("foundations removable")) { - if (d["foundations removable"].IsBool()) { - sr.foundations_removable = d["foundations removable"].GetBool(); - } else { - json_helper::json_parse_err("[foundations removable] must be a boolean"); - } - } - - 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()); @@ -253,61 +241,92 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } - if (d.HasMember("stock size")) { - if (d["stock size"].IsInt()) { - sr.stock_size = static_cast<uint8_t>(d["stock size"].GetInt()); - } else { - json_helper::json_parse_err("[stock size] must be an integer"); - } - } + if (d.HasMember("stock")) { + if (d["stock"].IsObject()) { + if (d["stock"].HasMember("size")) { + if (d["stock"]["size"].IsInt()) { + sr.stock_size = static_cast<uint8_t>(d["stock"]["size"].GetInt()); + } else { + json_helper::json_parse_err("[stock][size] must be an integer"); + } + } - if (d.HasMember("stock deal type")) { - if (d["stock deal type"].IsString()) { - if(string(d["stock deal type"].GetString()) == "waste") { - sr.stock_deal_t = sdt::WASTE; - } else { - sr.stock_deal_t = sdt::TABLEAU_PILES; + if (d["stock"].HasMember("deal type")) { + if (d["stock"]["deal type"].IsString()) { + if(string(d["stock"]["deal type"].GetString()) == "waste") { + sr.stock_deal_t = sdt::WASTE; + } else { + sr.stock_deal_t = sdt::TABLEAU_PILES; + } + } else { + json_helper::json_parse_err("[stock][deal type] must be a string"); + } + } + + if (d["stock"].HasMember("deal count")) { + if (d["stock"]["deal count"].IsInt()) { + sr.stock_deal_count = static_cast<uint8_t>(d["stock"]["deal count"].GetInt()); + } else { + json_helper::json_parse_err("[stock][deal count] must be an integer"); + } } - } else { - json_helper::json_parse_err("[stock deal type] must be a string"); - } - } - if (d.HasMember("stock deal count")) { - if (d["stock deal count"].IsInt()) { - sr.stock_deal_count = static_cast<uint8_t>(d["stock deal count"].GetInt()); + if (d["stock"].HasMember("redeal")) { + if (d["stock"]["redeal"].IsBool()) { + sr.stock_redeal = static_cast<uint8_t>(d["stock"]["redeal"].GetBool()); + } else { + json_helper::json_parse_err("[stock][redeal] must be a boolean"); + } + } } else { - json_helper::json_parse_err("[stock deal count] must be an integer"); + json_helper::json_parse_err("[stock] must be an object"); } } - if (d.HasMember("stock redeal")) { - if (d["stock redeal"].IsBool()) { - sr.stock_redeal = static_cast<uint8_t>(d["stock redeal"].GetBool()); + if (d.HasMember("reserve")) { + if (d["reserve"].IsObject()) { + if (d["reserve"].HasMember("size")) { + if (d["reserve"]["size"].IsInt()) { + sr.reserve_size = static_cast<uint8_t>(d["reserve"]["size"].GetInt()); + } else { + json_helper::json_parse_err("[size] must be an integer"); + } + } + + if (d["reserve"].HasMember("stacked")) { + if (d["reserve"]["stacked"].IsBool()) { + sr.reserve_stacked = d["reserve"]["stacked"].GetBool(); + } else { + json_helper::json_parse_err("[stacked] must be a boolean"); + } + } } else { - json_helper::json_parse_err("[stock redeal] must be a boolean"); + json_helper::json_parse_err("[reserve] must be an object"); } } - if (d.HasMember("reserve size")) { - if (d["reserve size"].IsInt()) { - sr.reserve_size = static_cast<uint8_t>(d["reserve size"].GetInt()); + 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("[reserve size] must be an integer"); + json_helper::json_parse_err("[max rank] must be an integer"); } } - if (d.HasMember("reserve stacked")) { - if (d["reserve stacked"].IsBool()) { - sr.reserve_stacked = d["reserve stacked"].GetBool(); + if (d.HasMember("two decks")) { + if (d["two decks"].IsBool()) { + sr.two_decks = d["two decks"].GetBool(); } else { - json_helper::json_parse_err("[reserve stacked] must be a boolean"); + json_helper::json_parse_err("[two decks] must be a boolean"); } } int solution_types = 0; if (sr.hole) solution_types++; - if (sr.foundations) solution_types++; + if (sr.foundations_present) solution_types++; if (solution_types != 1) { json_helper::json_parse_err("one and only one of [hole] and [foundations] " "must be true"); @@ -329,36 +348,130 @@ string rules_parser::rules_schema_json() { { "$schema": "http://json-schema.org/draft-04/schema#", "description": "JSON Schema representing a generic solitaire game", - - "type": "object", "properties": { - + "type": "object", + "properties": { "tableau piles": { - "type": "object", "properties": { - "count": {"type": "integer", "minimum": 0}, - "build policy": {"type": "string", "enum": ["any-suit", "red-black", "same-suit", "no-build"]}, - "spaces policy": {"type": "string", "enum": ["any", "no-build", "kings"]}, - "diagonal deal": {"type": "boolean"}, - "move built group": {"type": "boolean"}, - "move built group policy": {"type": "string", "enum": ["same-as-build", "any-suit", "red-black", "same-suit", "no-build"]}, - "face up cards": {"type": "string", "enum": ["all", "top"]} - }, "additionalProperties": false + "type": "object", + "properties": { + "count": { + "type": "integer", + "minimum": 0 + }, + "build policy": { + "type": "string", + "enum": [ + "any-suit", + "red-black", + "same-suit", + "no-build" + ] + }, + "spaces policy": { + "type": "string", + "enum": [ + "any", + "no-build", + "kings" + ] + }, + "diagonal deal": { + "type": "boolean" + }, + "move built group": { + "type": "boolean" + }, + "move built group policy": { + "type": "string", + "enum": [ + "same-as-build", + "any-suit", + "red-black", + "same-suit", + "no-build" + ] + }, + "face up cards": { + "type": "string", + "enum": [ + "all", + "top" + ] + } + }, + "additionalProperties": false + }, + "foundations": { + "type": "object", + "properties": { + "present": { + "type": "boolean" + }, + "initial card": { + "type": "boolean" + }, + "removable": { + "type": "boolean" + }, + "only complete pile moves": { + "type": "boolean" + } + }, + "additionalProperties": false + }, + "hole": { + "type": "boolean" + }, + "cells": { + "type": "integer", + "minimum": 0 }, - "max rank": {"type": "integer", "minimum": 1, "maximum": 13}, - "two decks": {"type": "boolean"}, - "hole": {"type": "boolean"}, - "foundations": {"type": "boolean"}, - "foundations initial card": {"type": "boolean"}, - "foundations removable": {"type": "boolean"}, - "foundations complete piles": {"type": "boolean"}, - "cells": {"type": "integer", "minimum": 0}, - "stock size": {"type": "integer", "minimum": 0}, - "stock deal type": {"type": "string", "enum": ["waste", "tableau piles"]}, - "stock deal count": {"type": "integer", "minimum": 1}, - "stock redeal": {"type": "boolean"}, - "reserve size": {"type": "integer", "minimum": 0}, - "reserve stacked": {"type": "boolean"} - - }, "additionalProperties": false + "stock": { + "type": "object", + "properties": { + "size": { + "type": "integer", + "minimum": 0 + }, + "deal type": { + "type": "string", + "enum": [ + "waste", + "tableau piles" + ] + }, + "deal count": { + "type": "integer", + "minimum": 1 + }, + "redeal": { + "type": "boolean" + } + }, + "additionalProperties": false + }, + "reserve": { + "type": "object", + "properties": { + "size": { + "type": "integer", + "minimum": 0 + }, + "stacked": { + "type": "boolean" + } + }, + "additionalProperties": false + }, + "max rank": { + "type": "integer", + "minimum": 1, + "maximum": 13 + }, + "two decks": { + "type": "boolean" + } + }, + "additionalProperties": false } )"; return schema_json; diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index e540a51..fdd353a 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -29,20 +29,26 @@ map<string, string> sol_preset_types::mp = { "move built group policy": "same-as-build", "face up cards": "all" }, - "max rank": 13, - "two decks": false, + "foundations": { + "present": true, + "initial card": false, + "removable": false, + "only complete pile moves": false + }, "hole": false, - "foundations": true, - "foundations initial card": false, - "foundations removable": false, - "foundations complete piles": false, "cells": 0, - "stock size": 0, - "stock deal type": "waste", - "stock deal count": 1, - "stock redeal": false, - "reserve size": 0, - "reserve stacked": false + "stock": { + "size": 0, + "deal type": "waste", + "deal count": 1, + "redeal": false + }, + "reserve": { + "size": 0, + "stacked": false + }, + "max rank": 13, + "two decks": false } )" @@ -55,7 +61,9 @@ map<string, string> sol_preset_types::mp = { "tableau piles": { "count": 13 }, - "foundations removable": true + "foundations": { + "removable": true + } } )" @@ -68,8 +76,10 @@ map<string, string> sol_preset_types::mp = { "tableau piles": { "count": 3 }, - "max rank": 2, - "foundations removable": true + "foundations": { + "removable": true + }, + "max rank": 2 } )" @@ -96,8 +106,8 @@ map<string, string> sol_preset_types::mp = { "count": 4, "build policy": "red-black" }, - "max rank": 4, - "cells": 1 + "cells": 1, + "max rank": 4 } )" @@ -111,7 +121,9 @@ map<string, string> sol_preset_types::mp = { "count": 13, "spaces policy": "no-build" }, - "foundations removable": true + "foundations": { + "removable": true + } } )" @@ -125,8 +137,10 @@ map<string, string> sol_preset_types::mp = { "count": 3, "spaces policy": "no-build" }, - "max rank": 3, - "foundations removable": true + "foundations": { + "removable": true + }, + "max rank": 3 } )" @@ -140,8 +154,10 @@ map<string, string> sol_preset_types::mp = { "count": 17, "build policy": "no-build" }, - "hole": true, - "foundations": false + "foundations": { + "present": false + }, + "hole": true } )" @@ -155,9 +171,11 @@ map<string, string> sol_preset_types::mp = { "count": 4, "build policy": "no-build" }, - "max rank": 5, + "foundations": { + "present": false + }, "hole": true, - "foundations": false + "max rank": 5 } )" @@ -171,8 +189,12 @@ map<string, string> sol_preset_types::mp = { "count": 12, "build policy": "same-suit" }, - "foundations initial card": true, - "stock size": 36 + "foundations": { + "initial card": true + }, + "stock": { + "size": 36 + } } )" @@ -186,9 +208,13 @@ map<string, string> sol_preset_types::mp = { "count": 3, "build policy": "same-suit" }, - "max rank": 4, - "foundations initial card": true, - "stock size": 3 + "foundations": { + "initial card": true + }, + "stock": { + "size": 3 + }, + "max rank": 4 } )" @@ -201,8 +227,12 @@ map<string, string> sol_preset_types::mp = { "tableau piles": { "count": 6 }, - "reserve size": 16, - "foundations removable": true + "reserve": { + "size": 16 + }, + "foundations": { + "removable": true + } } )" @@ -215,9 +245,13 @@ map<string, string> sol_preset_types::mp = { "tableau piles": { "count": 3 }, - "max rank": 3, - "reserve size": 3, - "foundations removable": true + "reserve": { + "size": 3 + }, + "foundations": { + "removable": true + }, + "max rank": 3 } )" @@ -232,9 +266,13 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "move built group": true }, - "stock size": 34, - "reserve size": 14, - "reserve stacked": true + "stock": { + "size": 34 + }, + "reserve": { + "size": 14, + "stacked": true + } } )" @@ -249,10 +287,14 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "move built group": true }, - "max rank": 3, - "stock size": 1, - "reserve size": 2, - "reserve stacked": true + "stock": { + "size": 1 + }, + "reserve": { + "size": 2, + "stacked": true + }, + "max rank": 3 } )" @@ -267,7 +309,9 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "diagonal deal": true }, - "foundations removable": true + "foundations": { + "removable": true + } } )" @@ -282,8 +326,10 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "diagonal deal": true }, - "max rank": 2, - "foundations removable": true + "foundations": { + "removable": true + }, + "max rank": 2 } )" @@ -298,7 +344,9 @@ map<string, string> sol_preset_types::mp = { "build policy": "same-suit", "move built group": true }, - "foundations initial card": true + "foundations": { + "initial card": true + } } )" @@ -313,8 +361,10 @@ map<string, string> sol_preset_types::mp = { "build policy": "same-suit", "move built group": true }, - "max rank": 6, - "foundations initial card": true + "foundations": { + "initial card": true + }, + "max rank": 6 } )" @@ -330,10 +380,14 @@ map<string, string> sol_preset_types::mp = { "move built group": true, "move built group policy": "same-suit" }, - "foundations complete piles": true, - "two decks": true, - "stock size": 50, - "stock deal type": "tableau piles" + "foundations": { + "only complete pile moves": true + }, + "stock": { + "size": 50, + "deal type": "tableau piles" + }, + "two decks": true } )" @@ -349,11 +403,15 @@ map<string, string> sol_preset_types::mp = { "move built group": true, "move built group policy": "same-suit" }, - "foundations complete piles": true, + "foundations": { + "only complete pile moves": true + }, + "stock": { + "size": 12, + "deal type": "tableau piles" + }, "two decks": true, - "max rank": 3, - "stock size": 12, - "stock deal type": "tableau piles" + "max rank": 3 } )" @@ -371,10 +429,12 @@ map<string, string> sol_preset_types::mp = { "diagonal deal": true, "face up cards": "top" }, - "max rank": 4, - "stock size": 6, - "stock deal count": 3, - "stock redeal": true + "stock": { + "size": 6, + "deal count": 3, + "redeal": true + }, + "max rank": 4 } )" }, @@ -390,9 +450,13 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "diagonal deal": true }, - "reserve size": 6, - "foundations removable": true, - "foundations initial card": true + "reserve": { + "size": 6 + }, + "foundations": { + "removable": true, + "initial card": true + } } )" @@ -411,10 +475,14 @@ map<string, string> sol_preset_types::mp = { "diagonal deal": true, "face up cards": "top" }, - "foundations removable": true, - "stock size": 24, - "stock deal count": 1, - "stock redeal": true + "foundations": { + "removable": true + }, + "stock": { + "size": 24, + "deal count": 1, + "redeal": true + } } )" @@ -433,10 +501,14 @@ map<string, string> sol_preset_types::mp = { "diagonal deal": true, "face up cards": "top" }, - "foundations removable": true, - "stock size": 24, - "stock deal count": 3, - "stock redeal": true + "foundations": { + "removable": true + }, + "stock": { + "size": 24, + "deal count": 3, + "redeal": true + } } )" @@ -451,7 +523,9 @@ map<string, string> sol_preset_types::mp = { "build policy": "any-suit", "spaces policy": "no-build" }, - "foundations removable": true, + "foundations": { + "removable": true + }, "cells": 1 } )" @@ -466,7 +540,9 @@ map<string, string> sol_preset_types::mp = { "count": 13, "build policy": "red-black" }, - "foundations removable": true + "foundations": { + "removable": true + } } )" @@ -480,7 +556,9 @@ map<string, string> sol_preset_types::mp = { "count": 12, "build policy": "same-suit" }, - "foundations initial card": true + "foundations": { + "initial card": true + } } )" @@ -494,8 +572,10 @@ map<string, string> sol_preset_types::mp = { "count": 12, "build policy": "red-black" }, - "foundations removable": true, - "foundations initial card": true + "foundations": { + "initial card": true, + "removable": true + } } )" @@ -509,8 +589,10 @@ map<string, string> sol_preset_types::mp = { "count": 13, "build policy": "red-black" }, - "foundations removable": true, - "foundations initial card": true, + "foundations": { + "initial card": true, + "removable": true + }, "cells": 1 } )" @@ -525,8 +607,10 @@ map<string, string> sol_preset_types::mp = { "count": 13, "build policy": "red-black" }, - "foundations removable": true, - "foundations initial card": true, + "foundations": { + "initial card": true, + "removable": true + }, "cells": 2 } )" @@ -706,7 +790,9 @@ map<string, string> sol_preset_types::mp = { "spaces policy": "kings" }, "cells": 7, - "foundations initial card": true + "foundations": { + "initial card": true + } } )" @@ -737,9 +823,15 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "move built group": true }, - "reserve size": 12, - "foundations initial card": true, - "stock size": 36 + "foundations": { + "initial card": true + }, + "reserve": { + "size": 12 + }, + "stock": { + "size": 36 + } } )" @@ -754,9 +846,13 @@ map<string, string> sol_preset_types::mp = { "build policy": "any-suit", "move built group": true }, - "reserve size": 13, - "reserve stacked": true, - "stock size": 36 + "reserve": { + "size": 13, + "stacked": true + }, + "stock": { + "size": 36 + } } )" @@ -771,7 +867,9 @@ map<string, string> sol_preset_types::mp = { "build policy": "same-suit", "spaces policy": "kings" }, - "foundations removable": true + "foundations": { + "removable": true + } } )" @@ -786,7 +884,9 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "spaces policy": "no-build" }, - "foundations removable": true + "foundations": { + "removable": true + } } )" @@ -801,9 +901,13 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "move built group": true }, - "stock size": 30, - "foundations initial card": true, - "foundations removable": true + "foundations": { + "initial card": true, + "removable": true + }, + "stock": { + "size": 30 + } } )" @@ -819,9 +923,13 @@ map<string, string> sol_preset_types::mp = { "move built group": true, "face up cards": "top" }, - "foundations removable": true, - "stock size": 31, - "stock deal type": "tableau piles" + "foundations": { + "removable": true + }, + "stock": { + "size": 31, + "deal type": "tableau piles" + } } )" @@ -837,8 +945,12 @@ map<string, string> sol_preset_types::mp = { "spaces policy": "kings", "diagonal deal": true }, - "foundations removable": true, - "reserve size": 16 + "foundations": { + "removable": true + }, + "reserve": { + "size": 16 + } } )" @@ -853,8 +965,12 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "diagonal deal": true }, - "foundations removable": true, - "reserve size": 7 + "foundations": { + "removable": true + }, + "reserve": { + "size": 7 + } } )" @@ -871,9 +987,13 @@ map<string, string> sol_preset_types::mp = { "move built group policy": "same-suit", "diagonal deal": true }, - "foundations complete piles": true, - "stock size": 24, - "stock deal type": "tableau piles" + "foundations": { + "only complete pile moves": true + }, + "stock": { + "size": 24, + "deal type": "tableau piles" + } } )" @@ -889,70 +1009,16 @@ map<string, string> sol_preset_types::mp = { "move built group": true, "move built group policy": "same-suit" }, - "foundations complete piles": true, - "stock size": 31, - "stock deal type": "tableau piles" -} -)" - - }, - { - "test-game-black-hole", - - R"( - -{ - "tableau piles": { - "count": 7, - "build policy": "no-build" + "foundations": { + "only complete pile moves": true }, - "max rank": 8, - "hole": true, - "foundations": false + "stock": { + "size": 31, + "deal type": "tableau piles" + } } )" - }, - { - "test-game-any-suit", - R"( - -{ - "tableau piles": { - "count": 4, - "build policy": "any-suit" - }, - "max rank": 5 -} -)" - }, - { - "test-game-red-black", - - R"( - -{ - "tableau piles": { - "count": 6, - "build policy": "red-black" - }, - "max rank": 7 -} -)" - }, - { - "test-game-same-suit", - - R"( - -{ - "tableau piles": { - "count": 10, - "build policy": "same-suit" - }, - "max rank": 9 -} -)" }, { "simple-simon", @@ -965,7 +1031,9 @@ map<string, string> sol_preset_types::mp = { "move built group": true, "move built group policy": "same-suit" }, - "foundations complete piles": true + "foundations": { + "only complete pile moves": true + } } )" diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index 9d13129..67702e2 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -4,12 +4,13 @@ #include "state_printer.h" -using namespace std; +using std::ostream; +using std::vector; typedef sol_rules::stock_deal_type sdt; ostream& state_printer::print(ostream& stream, const game_state& gs) { - if (gs.rules.foundations) { + if (gs.rules.foundations_present) { state_printer::print_header(stream, "Foundations"); state_printer::print_top_of_piles(stream, gs.foundations, gs); } @@ -114,3 +115,36 @@ void state_printer::print_top_of_pile(ostream& stream, void state_printer::print_card(std::ostream& s, const card c) { s << c.to_string(); } + +void state_printer::print_move(std::ostream& s, const move m) { + s << "move("; + switch (m.type) { + case move::mtype::regular: + s << "regular"; + break; + case move::mtype::dominance: + s << "dominance"; + break; + case move::mtype::built_group: + s << "built group"; + break; + case move::mtype::stock_k_plus: + s << "stock k-plus"; + break; + case move::mtype::stock_to_all_tableau: + s << "stock-to-all-tableau"; + break; + case move::mtype::null: + s << "null"; + break; + } + s << ", " + << int(m.from) + << ", " + << int(m.to) + << ", " + << int(m.count) + << ", " + << (m.reveal_move ? "true" : "false") + << ")\n"; +} diff --git a/src/main/input-output/output/state_printer.h b/src/main/input-output/output/state_printer.h index 2b2090b..7d4a702 100644 --- a/src/main/input-output/output/state_printer.h +++ b/src/main/input-output/output/state_printer.h @@ -12,6 +12,7 @@ class state_printer { public: static std::ostream& print(std::ostream&, const game_state&); + static void print_move(std::ostream&, move); private: static void print_header(std::ostream&, const char*); diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index e600835..6b0c980 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -12,6 +12,7 @@ #include "solver.h" #include "../game/move.h" #include "../input-output/output/log_helper.h" +#include "../input-output/output/state_printer.h" using std::atomic; using std::vector; @@ -57,6 +58,7 @@ solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver) if (current_node->mv.type == move::mtype::dominance) { LOG_DEBUG("(dominance move)"); } + state_printer::print_move(clog, current_node->mv); LOG_DEBUG(state); #endif @@ -134,11 +136,19 @@ bool solver::revert_to_last_node_with_children(optional<lru_cache::item_list::it // (as long as the move wasn't a dominance move) if (current_node->mv.type != move::mtype::dominance) { - if (cache.get_states_removed_from_cache() == 0) assert(cache.contains(state)); + if (cache.get_states_removed_from_cache() == 0) { + if (!cache.contains(state)) { + state_printer::print_move(clog, current_node->mv); + LOG_DEBUG("-------"); + LOG_DEBUG(state); + assert(false); + } + } LOG_DEBUG("(undo move)"); } else { LOG_DEBUG("(undo dominance move)"); } + state_printer::print_move(clog, current_node->mv); LOG_DEBUG(state); #endif diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index c85cf39..c4f902e 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -18,13 +18,14 @@ using std::vector; using std::ostream; typedef std::initializer_list<std::initializer_list<std::string>> string_il; +typedef game_state::streamliner_options sos; bool test_helper::is_solvable(const std::string& input_file, const std::string& preset_type) { const Document in_doc = json_helper::get_file_json(input_file); const sol_rules rules = rules_parser::from_preset(preset_type); - game_state gs(rules, in_doc, false); + game_state gs(rules, in_doc, sos::NONE); solver sol(gs, 1000000); return sol.run() == solver::sol_state::solved; diff --git a/src/test/unit_tests/k_plus_stock_test.cpp b/src/test/unit_tests/k_plus_stock_test.cpp index 8a26cbe..dcd4410 100644 --- a/src/test/unit_tests/k_plus_stock_test.cpp +++ b/src/test/unit_tests/k_plus_stock_test.cpp @@ -35,10 +35,10 @@ TEST(KPlusStock, NoRedealExact) { {"2C"} }, { - move(move::mtype::stock_k_plus, 0, 4, 2), - move(move::mtype::stock_k_plus, 0, 3, 8), + move(move::mtype::stock_k_plus, 0, 4, 3), + move(move::mtype::stock_k_plus, 0, 3, 9), }, - move(move::mtype::stock_k_plus, 0, 3, 8), + move(move::mtype::stock_k_plus, 0, 3, 9), 0, 8, card(), "2D" ); @@ -63,10 +63,10 @@ TEST(KPlusStock, NoRedealOneExtra) { {"2C"} }, { - move(move::mtype::stock_k_plus, 0, 4, 2), - move(move::mtype::stock_k_plus, 0, 3, 6), + move(move::mtype::stock_k_plus, 0, 4, 3), + move(move::mtype::stock_k_plus, 0, 3, 7), }, - move(move::mtype::stock_k_plus, 0, 3, 6), + move(move::mtype::stock_k_plus, 0, 3, 7), 0, 6, card(), "AD" ); @@ -91,9 +91,9 @@ TEST(KPlusStock, NoRedealTwoExtra) { {"2C"} }, { - move(move::mtype::stock_k_plus, 0, 4, 2) + move(move::mtype::stock_k_plus, 0, 4, 3) }, - move(move::mtype::stock_k_plus, 0, 4, 2), + move(move::mtype::stock_k_plus, 0, 4, 3), 5, 2, "3C", "AH" ); @@ -118,10 +118,10 @@ TEST(KPlusStock, RedealExact) { {"2C"} }, { - move(move::mtype::stock_k_plus, 0, 4, 2), - move(move::mtype::stock_k_plus, 0, 3, 8), + move(move::mtype::stock_k_plus, 0, 4, 3, false, false), + move(move::mtype::stock_k_plus, 0, 3, 9, false, true), }, - move(move::mtype::stock_k_plus, 0, 3, 8), + move(move::mtype::stock_k_plus, 0, 3, 9, false, true), 8, 0, "AS", card() ); @@ -146,10 +146,10 @@ TEST(KPlusStock, RedealOneExtra) { {"2C"} }, { - move(move::mtype::stock_k_plus, 0, 4, 2), - move(move::mtype::stock_k_plus, 0, 3, 6), + move(move::mtype::stock_k_plus, 0, 4, 3, false, false), + move(move::mtype::stock_k_plus, 0, 3, 7, false, true), }, - move(move::mtype::stock_k_plus, 0, 3, 6), + move(move::mtype::stock_k_plus, 0, 3, 7, false, true), 6, 0, "AS", card() ); @@ -174,9 +174,9 @@ TEST(KPlusStock, RedealTwoExtra) { {"2C"} }, { - move(move::mtype::stock_k_plus, 0, 4, 2) + move(move::mtype::stock_k_plus, 0, 4, 3, false, false) }, - move(move::mtype::stock_k_plus, 0, 4, 2), + move(move::mtype::stock_k_plus, 0, 4, 3, false, false), 5, 2, "3C", "AH" ); @@ -201,13 +201,12 @@ TEST(KPlusStock, RedealKPlus3Waste) { {"2C"} }, { - move(move::mtype::stock_k_plus, 0, 3, 5), - move(move::mtype::stock_k_plus, 0, 4, 8), - move(move::mtype::regular, 1, 4, 1) + move(move::mtype::stock_k_plus, 0, 3, 6, false, true), + move(move::mtype::stock_k_plus, 0, 4, 0, false, false) }, - move(move::mtype::stock_k_plus, 0, 4, 8), - 6, 2, - "3C", "AH" + move(move::mtype::stock_k_plus, 0, 3, 6, false, true), + 8, 0, + "AS", card() ); } @@ -230,11 +229,11 @@ TEST(KPlusStock, RedealKPlus4Waste) { {"2C"} }, { - move(move::mtype::stock_k_plus, 0, 3, 4), - move(move::mtype::stock_k_plus, 0, 4, 7), - move(move::mtype::stock_k_plus, 0, 2, 1) + move(move::mtype::stock_k_plus, 0, 3, 5, false, true), + move(move::mtype::stock_k_plus, 0, 4, -1, false, false), + move(move::mtype::stock_k_plus, 0, 2, 2, false, false) }, - move(move::mtype::stock_k_plus, 0, 2, 1), + move(move::mtype::stock_k_plus, 0, 2, 2, false, false), 3, 5, "3S", "2S" ); @@ -259,11 +258,11 @@ TEST(KPlusStock, RedealKPlus5Waste) { {"2C"} }, { - move(move::mtype::stock_k_plus, 0, 3, 2), - move(move::mtype::stock_k_plus, 0, 4, 7), - move(move::mtype::stock_k_plus, 0, 2, 3) + move(move::mtype::stock_k_plus, 0, 3, 3, false, false), + move(move::mtype::stock_k_plus, 0, 4, -2, false, false), + move(move::mtype::stock_k_plus, 0, 2, 4, false, false) }, - move(move::mtype::stock_k_plus, 0, 3, 2), + move(move::mtype::stock_k_plus, 0, 3, 3, false, false), 2, 7, "2D", "AD" ); @@ -288,11 +287,10 @@ TEST(KPlusStock, RedealKPlusEmptyStock) { {"2C"} }, { - move(move::mtype::stock_k_plus, 0, 3, 5), - move(move::mtype::stock_k_plus, 0, 4, 8), - move(move::mtype::regular, 1, 4, 1) + move(move::mtype::stock_k_plus, 0, 3, 6, false, true), + move(move::mtype::stock_k_plus, 0, 4, 0, false, false) }, - move(move::mtype::stock_k_plus, 0, 3, 5), + move(move::mtype::stock_k_plus, 0, 3, 6, false, true), 8, 0, "AS", card() ); @@ -306,7 +304,7 @@ TEST(KPlusStock, NoRedealCellFoundationMoves) { sr.stock_redeal = false; sr.stock_deal_count = 3; sr.cells = 2; - sr.foundations = true; + sr.foundations_present = true; test_helper::k_plus_stock_test( sr, @@ -318,16 +316,16 @@ TEST(KPlusStock, NoRedealCellFoundationMoves) { }, { // Cell moves - move(move::mtype::stock_k_plus, 6, 5, 2), - move(move::mtype::stock_k_plus, 6, 5, 5), + move(move::mtype::stock_k_plus, 6, 5, 3), + move(move::mtype::stock_k_plus, 6, 5, 6), // Foundation moves - move(move::mtype::stock_k_plus, 6, 1, 2), + move(move::mtype::stock_k_plus, 6, 1, 3), // Regular waste move - move(move::mtype::regular, 7, 5, 1) + move(move::mtype::stock_k_plus, 6, 5, 0) }, - move(move::mtype::stock_k_plus, 6, 1, 2), + move(move::mtype::stock_k_plus, 6, 1, 3), 3, 3, "2C", "2S" ); @@ -341,7 +339,7 @@ TEST(KPlusStock, RedealCellFoundationMoves) { sr.stock_redeal = true; sr.stock_deal_count = 3; sr.cells = 2; - sr.foundations = true; + sr.foundations_present = true; test_helper::k_plus_stock_test( sr, @@ -353,20 +351,76 @@ TEST(KPlusStock, RedealCellFoundationMoves) { }, { // Cell moves - move(move::mtype::stock_k_plus, 6, 5, 2), - move(move::mtype::stock_k_plus, 6, 5, 5), - move(move::mtype::stock_k_plus, 6, 5, 1), - move(move::mtype::stock_k_plus, 6, 5, 4), + move(move::mtype::stock_k_plus, 6, 5, 3, false, false), + move(move::mtype::stock_k_plus, 6, 5, 6, false, true), + move(move::mtype::stock_k_plus, 6, 5, 2, false, false), + move(move::mtype::stock_k_plus, 6, 5, 5, false, false), // Foundation moves - move(move::mtype::stock_k_plus, 6, 1, 2), - move(move::mtype::stock_k_plus, 6, 2, 4), + move(move::mtype::stock_k_plus, 6, 1, 3, false, false), + move(move::mtype::stock_k_plus, 6, 2, 5, false, false), // Regular waste move - move(move::mtype::regular, 7, 5, 1) + move(move::mtype::stock_k_plus, 6, 5, 0) }, - move(move::mtype::stock_k_plus, 6, 2, 4), + move(move::mtype::stock_k_plus, 6, 2, 5, false, false), 1, 5, "2H", "2C" ); } + +TEST(KPlusStock, RedealCount1BottomStock) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 9; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = true; + sr.stock_deal_count = 1; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","2S","AD","2D"}, + {}, + {"3D"}, + {"3H"}, + {"2C"} + }, + { + move(move::mtype::stock_k_plus, 0, 2, 1, false, false), + move(move::mtype::stock_k_plus, 0, 3, 4, false, true) + }, + move(move::mtype::stock_k_plus, 0, 3, 4, false, true), + 3, 0, + "2D", card() + ); +} + +TEST(KPlusStock, RedealCount1TopStock) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 9; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = true; + sr.stock_deal_count = 1; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","2S","AD","2D"}, + {}, + {"3D"}, + {"3H"}, + {"2C"} + }, + { + move(move::mtype::stock_k_plus, 0, 2, 1, false, false), + move(move::mtype::stock_k_plus, 0, 3, 4, false, true) + }, + move(move::mtype::stock_k_plus, 0, 2, 1, false, false), + 3, 0, + "AD", card() + ); +} diff --git a/src/test/unit_tests/legal_move_gen_test.cpp b/src/test/unit_tests/legal_move_gen_test.cpp index cbdeae0..4625163 100644 --- a/src/test/unit_tests/legal_move_gen_test.cpp +++ b/src/test/unit_tests/legal_move_gen_test.cpp @@ -199,7 +199,7 @@ TEST(LegalMoveGen, MoveBuiltGroupFalse) { TEST(LegalMoveGen, Foundations) { sol_rules sr; - sr.foundations = true; + sr.foundations_present = true; sr.tableau_pile_count = 4; game_state gs(sr, string_il{ @@ -223,7 +223,7 @@ TEST(LegalMoveGen, Foundations) { TEST(LegalMoveGen, FoundationsRemovable) { sol_rules sr; - sr.foundations = true; + sr.foundations_present = true; sr.foundations_removable = true; sr.tableau_pile_count = 1; @@ -264,7 +264,7 @@ TEST(LegalMoveGen, Cells) { TEST(LegalMoveGen, StockDealToTableau) { sol_rules sr; - sr.foundations = true; + sr.foundations_present = true; sr.tableau_pile_count = 3; sr.stock_size = 2; sr.stock_deal_t = sdt::TABLEAU_PILES; diff --git a/src/test/unit_tests/rules_parser_test.cpp b/src/test/unit_tests/rules_parser_test.cpp deleted file mode 100644 index 31a7101..0000000 --- a/src/test/unit_tests/rules_parser_test.cpp +++ /dev/null @@ -1,377 +0,0 @@ -// -// Created by thecharlesblake on 1/11/18. -// - -#include <gtest/gtest.h> - -#include "../../../lib/rapidjson/stringbuffer.h" -#include "../../../lib/rapidjson/document.h" - -#include "../../main/input-output/input/json-parsing/rules_parser.h" -#include "../../main/input-output/input/json-parsing/json_helper.h" - -using namespace std; -using namespace rapidjson; - -void run_rules_parser_test(bool, const char*); - -// invalid int, build pol, bool, other - -TEST(RulesParser, DefaultGameRules) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 8, - "build policy": "any-suit", - "spaces policy": "any", - "diagonal deal": false, - "move built group": false, - "move built group policy": "same-as-build" - }, - "max rank": 13, - "two decks": false, - "hole": false, - "foundations": true, - "foundations initial card": false, - "foundations removable": false, - "foundations complete piles": false, - "cells": 0, - "stock size": 0, - "stock deal type": "waste", - "reserve size": 0, - "reserve stacked": false -} -)"); -} - -TEST(RulesParser, InvalidType) { - run_rules_parser_test(false, R"( -{ - "tableau piles": { - "count": "13" - } -} -)"); -} - -TEST(RulesParser, InvalidNumber) { - run_rules_parser_test(false, R"( -{ - "tableau piles": { - "count": 13 - }, - "max rank": 20 -} -)"); -} - -TEST(RulesParser, InvalidEnum) { - run_rules_parser_test(false, R"( -{ - "tableau piles": { - "count": 13, - "build policy": "red-black-green" - } -} -)"); -} - -TEST(RulesParser, InvalidRuleName) { - run_rules_parser_test(false, R"( -{ - "tableau piles": { - "count number": 13 - } -} -)"); -} - -TEST(RulesParser, SpanishPatience) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 13 - }, - "foundations removable": true -} -)"); -} - -TEST(RulesParser, SimpleSpanishPatience) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 3 - }, - "max rank": 2, - "foundations removable": true -} -)"); -} - -TEST(RulesParser, FreeCell) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "build policy": "red-black" - }, - "cells": 4 -} -)"); -} - -TEST(RulesParser, SimpleFreeCell) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 4, - "build policy": "red-black" - }, - "max rank": 4, - "cells": 1 -} -)"); -} - -TEST(RulesParser, BakersDozen) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 13, - "spaces policy": "no-build" - }, - "foundations removable": true -} -)"); -} - -TEST(RulesParser, SimpleBakersDozen) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 3, - "spaces policy": "no-build" - }, - "max rank": 3, - "foundations removable": true -} -)"); -} - -TEST(RulesParser, BlackHole) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 17, - "build policy": "no-build" - }, - "hole": true, - "foundations": false -} -)"); -} - -TEST(RulesParser, SimpleBlackHole) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 4, - "build policy": "no-build" - }, - "max rank": 5, - "hole": true, - "foundations": false -} -)"); -} - -TEST(RulesParser, FortunesFavor) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 12, - "build policy": "same-suit" - }, - "foundations initial card": true, - "stock size": 36 -} -)"); -} - -TEST(RulesParser, SimpleFortunesFavor) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 3, - "build policy": "same-suit" - }, - "max rank": 4, - "foundations initial card": true, - "stock size": 3 -} -)"); -} - -TEST(RulesParser, FlowerGarden) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 6 - }, - "reserve size": 16, - "foundations removable": true -} -)"); -} - -TEST(RulesParser, SimpleFlowerGarden) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 3 - }, - "max rank": 3, - "reserve size": 3, - "foundations removable": true -} -)"); -} - -TEST(RulesParser, Canfield) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 4, - "build policy": "red-black" - }, - "stock size": 34, - "reserve size": 14, - "reserve stacked": true -} -)"); -} - -TEST(RulesParser, SimpleCanfield) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 3, - "build policy": "red-black" - }, - "max rank": 3, - "stock size": 1, - "reserve size": 2, - "reserve stacked": true -} -)"); -} - -TEST(RulesParser, Somerset) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 10, - "build policy": "red-black", - "diagonal deal": true - }, - "foundations removable": true -} -)"); -} - -TEST(RulesParser, SimpleSomerset) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 4, - "build policy": "red-black", - "diagonal deal": true - }, - "max rank": 2, - "foundations removable": true -} -)"); -} - -TEST(RulesParser, AlphaStar) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 12, - "build policy": "same-suit", - "move built group": true - }, - "foundations initial card": true -} -)"); -} - -TEST(RulesParser, SimpleAlphaStar) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 3, - "build policy": "same-suit", - "move built group": true - }, - "max rank": 5, - "foundations initial card": true -} -)"); -} - -TEST(RulesParser, Spider) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "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": 50, - "stock deal type": "tableau piles" -} -)"); -} - -TEST(RulesParser, SimpleSpider) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "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, - "stock deal type": "tableau piles" -} -)"); -} - -void run_rules_parser_test(bool validity, const char* json) { - Document in_doc; - in_doc.Parse(json); - if (in_doc.HasParseError()) { - throw runtime_error("invalid json"); - } - - Document val_doc; - val_doc.Parse(rules_parser::rules_schema_json().c_str()); - SchemaDocument sd(val_doc); - SchemaValidator validator(sd); - bool acc = in_doc.Accept(validator); - if (acc) { - ASSERT_TRUE(validity == acc) << "JSON accepted when it should not have " - "been:\n" << json; - } else { - ASSERT_TRUE(validity == acc) << json_helper::schema_err_str(validator) - << "\n" << json; - } -} |
