diff options
| author | Charlie Blake | 2018-07-03 14:51:07 +0100 |
|---|---|---|
| committer | GitHub | 2018-07-03 14:51:07 +0100 |
| commit | b7953f5b0a5d155fc87e9ec5b17525d9ac42c6b7 (patch) | |
| tree | e220211c8313be2b25f1aebf537ffbf4a352f1db /src | |
| parent | Merge pull request #71 from thecharlesblake/correct-klondike (diff) | |
| parent | Tidy up stock rep and amend broken unit tests (diff) | |
| download | metasol-b7953f5b0a5d155fc87e9ec5b17525d9ac42c6b7.tar.gz metasol-b7953f5b0a5d155fc87e9ec5b17525d9ac42c6b7.zip | |
Merge pull request #72 from thecharlesblake/k-plus-stock
K plus stock
Diffstat (limited to 'src')
19 files changed, 894 insertions, 245 deletions
diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index f688c33..2cec338 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -89,10 +89,10 @@ void cached_game_state::add_card(card c, const game_state& gs) { target.emplace_back(c); break; case pol::RED_BLACK: - target.emplace_back(c.get_colour(), c.get_rank()); + target.emplace_back(c.get_colour(), c.get_rank(), c.is_face_down()); break; default: - target.emplace_back(0, c.get_rank()); + target.emplace_back(0, c.get_rank(), c.is_face_down()); break; } } else { diff --git a/src/main/game/move.cpp b/src/main/game/move.cpp index 9e45c66..915bac6 100644 --- a/src/main/game/move.cpp +++ b/src/main/game/move.cpp @@ -8,7 +8,7 @@ move::move(mtype ty, pile::ref f, pile::ref t, pile::size_type i, bool reveal_mo type(ty), from(f), to(t), count(i), reveal_move(reveal_move_) { #ifndef NDEBUG if (ty == mtype::regular) assert(i == 1); - else assert(i >= 1); + else if (ty != mtype::stock_k_plus) assert(i >= 1); #endif } diff --git a/src/main/game/move.h b/src/main/game/move.h index 8651d1e..5e6892f 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_to_waste, stock_to_tableau, redeal, null}; + 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); 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 7870b1b..276d582 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -257,14 +257,11 @@ void game_state::make_move(const move m) { case move::mtype::built_group: make_built_group_move(m); break; - case move::mtype::stock_to_waste: - make_stock_to_waste_move(m); + case move::mtype::stock_k_plus: + make_stock_k_plus_move(m); break; - case move::mtype::stock_to_tableau: - make_stock_to_tableau_move(m); - break; - case move::mtype::redeal: - make_redeal_move(); + case move::mtype::stock_to_all_tableau: + make_stock_to_all_tableau_move(m); break; case move::mtype::null: assert(false); @@ -285,14 +282,11 @@ void game_state::undo_move(const move m) { case move::mtype::built_group: undo_built_group_move(m); break; - case move::mtype::stock_to_waste: - undo_stock_to_waste_move(m); - break; - case move::mtype::stock_to_tableau: - undo_stock_to_tableau_move(m); + case move::mtype::stock_k_plus: + undo_stock_k_plus_move(m); break; - case move::mtype::redeal: - undo_redeal_move(); + case move::mtype::stock_to_all_tableau: + undo_stock_to_all_tableau_move(m); break; case move::mtype::null: assert(false); @@ -382,23 +376,71 @@ void game_state::undo_built_group_move(move m) { } } -void game_state::make_stock_to_waste_move(const move m) { +void game_state::make_stock_k_plus_move(const move m) { assert(rules.stock_deal_t == sdt::WASTE); + assert(m.count < piles[stock].size() + 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 - for (int i = 0; i < m.count; i++) { - place_card(waste, take_card(stock)); + // Index of the card to be moved + auto count = m.count % (piles[stock].size() + piles[waste].size()); + + for (int i = 0; i <= count; i++) { + if (i < count) place_card(waste, take_card(stock)); + else place_card(m.to, take_card(stock)); + + 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)); + } } + +#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); } -void game_state::undo_stock_to_waste_move(const move m) { +void game_state::undo_stock_k_plus_move(move m) { assert(rules.stock_deal_t == sdt::WASTE); + assert(m.count < piles[stock].size() + piles[waste].size() + 1); + 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 = 0; i < m.count; i++) { - place_card(stock, take_card(waste)); + 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 (i < count) place_card(stock, take_card(waste)); + else place_card(stock, take_card(m.to )); } + +#ifndef NDEBUG + auto sz_before = 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); } -void game_state::make_stock_to_tableau_move(const move m) { +void game_state::make_stock_to_all_tableau_move(move m) { assert(rules.stock_deal_t == sdt::TABLEAU_PILES); for (pile::ref tab_pr = original_tableau_piles.front(); @@ -408,7 +450,7 @@ void game_state::make_stock_to_tableau_move(const move m) { } } -void game_state::undo_stock_to_tableau_move(const move m) { +void game_state::undo_stock_to_all_tableau_move(move m) { assert(rules.stock_deal_t == sdt::TABLEAU_PILES); for (pile::ref tab_pr = original_tableau_piles.front() + m.count; @@ -418,23 +460,6 @@ void game_state::undo_stock_to_tableau_move(const move m) { } } -void game_state::make_redeal_move() { - assert(piles[stock].empty()); - - // Re-deal waste back to stock - while (!piles[waste].empty()) { - place_card(stock, take_card(waste)); - } -} - -void game_state::undo_redeal_move() { - assert(piles[waste].empty()); - - while (!piles[stock].empty()) { - place_card(waste, take_card(stock)); - } -} - // 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) { diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index b8b0d03..78f1fb9 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -24,6 +24,7 @@ class game_state { friend class cached_game_state; friend class deal_parser; friend class state_printer; + friend class test_helper; public: /* Constructors */ @@ -73,12 +74,10 @@ private: void undo_regular_move(move move); void make_built_group_move(move move); void undo_built_group_move(move move); - void make_stock_to_waste_move(move move); - void undo_stock_to_waste_move(move move); - void make_stock_to_tableau_move(move move); - void undo_stock_to_tableau_move(move move); - void make_redeal_move(); - void undo_redeal_move(); + void make_stock_k_plus_move(move move); + 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); #ifndef NDEBUG void check_face_down_consistent() const; @@ -86,31 +85,33 @@ private: /* Legal move generation */ - bool stock_can_deal_tableau() const; - move get_stock_tableau_move() const; - bool stock_can_redeal() const; - bool undoes_prev_move(pile::ref, move) const; - bool is_foundation(pile::ref) const; - - void add_stock_to_waste_move(std::vector<move>&) const; - void add_tableau_moves(std::vector<move>&, pile::ref) const; - void add_cell_moves(std::vector<move>&, pile::ref) const; - void add_foundation_moves(std::vector<move>&, pile::ref) const; - void add_built_group_moves(std::vector<move> &) const; + 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; + 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; void add_foundation_complete_piles_moves(std::vector<move> &) const; bool is_valid_tableau_move(pile::ref, pile::ref) const; + bool is_valid_tableau_move(card, pile::ref) const; bool is_next_tableau_card(card, card) const; bool is_valid_foundations_move(pile::ref, pile::ref) const; + bool is_valid_foundations_move(card, pile::ref) const; 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) 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 9ad8b1d..5dfa00a 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -4,12 +4,16 @@ #include "game_state.h" #include "../sol_rules.h" +#include "../move.h" #include <boost/optional/optional.hpp> +#include <iostream> using std::vector; using std::min; using std::list; +using std::pair; +using std::set; using namespace rapidjson; typedef sol_rules::build_policy pol; @@ -17,7 +21,6 @@ typedef sol_rules::spaces_policy s_pol; typedef sol_rules::stock_deal_type sdt; typedef sol_rules::face_up_policy fu; - //////////////////////////////// // MAIN LEGAL MOVE GEN CYCLE /// //////////////////////////////// @@ -26,41 +29,35 @@ typedef sol_rules::face_up_policy fu; vector<move> game_state::get_legal_moves(move parent_move) { // Order: // Foundations complete piles moves - // Tableau built group moves - // Tableau / cells / reserve to hole / foundation moves + // Tableau / cells / reserve / stock-waste to hole / foundation moves --- // Cell to tableau moves // Tableau to tableau moves - // Waste to tableau moves + // Tableau built group moves + // Stock-waste to tableau moves // Reserve to tableau moves // Foundation to tableau moves - // Tableau to cell moves - // Stock to waste moves - // Stock to tableau / redeal moves + // Tableau / stock-waste to cell moves + // Stock to all tableau moves vector<move> moves; // Stock to tableau / redeal moves - if (rules.stock_size > 0) { - if (stock_can_deal_tableau()) - moves.emplace_back(get_stock_tableau_move()); - else if (stock_can_redeal()) - moves.emplace_back(move::mtype::redeal); - } - - // Stock to waste moves - if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty()) - add_stock_to_waste_move(moves); + if (rules.stock_size > 0 && stock_can_deal_all_tableau()) + moves.emplace_back(get_stock_to_all_tableau_move()); - // Tableau to cell moves + // Tableau / waste to cell moves pile::ref empty_cell = 255; for (auto c : cells) { if (piles[c].empty()) empty_cell = c; } if (empty_cell != 255) { for (auto t : tableau_piles) { - if (piles[t].empty() || parent_move.to == t || !is_valid_tableau_move(t, empty_cell)) continue; + if (piles[t].empty() || parent_move.to == t) continue; else moves.emplace_back(move::mtype::regular, t, empty_cell); } + + if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty()) + add_stock_to_cell_move(moves, empty_cell); } // Foundation to tableau moves @@ -87,14 +84,12 @@ vector<move> game_state::get_legal_moves(move parent_move) { } } - // Waste to tableau moves - if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[waste].empty()) { - for (auto t : tableau_piles) { - if (is_valid_tableau_move(waste, t)) { - moves.emplace_back(move::mtype::regular, waste, t); - } - } - } + // Stock to tableau moves + if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty()) + add_stock_to_tableau_moves(moves); + + if (rules.move_built_group) + add_built_group_moves(moves); // Tableau to tableau moves for (auto t_from : tableau_piles) { @@ -118,8 +113,12 @@ vector<move> game_state::get_legal_moves(move parent_move) { } } - // Tableau / cells to hole / foundation moves + // Tableau / cells / reserve / stock-waste to hole / foundation moves if (rules.hole || (rules.foundations && !rules.foundations_comp_piles)) { + // Stock + if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty()) + add_stock_to_hole_foundation_moves(moves); + list<pile::ref> from_piles = tableau_piles; if (rules.cells > 0) from_piles.insert(from_piles.end(), cells.begin(), cells.end()); if (rules.reserve_size > 0) from_piles.insert(from_piles.end(), reserve.begin(), reserve.end()); @@ -135,60 +134,11 @@ vector<move> game_state::get_legal_moves(move parent_move) { } } - if (rules.move_built_group) - add_built_group_moves(moves); - if (rules.foundations_comp_piles) // i.e. Spider-type winning condition add_foundation_complete_piles_moves(moves); - if (rules.face_up != fu::ALL) + if (rules.tableau_pile_count > 0 && rules.face_up != fu::ALL) turn_face_down_cards(moves); - /* - - vector<move> moves; - - // Attempts stock tableau deal and redeal - if (rules.stock_size > 0) { - if (stock_can_deal_tableau()) - moves.emplace_back(get_stock_tableau_move()); - else if (stock_can_redeal()) - moves.emplace_back(move::mtype::redeal); - } - - // Cycles through all of the piles from which we want to remove cards - for (pile::ref rem_ref = 0; rem_ref < piles.size(); rem_ref++) { - if ((rules.hole && rem_ref == hole) || piles[rem_ref].empty()) - continue; - if (undoes_prev_move(rem_ref, parent_move)) - continue; - - if (rules.foundations && is_foundation(rem_ref)) { - if (!rules.foundations_removable) continue; - else if (dominance_blocks_foundation_move(rem_ref)) continue; - } - - if (rules.stock_size > 0 && rem_ref == stock) { - if (rules.stock_deal_t == sdt::WASTE) - add_stock_to_waste_move(moves); - continue; - } - - if (rules.cells > 0) - add_cell_moves(moves, rem_ref); - if (rules.tableau_pile_count > 0) - add_tableau_moves(moves, rem_ref); - if (rules.foundations) - add_foundation_moves(moves, rem_ref); - if (rules.hole && is_valid_hole_move(rem_ref)) - moves.emplace_back(move::mtype::regular, rem_ref, hole); - } - - if (rules.move_built_group) - add_built_group_moves(moves); - - if (rules.foundations_comp_piles) // I.E. Spider-type winning condition - add_foundation_complete_piles_moves(moves); -*/ return moves; } @@ -198,7 +148,7 @@ vector<move> game_state::get_legal_moves(move parent_move) { // REGULAR MOVE GEN FUNCTIONS // //////////////////////////////// -bool game_state::stock_can_deal_tableau() const { +bool game_state::stock_can_deal_all_tableau() const { return rules.stock_deal_t == sdt::TABLEAU_PILES && !piles[stock].empty(); } @@ -206,59 +156,104 @@ bool game_state::stock_can_deal_tableau() const { // This is a special kind of move which the solver handles differently. // We must supply the number of stock cards // that we will deal, so that the move can be undone in backtracking. -move game_state::get_stock_tableau_move() const { +move game_state::get_stock_to_all_tableau_move() const { pile::size_type stock_moves = piles[stock].size() >= tableau_piles.size() ? pile::size_type(tableau_piles.size()) : piles[stock].size(); assert(stock_moves > 0 && stock_moves <= piles[stock].size()); - return move(move::mtype::stock_to_tableau, 0, 0, stock_moves); + return move(move::mtype::stock_to_all_tableau, 0, 0, stock_moves); } -bool game_state::stock_can_redeal() const { - return rules.stock_redeal - && rules.stock_deal_t == sdt::WASTE - && piles[stock].empty() - && !piles[waste].empty(); -} +set<pile::size_type> game_state::generate_stock_moves_to_check() const { + set<pile::size_type> stock_moves_to_check; -bool game_state::undoes_prev_move(pile::ref rem_ref, move parent_move) const { - return rem_ref == parent_move.to - && parent_move.count == 1 - && parent_move.from != stock; -} + // 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); + } -bool game_state::is_foundation(pile::ref ref) const { - return ref >= foundations.front() && ref <= foundations.back(); + // Adds last card + stock_moves_to_check.insert(static_cast<pile::size_type>(piles[stock].size() - 1)); + + // 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)); + } + } + + return stock_moves_to_check; } -void game_state::add_stock_to_waste_move(std::vector<move>& moves) const { - uint8_t cards_to_move = min(rules.stock_deal_count, piles[stock].size()); - moves.emplace_back(move::mtype::stock_to_waste, stock, waste, cards_to_move); +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 (!rules.stock_redeal && !piles[waste].empty()) + moves.emplace_back(move::mtype::regular, waste, empty_cell); } -void game_state::add_tableau_moves(std::vector<move>& moves, pile::ref rem_ref) const { - for (auto add_ref : tableau_piles) { - if (is_valid_tableau_move(rem_ref, add_ref)) { - moves.emplace_back(move::mtype::regular, rem_ref, add_ref); +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 t : tableau_piles) { + if (is_valid_tableau_move(from, t)) + moves.emplace_back(move::mtype::stock_k_plus, stock, t, i); } } -} -void game_state::add_cell_moves(std::vector<move>& moves, pile::ref rem_ref) const { - for (auto add_ref : cells) { - if (add_ref != rem_ref && piles[add_ref].empty()) { - moves.emplace_back(move::mtype::regular, rem_ref, add_ref); + // 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()) { + for (auto t : tableau_piles) { + if (is_valid_tableau_move(waste, t)) + moves.emplace_back(move::mtype::regular, waste, t); } } } -void game_state::add_foundation_moves(std::vector<move>& moves, pile::ref rem_ref) const { - for (auto add_ref : foundations) { - if (is_valid_foundations_move(rem_ref, add_ref)) { - moves.emplace_back(move::mtype::regular, rem_ref, add_ref); - } +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 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 (!rules.stock_redeal && !piles[waste].empty()) { + for (auto f : foundations) + if (is_valid_foundations_move(waste, f)) + moves.emplace_back(move::mtype::regular, waste, f); + + if (rules.hole && is_valid_hole_move(waste)) + moves.emplace_back(move::mtype::regular, waste, hole); + } +} + +card game_state::stock_card_from_index(pile::size_type i) const { + if (i < piles[stock].size()) { + return piles[stock][i]; + } 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]; } } @@ -281,17 +276,21 @@ bool game_state::is_valid_tableau_move(const pile::ref rem_ref, if (rem_ref == add_ref || rules.build_pol == pol::NO_BUILD) return false; + return is_valid_tableau_move(piles[rem_ref].top_card(), add_ref); +} + +bool game_state::is_valid_tableau_move(const card rem_c, + const pile::ref add_ref) const { if (piles[add_ref].empty()) { switch(rules.spaces_pol) { case sol_rules::spaces_policy::NO_BUILD: return false; case sol_rules::spaces_policy::KINGS: - return piles[rem_ref].top_card().get_rank() == 13; + return rem_c.get_rank() == 13; default: return true; } } else { - card rem_c = piles[rem_ref].top_card(); card add_c = piles[add_ref].top_card(); return is_next_tableau_card(add_c, rem_c); @@ -306,8 +305,11 @@ 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; - card rem_c = piles[rem_ref].top_card(); + return is_valid_foundations_move(piles[rem_ref].top_card(), add_ref); +} +bool game_state::is_valid_foundations_move(const card rem_c, + const pile::ref add_ref) const { // Checks same suit policy uint8_t suit_idx = (add_ref - foundations.front()) % uint8_t(4); if (rem_c.get_suit() != suit_idx) @@ -322,8 +324,11 @@ bool game_state::is_valid_foundations_move(const pile::ref rem_ref, bool game_state::is_valid_hole_move(const pile::ref rem_ref) const { if (rem_ref == hole) return false; + return is_valid_hole_move(piles[rem_ref].top_card()); +} - card::rank_t rank = piles[rem_ref].top_card().get_rank(); +bool game_state::is_valid_hole_move(const card c) const { + card::rank_t rank = c.get_rank(); card::rank_t hole_rank = piles[hole].top_card().get_rank(); return rank + 1 == hole_rank @@ -337,7 +342,7 @@ bool game_state::is_valid_hole_move(const pile::ref rem_ref) 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 @@ -347,23 +352,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]; - - if (piles[add_ref].empty()) { - if (rules.spaces_pol == s_pol::ANY || (rules.spaces_pol == s_pol::KINGS && bg_high.get_rank() == 13)) - add_empty_built_group_moves(moves, rem_ref, add_ref, bg_high); - } else { - if (is_next_built_group_card(piles[add_ref].top_card(), bg_high)) { - bool is_reveal_move = - piles[rem_ref].size() > built_group_height - && piles[rem_ref][built_group_height].is_face_down(); - moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, built_group_height, is_reveal_move); - } + add_built_group_moves(moves, rem_ref, built_group_height); + } +} + +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); } } } @@ -378,22 +388,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) 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)); - } 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 a10df54..e540a51 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -359,6 +359,25 @@ map<string, string> sol_preset_types::mp = { }, + { + "simple-klondike", + + R"( +{ + "tableau piles": { + "count": 4, + "build policy": "red-black", + "move built group": true, + "diagonal deal": true, + "face up cards": "top" + }, + "max rank": 4, + "stock size": 6, + "stock deal count": 3, + "stock redeal": true +} +)" + }, // --- END OF 'CANONICAL' GAMES --- { @@ -392,9 +411,10 @@ 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": false + "stock redeal": true } )" @@ -413,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 @@ -795,7 +816,8 @@ map<string, string> sol_preset_types::mp = { "tableau piles": { "count": 7, "build policy": "red-black", - "move built group": true + "move built group": true, + "face up cards": "top" }, "foundations removable": true, "stock size": 31, diff --git a/src/test/integration_tests/klondike_test.cpp b/src/test/integration_tests/klondike_test.cpp new file mode 100644 index 0000000..d449495 --- /dev/null +++ b/src/test/integration_tests/klondike_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/klondike/"; +const static std::string type = "simple-klondike"; + +TEST(Klondike, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(Klondike, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(Klondike, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(Klondike, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/src/test/resources/klondike/ComplexSolvable.json b/src/test/resources/klondike/ComplexSolvable.json new file mode 100644 index 0000000..8711780 --- /dev/null +++ b/src/test/resources/klondike/ComplexSolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["3d","2c","4c","2H"], + ["Ah","3h","2S"], + ["Ad","3C"], + ["4S"] + ], + "stock": ["3S","AS","AC","2D","4H","4D"] +}
\ No newline at end of file diff --git a/src/test/resources/klondike/ComplexUnsolvable.json b/src/test/resources/klondike/ComplexUnsolvable.json new file mode 100644 index 0000000..7685ce1 --- /dev/null +++ b/src/test/resources/klondike/ComplexUnsolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["3d","2c","4c","2H"], + ["Ah","3h","2S"], + ["Ad","3C"], + ["4S"] + ], + "stock": ["3S","4D","AC","2D","4H","AS"] +}
\ No newline at end of file diff --git a/src/test/resources/klondike/SimpleSolvable.json b/src/test/resources/klondike/SimpleSolvable.json new file mode 100644 index 0000000..f54ebbb --- /dev/null +++ b/src/test/resources/klondike/SimpleSolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["3d","2c","Ac","2H"], + ["4h","3h","2S"], + ["4d","3C"], + ["AS"] + ], + "stock": ["3S","4S","4C","2D","AH","AD"] +}
\ No newline at end of file diff --git a/src/test/resources/klondike/SimpleUnsolvable.json b/src/test/resources/klondike/SimpleUnsolvable.json new file mode 100644 index 0000000..21a5854 --- /dev/null +++ b/src/test/resources/klondike/SimpleUnsolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["As","2c","2d","2H"], + ["Ah","3h","2S"], + ["Ad","3C"], + ["4S"] + ], + "stock": ["3S","4D","AC","4C","4H","3D"] +}
\ No newline at end of file diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 1bc1f24..c85cf39 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -86,6 +86,43 @@ bool test_helper::moves_eq(vector<move>& exp_moves, vector<move>& actual_moves) return true; } + +void test_helper::k_plus_stock_test + ( sol_rules sr + , string_il piles + , vector<move> exp_moves + , move m + , pile::size_type stock_size + , pile::size_type waste_size + , card top_stock + , card top_waste + ) { + + game_state gs(sr, piles); + vector<move> actual_moves = gs.get_legal_moves(); + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; + + gs.make_move(m); + + ASSERT_EQ(gs.get_data()[gs.stock].size(), stock_size) << gs.get_data()[gs.stock].size(); + ASSERT_EQ(gs.get_data()[gs.waste].size(), waste_size) << gs.get_data()[gs.waste].size(); + + if (top_stock == card()) + ASSERT_TRUE(gs.get_data()[gs.stock].empty()); + else + ASSERT_EQ(gs.get_data()[gs.stock].top_card(), top_stock ) << gs.get_data()[gs.stock].top_card().to_string(); + + if (top_waste == card()) + ASSERT_TRUE(gs.get_data()[gs.waste].empty()); + else + ASSERT_EQ(gs.get_data()[gs.waste].top_card(), top_waste ) << gs.get_data()[gs.waste].top_card().to_string(); + + gs.undo_move(m); + + ASSERT_EQ(gs.get_data(), game_state(sr, piles).get_data()) << gs; +} + ostream& operator <<(ostream& os, const move& m) { return os << "move:(" << int(m.from) << "," << int(m.to) diff --git a/src/test/test_helper.h b/src/test/test_helper.h index 3df9ead..564310e 100644 --- a/src/test/test_helper.h +++ b/src/test/test_helper.h @@ -17,7 +17,18 @@ public: static bool is_solvable(const std::string&, const std::string&); static void run_foundations_dominance_test(sol_rules::build_policy policy, std::vector<card> cards); - static void expected_moves_test(sol_rules sr, std::initializer_list<std::initializer_list<std::string>>, std::vector<move>); + static void expected_moves_test(sol_rules sr, std::initializer_list<std::initializer_list<std::string>>, + std::vector<move>); + static void k_plus_stock_test + ( sol_rules sr + , std::initializer_list<std::initializer_list<std::string>> + , std::vector<move> + , move + , pile::size_type + , pile::size_type + , card + , card + ); static bool moves_eq(std::vector<move>&, std::vector<move>&); }; diff --git a/src/test/unit_tests/built_group_move_gen_test.cpp b/src/test/unit_tests/built_group_move_gen_test.cpp index 37a2cf0..7bc7f46 100644 --- a/src/test/unit_tests/built_group_move_gen_test.cpp +++ b/src/test/unit_tests/built_group_move_gen_test.cpp @@ -9,6 +9,7 @@ #include "../../main/game/sol_rules.h" typedef sol_rules::build_policy pol; +typedef sol_rules::spaces_policy s_pol; using std::vector; using std::ostream; @@ -155,7 +156,8 @@ TEST(BuiltGroupMoveGen, CantMove) { TEST(BuiltGroupMoveGen, SameSuitOccupied) { sol_rules sr; - sr.tableau_pile_count = 5; + sr.tableau_pile_count = 6; + sr.build_pol = pol::SAME_SUIT; sr.built_group_pol = pol::SAME_SUIT; sr.move_built_group = true; @@ -166,7 +168,8 @@ TEST(BuiltGroupMoveGen, SameSuitOccupied) { {"3C", "2C", "AC"}, // Same suit {"3H", "2S", "AH"}, // Red black {"3D", "2D", "AS"}, // Any suit - {} + {}, + {"3C"} }, { // Regular moves @@ -174,18 +177,22 @@ TEST(BuiltGroupMoveGen, SameSuitOccupied) { move(move::mtype::regular, 2, 4, 1), move(move::mtype::regular, 3, 4, 1), move(move::mtype::regular, 0, 4, 1), + move(move::mtype::regular, 5, 4, 1), + move(move::mtype::regular, 5, 0, 1), // Built group moves move(move::mtype::built_group, 1, 0, 3), move(move::mtype::built_group, 1, 4, 2), - move(move::mtype::built_group, 1, 4, 3) + move(move::mtype::built_group, 1, 4, 3), + move(move::mtype::built_group, 1, 5, 2) } ); } TEST(BuiltGroupMoveGen, RedBlackOccupied) { sol_rules sr; - sr.tableau_pile_count = 5; + sr.tableau_pile_count = 6; + sr.build_pol = pol::RED_BLACK; sr.built_group_pol = pol::RED_BLACK; sr.move_built_group = true; @@ -196,7 +203,8 @@ TEST(BuiltGroupMoveGen, RedBlackOccupied) { {"3C", "2C", "AC"}, // Same suit {"3H", "2S", "AH"}, // Red black {"3D", "2D", "AS"}, // Any suit - {} + {}, + {"3C"} }, { // Regular moves @@ -204,19 +212,22 @@ TEST(BuiltGroupMoveGen, RedBlackOccupied) { move(move::mtype::regular, 2, 4, 1), move(move::mtype::regular, 3, 4, 1), move(move::mtype::regular, 0, 4, 1), + move(move::mtype::regular, 5, 4, 1), // Built group moves move(move::mtype::built_group, 2, 0, 3), move(move::mtype::built_group, 2, 4, 2), move(move::mtype::built_group, 2, 4, 3), - move(move::mtype::built_group, 3, 4, 2) + move(move::mtype::built_group, 3, 4, 2), + move(move::mtype::built_group, 3, 5, 2) } ); } TEST(BuiltGroupMoveGen, AnySuitOccupied) { sol_rules sr; - sr.tableau_pile_count = 5; + sr.tableau_pile_count = 6; + sr.build_pol = pol::ANY_SUIT; sr.built_group_pol = pol::ANY_SUIT; sr.move_built_group = true; @@ -227,7 +238,8 @@ TEST(BuiltGroupMoveGen, AnySuitOccupied) { {"3C", "2C", "AC"}, // Same suit {"3H", "2S", "AH"}, // Red black {"3D", "2D", "AS"}, // Any suit - {} + {}, + {"3C"} }, { // Regular moves @@ -235,6 +247,8 @@ TEST(BuiltGroupMoveGen, AnySuitOccupied) { move(move::mtype::regular, 2, 4, 1), move(move::mtype::regular, 3, 4, 1), move(move::mtype::regular, 0, 4, 1), + move(move::mtype::regular, 5, 4, 1), + move(move::mtype::regular, 5, 0, 1), // Built group moves move(move::mtype::built_group, 1, 0, 3), @@ -249,6 +263,56 @@ TEST(BuiltGroupMoveGen, AnySuitOccupied) { move(move::mtype::built_group, 3, 4, 2), move(move::mtype::built_group, 3, 4, 3), + + move(move::mtype::built_group, 1, 5, 2), + move(move::mtype::built_group, 2, 5, 2), + move(move::mtype::built_group, 3, 5, 2) + } + ); +} + +TEST(BuiltGroupMoveGen, KingsOnly) { + sol_rules sr; + sr.tableau_pile_count = 5; + sr.build_pol = pol::SAME_SUIT; + sr.built_group_pol = pol::RED_BLACK; + sr.move_built_group = true; + sr.spaces_pol = s_pol::KINGS; + + test_helper::expected_moves_test( + sr, + { + {}, + {"KC", "QC", "JC"}, // Same suit + {"KH", "QS", "JH"}, // Red black + {"KD", "QD", "JS"}, // Any suit + {} + }, + { + // Built group moves + move(move::mtype::built_group, 2, 0, 3), + move(move::mtype::built_group, 2, 4, 3) } ); } + +TEST(BuiltGroupMoveGen, NoBuild) { + sol_rules sr; + sr.tableau_pile_count = 5; + sr.build_pol = pol::SAME_SUIT; + sr.built_group_pol = pol::RED_BLACK; + sr.move_built_group = true; + sr.spaces_pol = s_pol::NO_BUILD; + + test_helper::expected_moves_test( + sr, + { + {}, + {"KC", "QC", "JC"}, // Same suit + {"KH", "QS", "JH"}, // Red black + {"KD", "QD", "JS"}, // Any suit + {} + }, + {} + ); +} diff --git a/src/test/unit_tests/k_plus_stock_test.cpp b/src/test/unit_tests/k_plus_stock_test.cpp new file mode 100644 index 0000000..1507f1d --- /dev/null +++ b/src/test/unit_tests/k_plus_stock_test.cpp @@ -0,0 +1,369 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" +#include "../../main/game/search-state/game_state.h" +#include "../../main/game/sol_rules.h" + +typedef sol_rules::build_policy pol; +typedef sol_rules::stock_deal_type sdt; + +using std::vector; +using std::ostream; +using std::initializer_list; + + +TEST(KPlusStock, NoRedealExact) { + 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 = false; + sr.stock_deal_count = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","2D","3S","AD","2S","3C","AC","AH","AS"}, + {}, + {"3D"}, + {"3H"}, + {"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, 3, 8), + 0, 8, + card(), "2D" + ); +} + +TEST(KPlusStock, NoRedealOneExtra) { + 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 = false; + sr.stock_deal_count = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","AD","2S","3C","AC","AH","AS"}, + {}, + {"3D"}, + {"3H"}, + {"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, 3, 6), + 0, 6, + card(), "AD" + ); +} + +TEST(KPlusStock, NoRedealTwoExtra) { + 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 = false; + sr.stock_deal_count = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2S","2H","AD","2D","3C","AC","AH","AS"}, + {}, + {"3D"}, + {"3H"}, + {"2C"} + }, + { + move(move::mtype::stock_k_plus, 0, 4, 2) + }, + move(move::mtype::stock_k_plus, 0, 4, 2), + 5, 2, + "3C", "AH" + ); +} + +TEST(KPlusStock, RedealExact) { + 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 = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","2D","3S","AD","2S","3C","AC","AH","AS"}, + {}, + {"3D"}, + {"3H"}, + {"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, 3, 8), + 8, 0, + "AS", card() + ); +} + +TEST(KPlusStock, RedealOneExtra) { + 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 = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","AD","2S","3C","AC","AH","AS"}, + {}, + {"3D"}, + {"3H"}, + {"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, 3, 6), + 6, 0, + "AS", card() + ); +} + +TEST(KPlusStock, RedealTwoExtra) { + 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 = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2S","2H","AD","2D","3C","AC","AH","AS"}, + {}, + {"3D"}, + {"3H"}, + {"2C"} + }, + { + move(move::mtype::stock_k_plus, 0, 4, 2) + }, + move(move::mtype::stock_k_plus, 0, 4, 2), + 5, 2, + "3C", "AH" + ); +} + +TEST(KPlusStock, RedealKPlus3Waste) { + 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 = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","2D","3S","AD","2S","3C"}, + {"AS","AH","AC"}, + {"3D"}, + {"3H"}, + {"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, 4, 8), + 6, 2, + "3C", "AH" + ); +} + +TEST(KPlusStock, RedealKPlus4Waste) { + 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 = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","AD","3S","2D","2S"}, + {"AS","AH","AC","3C"}, + {"3D"}, + {"3H"}, + {"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, 2, 1), + 3, 5, + "3S", "2S" + ); +} + +TEST(KPlusStock, RedealKPlus5Waste) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 10; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = true; + sr.stock_deal_count = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2S","2D","2H","AD","2S"}, + {"AS","AH","AC","3C","4S"}, + {"3D"}, + {"3H"}, + {"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, 2), + 2, 7, + "2D", "AD" + ); +} + +TEST(KPlusStock, RedealKPlusEmptyStock) { + 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 = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","2D","3S","AD","2S","3C"}, + {"AS","AH","AC"}, + {"3D"}, + {"3H"}, + {"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, 5), + 8, 0, + "AS", card() + ); +} + +TEST(KPlusStock, NoRedealCellFoundationMoves) { + sol_rules sr; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 9; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = false; + sr.stock_deal_count = 3; + sr.cells = 2; + sr.foundations = true; + + test_helper::k_plus_stock_test( + sr, + { + {"AC"}, {}, {}, {}, + {"2D"}, {}, + {"2H","AS","2C","AH","2S","3C"}, + {"3S"} + }, + { + // Cell moves + move(move::mtype::stock_k_plus, 6, 5, 2), + move(move::mtype::stock_k_plus, 6, 5, 5), + + // Foundation moves + move(move::mtype::stock_k_plus, 6, 1, 2), + + // Regular waste move + move(move::mtype::regular, 7, 5, 1) + }, + move(move::mtype::stock_k_plus, 6, 1, 2), + 3, 3, + "2C", "2S" + ); +} + +TEST(KPlusStock, RedealCellFoundationMoves) { + sol_rules sr; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 9; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = true; + sr.stock_deal_count = 3; + sr.cells = 2; + sr.foundations = true; + + test_helper::k_plus_stock_test( + sr, + { + {"AC"}, {}, {}, {}, + {"2D"}, {}, + {"2H","AS","2C","AH","2S","3C"}, + {"3S"} + }, + { + // 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), + + // 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, 2, 4), + 1, 5, + "2H", "2C" + ); +} diff --git a/src/test/unit_tests/legal_move_gen_test.cpp b/src/test/unit_tests/legal_move_gen_test.cpp index 6ea73d5..cbdeae0 100644 --- a/src/test/unit_tests/legal_move_gen_test.cpp +++ b/src/test/unit_tests/legal_move_gen_test.cpp @@ -262,28 +262,6 @@ TEST(LegalMoveGen, Cells) { ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; } -TEST(LegalMoveGen, StockDealToWaste) { - sol_rules sr; - sr.foundations = true; - sr.tableau_pile_count = 3; - sr.stock_size = 2; - sr.stock_deal_t = sdt::WASTE; - - game_state gs(sr, string_il{ - {}, {}, {}, {}, // The foundations - {"AC", "AD"}, // The stock - {}, // The waste - {}, {}, {} // The tableau piles - }); - vector<move> actual_moves = gs.get_legal_moves(); - - vector<move> exp_moves = { - move(move::mtype::stock_to_waste, 4, 5) - }; - - ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; -} - TEST(LegalMoveGen, StockDealToTableau) { sol_rules sr; sr.foundations = true; @@ -299,7 +277,7 @@ TEST(LegalMoveGen, StockDealToTableau) { vector<move> actual_moves = gs.get_legal_moves(); vector<move> exp_moves = { - move(move::mtype::stock_to_tableau, 0, 0, 2) // Special stock-to-tableau syntax + move(move::mtype::stock_to_all_tableau, 0, 0, 2) // Special stock-to-tableau syntax }; ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; |
