diff options
| author | Charlie Blake | 2018-06-28 14:19:31 +0100 |
|---|---|---|
| committer | Charlie Blake | 2018-06-28 14:19:31 +0100 |
| commit | c84d8ccf133bc49c44c185dac7b49a46b8878ec1 (patch) | |
| tree | 6552511569ab94e28036c03ce5e889630d8df045 | |
| parent | Add kplus unit tests (diff) | |
| download | metasol-c84d8ccf133bc49c44c185dac7b49a46b8878ec1.tar.gz metasol-c84d8ccf133bc49c44c185dac7b49a46b8878ec1.zip | |
Implement to pass unit tests
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/main/game/move.cpp | 2 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.cpp | 31 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.legal_moves.cpp | 56 | ||||
| -rw-r--r-- | src/test/test_helper.cpp | 4 | ||||
| -rw-r--r-- | src/test/unit_tests/k_plus_stock_test.cpp (renamed from src/test/unit_tests/k-plus-stock-test.cpp) | 66 | ||||
| -rw-r--r-- | src/test/unit_tests/legal_move_gen_test.cpp | 22 |
7 files changed, 117 insertions, 66 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 9370835..cc4200c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,7 +75,7 @@ set(sources_test src/test/unit_tests/legal_move_gen_test.cpp src/test/unit_tests/built_group_move_gen_test.cpp src/test/unit_tests/face_up_cards_test.cpp - src/test/unit_tests/k-plus-stock-test.cpp) + src/test/unit_tests/k_plus_stock_test.cpp) ############################################################################### ## Rapid JSON setup ########################################################### diff --git a/src/main/game/move.cpp b/src/main/game/move.cpp index 9e45c66..c51a120 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_to_waste) assert(i >= 1); #endif } diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 7870b1b..d974c26 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -385,16 +385,39 @@ void game_state::undo_built_group_move(move m) { void game_state::make_stock_to_waste_move(const move m) { assert(rules.stock_deal_t == sdt::WASTE); - 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)); + } } } void game_state::undo_stock_to_waste_move(const move m) { assert(rules.stock_deal_t == sdt::WASTE); - for (int i = 0; i < m.count; i++) { - place_card(stock, take_card(waste)); + auto count = m.count % (piles[stock].size() + piles[waste].size() + 1); + + 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 )); } } 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..fd71e5c 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,15 @@ #include "game_state.h" #include "../sol_rules.h" +#include "../move.h" #include <boost/optional/optional.hpp> using std::vector; using std::min; using std::list; +using std::pair; +using std::set; using namespace rapidjson; typedef sol_rules::build_policy pol; @@ -88,7 +91,10 @@ 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()) { + if (rules.stock_size > 0 + && rules.stock_deal_t == sdt::WASTE + && !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); @@ -234,8 +240,52 @@ bool game_state::is_foundation(pile::ref ref) const { } 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); + set<pile::size_type> stock_moves_to_check; + + // 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); + } + + // 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)); + } + } + + // TODO: invariant -> no waste moves if redeal + // TODO: invariant -> count always < stock+waste + + // 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 : stock_moves_to_check) { + card from; + // If we are moving from the waste in a readeal (by looping), finds the correct card + if (i < piles[stock].size()) { + from = piles[stock][i]; + } else { + auto _i = static_cast<pile::size_type>(piles[stock].size() + piles[waste].size() - 1 - i); + from = piles[waste][_i]; + } + + for (auto t : tableau_piles) { + bool valid_move = piles[t].empty() + ? true + : is_next_tableau_card(piles[t].top_card(), from); + + if (valid_move) + moves.emplace_back(move::mtype::stock_to_waste, stock, t, i); + } + } } void game_state::add_tableau_moves(std::vector<move>& moves, pile::ref rem_ref) const { diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index b6584ed..333cc07 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -105,8 +105,8 @@ void test_helper::k_plus_stock_test gs.make_move(m); - ASSERT_EQ(gs.get_data()[0].size (), stock_size) << gs.get_data()[0].size (); - ASSERT_EQ(gs.get_data()[1].size (), waste_size) << gs.get_data()[1].size (); + ASSERT_EQ(gs.get_data()[0].size(), stock_size) << gs.get_data()[0].size(); + ASSERT_EQ(gs.get_data()[1].size(), waste_size) << gs.get_data()[1].size(); if (top_stock == card()) ASSERT_TRUE(gs.get_data()[0].empty()); diff --git a/src/test/unit_tests/k-plus-stock-test.cpp b/src/test/unit_tests/k_plus_stock_test.cpp index aea5b80..5877330 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_to_waste, 0, 4, 3), - move(move::mtype::stock_to_waste, 0, 3, 9), + move(move::mtype::stock_to_waste, 0, 4, 2), + move(move::mtype::stock_to_waste, 0, 3, 8), }, - move(move::mtype::stock_to_waste, 0, 3, 9), + move(move::mtype::stock_to_waste, 0, 3, 8), 0, 8, card(), "2D" ); @@ -63,10 +63,10 @@ TEST(KPlusStock, NoRedealOneExtra) { {"2C"} }, { - move(move::mtype::stock_to_waste, 0, 4, 3), - move(move::mtype::stock_to_waste, 0, 3, 7), + move(move::mtype::stock_to_waste, 0, 4, 2), + move(move::mtype::stock_to_waste, 0, 3, 6), }, - move(move::mtype::stock_to_waste, 0, 3, 7), + move(move::mtype::stock_to_waste, 0, 3, 6), 0, 6, card(), "AD" ); @@ -91,9 +91,9 @@ TEST(KPlusStock, NoRedealTwoExtra) { {"2C"} }, { - move(move::mtype::stock_to_waste, 0, 4, 3) + move(move::mtype::stock_to_waste, 0, 4, 2) }, - move(move::mtype::stock_to_waste, 0, 4, 3), + move(move::mtype::stock_to_waste, 0, 4, 2), 5, 2, "3C", "AH" ); @@ -118,10 +118,10 @@ TEST(KPlusStock, RedealExact) { {"2C"} }, { - move(move::mtype::stock_to_waste, 0, 4, 3), - move(move::mtype::stock_to_waste, 0, 3, 9), + move(move::mtype::stock_to_waste, 0, 4, 2), + move(move::mtype::stock_to_waste, 0, 3, 8), }, - move(move::mtype::stock_to_waste, 0, 3, 9), + move(move::mtype::stock_to_waste, 0, 3, 8), 8, 0, "AS", card() ); @@ -146,12 +146,12 @@ TEST(KPlusStock, RedealOneExtra) { {"2C"} }, { - move(move::mtype::stock_to_waste, 0, 4, 3), - move(move::mtype::stock_to_waste, 0, 3, 7), + move(move::mtype::stock_to_waste, 0, 4, 2), + move(move::mtype::stock_to_waste, 0, 3, 6), }, - move(move::mtype::stock_to_waste, 0, 3, 7), - 0, 6, - card(), "AD" + move(move::mtype::stock_to_waste, 0, 3, 6), + 6, 0, + "AS", card() ); } @@ -174,10 +174,10 @@ TEST(KPlusStock, RedealTwoExtra) { {"2C"} }, { - move(move::mtype::stock_to_waste, 0, 4, 3) + move(move::mtype::stock_to_waste, 0, 4, 2) }, - move(move::mtype::stock_to_waste, 0, 4, 3), - 8, 0, + move(move::mtype::stock_to_waste, 0, 4, 2), + 5, 2, "3C", "AH" ); } @@ -201,10 +201,10 @@ TEST(KPlusStock, RedealKPlus3Waste) { {"2C"} }, { - move(move::mtype::stock_to_waste, 0, 3, 9), - move(move::mtype::stock_to_waste, 0, 4, 3) + move(move::mtype::stock_to_waste, 0, 3, 5), + move(move::mtype::stock_to_waste, 0, 4, 8) }, - move(move::mtype::stock_to_waste, 0, 4, 3), + move(move::mtype::stock_to_waste, 0, 4, 8), 6, 2, "3C", "AH" ); @@ -229,11 +229,11 @@ TEST(KPlusStock, RedealKPlus4Waste) { {"2C"} }, { - move(move::mtype::stock_to_waste, 0, 3, 9), - move(move::mtype::stock_to_waste, 0, 4, 3), - move(move::mtype::stock_to_waste, 0, 2, 6) + move(move::mtype::stock_to_waste, 0, 3, 4), + move(move::mtype::stock_to_waste, 0, 4, 7), + move(move::mtype::stock_to_waste, 0, 2, 1) }, - move(move::mtype::stock_to_waste, 0, 2, 6), + move(move::mtype::stock_to_waste, 0, 2, 1), 3, 5, "3S", "2S" ); @@ -258,11 +258,11 @@ TEST(KPlusStock, RedealKPlus5Waste) { {"2C"} }, { - move(move::mtype::stock_to_waste, 0, 3, 8), - move(move::mtype::stock_to_waste, 0, 4, 3), - move(move::mtype::stock_to_waste, 0, 2, 9) + move(move::mtype::stock_to_waste, 0, 3, 2), + move(move::mtype::stock_to_waste, 0, 4, 7), + move(move::mtype::stock_to_waste, 0, 2, 3) }, - move(move::mtype::stock_to_waste, 0, 3, 8), + move(move::mtype::stock_to_waste, 0, 3, 2), 2, 7, "2D", "AD" ); @@ -287,10 +287,10 @@ TEST(KPlusStock, RedealKPlusEmptyStock) { {"2C"} }, { - move(move::mtype::stock_to_waste, 0, 3, 9), - move(move::mtype::stock_to_waste, 0, 4, 3) + move(move::mtype::stock_to_waste, 0, 3, 5), + move(move::mtype::stock_to_waste, 0, 4, 8) }, - move(move::mtype::stock_to_waste, 0, 3, 9), + move(move::mtype::stock_to_waste, 0, 3, 5), 8, 0, "AS", card() ); diff --git a/src/test/unit_tests/legal_move_gen_test.cpp b/src/test/unit_tests/legal_move_gen_test.cpp index 6ea73d5..5155138 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; |
