aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharlie Blake2018-06-25 11:50:34 +0100
committerCharlie Blake2018-06-25 11:50:34 +0100
commit2b92783762c2824e519b881afbf5d66a606c394f (patch)
tree649ff85cbec5f72ae7a574e28aede71d178cd991 /src
parentAdd face up cards json rule (diff)
downloadmetasol-2b92783762c2824e519b881afbf5d66a606c394f.tar.gz
metasol-2b92783762c2824e519b881afbf5d66a606c394f.zip

Add card face down unit tests

Diffstat (limited to 'src')
-rw-r--r--src/main/game/card.cpp10
-rw-r--r--src/main/game/card.h4
-rw-r--r--src/main/game/sol_rules.h6
-rw-r--r--src/test/test_helper.cpp6
-rw-r--r--src/test/test_helper.h2
-rw-r--r--src/test/unit_tests/built_group_move_gen_test.cpp56
-rw-r--r--src/test/unit_tests/face_up_cards_test.cpp273
7 files changed, 325 insertions, 32 deletions
diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp
index cfb94a9..710b394 100644
--- a/src/main/game/card.cpp
+++ b/src/main/game/card.cpp
@@ -85,6 +85,16 @@ std::string card::to_string() const {
return s;
}
+bool card::is_face_up() const {
+ return true;
+}
+
+void card::turn_face_up() {
+}
+
+void card::turn_face_down() {
+}
+
const card card::divider = card(0, 0);
bool operator==(const card& l, const card& r) {
diff --git a/src/main/game/card.h b/src/main/game/card.h
index a4c6b1e..ad14d2b 100644
--- a/src/main/game/card.h
+++ b/src/main/game/card.h
@@ -39,6 +39,10 @@ public:
colour_t get_colour() const;
rank_t get_rank() const;
+ bool is_face_up() const;
+ void turn_face_up();
+ void turn_face_down();
+
std::string to_string() const;
private:
diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h
index adab00d..a7b9ae6 100644
--- a/src/main/game/sol_rules.h
+++ b/src/main/game/sol_rules.h
@@ -28,6 +28,10 @@ struct sol_rules {
WASTE,
TABLEAU_PILES
};
+ enum class face_up_policy {
+ ALL,
+ TOP_CARDS
+ };
uint8_t tableau_pile_count;
build_policy build_pol;
@@ -49,6 +53,8 @@ struct sol_rules {
bool stock_redeal;
uint8_t reserve_size;
bool reserve_stacked;
+ face_up_policy face_up;
+
};
#endif //SOLVITAIRE_SOL_RULES_H
diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp
index ab9e0cb..e1aa0a7 100644
--- a/src/test/test_helper.cpp
+++ b/src/test/test_helper.cpp
@@ -62,9 +62,9 @@ void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy,
ASSERT_TRUE(i->child_moves.empty());
}
-void test_helper::run_built_group_test(sol_rules sr,
- initializer_list<pile> piles,
- vector<move> exp_moves) {
+void test_helper::expected_moves_test(sol_rules sr,
+ initializer_list<pile> piles,
+ vector<move> exp_moves) {
game_state gs(sr, piles);
vector<move> actual_moves = gs.get_legal_moves();
diff --git a/src/test/test_helper.h b/src/test/test_helper.h
index 463a935..0690d0e 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 run_built_group_test(sol_rules sr, std::initializer_list<pile>, std::vector<move>);
+ static void expected_moves_test(sol_rules sr, std::initializer_list<pile>, std::vector<move>);
static bool moves_eq(std::vector<move>&, std::vector<move>&);
};
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 34e2b07..37a2cf0 100644
--- a/src/test/unit_tests/built_group_move_gen_test.cpp
+++ b/src/test/unit_tests/built_group_move_gen_test.cpp
@@ -20,13 +20,13 @@ TEST(BuiltGroupMoveGen, SameSuitEmpty) {
sr.built_group_pol = pol::SAME_SUIT;
sr.move_built_group = true;
- test_helper::run_built_group_test(
+ test_helper::expected_moves_test(
sr,
{
{},
- {"3C","2C","AC"}, // Same suit
- {"3H","2S","AH"}, // Red black
- {"3D","2D","AS"}, // Any suit
+ {"3C", "2C", "AC"}, // Same suit
+ {"3H", "2S", "AH"}, // Red black
+ {"3D", "2D", "AS"}, // Any suit
{}
},
{
@@ -53,13 +53,13 @@ TEST(BuiltGroupMoveGen, RedBlackEmpty) {
sr.built_group_pol = pol::RED_BLACK;
sr.move_built_group = true;
- test_helper::run_built_group_test(
+ test_helper::expected_moves_test(
sr,
{
{},
- {"3C","2C","AC"}, // Same suit
- {"3H","2S","AH"}, // Red black
- {"3D","2D","AS"}, // Any suit
+ {"3C", "2C", "AC"}, // Same suit
+ {"3H", "2S", "AH"}, // Red black
+ {"3D", "2D", "AS"}, // Any suit
{}
},
{
@@ -89,13 +89,13 @@ TEST(BuiltGroupMoveGen, AnySuitEmpty) {
sr.built_group_pol = pol::ANY_SUIT;
sr.move_built_group = true;
- test_helper::run_built_group_test(
+ test_helper::expected_moves_test(
sr,
{
{},
- {"3C","2C","AC"}, // Same suit
- {"3H","2S","AH"}, // Red black
- {"3D","2D","AS"}, // Any suit
+ {"3C", "2C", "AC"}, // Same suit
+ {"3H", "2S", "AH"}, // Red black
+ {"3D", "2D", "AS"}, // Any suit
{}
},
{
@@ -132,13 +132,13 @@ TEST(BuiltGroupMoveGen, CantMove) {
sr.built_group_pol = pol::ANY_SUIT;
sr.move_built_group = false;
- test_helper::run_built_group_test(
+ test_helper::expected_moves_test(
sr,
{
{},
- {"3C","2C","AC"}, // Same suit
- {"3H","2S","AH"}, // Red black
- {"3D","2D","AS"}, // Any suit
+ {"3C", "2C", "AC"}, // Same suit
+ {"3H", "2S", "AH"}, // Red black
+ {"3D", "2D", "AS"}, // Any suit
{}
},
{
@@ -159,13 +159,13 @@ TEST(BuiltGroupMoveGen, SameSuitOccupied) {
sr.built_group_pol = pol::SAME_SUIT;
sr.move_built_group = true;
- test_helper::run_built_group_test(
+ test_helper::expected_moves_test(
sr,
{
{"4C"},
- {"3C","2C","AC"}, // Same suit
- {"3H","2S","AH"}, // Red black
- {"3D","2D","AS"}, // Any suit
+ {"3C", "2C", "AC"}, // Same suit
+ {"3H", "2S", "AH"}, // Red black
+ {"3D", "2D", "AS"}, // Any suit
{}
},
{
@@ -189,13 +189,13 @@ TEST(BuiltGroupMoveGen, RedBlackOccupied) {
sr.built_group_pol = pol::RED_BLACK;
sr.move_built_group = true;
- test_helper::run_built_group_test(
+ test_helper::expected_moves_test(
sr,
{
{"4C"},
- {"3C","2C","AC"}, // Same suit
- {"3H","2S","AH"}, // Red black
- {"3D","2D","AS"}, // Any suit
+ {"3C", "2C", "AC"}, // Same suit
+ {"3H", "2S", "AH"}, // Red black
+ {"3D", "2D", "AS"}, // Any suit
{}
},
{
@@ -220,13 +220,13 @@ TEST(BuiltGroupMoveGen, AnySuitOccupied) {
sr.built_group_pol = pol::ANY_SUIT;
sr.move_built_group = true;
- test_helper::run_built_group_test(
+ test_helper::expected_moves_test(
sr,
{
{"4C"},
- {"3C","2C","AC"}, // Same suit
- {"3H","2S","AH"}, // Red black
- {"3D","2D","AS"}, // Any suit
+ {"3C", "2C", "AC"}, // Same suit
+ {"3H", "2S", "AH"}, // Red black
+ {"3D", "2D", "AS"}, // Any suit
{}
},
{
diff --git a/src/test/unit_tests/face_up_cards_test.cpp b/src/test/unit_tests/face_up_cards_test.cpp
new file mode 100644
index 0000000..6e2d6a3
--- /dev/null
+++ b/src/test/unit_tests/face_up_cards_test.cpp
@@ -0,0 +1,273 @@
+//
+// Created by thecharlesblake on 1/11/18.
+//
+
+#include <gtest/gtest.h>
+
+#include "../test_helper.h"
+#include "../../main/game/search-state/game_state.h"
+#include "../../main/game/sol_rules.h"
+
+typedef sol_rules::build_policy pol;
+typedef sol_rules::face_up_policy fu;
+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"
+
+
+TEST(FaceUpCards, CapsFaceUpIfFaceUpNotAll) {
+ sol_rules sr;
+ sr.tableau_pile_count = 3;
+ 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 = {
+ {"3S"},
+ {"2s","AS"},
+ {}
+ };
+
+ game_state gs(sr, piles);
+
+ ASSERT_FACE_UP(gs.get_data()[0][0]);
+ ASSERT_FACE_UP(gs.get_data()[1][0]);
+ ASSERT_FACE_DOWN(gs.get_data()[1][1]);
+}
+
+TEST(FaceUpCards, IgnoreCapsIfFaceUpAll) {
+ sol_rules sr;
+ sr.tableau_pile_count = 3;
+ sr.built_group_pol = pol::SAME_SUIT;
+ sr.build_pol = pol::SAME_SUIT;
+ sr.move_built_group = true;
+ sr.face_up = fu::ALL;
+
+ initializer_list<pile> piles = {
+ {"3S"},
+ {"2s","AS"},
+ {}
+ };
+
+ game_state gs(sr, piles);
+
+ ASSERT_FACE_UP(gs.get_data()[0][0]);
+ ASSERT_FACE_UP(gs.get_data()[1][0]);
+ ASSERT_FACE_UP(gs.get_data()[1][1]);
+}
+
+TEST(FaceUpCards, MoveTurnsFaceUp) {
+ sol_rules sr;
+ sr.tableau_pile_count = 3;
+ 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 = {
+ {"3S"},
+ {"2s","AS"}, // from
+ {} // to
+ };
+
+ game_state gs(sr, piles);
+ gs.make_move(move(mt::regular, 1, 2));
+
+ ASSERT_FACE_UP(gs.get_data()[0][0]);
+ ASSERT_FACE_UP(gs.get_data()[1][0]);
+ ASSERT_FACE_UP(gs.get_data()[2][0]);
+}
+
+TEST(FaceUpCards, BuiltGroupMoveTurnsFaceUp) {
+ sol_rules sr;
+ sr.tableau_pile_count = 3;
+ 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"},
+ {"3s","2S","AS"}, // from
+ {} // to
+ };
+
+ game_state gs(sr, piles);
+ gs.make_move(move(mt::built_group, 1, 2, 2));
+
+ 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]);
+}
+
+TEST(FaceUpCards, MoveAlreadyFaceUpNoChange) {
+ sol_rules sr;
+ sr.tableau_pile_count = 3;
+ 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 = {
+ {"3S"},
+ {"2S","AS"}, // from
+ {} // to
+ };
+
+ game_state gs(sr, piles);
+ gs.make_move(move(mt::regular, 1, 2));
+
+ ASSERT_FACE_UP(gs.get_data()[0][0]);
+ ASSERT_FACE_UP(gs.get_data()[1][0]);
+ ASSERT_FACE_UP(gs.get_data()[2][0]);
+}
+
+TEST(FaceUpCards, FaceDownNoBuiltGroup) {
+ sol_rules sr;
+ sr.tableau_pile_count = 3;
+ 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 = {
+ {"3S"},
+ {"2s","AS"},
+ {}
+ };
+
+ test_helper::expected_moves_test(
+ sr,
+ {
+ {"3S"},
+ {"2s","AS"},
+ {}
+ },
+ {
+ // Regular moves
+ move(move::mtype::regular, 0, 2, 1),
+ move(move::mtype::regular, 1, 2, 1),
+ }
+ );
+}
+
+TEST(FaceUpCards, FaceUpBuiltGroup) {
+ sol_rules sr;
+ sr.tableau_pile_count = 3;
+ 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 = {
+ {"3S"},
+ {"2s","AS"},
+ {}
+ };
+
+ test_helper::expected_moves_test(
+ sr,
+ {
+ {"3S"},
+ {"2S","AS"},
+ {}
+ },
+ {
+ // Regular moves
+ move(mt::regular, 0, 2, 1),
+ move(mt::regular, 1, 2, 1),
+
+ // Built group moves
+ move(mt::built_group, 1, 0, 2),
+ move(mt::built_group, 1, 2, 2)
+ }
+ );
+}
+
+TEST(FaceUpCards, FaceUpBuiltGroup2Not3) {
+ sol_rules sr;
+ sr.tableau_pile_count = 3;
+ 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 = {
+ {"3S"},
+ {"2s","AS"},
+ {}
+ };
+
+ test_helper::expected_moves_test(
+ sr,
+ {
+ {"4S"},
+ {"3s","2S","AS"},
+ {}
+ },
+ {
+ // Regular moves
+ move(mt::regular, 0, 2, 1),
+ move(mt::regular, 1, 2, 1),
+
+ // Built group moves
+ move(mt::built_group, 1, 2, 2)
+ }
+ );
+}
+
+TEST(FaceUpCards, UndoTurnsBackFaceDown) {
+ sol_rules sr;
+ sr.tableau_pile_count = 3;
+ 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 = {
+ {"3S"},
+ {"2s","AS"}, // from
+ {} // to
+ };
+
+ game_state gs(sr, piles);
+ move m = move(mt::regular, 1, 2);
+ gs.make_move(m);
+ gs.undo_move(m);
+
+ ASSERT_FACE_UP(gs.get_data()[0][0]);
+ ASSERT_FACE_UP(gs.get_data()[1][0]);
+ ASSERT_FACE_DOWN(gs.get_data()[1][1]);
+}
+
+TEST(FaceUpCards, UndoBuiltGroupTurnsBackFaceDown) {
+ sol_rules sr;
+ sr.tableau_pile_count = 3;
+ 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"},
+ {"3s","2S","AS"}, // from
+ {} // to
+ };
+
+ game_state gs(sr, piles);
+ move m = move(mt::built_group, 1, 2, 2);
+ gs.make_move(m);
+ gs.undo_move(m);
+
+ ASSERT_FACE_UP(gs.get_data()[0][0]);
+ ASSERT_FACE_UP(gs.get_data()[1][0]);
+ ASSERT_FACE_UP(gs.get_data()[1][1]);
+ ASSERT_FACE_DOWN(gs.get_data()[1][2]);
+}