From c5e5d9d6bbd7d7d2a6c6463505e7b7031e846ebf Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 24 Oct 2017 01:23:00 +0100 Subject: Draft of OO game logic --- src/main/black_hole_state.cpp | 74 +++++++++++++++++++++++++++++++ src/main/black_hole_state.h | 36 +++++++++++++++ src/main/card.cpp | 2 + src/main/card.h | 3 ++ src/main/deal_parser.cpp | 101 ------------------------------------------ src/main/deal_parser.h | 27 ----------- src/main/game_state.cpp | 51 +++++++++++++++++++++ src/main/game_state.h | 28 ++++++++++++ src/main/io_utils.cpp | 101 ++++++++++++++++++++++++++++++++++++++++++ src/main/io_utils.h | 27 +++++++++++ src/main/main.cpp | 4 +- 11 files changed, 324 insertions(+), 130 deletions(-) create mode 100644 src/main/black_hole_state.cpp create mode 100644 src/main/black_hole_state.h delete mode 100644 src/main/deal_parser.cpp delete mode 100644 src/main/deal_parser.h create mode 100644 src/main/game_state.cpp create mode 100644 src/main/game_state.h create mode 100644 src/main/io_utils.cpp create mode 100644 src/main/io_utils.h (limited to 'src') 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 + +#include + +#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 black_hole_state::get_next_legal_states() const { + vector next; + + bool all_tableaux_empty = true; + for (int i = 0; i < tableau_piles.size(); i++) { + vector 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 +#include + +#include + +#include "card.h +#include "game_state.h" + +class black_hole_state : public game_state { +public: + black_hole_state(const rapidjson::Document& doc); + std::vector 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/deal_parser.cpp b/src/main/deal_parser.cpp deleted file mode 100644 index 6a02d51..0000000 --- a/src/main/deal_parser.cpp +++ /dev/null @@ -1,101 +0,0 @@ -// -// Created by thecharlesblake on 10/21/17. -// - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "deal_parser.h" - -using namespace std; -using namespace rapidjson; -using namespace boost; - -void deal_parser::parse(Document& doc, const std::string filename) { - const optional in_json = read_file(filename); - - if (!in_json) { - throw runtime_error("could not read file " + filename); - } - - Document sd; - gen_schema_doc(sd); - SchemaDocument schema(sd); - SchemaValidator validator(schema); - - if(!to_json(doc, *in_json)) { - throw runtime_error(filename + " not valid json"); - } - - if (!doc.Accept(validator)) { - throw runtime_error(schema_err_str(validator)); - } -} - -const optional deal_parser::read_file(const string filename) { - // Reads the file into a string - std::ifstream ifstr(filename); - std::stringstream buf; - buf << ifstr.rdbuf(); - - if (ifstr.fail()) { - return none; - } - - return buf.str(); -} - -void deal_parser::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\"]}]}"; - - bool success = to_json(d, schema_json); - assert(success); -} - -bool deal_parser::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) { - return to_json(d, in.c_str()); -} - -const string deal_parser::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"; - - StringBuffer sb; - validator.GetInvalidSchemaPointer().StringifyUriFragment(sb); - ret += "Invalid schema: "; - ret += sb.GetString(); - ret += "\n"; - - ret += "Invalid schema keyword: "; - ret += validator.GetInvalidSchemaKeyword(); - ret += "\n"; - - sb.Clear(); - validator.GetInvalidDocumentPointer().StringifyUriFragment(sb); - - ret += "Invalid document: "; - ret += sb.GetString(); - ret += "\n"; - - return ret; -} \ No newline at end of file diff --git a/src/main/deal_parser.h b/src/main/deal_parser.h deleted file mode 100644 index 99523f6..0000000 --- a/src/main/deal_parser.h +++ /dev/null @@ -1,27 +0,0 @@ -// -// Created by thecharlesblake on 10/21/17. -// - -#ifndef SOLVITAIRE_DEAL_PARSER_H -#define SOLVITAIRE_DEAL_PARSER_H - -#include - -#include -#include -#include - -#include "card.h" - -class deal_parser { -public: - static void parse(rapidjson::Document&, const std::string); -private: - static const boost::optional read_file(const std::string); - static void gen_schema_doc(rapidjson::Document&); - static bool to_json(rapidjson::Document&, const char*); - static bool to_json(rapidjson::Document&, const std::string); - static const std::string schema_err_str(const rapidjson::SchemaValidator&); -}; - -#endif //SOLVITAIRE_DEAL_PARSER_H 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 +#include + +#include + +#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 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 +#include + +#include + +#include "card.h + +class game_state { +public: + game_state(const rapidjson::Document&); + virtual std::vector 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> tableau_piles; +}; + +#endif //SOLVITAIRE_GAME_STATE_H diff --git a/src/main/io_utils.cpp b/src/main/io_utils.cpp new file mode 100644 index 0000000..4239c78 --- /dev/null +++ b/src/main/io_utils.cpp @@ -0,0 +1,101 @@ +// +// Created by thecharlesblake on 10/21/17. +// + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "io_utils.h" + +using namespace std; +using namespace rapidjson; +using namespace boost; + +void io_utils::parse(Document& doc, const std::string filename) { + const optional in_json = read_file(filename); + + if (!in_json) { + throw runtime_error("could not read file " + filename); + } + + Document sd; + gen_schema_doc(sd); + SchemaDocument schema(sd); + SchemaValidator validator(schema); + + if(!to_json(doc, *in_json)) { + throw runtime_error(filename + " not valid json"); + } + + if (!doc.Accept(validator)) { + throw runtime_error(schema_err_str(validator)); + } +} + +const optional io_utils::read_file(const string filename) { + // Reads the file into a string + std::ifstream ifstr(filename); + std::stringstream buf; + buf << ifstr.rdbuf(); + + if (ifstr.fail()) { + return none; + } + + return buf.str(); +} + +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 card\":{\"$ref\":\"#/definitions/card\"}}" + ",\"additionalProperties\":false,\"anyOf\":[{\"required\":[\"tableau" + " piles\"]}]}"; + + bool success = to_json(d, schema_json); + assert(success); +} + +bool io_utils::to_json(rapidjson::Document& d, const char* in) { + d.Parse(in); + return !d.HasParseError(); +} + +bool io_utils::to_json(rapidjson::Document& d, const string in) { + return to_json(d, in.c_str()); +} + +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"; + + StringBuffer sb; + validator.GetInvalidSchemaPointer().StringifyUriFragment(sb); + ret += "Invalid schema: "; + ret += sb.GetString(); + ret += "\n"; + + ret += "Invalid schema keyword: "; + ret += validator.GetInvalidSchemaKeyword(); + ret += "\n"; + + sb.Clear(); + validator.GetInvalidDocumentPointer().StringifyUriFragment(sb); + + ret += "Invalid document: "; + ret += sb.GetString(); + ret += "\n"; + + return ret; +} \ No newline at end of file diff --git a/src/main/io_utils.h b/src/main/io_utils.h new file mode 100644 index 0000000..659a0c4 --- /dev/null +++ b/src/main/io_utils.h @@ -0,0 +1,27 @@ +// +// Created by thecharlesblake on 10/21/17. +// + +#ifndef SOLVITAIRE_DEAL_PARSER_H +#define SOLVITAIRE_DEAL_PARSER_H + +#include + +#include +#include +#include + +#include "card.h" + +class io_utils { +public: + static void parse(rapidjson::Document&, const std::string); +private: + static const boost::optional read_file(const std::string); + static void gen_schema_doc(rapidjson::Document&); + static bool to_json(rapidjson::Document&, const char*); + static bool to_json(rapidjson::Document&, const std::string); + static const std::string schema_err_str(const rapidjson::SchemaValidator&); +}; + +#endif //SOLVITAIRE_DEAL_PARSER_H 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* > tableaux; const Value& json_tabx = d["tableau piles"]; -- cgit v1.2.1