diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/game/move.cpp | 9 | ||||
| -rw-r--r-- | src/main/game/move.h | 7 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.cpp | 75 | ||||
| -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 | 90 | ||||
| -rw-r--r-- | src/test/unit_tests/k_plus_stock_test.cpp | 148 |
6 files changed, 183 insertions, 150 deletions
diff --git a/src/main/game/move.cpp b/src/main/game/move.cpp index 915bac6..528b631 100644 --- a/src/main/game/move.cpp +++ b/src/main/game/move.cpp @@ -4,11 +4,12 @@ #include "move.h" -move::move(mtype ty, pile::ref f, pile::ref t, pile::size_type i, bool reveal_move_) : - type(ty), from(f), to(t), count(i), reveal_move(reveal_move_) { +move::move(mtype ty, pile::ref f, pile::ref t, int8_t count_, bool reveal_move_, bool flip_waste_) : + type(ty), from(f), to(t), count(count_), reveal_move(reveal_move_), flip_waste(flip_waste_) { #ifndef NDEBUG - if (ty == mtype::regular) assert(i == 1); - else if (ty != mtype::stock_k_plus) assert(i >= 1); + 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 5e6892f..27ba279 100644 --- a/src/main/game/move.h +++ b/src/main/game/move.h @@ -10,7 +10,7 @@ struct move { 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); + 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&); void make_reveal_move(); @@ -18,8 +18,11 @@ struct move { mtype type; pile::ref from; pile::ref to; - pile::size_type count; + // For built piles the size of the pile, or for stock k-plus moves, + // the number of cards to be moved from the stock to the waste before the waste card is played on the foundations + int8_t count; bool reveal_move; + bool flip_waste; }; #endif //SOLVITAIRE_MOVE_H diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 169c96e..0720f6e 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -378,75 +378,78 @@ void game_state::undo_built_group_move(move m) { } void game_state::make_stock_k_plus_move(const move m) { +#ifndef NDEBUG assert(rules.stock_deal_t == sdt::WASTE); - assert(m.count < piles[stock].size() + piles[waste].size()); + assert(m.from == stock); + if (rules.stock_redeal) assert(m.count <= piles[stock].size() && m.count > -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 - - // Index of the card to be moved - auto count = m.count % (piles[stock].size() + piles[waste].size()); - - for (int i = 0; i < count; i++) { - place_card(waste, 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)); + // Transfers count cards from the stock to the waste + if (m.count > 0) { + for (int i = 0; i < m.count; i++) { + place_card(waste, take_card(stock)); + } + } else { + for (int i = 0; i > m.count; i--) { + place_card(stock, take_card(waste)); } } - place_card(m.to, take_card(stock)); - if (rules.stock_redeal && piles[stock].empty() && count != 0) - while (!piles[waste].empty()) + // Moves the card on top of the waste to the target pile + place_card(m.to, take_card(waste)); + + // Flips the waste back on to the empty stock (if necessary) + if (m.flip_waste) { + assert(rules.stock_redeal && 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); +#endif } void game_state::undo_stock_k_plus_move(move m) { +#ifndef NDEBUG assert(rules.stock_deal_t == sdt::WASTE); - assert(m.count < piles[stock].size() + piles[waste].size() + 1); + assert(m.from == stock); 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); - - if (rules.stock_redeal && piles[waste].empty() && count != 0) - while (!piles[stock].empty()) + if (m.flip_waste) { + assert(rules.stock_redeal && piles[waste].empty()); + while (!piles[stock].empty()) { place_card(waste, take_card(stock)); - place_card(stock, take_card(m.to)); - - for (int i = 0; i < count; 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)); } + } + + place_card(waste, take_card(m.to)); - place_card(stock, take_card(waste)); + if (m.count > 0) { + for (int i = 0; i < m.count; i++) { + place_card(stock, take_card(waste)); + } + } else { + for (int i = 0; i > m.count; i--) { + place_card(waste, take_card(stock)); + } } #ifndef NDEBUG auto sz_before = piles[stock].size() + piles[waste].size(); assert(sz_before == sz_after + 1); -#endif + if (rules.stock_redeal) assert(m.count <= piles[stock].size() && m.count > -piles[waste].size()); assert(!(rules.stock_size > 0 && rules.stock_redeal && piles[stock].empty() && !piles[waste].empty())); assert(piles[stock].size() <= rules.stock_size); +#endif } void game_state::make_stock_to_all_tableau_move(move m) { diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index ecb8ace..3f354d1 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -90,11 +90,11 @@ private: 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; + std::set<std::pair<int8_t, bool>> generate_k_plus_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; + card stock_card_from_count(int8_t) const; void add_foundation_complete_piles_moves(std::vector<move> &) const; bool is_valid_tableau_move(pile::ref, pile::ref) const; diff --git a/src/main/game/search-state/game_state.legal_moves.cpp b/src/main/game/search-state/game_state.legal_moves.cpp index f68df0b..f6fb68f 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -171,95 +171,67 @@ move game_state::get_stock_to_all_tableau_move() const { return move(move::mtype::stock_to_all_tableau, 0, 0, stock_moves); } -set<pile::size_type> game_state::generate_stock_moves_to_check() const { - set<pile::size_type> stock_moves_to_check; +set<pair<int8_t, bool>> game_state::generate_k_plus_moves_to_check() const { + set<pair<int8_t, bool>> stock_moves_to_check; + + // If the waste isn't empty, adds the move from the top of the current waste + if (piles[waste].size() != 0) stock_moves_to_check.insert(pair<int8_t, bool>(0, false)); // 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); - } + for (int8_t count = rules.stock_deal_count; + count < piles[stock].size(); + count += rules.stock_deal_count) + stock_moves_to_check.insert(pair<int8_t, bool>(count, false)); // Adds last card - stock_moves_to_check.insert(static_cast<pile::size_type>(piles[stock].size() - 1)); + bool flip_waste = rules.stock_redeal; + stock_moves_to_check.insert(pair<int8_t, bool>(static_cast<int8_t>(piles[stock].size()), flip_waste)); // 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) { - if (i == stock_waste_sz - 1) continue; // No need to add top of waste card as it is a regular move - stock_moves_to_check.insert(static_cast<pile::size_type>(i % stock_waste_sz)); - } - } + if (rules.stock_redeal) + for (auto count = static_cast<int8_t>(-piles[waste].size() + rules.stock_deal_count); + count < piles[stock].size(); + count += rules.stock_deal_count) + stock_moves_to_check.insert(pair<int8_t, bool>(count, false)); return stock_moves_to_check; } 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 (!piles[waste].empty()) - moves.emplace_back(move::mtype::regular, waste, empty_cell); + for (auto k_plus_mv : generate_k_plus_moves_to_check()) + moves.emplace_back(move::mtype::stock_k_plus, stock, empty_cell, k_plus_mv.first, false, k_plus_mv.second); } 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 k_plus_mv : generate_k_plus_moves_to_check()) { + card from = stock_card_from_count(k_plus_mv.first); - for (auto t : tableau_piles) { + for (auto t : tableau_piles) if (is_valid_tableau_move(from, t)) - moves.emplace_back(move::mtype::stock_k_plus, stock, t, 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 tableau piles - if (!piles[waste].empty()) { - for (auto t : tableau_piles) { - if (is_valid_tableau_move(waste, t)) - moves.emplace_back(move::mtype::regular, waste, t); - } + moves.emplace_back(move::mtype::stock_k_plus, stock, t, k_plus_mv.first, false, k_plus_mv.second); } } 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 k_plus_mv : generate_k_plus_moves_to_check()) { + card from = stock_card_from_count(k_plus_mv.first); 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 (!piles[waste].empty()) { - for (auto f : foundations) - if (is_valid_foundations_move(waste, f)) - moves.emplace_back(move::mtype::regular, waste, f); + moves.emplace_back(move::mtype::stock_k_plus, stock, f, k_plus_mv.first, false, k_plus_mv.second); - if (rules.hole && is_valid_hole_move(waste)) - moves.emplace_back(move::mtype::regular, waste, hole); + if (rules.hole && is_valid_hole_move(from)) + moves.emplace_back(move::mtype::stock_k_plus, stock, hole, k_plus_mv.first, false, k_plus_mv.second); } } -card game_state::stock_card_from_index(pile::size_type i) const { - if (i < piles[stock].size()) { - return piles[stock][i]; +card game_state::stock_card_from_count(int8_t count) const { + if (count > 0) { + return piles[stock][count - 1]; } 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]; + return piles[waste][-count]; } } diff --git a/src/test/unit_tests/k_plus_stock_test.cpp b/src/test/unit_tests/k_plus_stock_test.cpp index eb376ef..dcd4410 100644 --- a/src/test/unit_tests/k_plus_stock_test.cpp +++ b/src/test/unit_tests/k_plus_stock_test.cpp @@ -35,10 +35,10 @@ TEST(KPlusStock, NoRedealExact) { {"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, 4, 3), + move(move::mtype::stock_k_plus, 0, 3, 9), }, - move(move::mtype::stock_k_plus, 0, 3, 8), + move(move::mtype::stock_k_plus, 0, 3, 9), 0, 8, card(), "2D" ); @@ -63,10 +63,10 @@ TEST(KPlusStock, NoRedealOneExtra) { {"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, 4, 3), + move(move::mtype::stock_k_plus, 0, 3, 7), }, - move(move::mtype::stock_k_plus, 0, 3, 6), + move(move::mtype::stock_k_plus, 0, 3, 7), 0, 6, card(), "AD" ); @@ -91,9 +91,9 @@ TEST(KPlusStock, NoRedealTwoExtra) { {"2C"} }, { - move(move::mtype::stock_k_plus, 0, 4, 2) + move(move::mtype::stock_k_plus, 0, 4, 3) }, - move(move::mtype::stock_k_plus, 0, 4, 2), + move(move::mtype::stock_k_plus, 0, 4, 3), 5, 2, "3C", "AH" ); @@ -118,10 +118,10 @@ TEST(KPlusStock, RedealExact) { {"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, 4, 3, false, false), + move(move::mtype::stock_k_plus, 0, 3, 9, false, true), }, - move(move::mtype::stock_k_plus, 0, 3, 8), + move(move::mtype::stock_k_plus, 0, 3, 9, false, true), 8, 0, "AS", card() ); @@ -146,10 +146,10 @@ TEST(KPlusStock, RedealOneExtra) { {"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, 4, 3, false, false), + move(move::mtype::stock_k_plus, 0, 3, 7, false, true), }, - move(move::mtype::stock_k_plus, 0, 3, 6), + move(move::mtype::stock_k_plus, 0, 3, 7, false, true), 6, 0, "AS", card() ); @@ -174,9 +174,9 @@ TEST(KPlusStock, RedealTwoExtra) { {"2C"} }, { - move(move::mtype::stock_k_plus, 0, 4, 2) + move(move::mtype::stock_k_plus, 0, 4, 3, false, false) }, - move(move::mtype::stock_k_plus, 0, 4, 2), + move(move::mtype::stock_k_plus, 0, 4, 3, false, false), 5, 2, "3C", "AH" ); @@ -201,13 +201,12 @@ TEST(KPlusStock, RedealKPlus3Waste) { {"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, 6, false, true), + move(move::mtype::stock_k_plus, 0, 4, 0, false, false) }, - move(move::mtype::stock_k_plus, 0, 4, 8), - 6, 2, - "3C", "AH" + move(move::mtype::stock_k_plus, 0, 3, 6, false, true), + 8, 0, + "AS", card() ); } @@ -230,11 +229,11 @@ TEST(KPlusStock, RedealKPlus4Waste) { {"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, 3, 5, false, true), + move(move::mtype::stock_k_plus, 0, 4, -1, false, false), + move(move::mtype::stock_k_plus, 0, 2, 2, false, false) }, - move(move::mtype::stock_k_plus, 0, 2, 1), + move(move::mtype::stock_k_plus, 0, 2, 2, false, false), 3, 5, "3S", "2S" ); @@ -259,11 +258,11 @@ TEST(KPlusStock, RedealKPlus5Waste) { {"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, 3, false, false), + move(move::mtype::stock_k_plus, 0, 4, -2, false, false), + move(move::mtype::stock_k_plus, 0, 2, 4, false, false) }, - move(move::mtype::stock_k_plus, 0, 3, 2), + move(move::mtype::stock_k_plus, 0, 3, 3, false, false), 2, 7, "2D", "AD" ); @@ -288,11 +287,10 @@ TEST(KPlusStock, RedealKPlusEmptyStock) { {"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, 6, false, true), + move(move::mtype::stock_k_plus, 0, 4, 0, false, false) }, - move(move::mtype::stock_k_plus, 0, 3, 5), + move(move::mtype::stock_k_plus, 0, 3, 6, false, true), 8, 0, "AS", card() ); @@ -318,16 +316,16 @@ TEST(KPlusStock, NoRedealCellFoundationMoves) { }, { // 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, 3), + move(move::mtype::stock_k_plus, 6, 5, 6), // Foundation moves - move(move::mtype::stock_k_plus, 6, 1, 2), + move(move::mtype::stock_k_plus, 6, 1, 3), // Regular waste move - move(move::mtype::regular, 7, 5, 1) + move(move::mtype::stock_k_plus, 6, 5, 0) }, - move(move::mtype::stock_k_plus, 6, 1, 2), + move(move::mtype::stock_k_plus, 6, 1, 3), 3, 3, "2C", "2S" ); @@ -353,20 +351,76 @@ TEST(KPlusStock, RedealCellFoundationMoves) { }, { // 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), + move(move::mtype::stock_k_plus, 6, 5, 3, false, false), + move(move::mtype::stock_k_plus, 6, 5, 6, false, true), + move(move::mtype::stock_k_plus, 6, 5, 2, false, false), + move(move::mtype::stock_k_plus, 6, 5, 5, false, false), // 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, 1, 3, false, false), + move(move::mtype::stock_k_plus, 6, 2, 5, false, false), // Regular waste move - move(move::mtype::regular, 7, 5, 1) + move(move::mtype::stock_k_plus, 6, 5, 0) }, - move(move::mtype::stock_k_plus, 6, 2, 4), + move(move::mtype::stock_k_plus, 6, 2, 5, false, false), 1, 5, "2H", "2C" ); } + +TEST(KPlusStock, RedealCount1BottomStock) { + 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 = 1; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","2S","AD","2D"}, + {}, + {"3D"}, + {"3H"}, + {"2C"} + }, + { + move(move::mtype::stock_k_plus, 0, 2, 1, false, false), + move(move::mtype::stock_k_plus, 0, 3, 4, false, true) + }, + move(move::mtype::stock_k_plus, 0, 3, 4, false, true), + 3, 0, + "2D", card() + ); +} + +TEST(KPlusStock, RedealCount1TopStock) { + 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 = 1; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","2S","AD","2D"}, + {}, + {"3D"}, + {"3H"}, + {"2C"} + }, + { + move(move::mtype::stock_k_plus, 0, 2, 1, false, false), + move(move::mtype::stock_k_plus, 0, 3, 4, false, true) + }, + move(move::mtype::stock_k_plus, 0, 2, 1, false, false), + 3, 0, + "AD", card() + ); +} |
