From df6b4405801f762cc536ed441c673e4bd1675620 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Sun, 15 Jul 2018 01:16:38 +0100 Subject: Add accordion move making --- src/main/evaluation/solvability_calc.cpp | 2 +- src/main/game/move.h | 2 +- src/main/game/search-state/game_state.cpp | 32 +++++++++++----- src/main/game/search-state/game_state.h | 6 ++- .../game/search-state/game_state.legal_moves.cpp | 44 ++++++++++++++++++++++ .../input/json-parsing/deal_parser.cpp | 5 ++- src/main/input-output/output/state_printer.cpp | 16 +++++++- src/main/input-output/output/state_printer.h | 2 + src/main/solver/solver.cpp | 3 +- 9 files changed, 96 insertions(+), 16 deletions(-) (limited to 'src/main') diff --git a/src/main/evaluation/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp index 1f1e76b..16c10c0 100644 --- a/src/main/evaluation/solvability_calc.cpp +++ b/src/main/evaluation/solvability_calc.cpp @@ -281,7 +281,7 @@ solvability_calc::sol_result solvability_calc::solve_seed(int seed, millisec tim // SEED RESULTS CLASS // //////////////////////// -solvability_calc::seed_results::seed_results(vector v) : solvable(v[0]), unsolvable(v[1]), timed_out(v[2]) { +solvability_calc::seed_results::seed_results(vector v) : solvable(v[0]), unsolvable(v[1]), timed_out(v[2]), mem_limit(0) { } void solvability_calc::seed_results::add_result(sol_result::type t) { diff --git a/src/main/game/move.h b/src/main/game/move.h index 3e96733..8250609 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, sequence, null}; + enum class mtype : uint8_t {regular, dominance, built_group, stock_k_plus, stock_to_all_tableau, sequence, accordion, 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/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index b1a8e5e..0388d9c 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -184,8 +184,8 @@ game_state::game_state(const sol_rules& s_rules, int seed, streamliner_options s // Deals to the accordion if (rules.accordion_size > 0) { - for (unsigned int i = 0; i < rules.accordion_size; i++) { - place_card(accordion[i], deck.back()); + for (auto pr : accordion) { + place_card(pr, deck.back()); deck.pop_back(); } } @@ -305,6 +305,9 @@ void game_state::make_move(const move m) { case move::mtype::sequence: make_sequence_move(m); break; + case move::mtype::accordion: + make_accordion_move(m); + break; case move::mtype::null: assert(false); break; @@ -333,6 +336,9 @@ void game_state::undo_move(const move m) { case move::mtype::sequence: undo_sequence_move(m); break; + case move::mtype::accordion: + undo_accordion_move(m); + break; case move::mtype::null: assert(false); break; @@ -377,10 +383,8 @@ void game_state::undo_regular_move(const move m) { } void game_state::make_built_group_move(move m) { - assert(rules.move_built_group); assert(m.from < piles.size() ); assert(m.to < piles.size() ); - assert(m.count <= rules.max_rank); // Adds the cards to the 'to' pile for (auto pile_idx = m.count; pile_idx-- > 0;) { @@ -400,9 +404,7 @@ void game_state::make_built_group_move(move m) { } void game_state::undo_built_group_move(move m) { - assert(rules.move_built_group); assert(m.to < piles.size()); - assert(m.count <= rules.max_rank); if (m.reveal_move) { assert(!piles[m.from].empty()); @@ -540,6 +542,16 @@ void game_state::undo_sequence_move(const move m) { piles[from_seq_ref][from_card_idx] = to_card; } +void game_state::make_accordion_move(move m) { + make_built_group_move(m); + accordion.remove(m.from); +} + +void game_state::undo_accordion_move(move m) { + accordion.insert(upper_bound(begin(accordion), end(accordion), m.from), m.from); + undo_built_group_move(m); +} + // 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) { @@ -595,9 +607,9 @@ bool game_state::is_solved() const { } 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;) { @@ -608,6 +620,8 @@ bool game_state::is_solved() const { } if (piles[sequences[i]].top_card() != "AS") solved = false; } + } else if (rules.accordion_size > 0) { + return accordion.size() == 1; } else { assert(false); } diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index 212229c..cbd72b4 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -82,6 +82,8 @@ private: void undo_stock_to_all_tableau_move(move move); void make_sequence_move(move move); void undo_sequence_move(move move); + void make_accordion_move(move move); + void undo_accordion_move(move move); #ifndef NDEBUG void check_face_down_consistent() const; @@ -98,6 +100,7 @@ private: void add_stock_to_hole_foundation_moves(std::vector&) const; card stock_card_from_count(int8_t) const; void add_foundation_complete_piles_moves(std::vector &) const; + void add_accordion_moves(std::vector&) const; bool is_valid_tableau_move(pile::ref, pile::ref) const; bool is_valid_tableau_move(card, pile::ref) const; @@ -117,6 +120,7 @@ private: void add_sequence_moves(std::vector&) const; bool is_next_legal_card(sol_rules::build_policy, card, card) const; + bool is_next_legal_card(std::vector, card, card) const; void turn_face_down_cards(std::vector&) const; /* Auto-foundation moves */ @@ -140,7 +144,7 @@ private: std::list reserve; std::vector foundations; std::vector sequences; - std::vector accordion; + std::list accordion; pile::ref hole; /* Pile references of starting/original layout */ 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 42d513a..e1cbc8e 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -18,6 +18,7 @@ using namespace rapidjson; typedef sol_rules::build_policy pol; typedef sol_rules::spaces_policy s_pol; +typedef sol_rules::accordion_policy acc_pol; typedef sol_rules::stock_deal_type sdt; typedef sol_rules::face_up_policy fu; typedef sol_rules::direction dir; @@ -29,6 +30,7 @@ typedef sol_rules::direction dir; // Note that the moves added last here, are tried first vector game_state::get_legal_moves(move parent_move) { // Order: + // Accordion moves // Foundations complete piles moves // Tableau / cells / reserve / stock-waste to hole / foundation moves --- // Cell to tableau moves @@ -152,6 +154,9 @@ vector game_state::get_legal_moves(move parent_move) { if (rules.tableau_pile_count > 0 && rules.face_up != fu::ALL) turn_face_down_cards(moves); + if (rules.accordion_size > 0) + add_accordion_moves(moves); + return moves; } @@ -466,6 +471,26 @@ void game_state::add_sequence_moves(std::vector& moves) const { } } +void game_state::add_accordion_moves(vector& moves) const { + int idx = 0, idx_max = static_cast(accordion.size()); + for (auto from_it = begin(accordion); from_it != end(accordion); from_it++, idx++) { + for (pair move_rule : rules.accordion_moves) { + auto to_it = from_it; + + if (move_rule.first == dir::LEFT && idx - move_rule.second >= 0) { + for (uint8_t i = 0; i < move_rule.second; i++) to_it--; + } else if (move_rule.first == dir::RIGHT && idx + move_rule.second < idx_max) { + for (uint8_t i = 0; i < move_rule.second; i++) to_it++; + } else { + continue; + } + + if (is_next_legal_card(rules.accordion_pol, piles[*to_it].top_card(), piles[*from_it].top_card())) + moves.emplace_back(move::mtype::accordion, *from_it, *to_it, piles[*from_it].size()); + } + } +} + /////////////////////// @@ -484,6 +509,25 @@ bool game_state::is_next_legal_card(pol p, card a, card b) const { return b.get_rank() + 1 == a.get_rank(); } +bool game_state::is_next_legal_card(vector vp, card a, card b) const { + for (auto p : vp) { + switch (p) { + case sol_rules::accordion_policy::SAME_RANK: + if (b.get_rank() == a.get_rank()) return true; + break; + case sol_rules::accordion_policy::SAME_SUIT: + if (b.get_suit() == a.get_suit()) return true; + break; + case sol_rules::accordion_policy::RED_BLACK: + if (b.get_colour() != a.get_colour()) return true; + break; + case sol_rules::accordion_policy::ANY_SUIT: + return true; + } + } + return false; +} + void game_state::turn_face_down_cards(vector& moves) const { for (auto& m : moves) { bool is_tableau_move = m.from >= original_tableau_piles.front() && m.from <= original_tableau_piles.back(); 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 5efcb63..7b8cf18 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -255,9 +255,10 @@ void deal_parser::parse_accordion(game_state &gs, const Document& doc) { json_helper::json_parse_err("Incorrect accordion size"); } - for (pile::ref i = 0; i < json_card_arr.Size(); i++) { + auto acc_it = begin(gs.accordion); + for (pile::ref i = 0; i < json_card_arr.Size(); i++, acc_it++) { assert(json_card_arr[i].IsString()); - pile::ref pr = gs.accordion[i]; + pile::ref pr = *acc_it; gs.place_card(pr, card(json_card_arr[i].GetString())); } } diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index b418a42..3f1978b 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -6,6 +6,7 @@ using std::ostream; using std::vector; +using std::list; typedef sol_rules::stock_deal_type sdt; @@ -49,7 +50,7 @@ ostream& state_printer::print(ostream& stream, const game_state& gs) { } if (gs.rules.accordion_size > 0) { state_printer::print_header(stream, "Accordion"); - state_printer::print_top_of_piles(stream, gs.accordion, gs); + state_printer::print_accordion(stream, gs.accordion, gs); } return stream << "==================================="; } @@ -133,6 +134,16 @@ void state_printer::print_top_of_piles(ostream& stream, stream << "\n"; } +void state_printer::print_accordion(ostream& stream, + const list& vp, + const game_state& gs) { + for (pile::ref p : vp) { + print_card(stream, gs.piles[p].top_card()); + stream << "\t"; + } + stream << "\n"; +} + void state_printer::print_top_of_pile(ostream& stream, const pile::ref pile_r, const game_state& gs) { @@ -164,6 +175,9 @@ void state_printer::print_move(std::ostream& s, const move m) { case move::mtype::sequence: s << "sequence"; break; + case move::mtype::accordion: + s << "accordion"; + break; case move::mtype::null: s << "null"; break; diff --git a/src/main/input-output/output/state_printer.h b/src/main/input-output/output/state_printer.h index 7e01aea..575b973 100644 --- a/src/main/input-output/output/state_printer.h +++ b/src/main/input-output/output/state_printer.h @@ -27,6 +27,8 @@ private: const game_state&); static void print_top_of_pile(std::ostream&, pile::ref, const game_state&); + static void print_accordion(std::ostream&, const std::list&, + const game_state&); static void print_card(std::ostream&, card); }; diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index cd8652f..4059b5b 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -48,7 +48,7 @@ solver::node::node(const move m) solver::sol_state solver::run(boost::optional &> terminate_solver) { bool states_exhausted = false; - while(!(/*state.is_solved() || */states_exhausted)) { + while(!(state.is_solved() || states_exhausted)) { // If the terminate flag was supplied and has been set to true, return if (terminate_solver && *terminate_solver) { return sol_state::timed_out; @@ -136,6 +136,7 @@ bool solver::revert_to_last_node_with_children(optionalmv.type != move::mtype::dominance) { if (cache.get_states_removed_from_cache() == 0) { + LOG_DEBUG(state); // TODO assert(cache.contains(state)); } LOG_DEBUG("(undo move)"); -- cgit v1.2.1