diff options
| author | Charlie Blake | 2018-07-14 15:06:04 +0100 |
|---|---|---|
| committer | GitHub | 2018-07-14 15:06:04 +0100 |
| commit | 7a2eaa8280ca35a52227084c037b1adb57193291 (patch) | |
| tree | 64ab75568e80890a6b5b66ab0481dd4ea24d1d24 /src/main/game | |
| parent | Merge pull request #75 from thecharlesblake/refactor-rules-json (diff) | |
| parent | Gaps / sequence implementation (diff) | |
| download | metasol-7a2eaa8280ca35a52227084c037b1adb57193291.tar.gz metasol-7a2eaa8280ca35a52227084c037b1adb57193291.zip | |
Merge pull request #76 from thecharlesblake/add-gaps
Add gaps
Diffstat (limited to 'src/main/game')
| -rw-r--r-- | src/main/game/global_cache.cpp | 41 | ||||
| -rw-r--r-- | src/main/game/move.cpp | 1 | ||||
| -rw-r--r-- | src/main/game/move.h | 2 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.cpp | 78 | ||||
| -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 | 64 | ||||
| -rw-r--r-- | src/main/game/sol_rules.cpp | 8 | ||||
| -rw-r--r-- | src/main/game/sol_rules.h | 9 |
8 files changed, 169 insertions, 38 deletions
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/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 0720f6e..b61be3b 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<pile::ref>(piles.size() - 1)); original_tableau_piles.push_back(static_cast<pile::ref>(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<pile::ref>(piles.size() - 1)); + } } // Constructs an initial game state from a JSON doc @@ -152,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++) { + // 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(); + } + } + // This only occurs during testing if (rules.tableau_pile_count == 0) return; @@ -264,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; @@ -289,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; @@ -472,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) { @@ -524,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 @@ -546,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(); @@ -556,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 3f354d1..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<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; + void add_sequence_moves(std::vector<move>&) const; bool is_next_legal_card(sol_rules::build_policy, card, card) const; void turn_face_down_cards(std::vector<move>&) const; @@ -136,6 +139,7 @@ private: pile::ref waste; std::list<pile::ref> reserve; std::vector<pile::ref> foundations; + std::vector<pile::ref> sequences; 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 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<move> 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<move> 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<move>& moves, pile::ref r } } +void game_state::add_sequence_moves(std::vector<move>& 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<pair<uint8_t, uint8_t>> 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<uint8_t, uint8_t> 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/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 |
