diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/input-output/deal_parser.cpp | 40 | ||||
| -rw-r--r-- | src/main/input-output/deal_parser.h | 1 | ||||
| -rw-r--r-- | src/test/free_cell_test.cpp | 28 | ||||
| -rw-r--r-- | src/test/resources/free_cell/ComplexSolvable.json | 7 | ||||
| -rw-r--r-- | src/test/resources/free_cell/ComplexUnsolvable.json | 7 | ||||
| -rw-r--r-- | src/test/resources/free_cell/SimpleSolvable.json | 7 | ||||
| -rw-r--r-- | src/test/resources/free_cell/SimpleUnsolvable.json | 7 |
7 files changed, 92 insertions, 5 deletions
diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp index bf4ebaa..2ff5a02 100644 --- a/src/main/input-output/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp @@ -22,6 +22,11 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { if (gs.rules.hole) { parse_hole(gs, doc); } + + // Construct filled cells + if (gs.rules.cells) { + parse_cells(gs, doc); + } } void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& doc) { @@ -38,15 +43,40 @@ void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& ++p.first, ++p.second) { for (auto& json_card : p.first->GetArray()) { + assert(json_card.IsString()); p.second->place(card(json_card.GetString())); } } } -void deal_parser::parse_hole(game_state &gs, const rapidjson::Document& doc) { - assert(doc.HasMember("hole")); - const Value& json_hole = doc["hole"]; - assert(json_hole.IsString()); +void deal_parser::parse_hole(game_state &gs, const Document& doc) { + if (doc.HasMember("hole")) { + const Value &json_hole = doc["hole"]; + assert(json_hole.IsString()); + + gs.hole.place(card(json_hole.GetString())); + } +} + +void deal_parser::parse_cells(game_state &gs, const Document& doc) { + if (doc.HasMember("cells")) { + const Value &json_cells = doc["cells"]; + assert(json_cells.IsArray()); - gs.hole.place(card(json_hole.GetString())); + const auto json_cell_arr = json_cells.GetArray(); + + if (json_cell_arr.Empty()) return; + else if (gs.rules.cells != json_cell_arr.Size()) { + util::json_parse_err("Incorrect number of cells"); + } + + for (auto p = std::make_pair(begin(json_cell_arr), begin(gs.cells)); + p.second != end(gs.cells); + ++p.first, ++p.second) { + + auto json_card = p.first; + assert(json_card->IsString()); + p.second->place(json_card->GetString()); + } + } } diff --git a/src/main/input-output/deal_parser.h b/src/main/input-output/deal_parser.h index 80a0f52..255116b 100644 --- a/src/main/input-output/deal_parser.h +++ b/src/main/input-output/deal_parser.h @@ -22,6 +22,7 @@ public: private: static void parse_tableau_piles(game_state&, const rapidjson::Document&); static void parse_hole(game_state&, const rapidjson::Document&); + static void parse_cells(game_state&, const rapidjson::Document&); }; #endif //SOLVITAIRE_DEAL_PARSER_H diff --git a/src/test/free_cell_test.cpp b/src/test/free_cell_test.cpp new file mode 100644 index 0000000..507c915 --- /dev/null +++ b/src/test/free_cell_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include <gtest/gtest.h> + +#include "test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/free_cell/"; +const static std::string type = "simple-free-cell"; + +TEST(FreeCell, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(FreeCell, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(FreeCell, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(FreeCell, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/src/test/resources/free_cell/ComplexSolvable.json b/src/test/resources/free_cell/ComplexSolvable.json new file mode 100644 index 0000000..2c284d9 --- /dev/null +++ b/src/test/resources/free_cell/ComplexSolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AH","AC","2C","3C"], + ["AD","2H","2D","3D"], + ["3H","2S","AS","3S"] + ] +} diff --git a/src/test/resources/free_cell/ComplexUnsolvable.json b/src/test/resources/free_cell/ComplexUnsolvable.json new file mode 100644 index 0000000..279c319 --- /dev/null +++ b/src/test/resources/free_cell/ComplexUnsolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AH","AC","3H","3C"], + ["AD","2H","2D","3D"], + ["2C","2S","AS","3S"] + ] +} diff --git a/src/test/resources/free_cell/SimpleSolvable.json b/src/test/resources/free_cell/SimpleSolvable.json new file mode 100644 index 0000000..2ae2f1a --- /dev/null +++ b/src/test/resources/free_cell/SimpleSolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["3H","2H","AH","AC"], + ["3D","2D","2C","AD"], + ["3S","3C","2S","AS"] + ] +} diff --git a/src/test/resources/free_cell/SimpleUnsolvable.json b/src/test/resources/free_cell/SimpleUnsolvable.json new file mode 100644 index 0000000..f69312d --- /dev/null +++ b/src/test/resources/free_cell/SimpleUnsolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AH","AC","3H","3C"], + ["AD","2C","2D","3D"], + ["AS","2S","2H","3S"] + ] +} |
