diff options
Diffstat (limited to 'src/main')
5 files changed, 155 insertions, 41 deletions
diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index b63823d..78f1fb9 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -93,7 +93,6 @@ private: 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; - void add_built_group_moves(std::vector<move> &) const; void add_foundation_complete_piles_moves(std::vector<move> &) const; bool is_valid_tableau_move(pile::ref, pile::ref) const; @@ -104,13 +103,15 @@ private: bool is_valid_hole_move(pile::ref) const; bool is_valid_hole_move(card) const; + void add_built_group_moves(std::vector<move>&) const; + void add_built_group_moves(std::vector<move>&, pile::ref, pile::size_type) const; pile::size_type get_built_group_height(pile::ref) const; - void add_empty_built_group_moves(std::vector<move>&, pile::ref, pile::ref, - card, bool) const; bool is_next_built_group_card(card, card) const; + void add_empty_built_group_moves(std::vector<move>&, pile::ref, pile::ref, pile::size_type, bool) const; + void add_kings_only_built_group_move(std::vector<move>&, pile::ref, pile::ref, pile::size_type, bool) const; + void add_non_empty_built_group_move(std::vector<move>&, pile::ref, pile::ref, pile::size_type, bool) const; bool is_next_legal_card(sol_rules::build_policy, card, card) const; - void turn_face_down_cards(std::vector<move>&) const; /* Auto-foundation moves */ 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 7a1f124..42ba578 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -7,6 +7,7 @@ #include "../move.h" #include <boost/optional/optional.hpp> +#include <iostream> using std::vector; using std::min; @@ -20,6 +21,10 @@ typedef sol_rules::spaces_policy s_pol; typedef sol_rules::stock_deal_type sdt; typedef sol_rules::face_up_policy fu; +// TODO: delete +using std::ostream; +std::ostream& operator <<(std::ostream&, const move&); +std::ostream& operator <<(std::ostream&, const std::vector<move>&); //////////////////////////////// // MAIN LEGAL MOVE GEN CYCLE /// @@ -140,9 +145,46 @@ vector<move> game_state::get_legal_moves(move parent_move) { if (rules.tableau_pile_count > 0 && rules.face_up != fu::ALL) turn_face_down_cards(moves); + // TODO: delete +//#ifndef NDEBUG +// std::cerr << "Moves:\n" << moves; + /*vector<move>::size_type i = 0; + std::cin >> i; + move m = moves[i]; + moves.erase(moves.begin() + i); + moves.push_back(m);*/ +//#endif + return moves; } +/*#ifndef NDEBUG +// TODO: delete +ostream& operator <<(ostream& os, const move& m) { + switch (m.type) { + case move::mtype::regular: os << "regular"; break; + case move::mtype::dominance: os << "dominance"; break; + case move::mtype::built_group: os << "built_group"; break; + case move::mtype::stock_k_plus: os << "stock_k_plus"; break; + case move::mtype::stock_to_all_tableau: os << "stock_to_all_tableau"; break; + case move::mtype::null: os << "null"; break; + } + os << " (" << int(m.from) + << "," << int(m.to) + << "," << int(m.count) + << "," << m.reveal_move + << ")"; + return os; +} +ostream& operator <<(ostream& os, const vector<move>& moves) { + int i = 0; + for (const move m : moves) { + os << m << " "<< i++ << "\n"; + } + return os; +} +#endif*/ + //////////////////////////////// // REGULAR MOVE GEN FUNCTIONS // @@ -216,7 +258,7 @@ void game_state::add_stock_to_tableau_moves(std::vector<move>& moves) const { // 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 (!rules.stock_redeal && !piles[waste].empty()) { + if (/*!rules.stock_redeal && */!piles[waste].empty()) { for (auto t : tableau_piles) { if (is_valid_tableau_move(waste, t)) moves.emplace_back(move::mtype::regular, waste, t); @@ -342,7 +384,7 @@ bool game_state::is_valid_hole_move(const card c) const { // BUILT-GROUP MOVE GEN FUNCTIONS // //////////////////////////////////// -void game_state::add_built_group_moves(vector<move> &moves) const { +void game_state::add_built_group_moves(vector<move>& moves) const { assert(rules.built_group_pol != pol::NO_BUILD); // Cycles through each pile to see if it contains a built group @@ -352,26 +394,28 @@ void game_state::add_built_group_moves(vector<move> &moves) const { auto built_group_height = get_built_group_height(rem_ref); if (built_group_height == 1) continue; - // We have found a built group. Cycles through each pile to see if it can be added - for (auto add_ref : tableau_piles) { - if (add_ref == rem_ref) continue; - - card bg_high = piles[rem_ref][built_group_height - 1]; - - bool is_reveal_move = - piles[rem_ref].size() > built_group_height - && piles[rem_ref][built_group_height].is_face_down(); + add_built_group_moves(moves, rem_ref, built_group_height); + } +} - if (piles[add_ref].empty()) { - if (rules.spaces_pol == s_pol::ANY) - add_empty_built_group_moves(moves, rem_ref, add_ref, bg_high, is_reveal_move); - else if (rules.spaces_pol == s_pol::KINGS && bg_high.get_rank() == 13) - moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, built_group_height, is_reveal_move); - } else { - if (is_next_built_group_card(piles[add_ref].top_card(), bg_high)) { - moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, built_group_height, is_reveal_move); - } +void game_state::add_built_group_moves(vector<move>& moves, pile::ref rem_ref, pile::size_type built_group_height) const { + // We have found a built group. Cycles through each pile to see if it can be added + for (auto add_ref : tableau_piles) { + if (add_ref == rem_ref) continue; + + card bg_high = piles[rem_ref][built_group_height - 1]; + bool base_face_down = + piles[rem_ref].size() > built_group_height + && piles[rem_ref][built_group_height].is_face_down(); + + if (piles[add_ref].empty()) { + if (rules.spaces_pol == s_pol::ANY) { + add_empty_built_group_moves(moves, rem_ref, add_ref, built_group_height, base_face_down); + } else if (rules.spaces_pol == s_pol::KINGS && bg_high.get_rank() == 13) { + add_kings_only_built_group_move(moves, rem_ref, add_ref, built_group_height, base_face_down); } + } else { + add_non_empty_built_group_move(moves, rem_ref, add_ref, built_group_height, base_face_down); } } } @@ -386,23 +430,45 @@ pile::size_type game_state::get_built_group_height(pile::ref ref) const { return i; } +bool game_state::is_next_built_group_card(card a, card b) const { + return is_next_legal_card(rules.built_group_pol, a, b); +} + // Loops through each possible built group move to an empty pile and adds it to the list void game_state::add_empty_built_group_moves(vector<move>& moves, pile::ref rem_ref, pile::ref add_ref, - card bg_high, bool is_reveal_move) const { - pile::size_type card_idx = 1; - do { - moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, static_cast<pile::size_type>(card_idx + 1), - is_reveal_move); - } while (piles[rem_ref][card_idx++] != bg_high); - - if (card_idx < piles[rem_ref].size() && piles[rem_ref][card_idx].is_face_down()) - moves.back().make_reveal_move(); + pile::size_type built_group_height, bool base_face_down) const { + + for (pile::size_type card_idx = 1; card_idx < built_group_height; card_idx++) { + bool is_reveal_move = card_idx + 1 == built_group_height && base_face_down; + moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, card_idx + 1, is_reveal_move); + } } -bool game_state::is_next_built_group_card(card a, card b) const { - return is_next_legal_card(rules.built_group_pol, a, b); +void game_state::add_kings_only_built_group_move(vector<move>& moves, pile::ref rem_ref, pile::ref add_ref, + pile::size_type built_group_height, bool base_face_down) const { + + moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, built_group_height, base_face_down); } +// Loops through each possible built group move to an empty pile and adds it to the list +void game_state::add_non_empty_built_group_move(vector<move>& moves, pile::ref rem_ref, pile::ref add_ref, + pile::size_type built_group_height, bool base_face_down) const { + + // Given rank of add pile, get card of that rank - 1 in rem pile and see if next bg card + // For each card going down from top card in rem pile, check if is next bg card + for (pile::ref r = 1; r < built_group_height; r++) { + if (is_next_built_group_card(piles[add_ref].top_card(), piles[rem_ref][r])) { + + bool is_reveal_move = r + 1 == built_group_height && base_face_down; + moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, r + 1, is_reveal_move); + break; + } + } +} + + +/////////////////////// + bool game_state::is_next_legal_card(pol p, card a, card b) const { // Checks build pol violations switch(p) { 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 e776cbd..bc31cbc 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -11,6 +11,7 @@ using namespace std; using namespace rapidjson; typedef sol_rules::build_policy pol; +typedef sol_rules::stock_deal_type sdt; typedef sol_rules::face_up_policy fu; void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { @@ -39,6 +40,11 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { if (gs.rules.stock_size > 0) { parse_stock(gs, doc); } + + // Construct waste + if (gs.rules.stock_deal_t == sdt::WASTE) { + parse_waste(gs, doc); + } // Construct reserve if (gs.rules.reserve_size > 0) { @@ -46,8 +52,12 @@ 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_init_card) { - fill_foundations(gs); + if (gs.rules.foundations) { + bool supplied_foundations = parse_foundations(gs, doc); + + if (!supplied_foundations && gs.rules.foundations_init_card) { + fill_foundations(gs); + } } Document d; @@ -76,6 +86,9 @@ string deal_parser::deal_schema_json() { "tableau piles": { "type": "array", "items": {"$ref": "#/definitions/cardarray"} }, + "foundations": { + "type": "array", "items": {"$ref": "#/definitions/cardarray"} + }, "hole": {"$ref": "#/definitions/card"}, "stock": {"$ref": "#/definitions/cardarray"}, "waste": {"$ref": "#/definitions/cardarray"}, @@ -144,10 +157,6 @@ void deal_parser::parse_stock(game_state &gs, const Document& doc) { const Value &json_stock = doc["stock"]; assert(json_stock.IsArray()); - if (json_stock.Size() != gs.rules.stock_size) { - json_helper::json_parse_err("Incorrect stock size"); - } - for (const Value& json_card : json_stock.GetArray()) { assert(json_card.IsString()); gs.place_card(gs.stock, card(json_card.GetString())); @@ -155,6 +164,18 @@ void deal_parser::parse_stock(game_state &gs, const Document& doc) { } } +void deal_parser::parse_waste(game_state &gs, const Document& doc) { + if (doc.HasMember("waste")) { + const Value &json_waste = doc["waste"]; + assert(json_waste.IsArray()); + + for (const Value& json_card : json_waste.GetArray()) { + assert(json_card.IsString()); + gs.place_card(gs.waste, card(json_card.GetString())); + } + } +} + void deal_parser::parse_reserve(game_state &gs, const Document& doc) { assert(doc.HasMember("reserve")); const Value& json_reserve_piles = doc["reserve"]; @@ -176,6 +197,28 @@ void deal_parser::parse_reserve(game_state &gs, const Document& doc) { } } +bool deal_parser::parse_foundations(game_state &gs, const rapidjson::Document& doc) { + if (!doc.HasMember("foundations")) return false; + + const Value& json_foundations = doc["foundations"]; + assert(json_foundations.IsArray()); + + if (json_foundations.Size() != 4 * (gs.rules.two_decks ? 2 : 1) ) { + json_helper::json_parse_err("Incorrect number of foundations"); + } + + for (auto j = begin(json_foundations.GetArray()); j != end(json_foundations.GetArray()); j++) { + + for (auto& json_card : j->GetArray()) { + assert(json_card.IsString()); + card c = card(json_card.GetString()); + gs.place_card(gs.foundations[c.get_suit()], c); + } + } + + return true; +} + void deal_parser::fill_foundations(game_state &gs) { auto foundations_count = uint8_t(4 * (gs.rules.two_decks ? 2:1)); assert(gs.foundations.size() == foundations_count); 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 dc67573..c34a93f 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.h +++ b/src/main/input-output/input/json-parsing/deal_parser.h @@ -21,7 +21,9 @@ public: static void parse_hole(game_state&, const rapidjson::Document&); static void parse_cells(game_state&, const rapidjson::Document&); static void parse_stock(game_state&, const rapidjson::Document&); + static void parse_waste(game_state&, const rapidjson::Document&); static void parse_reserve(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 363a801..ee19b5c 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -411,6 +411,7 @@ 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 @@ -432,6 +433,7 @@ 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 |
