From 900cd63d095cdc1b9637a4873326e075467ade7a Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Sat, 4 Aug 2018 17:31:25 +0100 Subject: Add two-deck presets & edit mv order to make work --- .../search-state/game_state.dominance_moves.cpp | 2 +- src/main/game/search-state/game_state.h | 3 +- .../game/search-state/game_state.legal_moves.cpp | 44 +++-- src/main/input-output/input/sol_preset_types.cpp | 209 ++++++++++++++++++++- src/test/test_helper.cpp | 2 +- src/test/unit_tests/built_group_move_gen_test.cpp | 6 - src/test/unit_tests/face_up_cards_test.cpp | 29 ++- src/test/unit_tests/legal_move_gen_test.cpp | 45 ++++- 8 files changed, 299 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/main/game/search-state/game_state.dominance_moves.cpp b/src/main/game/search-state/game_state.dominance_moves.cpp index b7e6e15..bc20cb5 100644 --- a/src/main/game/search-state/game_state.dominance_moves.cpp +++ b/src/main/game/search-state/game_state.dominance_moves.cpp @@ -15,7 +15,7 @@ using std::max; using boost::optional; bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const { - if (rules.foundations_only_comp_piles) + if (rules.foundations_only_comp_piles || rules.two_decks) return false; else if ( stream_opts == sos::AUTO_FOUNDATIONS || stream_opts == sos::BOTH diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index d8d9f12..3df67ec 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -95,7 +96,7 @@ private: bool stock_can_deal_all_tableau() const; move get_stock_to_all_tableau_move() const; - std::set> generate_k_plus_moves_to_check() const; + std::set, std::greater<>> generate_k_plus_moves_to_check() const; void add_stock_to_cell_move(std::vector&, pile::ref) const; void add_stock_to_tableau_moves(std::vector&) const; void add_stock_to_hole_foundation_moves(std::vector&) 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 f97814a..b4ed810 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -14,6 +14,7 @@ using std::min; using std::list; using std::pair; using std::set; +using std::greater; using namespace rapidjson; typedef sol_rules::build_policy pol; @@ -33,14 +34,16 @@ vector game_state::get_legal_moves(move parent_move) { // Order: // Accordion moves // Foundations complete piles moves - // Tableau / cells / reserve / stock-waste to hole / foundation moves --- + // Tableau / cells / reserve / stock-waste (redeal) 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 + // Stock-waste to tableau moves (redeal) // Reserve to tableau moves // Foundation to tableau moves + // Stock-waste to tableau moves (no redeal) + // Stock-waste (no redeal) to hole / foundation moves // Tableau / reserve / stock-waste to cell moves // Stock to all tableau moves @@ -50,7 +53,7 @@ vector game_state::get_legal_moves(move parent_move) { if (rules.stock_size > 0 && stock_can_deal_all_tableau()) moves.emplace_back(get_stock_to_all_tableau_move()); - // Tableau / stock to cell moves + // Tableau / reserve / stock-waste to cell moves pile::ref empty_cell = 255; for (auto c : cells) { if (piles[c].empty()) empty_cell = c; @@ -69,6 +72,16 @@ vector game_state::get_legal_moves(move parent_move) { add_stock_to_cell_move(moves, empty_cell); } + // Stock-waste (no redeal) to hole / foundation moves + if (rules.hole || (rules.foundations_present && !rules.foundations_only_comp_piles)) { + if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !rules.stock_redeal) + add_stock_to_hole_foundation_moves(moves); + } + + // Stock-waste to tableau moves (no redeal) + if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !rules.stock_redeal) + add_stock_to_tableau_moves(moves); + // Foundation to tableau / empty cell moves if (rules.foundations_removable) { for (auto f : foundations) { @@ -88,8 +101,8 @@ vector game_state::get_legal_moves(move parent_move) { add_valid_tableau_moves(moves, r); } - // Stock-waste to tableau moves - if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE) + // Stock-waste to tableau moves (redeal) + if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && rules.stock_redeal) add_stock_to_tableau_moves(moves); // Tableau built group moves @@ -108,9 +121,16 @@ vector game_state::get_legal_moves(move parent_move) { // If only whole pile moves are available, doesn't make regular ones if (rules.move_built_group != bgt::WHOLE_PILE) { for (auto t_from : tableau_piles) { - if (piles[t_from].empty() || parent_move.to == t_from) continue; - - add_valid_tableau_moves(moves, t_from); + // Forbids moves from empty piles, reversing parent moves, or from single- + // card piles to empty piles + if (piles[t_from].empty() || parent_move.to == t_from || tableau_space_and_auto_reserve()) continue; + + for (auto to : tableau_piles) { + if (is_valid_tableau_move(t_from, to) + && !(piles[t_from].size() == 1 && piles[to].empty())) { + moves.emplace_back(move::mtype::regular, t_from, to); + } + } } } @@ -126,10 +146,10 @@ vector game_state::get_legal_moves(move parent_move) { add_valid_tableau_moves(moves, c); } - // Tableau / cells / reserve / stock-waste to hole / foundation moves + // Tableau / cells / reserve / stock-waste (redeal) to hole / foundation moves if (rules.hole || (rules.foundations_present && !rules.foundations_only_comp_piles)) { // Stock - if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE) + if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && rules.stock_redeal) add_stock_to_hole_foundation_moves(moves); list from_piles = tableau_piles; @@ -182,8 +202,8 @@ move game_state::get_stock_to_all_tableau_move() const { return move(move::mtype::stock_to_all_tableau, 0, 0, stock_moves); } -set> game_state::generate_k_plus_moves_to_check() const { - set> stock_moves_to_check; +set, greater<>> game_state::generate_k_plus_moves_to_check() const { + set, greater<>> stock_moves_to_check; // If the waste isn't empty, adds the move from the top of the current waste if (!piles[waste].empty()) stock_moves_to_check.insert(pair(0, false)); diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index e380225..93a609a 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -1177,7 +1177,214 @@ map sol_preset_types::mp = { "build policy": "same-suit" }, "stock": { - "size": 64 + "size": 64, + "redeal": false + }, + "two decks": true +} +)" + + }, + { + "lucas", + + R"( +{ + "tableau piles": { + "count": 13, + "build policy": "same-suit" + }, + "stock": { + "size": 61, + "redeal": false + }, + "foundations": { + "initial cards": "all" + }, + "two decks": true +} +)" + + }, + { + "maria", + + R"( +{ + "tableau piles": { + "count": 9, + "build policy": "red-black" + }, + "stock": { + "size": 68, + "redeal": false + }, + "two decks": true +} +)" + + }, + { + "limited", + + R"( +{ + "tableau piles": { + "count": 12, + "build policy": "same-suit" + }, + "stock": { + "size": 68, + "redeal": false + }, + "two decks": true +} +)" + + }, + { + "streets", + + R"( +{ + "tableau piles": { + "count": 10, + "build policy": "red-black" + }, + "stock": { + "size": 64, + "redeal": false + }, + "two decks": true +} +)" + + }, + { + "rank-and-file", + + R"( +{ + "tableau piles": { + "count": 10, + "build policy": "red-black", + "face up cards": "top", + "move built group": "yes" + }, + "stock": { + "size": 64, + "redeal": false + }, + "two decks": true +} +)" + + }, + { + "american-toad", + + R"( +{ + "tableau piles": { + "count": 8, + "build policy": "same-suit", + "move built group": "whole-pile", + "spaces policy": "auto-reserve-then-waste", + "wraps": true + }, + "foundations": { + "initial cards": "one", + "base card": "random" + }, + "stock": { + "size": 75, + "deal count": 1, + "redeal": false + }, + "reserve": { + "size": 20, + "stacked": true + }, + "two decks": true +} +)" + + }, + + { + "gargantua", + + R"( +{ + "tableau piles": { + "count": 9, + "build policy": "red-black", + "spaces policy": "kings", + "move built group": "yes", + "diagonal deal": true, + "face up cards": "top" + }, + "foundations": { + "removable": true + }, + "stock": { + "size": 59, + "deal count": 1, + "redeal": false + }, + "two decks": true +} +)" + + }, + + { + "gargantua-redeal", + + R"( +{ + "tableau piles": { + "count": 9, + "build policy": "red-black", + "spaces policy": "kings", + "move built group": "yes", + "diagonal deal": true, + "face up cards": "top" + }, + "foundations": { + "removable": true + }, + "stock": { + "size": 59, + "deal count": 1, + "redeal": true + }, + "two decks": true +} +)" + + }, + + { + "ultra-klondike-2-deck", + + R"( +{ + "tableau piles": { + "count": 12, + "build policy": "red-black", + "spaces policy": "kings", + "move built group": "yes", + "diagonal deal": true, + "face up cards": "top" + }, + "foundations": { + "removable": true + }, + "stock": { + "size": 26, + "deal count": 3, + "redeal": true }, "two decks": true } diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 33a81ca..6cc68aa 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -28,7 +28,7 @@ bool test_helper::is_solvable(const std::string& input_file, const std::string& game_state gs(rules, in_doc, sos::NONE); solver sol(gs, 1000000); - return sol.run() == solver::sol_state::solved; + return sol.run().sol_type == solver::result::type::SOLVED; } void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy, 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 b16a874..5e3a492 100644 --- a/src/test/unit_tests/built_group_move_gen_test.cpp +++ b/src/test/unit_tests/built_group_move_gen_test.cpp @@ -177,8 +177,6 @@ TEST(BuiltGroupMoveGen, SameSuitOccupied) { move(move::mtype::regular, 1, 4, 1), 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 @@ -212,8 +210,6 @@ TEST(BuiltGroupMoveGen, RedBlackOccupied) { move(move::mtype::regular, 1, 4, 1), 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), @@ -247,8 +243,6 @@ TEST(BuiltGroupMoveGen, AnySuitOccupied) { move(move::mtype::regular, 1, 4, 1), 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 diff --git a/src/test/unit_tests/face_up_cards_test.cpp b/src/test/unit_tests/face_up_cards_test.cpp index eb828a0..1311958 100644 --- a/src/test/unit_tests/face_up_cards_test.cpp +++ b/src/test/unit_tests/face_up_cards_test.cpp @@ -74,9 +74,9 @@ TEST(FaceUpCards, MoveTurnsFaceUp) { sr.face_up = fu::TOP_CARDS; string_il piles = { - {"3S"}, - {"2s","AS"}, // from - {} // to + {"4S"}, + {"3s","AS"}, // from + {"2S"} // to }; test_helper::expected_moves_test( @@ -84,7 +84,6 @@ TEST(FaceUpCards, MoveTurnsFaceUp) { piles, { // Regular moves - move(move::mtype::regular, 0, 2, 1, false), move(move::mtype::regular, 1, 2, 1, true) } ); @@ -138,9 +137,9 @@ TEST(FaceUpCards, MoveAlreadyFaceUpNoChange) { sr.face_up = fu::TOP_CARDS; string_il piles = { - {"3S"}, - {"2s","AS"}, // from - {} // to + {"4S"}, + {"3s","AS"}, // from + {"2S"} // to }; test_helper::expected_moves_test( @@ -148,7 +147,6 @@ TEST(FaceUpCards, MoveAlreadyFaceUpNoChange) { piles, { // Regular moves - move(move::mtype::regular, 0, 2, 1, false), move(move::mtype::regular, 1, 2, 1, true) } ); @@ -172,14 +170,13 @@ TEST(FaceUpCards, FaceDownNoBuiltGroup) { test_helper::expected_moves_test( sr, { - {"3S"}, - {"2s","AS"}, - {} + {"4S"}, + {"3s","AS"}, + {"2S"} }, { // Regular moves - move(move::mtype::regular, 0, 2, 1, false), - move(move::mtype::regular, 1, 2, 1, true), + move(move::mtype::regular, 1, 2, 1, true) } ); } @@ -201,7 +198,6 @@ TEST(FaceUpCards, FaceUpBuiltGroup) { }, { // Regular moves - move(mt::regular, 0, 2, 1), move(mt::regular, 1, 2, 1), // Built group moves @@ -224,12 +220,11 @@ TEST(FaceUpCards, FaceUpBuiltGroup2Not3) { { {"4S"}, {"3s","2S","AS"}, - {} + {"3S"} }, { // Regular moves - move(mt::regular, 0, 2, 1, false), - move(mt::regular, 1, 2, 1, false), + move(mt::regular, 2, 0, 1, false), // Built group moves move(mt::built_group, 1, 2, 2, true) diff --git a/src/test/unit_tests/legal_move_gen_test.cpp b/src/test/unit_tests/legal_move_gen_test.cpp index a948bba..da5c63c 100644 --- a/src/test/unit_tests/legal_move_gen_test.cpp +++ b/src/test/unit_tests/legal_move_gen_test.cpp @@ -119,7 +119,7 @@ TEST(LegalMoveGen, SpacesPolAny) { vector actual_moves = gs.get_legal_moves(); vector exp_moves = { - move(move::mtype::regular, 1, 0) + // move(move::mtype::regular, 1, 0) // Now forbidden as always pointless }; ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; @@ -133,7 +133,7 @@ TEST(LegalMoveGen, SpacesPolKings) { game_state gs(sr, string_il{ {}, {"AC"}, - {"KD"} + {"3S","KD"} }); vector actual_moves = gs.get_legal_moves(); @@ -244,6 +244,47 @@ TEST(LegalMoveGen, FoundationsRemovable) { ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; } + +TEST(LegalMoveGen, TwoDecksFoundations) { + sol_rules sr; + sr.two_decks = true; + sr.foundations_removable = true; + sr.foundations_present = true; + sr.tableau_pile_count = 4; + + game_state gs(sr, string_il{ + {"AC"}, {}, {}, {}, {}, {}, {"AS"}, {}, + {}, + {"AH"}, + {}, + {"AD"}, + }); + vector actual_moves = gs.get_legal_moves(); + + vector exp_moves = { + // Aces up + move(move::mtype::regular, 9, 1), + move(move::mtype::regular, 9, 5), + move(move::mtype::regular, 11, 3), + move(move::mtype::regular, 11, 7), + + // Aces down + move(move::mtype::regular, 0, 8), + move(move::mtype::regular, 0, 10), + move(move::mtype::regular, 6, 8), + move(move::mtype::regular, 6, 10), + + // Aces across (forbidden now) + //move(move::mtype::regular, 9, 8), + //move(move::mtype::regular, 9, 10), + //move(move::mtype::regular, 11, 8), + //move(move::mtype::regular, 11, 10), + + }; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; +} + TEST(LegalMoveGen, Cells) { sol_rules sr; sr.cells = 2; -- cgit v1.2.1