diff options
| author | thecharlesblake | 2018-04-04 16:00:47 +0100 |
|---|---|---|
| committer | thecharlesblake | 2018-04-04 16:00:47 +0100 |
| commit | ec02b3eaf85f222335463b3e2e6790f7e2691047 (patch) | |
| tree | 2f6a6473993d6df448f16253950f03c9f5c5031b /src | |
| parent | Add pile tests (diff) | |
| download | metasol-ec02b3eaf85f222335463b3e2e6790f7e2691047.tar.gz metasol-ec02b3eaf85f222335463b3e2e6790f7e2691047.zip | |
Add legal move gen tests and refactor
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/game/game_state.h | 3 | ||||
| -rw-r--r-- | src/main/game/game_state.legal_moves.cpp | 12 | ||||
| -rw-r--r-- | src/main/solver/solver.cpp | 11 | ||||
| -rw-r--r-- | src/test/integration_tests/alpha_star_test.cpp (renamed from src/test/alpha_star_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/integration_tests/bakers_dozen_test.cpp (renamed from src/test/bakers_dozen_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/integration_tests/black_hole_test.cpp (renamed from src/test/black_hole_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/integration_tests/canfield_test.cpp (renamed from src/test/canfield_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/integration_tests/flower_garden_test.cpp (renamed from src/test/flower_garden_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/integration_tests/fortunes_favor_test.cpp (renamed from src/test/fortunes_favor_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/integration_tests/free_cell_test.cpp (renamed from src/test/free_cell_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/integration_tests/somerset_test.cpp (renamed from src/test/somerset_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/integration_tests/spanish_patience_test.cpp (renamed from src/test/spanish_patience_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/integration_tests/spider_test.cpp (renamed from src/test/spider_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/unit_tests/card_test.cpp (renamed from src/test/card_test.cpp) | 4 | ||||
| -rw-r--r-- | src/test/unit_tests/deal_parser_test.cpp (renamed from src/test/deal_parser_test.cpp) | 4 | ||||
| -rw-r--r-- | src/test/unit_tests/foundations_dominance_test.cpp (renamed from src/test/foundations_dominance_test.cpp) | 6 | ||||
| -rw-r--r-- | src/test/unit_tests/global_cache_test.cpp (renamed from src/test/global_cache_test.cpp) | 6 | ||||
| -rw-r--r-- | src/test/unit_tests/legal_move_gen_test.cpp | 318 | ||||
| -rw-r--r-- | src/test/unit_tests/pile_test.cpp (renamed from src/test/pile_test.cpp) | 4 | ||||
| -rw-r--r-- | src/test/unit_tests/rules_parser_test.cpp (renamed from src/test/rules_parser_test.cpp) | 4 |
20 files changed, 359 insertions, 33 deletions
diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 409fee9..a49db2c 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -30,6 +30,7 @@ public: struct move { move(pile_ref, pile_ref, pile::size_type = 1); bool is_dominance() const; + friend bool operator==(const move&, const move&); const static pile::size_type dominance_flag; pile_ref from; pile_ref to; pile::size_type count; @@ -51,7 +52,7 @@ public: std::vector<move> get_legal_moves( #ifndef NO_CARD_JUST_MOVED - move + move = move(255, 255, 255) #endif ); diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp index f0502d1..c137b07 100644 --- a/src/main/game/game_state.legal_moves.cpp +++ b/src/main/game/game_state.legal_moves.cpp @@ -22,8 +22,10 @@ vector<game_state::move> game_state::get_legal_moves( vector<move> moves; // If we have a stock that can deal to the tableau piles, adds this move - if (rules.stock_deal_t == sdt::TABLEAU_PILES && !piles[stock].empty()) { - // Only does this if all the tableau piles are empty + if (rules.stock_size > 0 + && rules.stock_deal_t == sdt::TABLEAU_PILES + && !piles[stock].empty()) { + // Only does this if none of the tableau piles are empty bool no_tableau_empty = true; for (auto tab_ref : tableau_piles) { if (piles[tab_ref].empty()) { @@ -394,3 +396,9 @@ bool game_state::dominance_blocks_foundation_move(pile_ref target_pile) { return blocked; } + +bool operator==(const game_state::move& a, const game_state::move& b) { + return (a.from == b.from ) + && (a.to == b.to ) + && (a.count == b.count); +} diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 474800c..2fca763 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -61,14 +61,14 @@ solver::sol_state solver::run_with_cutoff( if (cutoff_depth && depth >= *cutoff_depth) { cutoff_triggered = true; - states_exhausted = revert_to_last_node_with_children();//TODO- + states_exhausted = revert_to_last_node_with_children(); } else { // If there is a dominance move available, adds it to the search tree // and repeats. Doesn't cache the state. optional<game_state::move> dominance_move = state.get_dominance_move(); if (dominance_move) { // Adds the dominance move as a child of the current search node - add_child(*dominance_move);//TODO+ + add_child(*dominance_move); } else { // Caches the current state bool is_new_state = cache.insert(state); @@ -78,14 +78,14 @@ solver::sol_state solver::run_with_cutoff( // If there are none, reverts to the last node with children if (next_moves.empty()) { - states_exhausted = revert_to_last_node_with_children();//TODO- + states_exhausted = revert_to_last_node_with_children(); } else { - add_children(next_moves);//TODO+ + add_children(next_moves); } } // If the state is not a new one, reverts to the last node with children else { - states_exhausted = revert_to_last_node_with_children();//TODO- + states_exhausted = revert_to_last_node_with_children(); } } } @@ -175,7 +175,6 @@ void solver::print_solution() const { if (states_searched > 1) { while (!n->children.empty()) { - // TODO print move as well state_copy.make_move(n->move); cout << state_copy << "\n"; n = &n->children.back(); diff --git a/src/test/alpha_star_test.cpp b/src/test/integration_tests/alpha_star_test.cpp index eeff1be..c3b23fe 100644 --- a/src/test/alpha_star_test.cpp +++ b/src/test/integration_tests/alpha_star_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/bakers_dozen_test.cpp b/src/test/integration_tests/bakers_dozen_test.cpp index 0af0669..f3d4763 100644 --- a/src/test/bakers_dozen_test.cpp +++ b/src/test/integration_tests/bakers_dozen_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/black_hole_test.cpp b/src/test/integration_tests/black_hole_test.cpp index 89ee50a..a0f6707 100644 --- a/src/test/black_hole_test.cpp +++ b/src/test/integration_tests/black_hole_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/canfield_test.cpp b/src/test/integration_tests/canfield_test.cpp index 63fd615..6a045ea 100644 --- a/src/test/canfield_test.cpp +++ b/src/test/integration_tests/canfield_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/flower_garden_test.cpp b/src/test/integration_tests/flower_garden_test.cpp index b1aee9e..641ccc0 100644 --- a/src/test/flower_garden_test.cpp +++ b/src/test/integration_tests/flower_garden_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/fortunes_favor_test.cpp b/src/test/integration_tests/fortunes_favor_test.cpp index eab2e7e..5511f9e 100644 --- a/src/test/fortunes_favor_test.cpp +++ b/src/test/integration_tests/fortunes_favor_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/free_cell_test.cpp b/src/test/integration_tests/free_cell_test.cpp index 507c915..659d86b 100644 --- a/src/test/free_cell_test.cpp +++ b/src/test/integration_tests/free_cell_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/somerset_test.cpp b/src/test/integration_tests/somerset_test.cpp index 22cfe5a..fce8e8c 100644 --- a/src/test/somerset_test.cpp +++ b/src/test/integration_tests/somerset_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/spanish_patience_test.cpp b/src/test/integration_tests/spanish_patience_test.cpp index 8973a53..da32fd5 100644 --- a/src/test/spanish_patience_test.cpp +++ b/src/test/integration_tests/spanish_patience_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/spider_test.cpp b/src/test/integration_tests/spider_test.cpp index ffca592..50f16fe 100644 --- a/src/test/spider_test.cpp +++ b/src/test/integration_tests/spider_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/card_test.cpp b/src/test/unit_tests/card_test.cpp index a991d9c..346dff9 100644 --- a/src/test/card_test.cpp +++ b/src/test/unit_tests/card_test.cpp @@ -4,8 +4,8 @@ #include <gtest/gtest.h> -#include "test_helper.h" -#include "../main/game/card.h" +#include "../test_helper.h" +#include "../../main/game/card.h" TEST(Card, Rank) { for (card::rank_t r = 1; r <= 13; r++) { diff --git a/src/test/deal_parser_test.cpp b/src/test/unit_tests/deal_parser_test.cpp index 750913d..58e5e0e 100644 --- a/src/test/deal_parser_test.cpp +++ b/src/test/unit_tests/deal_parser_test.cpp @@ -7,8 +7,8 @@ #include <rapidjson/stringbuffer.h> #include <rapidjson/document.h> -#include "../main/input-output/input/json-parsing/deal_parser.h" -#include "../main/input-output/input/json-parsing/json_helper.h" +#include "../../main/input-output/input/json-parsing/deal_parser.h" +#include "../../main/input-output/input/json-parsing/json_helper.h" using namespace std; using namespace rapidjson; diff --git a/src/test/foundations_dominance_test.cpp b/src/test/unit_tests/foundations_dominance_test.cpp index 77a33ff..4a7d2aa 100644 --- a/src/test/foundations_dominance_test.cpp +++ b/src/test/unit_tests/foundations_dominance_test.cpp @@ -4,9 +4,9 @@ #include <gtest/gtest.h> -#include "test_helper.h" -#include "../main/game/game_state.h" -#include "../main/game/global_cache.h" +#include "../test_helper.h" +#include "../../main/game/game_state.h" +#include "../../main/game/global_cache.h" typedef sol_rules::build_policy pol; diff --git a/src/test/global_cache_test.cpp b/src/test/unit_tests/global_cache_test.cpp index 780146b..97490ba 100644 --- a/src/test/global_cache_test.cpp +++ b/src/test/unit_tests/global_cache_test.cpp @@ -4,9 +4,9 @@ #include <gtest/gtest.h> -#include "test_helper.h" -#include "../main/game/game_state.h" -#include "../main/game/global_cache.h" +#include "../test_helper.h" +#include "../../main/game/game_state.h" +#include "../../main/game/global_cache.h" typedef sol_rules::build_policy pol; diff --git a/src/test/unit_tests/legal_move_gen_test.cpp b/src/test/unit_tests/legal_move_gen_test.cpp new file mode 100644 index 0000000..23955c2 --- /dev/null +++ b/src/test/unit_tests/legal_move_gen_test.cpp @@ -0,0 +1,318 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" +#include "../../main/game/game_state.h" +#include "../../main/game/sol_rules.h" + +typedef sol_rules::build_policy pol; +typedef sol_rules::spaces_policy s_pol; +typedef sol_rules::stock_deal_type sdt; +typedef game_state::move mv; + +using namespace std; + +bool moves_eq(vector<mv>&, vector<mv>&); +ostream& operator <<(ostream&, const mv&); +ostream& operator <<(ostream&, const vector<mv>&); + +TEST(LegalMoveGen, BuildPolAnySuit) { + sol_rules sr; + sr.build_pol = pol::ANY_SUIT; + sr.tableau_pile_count = 5; + + game_state gs(sr, initializer_list<pile>{ + {"2C"}, + {"2H"}, + {"2D"}, + {"2S"}, + {"AC"} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(4,0), + mv(4,1), + mv(4,2), + mv(4,3) + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, BuildPolRedBlack) { + sol_rules sr; + sr.build_pol = pol::RED_BLACK; + sr.tableau_pile_count = 5; + + game_state gs(sr, initializer_list<pile>{ + {"2C"}, + {"2H"}, + {"2S"}, + {"2D"}, + {"AC"} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(4,1), + mv(4,3) + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, BuildPolSameSuit) { + sol_rules sr; + sr.build_pol = pol::SAME_SUIT; + sr.tableau_pile_count = 5; + + game_state gs(sr, initializer_list<pile>{ + {"2C"}, + {"2H"}, + {"2S"}, + {"2D"}, + {"AC"} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(4,0) + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, BuildPolNoBuild) { + sol_rules sr; + sr.build_pol = pol::NO_BUILD; + sr.tableau_pile_count = 5; + + game_state gs(sr, initializer_list<pile>{ + {"2C"}, + {"2H"}, + {"2S"}, + {"2D"}, + {"AC"} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, SpacesPolAny) { + sol_rules sr; + sr.spaces_pol = s_pol::ANY; + sr.tableau_pile_count = 2; + + game_state gs(sr, initializer_list<pile>{ + {}, + {"AC"} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(1, 0) + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, SpacesPolNoBuild) { + sol_rules sr; + sr.spaces_pol = s_pol::NO_BUILD; + sr.tableau_pile_count = 2; + + game_state gs(sr, initializer_list<pile>{ + {}, + {"AC"} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, MoveBuiltGroupTrue) { + sol_rules sr; + sr.move_built_group = true; + sr.tableau_pile_count = 2; + + game_state gs(sr, initializer_list<pile>{ + {}, + {"2C", "AC"} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(1, 0, 1), + mv(1, 0, 2) + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, MoveBuiltGroupFalse) { + sol_rules sr; + sr.move_built_group = false; + sr.tableau_pile_count = 2; + + game_state gs(sr, initializer_list<pile>{ + {}, + {"AC", "2C"} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(1, 0, 1) + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, Foundations) { + sol_rules sr; + sr.foundations = true; + sr.tableau_pile_count = 4; + + game_state gs(sr, initializer_list<pile>{ + {}, {}, {}, {}, + {"AC"}, + {"AH"}, + {"AS"}, + {"AD"}, + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(4, 0), + mv(5, 1), + mv(6, 2), + mv(7, 3) + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, FoundationsRemovable) { + sol_rules sr; + sr.foundations = true; + sr.foundations_removable = true; + sr.tableau_pile_count = 1; + + game_state gs(sr, initializer_list<pile>{ + {"AC","2C","3C"}, // Without a few cards here dominance logic blocks + {}, // removing from foundation + {}, + {}, + {} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(0, 4) + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, Cells) { + sol_rules sr; + sr.cells = 2; + sr.tableau_pile_count = 1; + + game_state gs(sr, initializer_list<pile>{ + {"3D"}, {}, // The cells + {"4H"} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(0, 1), + mv(0, 2), + mv(2, 1) + }; + + ASSERT_TRUE(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, initializer_list<pile>{ + {}, {}, {}, {}, // The foundations + {"AC", "AD"}, // The stock + {}, // The waste + {}, {}, {} // The tableau piles + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(4, 5) + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, StockDealToTableau) { + sol_rules sr; + sr.foundations = true; + sr.tableau_pile_count = 3; + sr.stock_size = 2; + sr.stock_deal_t = sdt::TABLEAU_PILES; + + game_state gs(sr, initializer_list<pile>{ + {}, {}, {}, {}, // The foundations + {"AC", "AD"}, // The stock + {"3H"}, {"5D"}, {"7C"} // The tableau piles + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(4, 255, 2) // Special stock-to-tableau syntax + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + + + +bool moves_eq(vector<mv>& exp_moves, vector<mv>& actual_moves) { + if (exp_moves.size() != actual_moves.size()) return false; + + for (mv m : actual_moves) { + auto it = find(begin(exp_moves), end(exp_moves), m); + bool found = it != end(exp_moves); + + if (found) exp_moves.erase(it); + else return false; + } + + return true; +} + +ostream& operator <<(ostream& os, const mv& m) { + return os << "move:(" << int(m.from) + << "," << int(m.to) + << "," << int(m.count) + << ")"; +} + +ostream& operator <<(ostream& os, const vector<mv>& moves) { + for (const mv m : moves) { + os << m << ",\n"; + } + return os; +} diff --git a/src/test/pile_test.cpp b/src/test/unit_tests/pile_test.cpp index 29c617f..a5aeb66 100644 --- a/src/test/pile_test.cpp +++ b/src/test/unit_tests/pile_test.cpp @@ -4,8 +4,8 @@ #include <gtest/gtest.h> -#include "test_helper.h" -#include "../main/game/pile.h" +#include "../test_helper.h" +#include "../../main/game/pile.h" TEST(Pile, Size) { pile p = {}; diff --git a/src/test/rules_parser_test.cpp b/src/test/unit_tests/rules_parser_test.cpp index f5f3f76..581a536 100644 --- a/src/test/rules_parser_test.cpp +++ b/src/test/unit_tests/rules_parser_test.cpp @@ -7,8 +7,8 @@ #include <rapidjson/stringbuffer.h> #include <rapidjson/document.h> -#include "../main/input-output/input/json-parsing/rules_parser.h" -#include "../main/input-output/input/json-parsing/json_helper.h" +#include "../../main/input-output/input/json-parsing/rules_parser.h" +#include "../../main/input-output/input/json-parsing/json_helper.h" using namespace std; using namespace rapidjson; |
