diff options
| author | thecharlesblake | 2017-10-24 01:23:00 +0100 |
|---|---|---|
| committer | thecharlesblake | 2017-10-26 14:51:33 +0100 |
| commit | c5e5d9d6bbd7d7d2a6c6463505e7b7031e846ebf (patch) | |
| tree | 09bfac6f5499620317602cd8e8ab3c2933677a50 | |
| parent | Add deal file parsing and verifying (diff) | |
| download | metasol-c5e5d9d6bbd7d7d2a6c6463505e7b7031e846ebf.tar.gz metasol-c5e5d9d6bbd7d7d2a6c6463505e7b7031e846ebf.zip | |
Draft of OO game logic
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | resources/input-file-schema.json | 7 | ||||
| -rw-r--r-- | src/main/black_hole_state.cpp | 74 | ||||
| -rw-r--r-- | src/main/black_hole_state.h | 36 | ||||
| -rw-r--r-- | src/main/card.cpp | 2 | ||||
| -rw-r--r-- | src/main/card.h | 3 | ||||
| -rw-r--r-- | src/main/game_state.cpp | 51 | ||||
| -rw-r--r-- | src/main/game_state.h | 28 | ||||
| -rw-r--r-- | src/main/io_utils.cpp (renamed from src/main/deal_parser.cpp) | 20 | ||||
| -rw-r--r-- | src/main/io_utils.h (renamed from src/main/deal_parser.h) | 2 | ||||
| -rw-r--r-- | src/main/main.cpp | 4 |
11 files changed, 210 insertions, 19 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index d34c1e3..36470b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) ## source files ############################################################### ############################################################################### -set(sources src/main/main.cpp src/main/command_line_helper.cpp src/main/command_line_helper.h src/main/log_helper.h src/main/card.h src/main/card.cpp src/main/deal_parser.cpp src/main/deal_parser.h) +set(sources src/main/main.cpp src/main/command_line_helper.cpp src/main/command_line_helper.h src/main/log_helper.h src/main/card.h src/main/card.cpp src/main/io_utils.cpp src/main/io_utils.h src/main/game_state.cpp src/main/game_state.h src/main/black_hole_state.cpp src/main/black_hole_state.h) set(sources_test src/test/main_test.cpp) ############################################################################### diff --git a/resources/input-file-schema.json b/resources/input-file-schema.json index 2086eb0..1b55e4f 100644 --- a/resources/input-file-schema.json +++ b/resources/input-file-schema.json @@ -17,11 +17,8 @@ } } }, - "hole cards": { - "type": "array", - "items": { - "$ref": "#/definitions/card" - } + "hole card": { + "$ref": "#/definitions/card" } }, "additionalProperties": false, diff --git a/src/main/black_hole_state.cpp b/src/main/black_hole_state.cpp new file mode 100644 index 0000000..4b54bc6 --- /dev/null +++ b/src/main/black_hole_state.cpp @@ -0,0 +1,74 @@ +// +// Created by thecharlesblake on 10/23/17. +// + +#include <vector> + +#include <rapidjson/document.h> + +#include "card.h +#include "game_state.h" +#include "black_hole_state.h" + +using namespace std; +using namespace rapidjson; + + +black_hole_state::black_hole_state(const Document& doc) + : game_state(doc), solved(false) { + // Assign hole card + assert(doc.HasMember("hole card")); + const Value& json_h_card = doc["hole card"]; + assert(json_h_card.IsArray()); + hole_card = doc["hole card"].GetString(); +}; + +vector<game_state> black_hole_state::get_next_legal_states() const { + vector<game_state> next; + + bool all_tableaux_empty = true; + for (int i = 0; i < tableau_piles.size(); i++) { + vector<card> tableau_pile = tableau_piles[i]; + card top_card = tableau_pile.back(); + + if (tableau_pile.empty()) { + continue; + } else { + all_tableaux_empty = false; + } + + if (adjacent(top_card, hole_card)) { + black_hole_state s = *this; + s.move_to_hole(i); + next.push_back(s); + } + } + + // If there are no cards left in play, deal solved + solved = all_tableaux_empty; + + return next; +} + +void black_hole_state::move_to_hole(int tab_idx) { + hole_card = tableau_piles[tab_idx].back(); + tableau_piles[tab_idx].pop_back(); +} + +bool black_hole_state::is_solved() const { + return solved; +} + +ostream& black_hole_state::print(ostream& stream) const { + return game_state::print(stream) << "Hole: " << hole_card << "\n"; +} + +bool black_hole_state::adjacent(const card& a, const card& b) { + int x = a.get_rank(); + int y = b.get_rank(); + + return x == y + 1 + || x + 1 == y + || x == 1 && y == MAX_RANK + || x == MAX_RANK && y == 1; +}
\ No newline at end of file diff --git a/src/main/black_hole_state.h b/src/main/black_hole_state.h new file mode 100644 index 0000000..75fda71 --- /dev/null +++ b/src/main/black_hole_state.h @@ -0,0 +1,36 @@ +// +// Created by thecharlesblake on 10/23/17. +// + +#ifndef SOLVITAIRE_BLACK_HOLE_STATE_H +#define SOLVITAIRE_BLACK_HOLE_STATE_H + +#include <vector> +#include <ostream> + +#include <rapidjson/document.h> + +#include "card.h +#include "game_state.h" + +class black_hole_state : public game_state { +public: + black_hole_state(const rapidjson::Document& doc); + std::vector<game_state> get_next_legal_states() const override; + bool is_solved() const override; + std::ostream& print(std::ostream&) const override; + + friend std::ostream& operator<< (std::ostream&, const black_hole_state&); + +protected: + virtual static const int MAX_RANK = 13; + +private: + void move_to_hole(int); + static bool adjacent(const card& a, const card& b); + + card hole_card; + mutable bool solved; +}; + +#endif //SOLVITAIRE_BLACK_HOLE_STATE_H diff --git a/src/main/card.cpp b/src/main/card.cpp index 98459a3..266d675 100644 --- a/src/main/card.cpp +++ b/src/main/card.cpp @@ -8,6 +8,8 @@ using namespace std; +card::card() {} + card::card(const char* c) { switch(tolower(c[0])) { case 'a': rank = 1; diff --git a/src/main/card.h b/src/main/card.h index 8397931..c6fbd8e 100644 --- a/src/main/card.h +++ b/src/main/card.h @@ -9,9 +9,12 @@ class card { public: + card(); card(const char*); int get_rank() const; char get_suit() const; + + friend std::string& operator+= (std::ostream&, const game_state&); private: int rank; char suit; diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp new file mode 100644 index 0000000..c6278e2 --- /dev/null +++ b/src/main/game_state.cpp @@ -0,0 +1,51 @@ +// +// Created by thecharlesblake on 10/23/17. +// + +#include <vector> +#include <ostream> + +#include <rapidjson/document.h> + +#include "game_state.h" + +using namespace std; +using namespace rapidjson; + +game_state::game_state(const Document& doc) { + const Value& json_tab_piles = doc["tableau piles"]; + assert(json_tab_piles.IsArray()); + + for (auto& json_tab : json_tab_piles.GetArray()) { + vector<card> tableau_pile; + + for (auto& json_card : json_tab.GetArray()) { + tableau_pile.push_back(card(json_card.GetString())); + } + + tableau_piles.push_back(tableau_pile); + } +} + +ostream& game_state::print(ostream& stream) const { + bool empty_row = false; + int row_idx = 0; + + while (!empty_row) { + empty_row = true; + for (auto tableau_pile : tableau_piles) { + if (tableau_pile.size() > row_idx) { + empty_row = false; + stream << tableau_pile[row_idx]; + } + stream << "\t"; + } + stream << "\n"; + row_idx++; + } + return stream; +} + +ostream& operator <<(ostream& stream, const game_state& gs) { + return gs.print(stream); +}
\ No newline at end of file diff --git a/src/main/game_state.h b/src/main/game_state.h new file mode 100644 index 0000000..693b127 --- /dev/null +++ b/src/main/game_state.h @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/23/17. +// + +#ifndef SOLVITAIRE_GAME_STATE_H +#define SOLVITAIRE_GAME_STATE_H + +#include <vector> +#include <ostream> + +#include <rapidjson/document.h> + +#include "card.h + +class game_state { +public: + game_state(const rapidjson::Document&); + virtual std::vector<game_state> get_next_legal_states() const = 0; + virtual bool is_solved() const = 0; + virtual std::ostream& print(std::ostream&) const; + + friend std::ostream& operator<< (std::ostream&, const game_state&); + +protected: + std::vector<std::vector<card>> tableau_piles; +}; + +#endif //SOLVITAIRE_GAME_STATE_H diff --git a/src/main/deal_parser.cpp b/src/main/io_utils.cpp index 6a02d51..4239c78 100644 --- a/src/main/deal_parser.cpp +++ b/src/main/io_utils.cpp @@ -12,13 +12,13 @@ #include <rapidjson/schema.h> #include <rapidjson/stringbuffer.h> -#include "deal_parser.h" +#include "io_utils.h" using namespace std; using namespace rapidjson; using namespace boost; -void deal_parser::parse(Document& doc, const std::string filename) { +void io_utils::parse(Document& doc, const std::string filename) { const optional<string> in_json = read_file(filename); if (!in_json) { @@ -39,7 +39,7 @@ void deal_parser::parse(Document& doc, const std::string filename) { } } -const optional<string> deal_parser::read_file(const string filename) { +const optional<string> io_utils::read_file(const string filename) { // Reads the file into a string std::ifstream ifstr(filename); std::stringstream buf; @@ -52,30 +52,30 @@ const optional<string> deal_parser::read_file(const string filename) { return buf.str(); } -void deal_parser::gen_schema_doc(Document& d) { +void io_utils::gen_schema_doc(Document& d) { const char* schema_json = "{\"$schema\":\"http://json-schema.org/draft-04/sc" "hema#\",\"definitions\":{\"card\":{\"type\":\"string\",\"pattern" "\":\"^(?i)([A|[1-9]|1[0-3]|J|Q|K])([C|D|H|S])$\"}},\"type\":\"" "object\",\"properties\":{\"tableau piles\":{\"type\":\"array\"" ",\"items\":{\"type\":\"array\",\"items\":{\"$ref\":\"#/definit" - "ions/card\"}}},\"hole cards\":{\"type\":\"array\",\"items\":{" - "\"$ref\":\"#/definitions/card\"}}},\"additionalProperties\":fa" - "lse,\"anyOf\":[{\"required\":[\"tableau piles\"]}]}"; + "ions/card\"}}},\"hole card\":{\"$ref\":\"#/definitions/card\"}}" + ",\"additionalProperties\":false,\"anyOf\":[{\"required\":[\"tableau" + " piles\"]}]}"; bool success = to_json(d, schema_json); assert(success); } -bool deal_parser::to_json(rapidjson::Document& d, const char* in) { +bool io_utils::to_json(rapidjson::Document& d, const char* in) { d.Parse(in); return !d.HasParseError(); } -bool deal_parser::to_json(rapidjson::Document& d, const string in) { +bool io_utils::to_json(rapidjson::Document& d, const string in) { return to_json(d, in.c_str()); } -const string deal_parser::schema_err_str(const SchemaValidator& validator) { +const string io_utils::schema_err_str(const SchemaValidator& validator) { // Input JSON is invalid according to the schema // Output diagnostic information string ret = "input JSON failed to match the required schema.\n"; diff --git a/src/main/deal_parser.h b/src/main/io_utils.h index 99523f6..659a0c4 100644 --- a/src/main/deal_parser.h +++ b/src/main/io_utils.h @@ -13,7 +13,7 @@ #include "card.h" -class deal_parser { +class io_utils { public: static void parse(rapidjson::Document&, const std::string); private: diff --git a/src/main/main.cpp b/src/main/main.cpp index 4d1c60f..7e9d132 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -6,7 +6,7 @@ #include "command_line_helper.h" #include "card.h" -#include "deal_parser.h" +#include "io_utils.h" using namespace rapidjson; @@ -26,7 +26,7 @@ int main(int argc, const char* argv[]) { for (auto filename : clh.get_input_files()) { try { Document d; - deal_parser::parse(d, filename); + io_utils::parse(d, filename); vector<vector<card*>* > tableaux; const Value& json_tabx = d["tableau piles"]; |
