From 40feff1bdda4bb04f0bd9dd222b512872ee16789 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Tue, 10 Jul 2018 16:39:37 +0100 Subject: Update rules schema for gaps --- .../input/json-parsing/rules_parser.cpp | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/main') 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 c47ed2b..55a414c 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -462,6 +462,32 @@ string rules_parser::rules_schema_json() { }, "additionalProperties": false }, + "sequence": { + "type": "object", + "properties": { + "count": { + "type": "integer", + "minimum": 0 + }, + "direction": { + "type": "string", + "enum": [ + "L", + "R", + "LR" + ] + }, + "build policy": { + "type": "string", + "enum": [ + "any-suit", + "red-black", + "same-suit" + ] + } + }, + "additionalProperties": false + }, "max rank": { "type": "integer", "minimum": 1, -- cgit v1.2.1 From cce43788d58d8577e51c7d410a0dda27aa60b1fe Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Tue, 10 Jul 2018 16:46:02 +0100 Subject: Update default json for sequences --- src/main/input-output/input/sol_preset_types.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/main') diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index fdd353a..d450c4c 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -47,6 +47,11 @@ map sol_preset_types::mp = { "size": 0, "stacked": false }, + "sequence": { + "count": 0, + "direction": "L", + "build policy": "same-suit" + }, "max rank": 13, "two decks": false } -- cgit v1.2.1 From 40101d80b2f2956265ee606c45e03ccee4d71333 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Tue, 10 Jul 2018 16:47:52 +0100 Subject: Add json rules for gaps --- src/main/input-output/input/sol_preset_types.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/main') diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index d450c4c..5d22ee7 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -1040,6 +1040,23 @@ map sol_preset_types::mp = { "only complete pile moves": true } } +)" + + }, + { + "gaps", + + R"( +{ + "foundations": { + "present": false + }, + "sequence": { + "count": 4, + "direction": "L", + "build policy": "same-suit" + } +} )" } -- cgit v1.2.1 From 6d1a7f2fbf08a6b9808ad977a83caa4504372e06 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Tue, 10 Jul 2018 22:09:58 +0100 Subject: Add Gaps integration tests --- src/main/input-output/input/sol_preset_types.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/main') diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 5d22ee7..2e3083c 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -1057,6 +1057,23 @@ map sol_preset_types::mp = { "build policy": "same-suit" } } +)" + + }, + { + "simple-gaps", + + R"( +{ + "foundations": { + "present": false + }, + "sequence": { + "count": 4, + "direction": "L", + "build policy": "same-suit" + } +} )" } -- cgit v1.2.1 From 00e1416edd74ad6cb32123020aa3b2ca115afb52 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Tue, 10 Jul 2018 23:03:10 +0100 Subject: Add deal parser json and fix bugs --- src/main/game/sol_rules.cpp | 8 +++- src/main/game/sol_rules.h | 9 +++- .../input/json-parsing/deal_parser.cpp | 14 +++++- .../input/json-parsing/rules_parser.cpp | 56 +++++++++++++++++++++- src/main/input-output/input/sol_preset_types.cpp | 12 +++-- 5 files changed, 91 insertions(+), 8 deletions(-) (limited to 'src/main') diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index d50d852..e9f4edf 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -7,6 +7,8 @@ typedef sol_rules::build_policy pol; typedef sol_rules::spaces_policy s_pol; typedef sol_rules::stock_deal_type sdt; +typedef sol_rules::direction dir; +typedef sol_rules::face_up_policy fu; // Note that the 'default' sol rules are set up by parsing the default json // rules description. This initialisation is really just for testing purposes. @@ -28,5 +30,9 @@ sol_rules::sol_rules() : stock_deal_t(sdt::TABLEAU_PILES), stock_deal_count(1), reserve_size(0), - reserve_stacked(false) { + reserve_stacked(false), + face_up(fu::ALL), + sequence_count(0), + sequence_direction(dir::LEFT), + sequence_build_pol(pol::SAME_SUIT) { } diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index 8075821..b03d854 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -32,6 +32,11 @@ struct sol_rules { ALL, TOP_CARDS }; + enum class direction { + LEFT, + RIGHT, + BOTH + }; uint8_t tableau_pile_count; build_policy build_pol; @@ -54,7 +59,9 @@ struct sol_rules { uint8_t reserve_size; bool reserve_stacked; face_up_policy face_up; - + uint8_t sequence_count; + direction sequence_direction; + build_policy sequence_build_pol; }; #endif //SOLVITAIRE_SOL_RULES_H 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 5a6dff6..7ab2cd2 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -78,7 +78,16 @@ string deal_parser::deal_schema_json() { "definitions": { "card": {"type": "string", "pattern": "^(([0-9]|1[0-3]|a|A|j|J|q|Q|k|K)(c|C|d|D|h|H|s|S))$"}, - "cardarray": {"type": "array", "items": {"$ref": "#/definitions/card"}} + "cardarray": {"type": "array", "items": {"$ref": "#/definitions/card"}}, + "cardarraywithempty": { + "type": "array", + "items": { + "oneOf":[ + {"$ref": "#/definitions/card"}, + {"type": "string","enum": [""]} + ] + } + } }, "type": "object", "properties": { @@ -89,6 +98,9 @@ string deal_parser::deal_schema_json() { "foundations": { "type": "array", "items": {"$ref": "#/definitions/cardarray"} }, + "sequences": { + "type": "array", "items": {"$ref": "#/definitions/cardarraywithempty"} + }, "hole": {"$ref": "#/definitions/card"}, "stock": {"$ref": "#/definitions/cardarray"}, "waste": {"$ref": "#/definitions/cardarray"}, 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 55a414c..e3c4601 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -22,6 +22,7 @@ typedef sol_rules::build_policy pol; typedef sol_rules::spaces_policy s_pol; typedef sol_rules::stock_deal_type sdt; typedef sol_rules::face_up_policy fu; +typedef sol_rules::direction dir; const sol_rules rules_parser::from_file(const string rules_file) { sol_rules sr = get_default(); @@ -305,6 +306,56 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } + if (d.HasMember("sequences")) { + if (d["sequences"].IsObject()) { + if (d["sequences"].HasMember("count")) { + if (d["sequences"]["count"].IsInt()) { + sr.sequence_count = static_cast(d["sequences"]["count"].GetInt()); + } else { + json_helper::json_parse_err("[count] must be an integer"); + } + } + + if (d["sequences"].HasMember("build policy")) { + if (d["sequences"]["build policy"].IsString()) { + string bp_str = d["sequences"]["build policy"].GetString(); + + if (bp_str == "any-suit") { + sr.sequence_build_pol = pol::ANY_SUIT; + } else if (bp_str == "red-black") { + sr.sequence_build_pol = pol::RED_BLACK; + } else if (bp_str == "same-suit") { + sr.sequence_build_pol = pol::SAME_SUIT; + } else { + json_helper::json_parse_err("[sequences][build policy] is invalid"); + } + } else { + json_helper::json_parse_err("[sequences][build policy] must be an string"); + } + } + + if (d["sequences"].HasMember("direction")) { + if (d["sequences"]["direction"].IsString()) { + string dir_str = d["sequences"]["direction"].GetString(); + + if (dir_str == "L") { + sr.sequence_direction = dir::LEFT; + } else if (dir_str == "R") { + sr.sequence_direction = dir::RIGHT; + } else if (dir_str == "LR") { + sr.sequence_direction = dir::BOTH; + } else { + json_helper::json_parse_err("[sequences][direction] is invalid"); + } + } else { + json_helper::json_parse_err("[sequences][direction] must be an string"); + } + } + } else { + json_helper::json_parse_err("[sequences] must be an object"); + } + } + if (d.HasMember("max rank")) { if (d["max rank"].IsInt()) { sr.max_rank = static_cast(d["max rank"].GetInt()); @@ -327,8 +378,9 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { int solution_types = 0; if (sr.hole) solution_types++; if (sr.foundations_present) solution_types++; + if (sr.sequence_count > 0) solution_types++; if (solution_types != 1) { - json_helper::json_parse_err("one and only one of [hole] and [foundations] " + json_helper::json_parse_err("one and only one of [hole], [foundations] and [sequences][count] > 0 " "must be true"); } @@ -462,7 +514,7 @@ string rules_parser::rules_schema_json() { }, "additionalProperties": false }, - "sequence": { + "sequences": { "type": "object", "properties": { "count": { diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 2e3083c..a004136 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -47,7 +47,7 @@ map sol_preset_types::mp = { "size": 0, "stacked": false }, - "sequence": { + "sequences": { "count": 0, "direction": "L", "build policy": "same-suit" @@ -1051,7 +1051,10 @@ map sol_preset_types::mp = { "foundations": { "present": false }, - "sequence": { + "tableau piles": { + "count": 0 + }, + "sequences": { "count": 4, "direction": "L", "build policy": "same-suit" @@ -1068,7 +1071,10 @@ map sol_preset_types::mp = { "foundations": { "present": false }, - "sequence": { + "tableau piles": { + "count": 0 + }, + "sequences": { "count": 4, "direction": "L", "build policy": "same-suit" -- cgit v1.2.1 From b9f90b18dacdc8a163b4c464a25030d47e82a190 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Tue, 10 Jul 2018 23:20:04 +0100 Subject: Add logic to take cards from within piles --- src/main/game/pile.cpp | 13 +++++++++++++ src/main/game/pile.h | 2 ++ 2 files changed, 15 insertions(+) (limited to 'src/main') diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp index 23a02af..10d81d3 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -44,6 +44,11 @@ void pile::place(const card c) { pile_vec.emplace_back(c); } +void pile::place(size_type i, const card c) { + vector::size_type s = pile_vec.size() - 1 - i; + pile_vec.insert(begin(pile_vec) + s, c); +} + card pile::take() { assert(!empty()); card c = top_card(); @@ -51,6 +56,14 @@ card pile::take() { return c; } +card pile::take(size_type i) { + assert(i < size()); + vector::size_type s = pile_vec.size() - 1 - i; + card c = pile_vec[s]; + pile_vec.erase(begin(pile_vec) + s); + return c; +} + bool operator==(const pile& a, const pile& b) { return a.pile_vec == b.pile_vec; } diff --git a/src/main/game/pile.h b/src/main/game/pile.h index 5ab2404..391cdf7 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -42,7 +42,9 @@ public: // Modify void place(card); + void place(size_type, card); card take(); + card take(size_type); private: // Underlying vector -- cgit v1.2.1 From 5f67ae3c634cdeebda84189f41734c9f4a4657b3 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 11 Jul 2018 11:56:37 +0100 Subject: Parse sequence from deal --- src/main/game/search-state/game_state.cpp | 6 +++++ src/main/game/search-state/game_state.h | 1 + .../input/json-parsing/deal_parser.cpp | 27 ++++++++++++++++++++++ .../input-output/input/json-parsing/deal_parser.h | 1 + src/main/input-output/input/sol_preset_types.cpp | 2 +- 5 files changed, 36 insertions(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 0720f6e..1af554c 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -104,6 +104,12 @@ game_state::game_state(const sol_rules& s_rules, streamliner_options stream_opts tableau_piles.push_back(static_cast(piles.size() - 1)); original_tableau_piles.push_back(static_cast(piles.size() - 1)); } + + // Creates the sequence piles + for (uint8_t i = 0; i < rules.sequence_count; i++) { + piles.emplace_back(); + sequences.push_back(static_cast(piles.size() - 1)); + } } // Constructs an initial game state from a JSON doc diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index 3f354d1..15d237a 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -136,6 +136,7 @@ private: pile::ref waste; std::list reserve; std::vector foundations; + std::vector sequences; pile::ref hole; /* Pile references of starting/original layout */ 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 7ab2cd2..1b03ef1 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -51,6 +51,11 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { parse_reserve(gs, doc); } + // Construct sequences + if (gs.rules.sequence_count > 0) { + parse_sequences(gs, doc); + } + // If the foundations begin with cards in them, fill them if (gs.rules.foundations_present) { bool supplied_foundations = parse_foundations(gs, doc); @@ -209,6 +214,28 @@ void deal_parser::parse_reserve(game_state &gs, const Document& doc) { } } +void deal_parser::parse_sequences(game_state& gs, const rapidjson::Document& doc) { + assert(doc.HasMember("sequences")); + const Value& json_seqs = doc["sequences"]; + assert(json_seqs.IsArray()); + + if (json_seqs.Size() != gs.rules.sequence_count ) { + json_helper::json_parse_err("Incorrect number of sequences"); + } + + for (auto p = std::make_pair(begin(json_seqs.GetArray()), begin(gs.sequences)); + p.second != end(gs.sequences); + ++p.first, ++p.second) { + + for (auto& json_card : p.first->GetArray()) { + assert(json_card.IsString()); + string card_str = json_card.GetString(); + card c = card_str.empty() ? card() : card(json_card.GetString()); + gs.place_card(*p.second, c); + } + } +} + bool deal_parser::parse_foundations(game_state &gs, const rapidjson::Document& doc) { if (!doc.HasMember("foundations")) return false; 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 c34a93f..bce4a29 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.h +++ b/src/main/input-output/input/json-parsing/deal_parser.h @@ -23,6 +23,7 @@ public: static void parse_stock(game_state&, const rapidjson::Document&); static void parse_waste(game_state&, const rapidjson::Document&); static void parse_reserve(game_state&, const rapidjson::Document&); + static void parse_sequences(game_state&, const rapidjson::Document&); static bool parse_foundations(game_state&, const rapidjson::Document&); static void fill_foundations(game_state&); static std::string deal_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 a004136..f3606e9 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -1075,7 +1075,7 @@ map sol_preset_types::mp = { "count": 0 }, "sequences": { - "count": 4, + "count": 3, "direction": "L", "build policy": "same-suit" } -- cgit v1.2.1 From f837ec82cba32450bd37d129220be8423ef2d0bc Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 11 Jul 2018 12:06:48 +0100 Subject: Add random deal for sequences --- src/main/game/search-state/game_state.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/main') diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 1af554c..fdfb1ce 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -195,6 +195,21 @@ game_state::game_state(const sol_rules& s_rules, int seed, streamliner_options s if (!p.empty()) p[0].turn_face_up(); + // Deals to the sequence piles + if (rules.sequence_count > 0) { + for (int t = 0; !deck.empty(); t++) { + card c = deck.back(); + if (c.get_rank() == 1) c = card(); // Inserts gaps where aces would be + + // Adds the randomly generated card to the tableau piles + auto s = t % sequences.size(); + + pile::ref seq_pile = sequences[s]; + place_card(seq_pile, c); + 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(); -- cgit v1.2.1 From bb9732850beb1baadf232ae33305ca32090b0c80 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 11 Jul 2018 12:30:46 +0100 Subject: Add sequence state printing and fix small bugs --- src/main/game/card.cpp | 1 + src/main/game/search-state/game_state.cpp | 30 +++++++++++++------------- src/main/input-output/output/state_printer.cpp | 19 ++++++++++++++++ src/main/input-output/output/state_printer.h | 3 +++ src/main/solver/solver.cpp | 9 +------- 5 files changed, 39 insertions(+), 23 deletions(-) (limited to 'src/main') diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 60aaa74..1d18029 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -83,6 +83,7 @@ card::rank_t card::get_rank() const { std::string card::to_string() const { if (face_down) return "##"; + if (card_rank == 0) return "__"; string s; switch(card_rank) { diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index fdfb1ce..0139262 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -158,6 +158,21 @@ game_state::game_state(const sol_rules& s_rules, int seed, streamliner_options s } } + // Deals to the sequence piles + if (rules.sequence_count > 0) { + for (int t = 0; !deck.empty(); t++) { + card c = deck.back(); + if (c.get_rank() == 1) c = card(); // Inserts gaps where aces would be + + // Adds the randomly generated card to the tableau piles + auto s = t % sequences.size(); + + pile::ref seq_pile = sequences[s]; + place_card(seq_pile, c); + deck.pop_back(); + } + } + // This only occurs during testing if (rules.tableau_pile_count == 0) return; @@ -195,21 +210,6 @@ game_state::game_state(const sol_rules& s_rules, int seed, streamliner_options s if (!p.empty()) p[0].turn_face_up(); - // Deals to the sequence piles - if (rules.sequence_count > 0) { - for (int t = 0; !deck.empty(); t++) { - card c = deck.back(); - if (c.get_rank() == 1) c = card(); // Inserts gaps where aces would be - - // Adds the randomly generated card to the tableau piles - auto s = t % sequences.size(); - - pile::ref seq_pile = sequences[s]; - place_card(seq_pile, c); - 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(); diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index 67702e2..f673319 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -43,6 +43,10 @@ ostream& state_printer::print(ostream& stream, const game_state& gs) { state_printer::print_header(stream, "Hole Card"); state_printer::print_top_of_pile(stream, gs.hole, gs); } + if (gs.rules.sequence_count > 0) { + state_printer::print_header(stream, "Sequences"); + state_printer::print_sequences(stream, gs.sequences, gs); + } return stream << "==================================="; } @@ -90,6 +94,21 @@ void state_printer::print_piles(ostream& stream, } } +void state_printer::print_sequences(ostream& stream, + const vector& seq_rs, + const game_state& gs) { + for (auto s : seq_rs) { + stream << "\n"; + auto& pile = gs.piles[s]; + for (auto i = pile.size(); i --> 0 ;) { + print_card(stream, pile[i]); + stream << " "; + } + stream << "\n"; + } + stream << "\n"; +} + void state_printer::print_top_of_piles(ostream& stream, const vector& vp, const game_state& gs) { diff --git a/src/main/input-output/output/state_printer.h b/src/main/input-output/output/state_printer.h index 7d4a702..7e01aea 100644 --- a/src/main/input-output/output/state_printer.h +++ b/src/main/input-output/output/state_printer.h @@ -19,6 +19,9 @@ private: static void print_piles(std::ostream&, const std::vector&, const game_state&); + static void print_sequences(std::ostream&, + const std::vector&, + const game_state&); static void print_top_of_piles(std::ostream&, const std::vector&, const game_state&); diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 6b0c980..3bfd6bd 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -58,7 +58,6 @@ solver::sol_state solver::run(boost::optional &> 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 @@ -137,18 +136,12 @@ bool solver::revert_to_last_node_with_children(optionalmv.type != move::mtype::dominance) { 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); - } + assert(cache.contains(state)); } LOG_DEBUG("(undo move)"); } else { LOG_DEBUG("(undo dominance move)"); } - state_printer::print_move(clog, current_node->mv); LOG_DEBUG(state); #endif -- cgit v1.2.1 From f845a2eed1eb5a10977cba36e3c448397a455c68 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Thu, 12 Jul 2018 00:04:09 +0100 Subject: Gaps / sequence implementation --- src/main/game/card.cpp | 1 - src/main/game/global_cache.cpp | 41 ++++---------- src/main/game/move.cpp | 1 - src/main/game/move.h | 2 +- src/main/game/pile.cpp | 13 ----- src/main/game/pile.h | 2 - src/main/game/search-state/game_state.cpp | 63 ++++++++++++++++++--- src/main/game/search-state/game_state.h | 3 + .../game/search-state/game_state.legal_moves.cpp | 64 ++++++++++++++++++++++ .../input/json-parsing/deal_parser.cpp | 2 +- src/main/input-output/input/sol_preset_types.cpp | 5 +- src/main/input-output/output/state_printer.cpp | 9 ++- 12 files changed, 147 insertions(+), 59 deletions(-) (limited to 'src/main') diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 1d18029..60aaa74 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -83,7 +83,6 @@ card::rank_t card::get_rank() const { std::string card::to_string() const { if (face_down) return "##"; - if (card_rank == 0) return "__"; string s; switch(card_rank) { diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index ee2a2b8..e24cb4c 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -68,6 +68,11 @@ cached_game_state::cached_game_state(const game_state& gs) : live(true) { add_pile(pr, gs); add_card_divider(); } + + for (pile::ref pr : gs.sequences) { + add_pile(pr, gs); + add_card_divider(); + } } @@ -83,9 +88,9 @@ void cached_game_state::add_card(card c, const game_state& gs) { // If the game is a 'hole-based' game, or suit-reduction is on, reduces // the cached suit of the card where possible - bool is_suit_symmetry = gs.stream_opts == sos::SUIT_SYMMETRY - || gs.stream_opts == sos::BOTH - || gs.rules.hole; + bool is_suit_symmetry = (gs.rules.foundations_present + && (gs.stream_opts == sos::SUIT_SYMMETRY || gs.stream_opts == sos::BOTH)) + || gs.rules.hole; if (is_suit_symmetry) { switch (gs.rules.build_pol) { @@ -139,10 +144,10 @@ size_t hasher::hash_value(card const& c) const { // If the game is a 'hole-based' game, or suit-reduction is enabled, hash // the reduced suit - bool is_suit_symmetry = init_gs.stream_opts == sos::SUIT_SYMMETRY - || init_gs.stream_opts == sos::BOTH - || init_gs.rules.hole; - + bool is_suit_symmetry = (init_gs.rules.foundations_present + && (init_gs.stream_opts == sos::SUIT_SYMMETRY || init_gs.stream_opts == sos::BOTH)) + || init_gs.rules.hole; + uint8_t suit_val; if (is_suit_symmetry) { switch (init_gs.rules.build_pol) { @@ -164,28 +169,6 @@ size_t hasher::hash_value(card const& c) const { } - -/*////////////////// -// GLOBAL CACHE // -////////////////// - -unlimited_cache::unlimited_cache(const game_state& gs) - : cache(0, hasher(gs)) { -} - -bool unlimited_cache::insert(const game_state& gs) { - return cache.emplace(gs).second; -} - -bool unlimited_cache::contains(const game_state& gs) const { - return cache.count(cached_game_state(gs)) > 0; -} - -void unlimited_cache::clear() { - cache.clear(); -}*/ - - /////////////// // LRU CACHE // /////////////// diff --git a/src/main/game/move.cpp b/src/main/game/move.cpp index 528b631..b349ddd 100644 --- a/src/main/game/move.cpp +++ b/src/main/game/move.cpp @@ -9,7 +9,6 @@ move::move(mtype ty, pile::ref f, pile::ref t, int8_t count_, bool reveal_move_, #ifndef NDEBUG 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 27ba279..3e96733 100644 --- a/src/main/game/move.h +++ b/src/main/game/move.h @@ -8,7 +8,7 @@ #include "pile.h" struct move { - enum class mtype : uint8_t {regular, dominance, built_group, stock_k_plus, stock_to_all_tableau, null}; + enum class mtype : uint8_t {regular, dominance, built_group, stock_k_plus, stock_to_all_tableau, sequence, null}; 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&); diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp index 10d81d3..23a02af 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -44,11 +44,6 @@ void pile::place(const card c) { pile_vec.emplace_back(c); } -void pile::place(size_type i, const card c) { - vector::size_type s = pile_vec.size() - 1 - i; - pile_vec.insert(begin(pile_vec) + s, c); -} - card pile::take() { assert(!empty()); card c = top_card(); @@ -56,14 +51,6 @@ card pile::take() { return c; } -card pile::take(size_type i) { - assert(i < size()); - vector::size_type s = pile_vec.size() - 1 - i; - card c = pile_vec[s]; - pile_vec.erase(begin(pile_vec) + s); - return c; -} - bool operator==(const pile& a, const pile& b) { return a.pile_vec == b.pile_vec; } diff --git a/src/main/game/pile.h b/src/main/game/pile.h index 391cdf7..5ab2404 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -42,9 +42,7 @@ public: // Modify void place(card); - void place(size_type, card); card take(); - card take(size_type); private: // Underlying vector diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 0139262..b61be3b 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -161,12 +161,12 @@ game_state::game_state(const sol_rules& s_rules, int seed, streamliner_options s // Deals to the sequence piles if (rules.sequence_count > 0) { for (int t = 0; !deck.empty(); t++) { - card c = deck.back(); - if (c.get_rank() == 1) c = card(); // Inserts gaps where aces would be - // Adds the randomly generated card to the tableau piles auto s = t % sequences.size(); + card c = deck.back(); + if (c.get_rank() == 1) c = "AS"; // The dummy "gap" placeholder + pile::ref seq_pile = sequences[s]; place_card(seq_pile, c); deck.pop_back(); @@ -285,6 +285,9 @@ void game_state::make_move(const move m) { case move::mtype::stock_to_all_tableau: make_stock_to_all_tableau_move(m); break; + case move::mtype::sequence: + make_sequence_move(m); + break; case move::mtype::null: assert(false); break; @@ -310,6 +313,9 @@ void game_state::undo_move(const move m) { case move::mtype::stock_to_all_tableau: undo_stock_to_all_tableau_move(m); break; + case move::mtype::sequence: + undo_sequence_move(m); + break; case move::mtype::null: assert(false); break; @@ -493,6 +499,30 @@ void game_state::undo_stock_to_all_tableau_move(move m) { } } +void game_state::make_sequence_move(const move m) { + pile::ref from_seq_ref = m.from / rules.max_rank; + pile::size_type from_card_idx = m.from % rules.max_rank; + card from_card = piles[from_seq_ref][from_card_idx]; + piles[from_seq_ref][from_card_idx] = "AS"; + + pile::ref to_seq_ref = m.to / rules.max_rank; + pile::size_type to_card_idx = m.to % rules.max_rank; + assert(piles[to_seq_ref][to_card_idx] == "AS"); + piles[to_seq_ref][to_card_idx] = from_card; +} + +void game_state::undo_sequence_move(const move m) { + pile::ref to_seq_ref = m.to / rules.max_rank; + pile::size_type to_card_idx = m.to % rules.max_rank; + card to_card = piles[to_seq_ref][to_card_idx]; + piles[to_seq_ref][to_card_idx] = "AS"; + + pile::ref from_seq_ref = m.from / rules.max_rank; + pile::size_type from_card_idx = m.from % rules.max_rank; + assert(piles[from_seq_ref][from_card_idx] == "AS"); + piles[from_seq_ref][from_card_idx] = to_card; +} + // Places a card on a pile and if it is on a tableau, cell or reserve pile, // reorders the pile refs so that the largest pile is first void game_state::place_card(pile::ref pr, card c) { @@ -545,13 +575,24 @@ bool game_state::is_solved() const { if (rules.hole) { solved = piles[hole].size() == rules.max_rank * 4 * (rules.two_decks ? 2 : 1); - } else { - assert(rules.foundations_present); + } else if (rules.foundations_present) { for (auto f : foundations) { if (piles[f].size() != rules.max_rank) { - solved = false; + solved = false; + } +} + } else if (rules.sequence_count > 0) { + for (pile::ref i = 0; i < sequences.size() && solved; i++) { + for (pile::ref j = piles[sequences[i]].size(); j-- > 2;) { + if (!is_next_legal_card(rules.sequence_build_pol, piles[sequences[i]][j-1], piles[sequences[i]][j])) { + solved = false; + break; + } } + if (piles[sequences[i]].top_card() != "AS") solved = false; } + } else { + assert(false); } // Runs some alternative checks in debug mode to make sure the game state @@ -567,7 +608,7 @@ bool game_state::is_solved() const { } } // All piles other than the foundations must be empty - else { + else if (rules.foundations_present) { bool non_foundation_ref = pr < foundations.front() || pr >= foundations.front() + foundations.size(); @@ -577,7 +618,13 @@ bool game_state::is_solved() const { } } } - assert(solved == rest_empty); + if (rules.sequence_count > 0) { + for (auto seq : sequences) { + assert(piles[seq].size() == rules.max_rank); + } + } else { + assert(solved == rest_empty); + } #endif return solved; diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index 15d237a..05f6974 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -80,6 +80,8 @@ private: void undo_stock_k_plus_move(move move); void make_stock_to_all_tableau_move(move move); void undo_stock_to_all_tableau_move(move move); + void make_sequence_move(move move); + void undo_sequence_move(move move); #ifndef NDEBUG void check_face_down_consistent() const; @@ -112,6 +114,7 @@ private: void add_empty_built_group_moves(std::vector&, pile::ref, pile::ref, pile::size_type, bool) const; void add_kings_only_built_group_move(std::vector&, pile::ref, pile::ref, pile::size_type, bool) const; void add_non_empty_built_group_move(std::vector&, pile::ref, pile::ref, pile::size_type, bool) const; + void add_sequence_moves(std::vector&) const; bool is_next_legal_card(sol_rules::build_policy, card, card) const; void turn_face_down_cards(std::vector&) 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 f6fb68f..42d513a 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -20,6 +20,7 @@ typedef sol_rules::build_policy pol; typedef sol_rules::spaces_policy s_pol; typedef sol_rules::stock_deal_type sdt; typedef sol_rules::face_up_policy fu; +typedef sol_rules::direction dir; //////////////////////////////// // MAIN LEGAL MOVE GEN CYCLE /// @@ -31,6 +32,7 @@ vector game_state::get_legal_moves(move parent_move) { // Foundations complete piles moves // Tableau / cells / reserve / stock-waste to hole / foundation moves --- // Cell to tableau moves + // Sequence to sequence moves // Tableau to tableau moves // Tableau built group moves // Stock-waste to tableau moves @@ -107,6 +109,11 @@ vector game_state::get_legal_moves(move parent_move) { } } + // Sequence to sequence moves + if (rules.sequence_count > 0) { + add_sequence_moves(moves); + } + // Cell to tableau moves for (auto c : cells) { if (piles[c].empty() || parent_move.to == c) continue; @@ -402,6 +409,63 @@ void game_state::add_non_empty_built_group_move(vector& moves, pile::ref r } } +void game_state::add_sequence_moves(std::vector& moves) const { + // Note, the sequence moves are encoded by counting each card's location (1-52) and adding that into 'to' and 'from' + + // Finds the spaces + vector> space_locations; + for (pile::ref i = 0; i < sequences.size(); i++) { + + for (pile::ref j = piles[sequences[i]].size(); j --> 0 ;) { + if (piles[sequences[i]][j] == "AS") { + space_locations.emplace_back(i, j); + } + } + } + + // Searches through the cards again to find valid moves into the spaces + for (pile::ref i = 0; i < sequences.size(); i++) { + for (pile::ref j = piles[sequences[i]].size(); j --> 0 ;) { + pile::ref from_idx = i * piles[sequences[i]].size() + j; // Encoded index of card to be moved + card from_card = piles[sequences[i]][j]; + if (from_card == "AS") continue; + + // Adds valid moves + for (pair space_loc : space_locations) { + pile::ref space_idx = space_loc.first * piles[sequences[i]].size() + space_loc.second; // Encoded index of space + + if (rules.sequence_direction == dir::LEFT || rules.sequence_direction == dir::BOTH) { + // Only twos in the far left column + if (space_loc.second == piles[sequences[i]].size() - 1) { + if (from_card.get_rank() == 2) + moves.emplace_back(move::mtype::sequence, from_idx, space_idx); + } + // Otherwise must agree with left neighbour + else { + card neighbour_card = piles[sequences[space_loc.first]][space_loc.second + 1]; + if (neighbour_card != "AS" && is_next_legal_card(rules.sequence_build_pol, from_card, neighbour_card)) + moves.emplace_back(move::mtype::sequence, from_idx, space_idx); + } + } + + if (rules.sequence_direction == dir::RIGHT || rules.sequence_direction == dir::BOTH) { + // Only max rank in the far right column + if (space_loc.second == 0) { + if (from_card.get_rank() == rules.max_rank) + moves.emplace_back(move::mtype::sequence, from_idx, space_idx); + } + // Otherwise must agree with right neighbour + else { + card neighbour_card = piles[sequences[space_loc.first]][space_loc.second - 1]; + if (neighbour_card != "AS" && is_next_legal_card(rules.sequence_build_pol, neighbour_card, from_card)) + moves.emplace_back(move::mtype::sequence, from_idx, space_idx); + } + } + } + } + } +} + /////////////////////// 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 1b03ef1..998f337 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -230,7 +230,7 @@ void deal_parser::parse_sequences(game_state& gs, const rapidjson::Document& doc for (auto& json_card : p.first->GetArray()) { assert(json_card.IsString()); string card_str = json_card.GetString(); - card c = card_str.empty() ? card() : card(json_card.GetString()); + card c = card_str.empty() ? "AS" : card(json_card.GetString()); gs.place_card(*p.second, c); } } diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index f3606e9..88ebb5f 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -1075,10 +1075,11 @@ map sol_preset_types::mp = { "count": 0 }, "sequences": { - "count": 3, + "count": 4, "direction": "L", "build policy": "same-suit" - } + }, + "max rank": 4 } )" diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index f673319..e48ed51 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -101,7 +101,11 @@ void state_printer::print_sequences(ostream& stream, stream << "\n"; auto& pile = gs.piles[s]; for (auto i = pile.size(); i --> 0 ;) { - print_card(stream, pile[i]); + card c = pile[i]; + if (c == "AS") + stream << "__"; + else + print_card(stream, pile[i]); stream << " "; } stream << "\n"; @@ -153,6 +157,9 @@ void state_printer::print_move(std::ostream& s, const move m) { case move::mtype::stock_to_all_tableau: s << "stock-to-all-tableau"; break; + case move::mtype::sequence: + s << "sequence"; + break; case move::mtype::null: s << "null"; break; -- cgit v1.2.1