diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/game/card.cpp | 21 | ||||
| -rw-r--r-- | src/main/game/card.h | 3 | ||||
| -rw-r--r-- | src/main/game/move.cpp | 10 | ||||
| -rw-r--r-- | src/main/game/move.h | 5 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.auto_foundation_moves.cpp | 8 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.cpp | 60 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.h | 8 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.legal_moves.cpp | 31 | ||||
| -rw-r--r-- | src/test/test_helper.cpp | 9 | ||||
| -rw-r--r-- | src/test/test_helper.h | 2 | ||||
| -rw-r--r-- | src/test/unit_tests/face_up_cards_test.cpp | 134 | ||||
| -rw-r--r-- | src/test/unit_tests/global_cache_test.cpp | 55 | ||||
| -rw-r--r-- | src/test/unit_tests/legal_move_gen_test.cpp | 32 |
13 files changed, 271 insertions, 107 deletions
diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 12e577f..60aaa74 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -25,7 +25,8 @@ const card::colour_t card::colour::Red = 1; card::card(suit_t s, rank_t r, bool fd) : card_rank(r), card_suit(s), face_down(fd) {} card::card(const char* c, bool face_down_possible) : - card_rank(rank_from_str(c)), card_suit(suit_from_str(c)), face_down(face_down_possible) {} // TODO: CHANGE! + card_rank(rank_from_str(c)), card_suit(suit_from_str(c)), + face_down(face_down_from_str(c, face_down_possible)) {} card::card() : card(0, 0, false) {} @@ -52,6 +53,18 @@ card::rank_t card::rank_from_str(const char* c) { } } +bool card::face_down_from_str(const char *c, bool possible) { + if (!possible) return false; + + switch(c[strlen(c) - 1]) { + case 'c': return true; + case 'h': return true; + case 's': return true; + case 'd': return true; + default: return false; + } +} + card::suit_t card::get_suit() const { return card_suit; } @@ -88,14 +101,16 @@ std::string card::to_string() const { return s; } -bool card::is_face_up() const { - return true; +bool card::is_face_down() const { + return face_down; } void card::turn_face_up() { + face_down = false; } void card::turn_face_down() { + face_down = true; } const card card::divider = card(); diff --git a/src/main/game/card.h b/src/main/game/card.h index ea808b4..7885d66 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -39,7 +39,7 @@ public: colour_t get_colour() const; rank_t get_rank() const; - bool is_face_up() const; + bool is_face_down() const; void turn_face_up(); void turn_face_down(); @@ -48,6 +48,7 @@ public: private: static suit_t suit_from_str(const char* c); static rank_t rank_from_str(const char* c); + bool face_down_from_str(const char *c, bool possible); rank_t card_rank : 4; suit_t card_suit : 2; diff --git a/src/main/game/move.cpp b/src/main/game/move.cpp index ff8269c..9e45c66 100644 --- a/src/main/game/move.cpp +++ b/src/main/game/move.cpp @@ -4,16 +4,22 @@ #include "move.h" -move::move(mtype ty, pile::ref f, pile::ref t, pile::size_type i) : type(ty), from(f), to(t), count(i) { +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_) { #ifndef NDEBUG if (ty == mtype::regular) assert(i == 1); else assert(i >= 1); #endif } +void move::make_reveal_move() { + reveal_move = true; +} + bool operator==(const move& a, const move& b) { return (a.from == b.from ) && (a.to == b.to ) && (a.count == b.count) - && (a.type == b.type ); + && (a.type == b.type ) + && (a.reveal_move == b.reveal_move); } diff --git a/src/main/game/move.h b/src/main/game/move.h index 1e3a9d1..8651d1e 100644 --- a/src/main/game/move.h +++ b/src/main/game/move.h @@ -10,13 +10,16 @@ struct move { enum class mtype : uint8_t {regular, dominance, built_group, stock_to_waste, stock_to_tableau, redeal, null}; - explicit move(mtype, pile::ref = 255 , pile::ref = 255, pile::size_type = 1); + explicit move(mtype, pile::ref = 255 , pile::ref = 255, pile::size_type = 1, bool reveal_move_ = false); friend bool operator==(const move&, const move&); + void make_reveal_move(); + mtype type; pile::ref from; pile::ref to; pile::size_type count; + bool reveal_move; }; #endif //SOLVITAIRE_MOVE_H diff --git a/src/main/game/search-state/game_state.auto_foundation_moves.cpp b/src/main/game/search-state/game_state.auto_foundation_moves.cpp index 794c040..99b43a2 100644 --- a/src/main/game/search-state/game_state.auto_foundation_moves.cpp +++ b/src/main/game/search-state/game_state.auto_foundation_moves.cpp @@ -86,7 +86,13 @@ boost::optional<move> game_state::get_dominance_move() const { + card::rank_t(1); if (target_rank == c.get_rank() && auto_foundation_moves[c.get_suit()]) { - return move(move::mtype::dominance, pr, target_foundation); + move m(move::mtype::dominance, pr, target_foundation); + + // If this dominance move reveals a card, adds this to the move + if (piles[pr].size() > 1 && piles[pr][1].is_face_down()) + m.make_reveal_move(); + + return m; } } #endif diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 8a1298b..7870b1b 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -32,6 +32,7 @@ using std::runtime_error; using std::max; using std::ostream; using std::mt19937; +using std::string; typedef sol_rules::build_policy pol; typedef sol_rules::stock_deal_type sdt; @@ -196,11 +197,11 @@ game_state::game_state(const sol_rules& s_rules, int seed, bool streamliners_) } game_state::game_state(const sol_rules& s_rules, - std::initializer_list<pile> il) - : game_state(s_rules, false) { - pile::ref pr = 0; - for (const pile& p : il) { - for (const card c : p.pile_vec) { + std::initializer_list<std::initializer_list<string>> il) + : game_state(s_rules, false) {pile::ref pr = 0; + for (auto& p_il : il) { + for (const string& card_str : p_il) { + card c(card_str.c_str(), s_rules.face_up == fu::TOP_CARDS); place_card(pr, c); } pr++; @@ -269,6 +270,10 @@ void game_state::make_move(const move m) { assert(false); break; } + +#ifndef NDEBUG + check_face_down_consistent(); +#endif } void game_state::undo_move(const move m) { @@ -293,6 +298,10 @@ void game_state::undo_move(const move m) { assert(false); break; } + +#ifndef NDEBUG + check_face_down_consistent(); +#endif } void game_state::make_regular_move(const move m) { @@ -301,6 +310,12 @@ void game_state::make_regular_move(const move m) { place_card(m.to, take_card(m.from)); + if (m.reveal_move) { + assert(!piles[m.from].empty()); + assert(piles[m.from][0].is_face_down()); + piles[m.from][0].turn_face_up(); + } + #ifndef NO_AUTO_FOUNDATIONS update_auto_foundation_moves(m.to); #endif @@ -309,6 +324,12 @@ void game_state::make_regular_move(const move m) { void game_state::undo_regular_move(const move m) { assert(m.to < piles.size()); + if (m.reveal_move) { + assert(!piles[m.from].empty()); + assert(!piles[m.from][0].is_face_down()); + piles[m.from][0].turn_face_down(); + } + place_card(m.from, take_card(m.to)); #ifndef NO_AUTO_FOUNDATIONS @@ -331,6 +352,12 @@ void game_state::make_built_group_move(move m) { for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) { take_card(m.from); } + + if (m.reveal_move) { + assert(!piles[m.from].empty()); + assert(piles[m.from][0].is_face_down()); + piles[m.from][0].turn_face_up(); + } } void game_state::undo_built_group_move(move m) { @@ -338,6 +365,12 @@ void game_state::undo_built_group_move(move m) { assert(m.to < piles.size()); assert(m.count <= rules.max_rank); + if (m.reveal_move) { + assert(!piles[m.from].empty()); + assert(!piles[m.from][0].is_face_down()); + piles[m.from][0].turn_face_down(); + } + // Adds the cards to the 'from' pile for (auto pile_idx = m.count; pile_idx-- > 0;) { place_card(m.from, piles[m.to][pile_idx]); @@ -427,6 +460,23 @@ card game_state::take_card(pile::ref pr) { return c; } +#ifndef NDEBUG +void game_state::check_face_down_consistent() const { + for (auto& p : original_tableau_piles) { + if (piles[p].empty()) continue; + // Makes sure the top cards of each tableau pile are face up + assert(!piles[p].top_card().is_face_down()); + + // Makes sure face down cards are never above face down ones + bool seen_face_up = false; + for (auto& c : piles[p].pile_vec) { + seen_face_up = seen_face_up || !c.is_face_down(); + assert(!(c.is_face_down() && seen_face_up)); + } + } +} +#endif + //////////////////////// // INSPECT GAME STATE // diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index f335de1..b8b0d03 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -32,7 +32,7 @@ public: // Does the same from a seed game_state(const sol_rules&, int seed, bool); // Does the same but with an initialiser list (useful for testing) - game_state(const sol_rules&, std::initializer_list<pile>); + game_state(const sol_rules&, std::initializer_list<std::initializer_list<std::string>>); /* Altering state */ @@ -80,6 +80,10 @@ private: void make_redeal_move(); void undo_redeal_move(); +#ifndef NDEBUG + void check_face_down_consistent() const; +#endif + /* Legal move generation */ bool stock_can_deal_tableau() const; @@ -107,6 +111,8 @@ private: bool is_next_legal_card(sol_rules::build_policy, card, card) const; + void turn_face_down_cards(std::vector<move>&) const; + /* Auto-foundation moves */ bool is_valid_auto_foundation_move(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 b7b910e..9ad8b1d 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -3,6 +3,7 @@ // #include "game_state.h" +#include "../sol_rules.h" #include <boost/optional/optional.hpp> @@ -14,6 +15,7 @@ using namespace rapidjson; typedef sol_rules::build_policy pol; typedef sol_rules::spaces_policy s_pol; typedef sol_rules::stock_deal_type sdt; +typedef sol_rules::face_up_policy fu; //////////////////////////////// @@ -136,9 +138,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 + if (rules.foundations_comp_piles) // i.e. Spider-type winning condition add_foundation_complete_piles_moves(moves); + if (rules.face_up != fu::ALL) + turn_face_down_cards(moves); /* vector<move> moves; @@ -185,6 +189,7 @@ vector<move> game_state::get_legal_moves(move parent_move) { if (rules.foundations_comp_piles) // I.E. Spider-type winning condition add_foundation_complete_piles_moves(moves); */ + return moves; } @@ -352,8 +357,12 @@ void game_state::add_built_group_moves(vector<move> &moves) const { 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)) - moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, built_group_height); + 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); + } } } } @@ -362,7 +371,9 @@ void game_state::add_built_group_moves(vector<move> &moves) const { // Finds the size of the built group at the top of a pile pile::size_type game_state::get_built_group_height(pile::ref ref) const { pile::size_type i = 1; - while (i < piles[ref].size() && is_next_built_group_card(piles[ref][i], piles[ref][i-1])) + while (i < piles[ref].size() + && is_next_built_group_card(piles[ref][i], piles[ref][i-1]) + && !piles[ref][i].is_face_down()) i++; return i; } @@ -374,6 +385,9 @@ void game_state::add_empty_built_group_moves(vector<move>& moves, pile::ref rem_ 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(); } bool game_state::is_next_built_group_card(card a, card b) const { @@ -394,3 +408,12 @@ bool game_state::is_next_legal_card(pol p, card a, card b) const { // Checks rank return b.get_rank() + 1 == a.get_rank(); } + +void game_state::turn_face_down_cards(vector<move>& moves) const { + for (auto& m : moves) { + bool is_tableau_move = m.from >= original_tableau_piles.front() && m.from <= original_tableau_piles.back(); + if (is_tableau_move && piles[m.from].size() > 1 && piles[m.from][1].is_face_down()) { + m.make_reveal_move(); + } + } +} diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index e1aa0a7..1bc1f24 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -14,10 +14,12 @@ #define ASSERT_(t_f, statement) ASSERT_TRUE((statement) == (t_f)) using namespace rapidjson; -using std::initializer_list; using std::vector; using std::ostream; +typedef std::initializer_list<std::initializer_list<std::string>> string_il; + + bool test_helper::is_solvable(const std::string& input_file, const std::string& preset_type) { const Document in_doc = json_helper::get_file_json(input_file); const sol_rules rules = rules_parser::from_preset(preset_type); @@ -35,7 +37,7 @@ void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy, rules.foundations_removable = false; rules.cells = 0; - game_state gs = game_state(rules, std::initializer_list<pile>{ + game_state gs = game_state(rules, string_il{ {}, {}, {}, @@ -63,7 +65,7 @@ void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy, } void test_helper::expected_moves_test(sol_rules sr, - initializer_list<pile> piles, + string_il piles, vector<move> exp_moves) { game_state gs(sr, piles); vector<move> actual_moves = gs.get_legal_moves(); @@ -88,6 +90,7 @@ ostream& operator <<(ostream& os, const move& m) { return os << "move:(" << int(m.from) << "," << int(m.to) << "," << int(m.count) + << "," << m.reveal_move << ")"; } diff --git a/src/test/test_helper.h b/src/test/test_helper.h index 0690d0e..3df9ead 100644 --- a/src/test/test_helper.h +++ b/src/test/test_helper.h @@ -17,7 +17,7 @@ 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<pile>, std::vector<move>); + static void expected_moves_test(sol_rules sr, std::initializer_list<std::initializer_list<std::string>>, std::vector<move>); static bool moves_eq(std::vector<move>&, std::vector<move>&); }; diff --git a/src/test/unit_tests/face_up_cards_test.cpp b/src/test/unit_tests/face_up_cards_test.cpp index 6e2d6a3..5bf65cb 100644 --- a/src/test/unit_tests/face_up_cards_test.cpp +++ b/src/test/unit_tests/face_up_cards_test.cpp @@ -7,6 +7,7 @@ #include "../test_helper.h" #include "../../main/game/search-state/game_state.h" #include "../../main/game/sol_rules.h" +#include "../../main/game/global_cache.h" typedef sol_rules::build_policy pol; typedef sol_rules::face_up_policy fu; @@ -14,10 +15,11 @@ typedef move::mtype mt; using std::vector; using std::ostream; -using std::initializer_list; -#define ASSERT_FACE_UP(card) ASSERT_TRUE(card.is_face_up()) << card.to_string() << " should be face up" -#define ASSERT_FACE_DOWN(card) ASSERT_FALSE(card.is_face_up()) << card.to_string() << " should be face down" +typedef std::initializer_list<std::initializer_list<std::string>> string_il; + +#define ASSERT_FACE_UP(card) ASSERT_FALSE(card.is_face_down()) << card.to_string() << " should be face up" +#define ASSERT_FACE_DOWN(card) ASSERT_TRUE(card.is_face_down()) << card.to_string() << " should be face down" TEST(FaceUpCards, CapsFaceUpIfFaceUpNotAll) { @@ -28,7 +30,7 @@ TEST(FaceUpCards, CapsFaceUpIfFaceUpNotAll) { sr.move_built_group = true; sr.face_up = fu::TOP_CARDS; - initializer_list<pile> piles = { + string_il piles = { {"3S"}, {"2s","AS"}, {} @@ -49,7 +51,7 @@ TEST(FaceUpCards, IgnoreCapsIfFaceUpAll) { sr.move_built_group = true; sr.face_up = fu::ALL; - initializer_list<pile> piles = { + string_il piles = { {"3S"}, {"2s","AS"}, {} @@ -70,14 +72,24 @@ TEST(FaceUpCards, MoveTurnsFaceUp) { sr.move_built_group = true; sr.face_up = fu::TOP_CARDS; - initializer_list<pile> piles = { + string_il piles = { {"3S"}, {"2s","AS"}, // from {} // to }; + test_helper::expected_moves_test( + sr, + piles, + { + // Regular moves + move(move::mtype::regular, 0, 2, 1, false), + move(move::mtype::regular, 1, 2, 1, true) + } + ); + game_state gs(sr, piles); - gs.make_move(move(mt::regular, 1, 2)); + gs.make_move(move(mt::regular, 1, 2, 1, true)); ASSERT_FACE_UP(gs.get_data()[0][0]); ASSERT_FACE_UP(gs.get_data()[1][0]); @@ -86,25 +98,34 @@ TEST(FaceUpCards, MoveTurnsFaceUp) { TEST(FaceUpCards, BuiltGroupMoveTurnsFaceUp) { sol_rules sr; - sr.tableau_pile_count = 3; + sr.tableau_pile_count = 2; sr.built_group_pol = pol::SAME_SUIT; sr.build_pol = pol::SAME_SUIT; sr.move_built_group = true; sr.face_up = fu::TOP_CARDS; - initializer_list<pile> piles = { - {"4S"}, + string_il piles = { {"3s","2S","AS"}, // from {} // to }; + test_helper::expected_moves_test( + sr, + piles, + { + // Regular moves + move(move::mtype::regular, 0, 1, 1, false), + // Built-group moves + move(move::mtype::built_group, 0, 1, 2, true) + } + ); + game_state gs(sr, piles); - gs.make_move(move(mt::built_group, 1, 2, 2)); + gs.make_move(move(mt::built_group, 0, 1, 2, true)); ASSERT_FACE_UP(gs.get_data()[0][0]); ASSERT_FACE_UP(gs.get_data()[1][0]); - ASSERT_FACE_UP(gs.get_data()[2][0]); - ASSERT_FACE_UP(gs.get_data()[2][1]); + ASSERT_FACE_UP(gs.get_data()[1][1]); } TEST(FaceUpCards, MoveAlreadyFaceUpNoChange) { @@ -115,14 +136,24 @@ TEST(FaceUpCards, MoveAlreadyFaceUpNoChange) { sr.move_built_group = true; sr.face_up = fu::TOP_CARDS; - initializer_list<pile> piles = { + string_il piles = { {"3S"}, - {"2S","AS"}, // from + {"2s","AS"}, // from {} // to }; + test_helper::expected_moves_test( + sr, + piles, + { + // Regular moves + move(move::mtype::regular, 0, 2, 1, false), + move(move::mtype::regular, 1, 2, 1, true) + } + ); + game_state gs(sr, piles); - gs.make_move(move(mt::regular, 1, 2)); + gs.make_move(move(mt::regular, 1, 2, 1, true)); ASSERT_FACE_UP(gs.get_data()[0][0]); ASSERT_FACE_UP(gs.get_data()[1][0]); @@ -137,12 +168,6 @@ TEST(FaceUpCards, FaceDownNoBuiltGroup) { sr.move_built_group = true; sr.face_up = fu::TOP_CARDS; - initializer_list<pile> piles = { - {"3S"}, - {"2s","AS"}, - {} - }; - test_helper::expected_moves_test( sr, { @@ -152,8 +177,8 @@ TEST(FaceUpCards, FaceDownNoBuiltGroup) { }, { // Regular moves - move(move::mtype::regular, 0, 2, 1), - move(move::mtype::regular, 1, 2, 1), + move(move::mtype::regular, 0, 2, 1, false), + move(move::mtype::regular, 1, 2, 1, true), } ); } @@ -166,12 +191,6 @@ TEST(FaceUpCards, FaceUpBuiltGroup) { sr.move_built_group = true; sr.face_up = fu::TOP_CARDS; - initializer_list<pile> piles = { - {"3S"}, - {"2s","AS"}, - {} - }; - test_helper::expected_moves_test( sr, { @@ -199,12 +218,6 @@ TEST(FaceUpCards, FaceUpBuiltGroup2Not3) { sr.move_built_group = true; sr.face_up = fu::TOP_CARDS; - initializer_list<pile> piles = { - {"3S"}, - {"2s","AS"}, - {} - }; - test_helper::expected_moves_test( sr, { @@ -214,11 +227,11 @@ TEST(FaceUpCards, FaceUpBuiltGroup2Not3) { }, { // Regular moves - move(mt::regular, 0, 2, 1), - move(mt::regular, 1, 2, 1), + move(mt::regular, 0, 2, 1, false), + move(mt::regular, 1, 2, 1, false), // Built group moves - move(mt::built_group, 1, 2, 2) + move(mt::built_group, 1, 2, 2, true) } ); } @@ -231,14 +244,14 @@ TEST(FaceUpCards, UndoTurnsBackFaceDown) { sr.move_built_group = true; sr.face_up = fu::TOP_CARDS; - initializer_list<pile> piles = { + string_il piles = { {"3S"}, {"2s","AS"}, // from {} // to }; game_state gs(sr, piles); - move m = move(mt::regular, 1, 2); + move m = move(mt::regular, 1, 2, 1, true); gs.make_move(m); gs.undo_move(m); @@ -255,14 +268,14 @@ TEST(FaceUpCards, UndoBuiltGroupTurnsBackFaceDown) { sr.move_built_group = true; sr.face_up = fu::TOP_CARDS; - initializer_list<pile> piles = { + string_il piles = { {"4S"}, {"3s","2S","AS"}, // from {} // to }; game_state gs(sr, piles); - move m = move(mt::built_group, 1, 2, 2); + move m = move(mt::built_group, 1, 2, 2, true); gs.make_move(m); gs.undo_move(m); @@ -271,3 +284,38 @@ TEST(FaceUpCards, UndoBuiltGroupTurnsBackFaceDown) { ASSERT_FACE_UP(gs.get_data()[1][1]); ASSERT_FACE_DOWN(gs.get_data()[1][2]); } + +TEST(FaceUpCards, CacheEncodesFaceDown) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::RED_BLACK; + sr.build_pol = pol::RED_BLACK; + sr.move_built_group = true; + sr.face_up = fu::TOP_CARDS; + + string_il piles = { + {"2C"}, + {"2s","AH"}, + {} + }; + + game_state gs(sr, piles); + lru_cache cache(gs, 1000); + bool new_state; + + gs.make_move(move(mt::regular, 1, 2, 1, true)); + new_state = cache.insert(gs).second; + ASSERT_TRUE(new_state) << "AH to empty pile"; + + gs.make_move(move(mt::regular, 2, 1, 1, false)); + new_state = cache.insert(gs).second; + ASSERT_TRUE(new_state) << "AH back to face-up 2S"; + + gs.make_move(move(mt::regular, 1, 0, 1, false)); + new_state = cache.insert(gs).second; + ASSERT_TRUE(new_state) << "AH to 2C"; + + gs.make_move(move(mt::regular, 0, 1, 1, false)); + new_state = cache.insert(gs).second; + ASSERT_FALSE(new_state) << "AH to 2S"; +} diff --git a/src/test/unit_tests/global_cache_test.cpp b/src/test/unit_tests/global_cache_test.cpp index cd3c686..93ef105 100644 --- a/src/test/unit_tests/global_cache_test.cpp +++ b/src/test/unit_tests/global_cache_test.cpp @@ -9,69 +9,70 @@ #include "../../main/game/global_cache.h" typedef sol_rules::build_policy pol; +typedef std::initializer_list<std::initializer_list<std::string>> string_il; TEST(GlobalCache, CommutativeTableauPiles) { sol_rules rules; rules.tableau_pile_count = 3; rules.build_pol = sol_rules::build_policy::SAME_SUIT; - game_state gs(rules, std::initializer_list<pile>{{},{},{}}); + game_state gs(rules, string_il{{},{},{}}); lru_cache cache(gs, 1000); - cache.insert (game_state(rules, {{card("AC")},{card("2D")},{card("3H")}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{card("2D")},{card("3H")},{card("AC")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{card("3H")},{card("2D")},{card("3H")}}))); + cache.insert (game_state(rules, {{"AC"},{"2D"},{"3H"}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{"2D"},{"3H"},{"AC"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{"3H"},{"2D"},{"3H"}}))); // Test with empty piles cache.clear(); - cache.insert (game_state(rules, {{card("4C")},{card("5D")},{}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{},{card("4C")},{card("5D")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{},{card("4C")},{}}))); + cache.insert (game_state(rules, {{"4C"},{"5D"},{}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{},{"4C"},{"5D"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{},{"4C"},{}}))); // Test with piles of size > 1 cache.clear(); - cache.insert (game_state(rules, {{card("6C"),card("7D")},{card("8C")},{card("9D")}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("7D")},{card("9D")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("KD")},{card("9D")}}))); + cache.insert (game_state(rules, {{"6C","7D"},{"8C"},{"9D"}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{"8C"},{"6C","7D"},{"9D"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{"8C"},{"6C","KD"},{"9D"}}))); } TEST(GlobalCache, CommutativeReserve) { sol_rules rules; rules.reserve_size = 3; rules.build_pol = sol_rules::build_policy::SAME_SUIT; - game_state gs(rules, std::initializer_list<pile>{{},{},{}}); + game_state gs(rules, string_il{{},{},{}}); lru_cache cache(gs, 1000); - cache.insert (game_state(rules, {{card("AC")},{card("2D")},{card("3H")}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{card("2D")},{card("3H")},{card("AC")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{card("3H")},{card("2D")},{card("3H")}}))); + cache.insert (game_state(rules, {{"AC"},{"2D"},{"3H"}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{"2D"},{"3H"},{"AC"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{"3H"},{"2D"},{"3H"}}))); // Test with empty piles cache.clear(); - cache.insert (game_state(rules, {{card("4C")},{card("5D")},{}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{},{card("4C")},{card("5D")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{},{card("4C")},{}}))); + cache.insert (game_state(rules, {{"4C"},{"5D"},{}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{},{"4C"},{"5D"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{},{"4C"},{}}))); // Test with piles of size > 1 cache.clear(); - cache.insert (game_state(rules, {{card("6C"),card("7D")},{card("8C")},{card("9D")}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("7D")},{card("9D")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("KD")},{card("9D")}}))); + cache.insert (game_state(rules, {{"6C","7D"},{"8C"},{"9D"}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{"8C"},{"6C","7D"},{"9D"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{"8C"},{"6C","KD"},{"9D"}}))); } TEST(GlobalCache, CommutativeCells) { sol_rules rules; rules.cells = 3; rules.build_pol = sol_rules::build_policy::SAME_SUIT; - game_state gs(rules, std::initializer_list<pile>{{},{},{}}); + game_state gs(rules, string_il{{},{},{}}); lru_cache cache(gs, 1000); - cache.insert (game_state(rules, {{card("AC")},{card("2D")},{card("3H")}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{card("2D")},{card("3H")},{card("AC")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{card("3H")},{card("2D")},{card("3H")}}))); + cache.insert (game_state(rules, {{"AC"},{"2D"},{"3H"}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{"2D"},{"3H"},{"AC"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{"3H"},{"2D"},{"3H"}}))); // Test with empty piles cache.clear(); - cache.insert (game_state(rules, {{card("4C")},{card("5D")},{}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{},{card("4C")},{card("5D")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{},{card("4C")},{}}))); + cache.insert (game_state(rules, {{"4C"},{"5D"},{}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{},{"4C"},{"5D"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{},{"4C"},{}}))); } diff --git a/src/test/unit_tests/legal_move_gen_test.cpp b/src/test/unit_tests/legal_move_gen_test.cpp index de6733e..6ea73d5 100644 --- a/src/test/unit_tests/legal_move_gen_test.cpp +++ b/src/test/unit_tests/legal_move_gen_test.cpp @@ -15,14 +15,16 @@ typedef sol_rules::stock_deal_type sdt; using std::vector; using std::ostream; -using std::initializer_list; + +typedef std::initializer_list<std::initializer_list<std::string>> string_il; + TEST(LegalMoveGen, BuildPolAnySuit) { sol_rules sr; sr.build_pol = pol::ANY_SUIT; sr.tableau_pile_count = 5; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {"2C"}, {"2H"}, {"2D"}, @@ -46,7 +48,7 @@ TEST(LegalMoveGen, BuildPolRedBlack) { sr.build_pol = pol::RED_BLACK; sr.tableau_pile_count = 5; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {"2C"}, {"2H"}, {"2S"}, @@ -68,7 +70,7 @@ TEST(LegalMoveGen, BuildPolSameSuit) { sr.build_pol = pol::SAME_SUIT; sr.tableau_pile_count = 5; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {"2C"}, {"2H"}, {"2S"}, @@ -89,7 +91,7 @@ TEST(LegalMoveGen, BuildPolNoBuild) { sr.build_pol = pol::NO_BUILD; sr.tableau_pile_count = 5; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {"2C"}, {"2H"}, {"2S"}, @@ -109,7 +111,7 @@ TEST(LegalMoveGen, SpacesPolAny) { sr.spaces_pol = s_pol::ANY; sr.tableau_pile_count = 2; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {}, {"AC"} }); @@ -127,7 +129,7 @@ TEST(LegalMoveGen, SpacesPolKings) { sr.spaces_pol = s_pol::KINGS; sr.tableau_pile_count = 3; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {}, {"AC"}, {"KD"} @@ -146,7 +148,7 @@ TEST(LegalMoveGen, SpacesPolNoBuild) { sr.spaces_pol = s_pol::NO_BUILD; sr.tableau_pile_count = 2; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {}, {"AC"} }); @@ -163,7 +165,7 @@ TEST(LegalMoveGen, MoveBuiltGroupTrue) { sr.move_built_group = true; sr.tableau_pile_count = 2; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {}, {"2C", "AC"} }); @@ -182,7 +184,7 @@ TEST(LegalMoveGen, MoveBuiltGroupFalse) { sr.move_built_group = false; sr.tableau_pile_count = 2; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {}, {"AC", "2C"} }); @@ -200,7 +202,7 @@ TEST(LegalMoveGen, Foundations) { sr.foundations = true; sr.tableau_pile_count = 4; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {}, {}, {}, {}, {"AC"}, {"AH"}, @@ -225,7 +227,7 @@ TEST(LegalMoveGen, FoundationsRemovable) { sr.foundations_removable = true; sr.tableau_pile_count = 1; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {"AC","2C","3C"}, // Without a few cards here dominance logic blocks {}, // removing from foundation {}, @@ -246,7 +248,7 @@ TEST(LegalMoveGen, Cells) { sr.cells = 2; sr.tableau_pile_count = 1; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {"3D"}, {}, // The cells {"4H"} }); @@ -267,7 +269,7 @@ TEST(LegalMoveGen, StockDealToWaste) { sr.stock_size = 2; sr.stock_deal_t = sdt::WASTE; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {}, {}, {}, {}, // The foundations {"AC", "AD"}, // The stock {}, // The waste @@ -289,7 +291,7 @@ TEST(LegalMoveGen, StockDealToTableau) { sr.stock_size = 2; sr.stock_deal_t = sdt::TABLEAU_PILES; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {}, {}, {}, {}, // The foundations {"AC", "AD"}, // The stock {"3H"}, {"5D"}, {"7C"} // The tableau piles |
