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/game_state.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/main/game_state.cpp (limited to 'src/main/game_state.cpp') 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 -- cgit v1.2.1 From 3eff79ed14f1fda1f148a9a4c5c3a1ae4edbee37 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 26 Oct 2017 14:51:13 +0100 Subject: BH Solver with path outputted, based on user input --- src/main/game_state.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 6 deletions(-) (limited to 'src/main/game_state.cpp') diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp index c6278e2..0e4f210 100644 --- a/src/main/game_state.cpp +++ b/src/main/game_state.cpp @@ -1,5 +1,5 @@ // -// Created by thecharlesblake on 10/23/17. +// Created by thecharlesblake on 10/24/17. // #include @@ -9,10 +9,13 @@ #include "game_state.h" -using namespace std; using namespace rapidjson; +using namespace std; -game_state::game_state(const Document& doc) { +// Construct an initial game state from a JSON doc +game_state::game_state(const Document& doc) : max_rank(1) { + // Construct tableau piles + assert(doc.HasMember("tableau piles")); const Value& json_tab_piles = doc["tableau piles"]; assert(json_tab_piles.IsArray()); @@ -20,29 +23,95 @@ game_state::game_state(const Document& doc) { vector tableau_pile; for (auto& json_card : json_tab.GetArray()) { - tableau_pile.push_back(card(json_card.GetString())); + card c(json_card.GetString()); + if (c.get_rank() > max_rank) max_rank = c.get_rank(); + tableau_pile.push_back(c); } tableau_piles.push_back(tableau_pile); } + + // Assign hole card + assert(doc.HasMember("hole card")); + const Value& json_h_card = doc["hole card"]; + assert(json_h_card.IsString()); + hole_card = json_h_card.GetString(); +} + +vector game_state::get_next_legal_states() const { + vector next; + + // Searches through each column for a top card that can be moved to the hole + bool all_tableaux_empty = true; + for (vector>::size_type i = 0; i < tableau_piles.size(); i++) { + vector tableau_pile = tableau_piles[i]; + + // Ignore empty piles + if (tableau_pile.empty()) { + continue; + } else { + all_tableaux_empty = false; + } + card top_card = tableau_pile.back(); + + // If a card at the top of a tableau pile is adjacent to the hole card, + // move it to the hole and add that state + if (adjacent(top_card, hole_card)) { + game_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; +} + +bool game_state::adjacent(const card& a, const card& b) const { + 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); +} + +void game_state::move_to_hole(int tab_idx) { + hole_card = tableau_piles[tab_idx].back(); + tableau_piles[tab_idx].pop_back(); +} + +bool game_state::is_solved() const { + return solved; } ostream& game_state::print(ostream& stream) const { bool empty_row = false; - int row_idx = 0; + vector::size_type row_idx = 0; while (!empty_row) { empty_row = true; - for (auto tableau_pile : tableau_piles) { + + for (vector tableau_pile : tableau_piles) { if (tableau_pile.size() > row_idx) { empty_row = false; stream << tableau_pile[row_idx]; } stream << "\t"; } + + if (!empty_row) stream << "|"; + if (row_idx == 0) { + stream << " Hole card: " << hole_card; + } + stream << "\n"; row_idx++; } + return stream; } -- cgit v1.2.1 From a841befd0ecdc0b469d6bc46b209f259358778ec Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 27 Oct 2017 23:25:50 +0100 Subject: Add random game generation to bh --- src/main/game_state.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src/main/game_state.cpp') diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp index 0e4f210..527f080 100644 --- a/src/main/game_state.cpp +++ b/src/main/game_state.cpp @@ -4,6 +4,9 @@ #include #include +#include +#include +#include #include @@ -38,6 +41,38 @@ game_state::game_state(const Document& doc) : max_rank(1) { hole_card = json_h_card.GetString(); } +// Construct an initial game state from a seed +game_state::game_state(int seed) : max_rank(7) { + int start = 1; // Ignore the first (hole) card + int end = max_rank * 4; + + // Create a vector of pointers to ints from [start, end) + vector v; + for (int i = start; i < end; i++) { + v.push_back(new int(i)); + } + + // Randomly shuffle the pointers + auto rng = std::default_random_engine(seed); + std::shuffle(std::begin(v), std::end(v), rng); + + for (vector::size_type i = 0; i < v.size(); i++) { + int r = ((*v[i]) % max_rank); + int s = (*v[i]) / max_rank; + + // Add the randomly generated card to the tableau piles + if (i % 3 == 0) tableau_piles.push_back(vector()); + tableau_piles[i / 3].push_back(card(r + 1, s)); + } + + hole_card = "AS"; + + // release memory + for (int i = start; i < max_rank; i++) { + delete v[i]; + } +} + vector game_state::get_next_legal_states() const { vector next; -- cgit v1.2.1 From 70522b28586ba117ff9e1e27c8f4b183f8414fd1 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 28 Oct 2017 10:13:13 +0100 Subject: Add simple and regular bh types --- src/main/game_state.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/main/game_state.cpp') diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp index 527f080..ef9db97 100644 --- a/src/main/game_state.cpp +++ b/src/main/game_state.cpp @@ -16,7 +16,10 @@ using namespace rapidjson; using namespace std; // Construct an initial game state from a JSON doc -game_state::game_state(const Document& doc) : max_rank(1) { +game_state::game_state(const Document& doc, string sol_type) + : max_rank(1) { + simple = sol_type == "simple-black-hole"; + // Construct tableau piles assert(doc.HasMember("tableau piles")); const Value& json_tab_piles = doc["tableau piles"]; @@ -42,7 +45,10 @@ game_state::game_state(const Document& doc) : max_rank(1) { } // Construct an initial game state from a seed -game_state::game_state(int seed) : max_rank(7) { +game_state::game_state(int seed, string sol_type) { + simple = sol_type == "simple-black-hole"; + max_rank = simple ? 7 : 13; + int start = 1; // Ignore the first (hole) card int end = max_rank * 4; @@ -51,7 +57,7 @@ game_state::game_state(int seed) : max_rank(7) { for (int i = start; i < end; i++) { v.push_back(new int(i)); } - + // Randomly shuffle the pointers auto rng = std::default_random_engine(seed); std::shuffle(std::begin(v), std::end(v), rng); -- cgit v1.2.1