aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorthecharlesblake2018-01-03 15:21:47 +0000
committerthecharlesblake2018-01-03 15:21:47 +0000
commit4208d2570e74ab54d1878c72110bd380e7b197f2 (patch)
treeb56d48744597a426a3218c75b0342a8c0fbabdeb /src
parentRefactor io dir structure (diff)
downloadmetasol-4208d2570e74ab54d1878c72110bd380e7b197f2.tar.gz
metasol-4208d2570e74ab54d1878c72110bd380e7b197f2.zip

Move game state printing into dedicated class

Diffstat (limited to 'src')
-rw-r--r--src/main/game/game_state.cpp106
-rw-r--r--src/main/game/game_state.h13
-rw-r--r--src/main/input-output/input/json-parsing/rules_parser.h2
-rw-r--r--src/main/input-output/output/state_printer.cpp96
-rw-r--r--src/main/input-output/output/state_printer.h28
-rw-r--r--src/main/main.cpp2
-rw-r--r--src/main/solver/solver.cpp1
7 files changed, 138 insertions, 110 deletions
diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp
index 78a5253..1a7310d 100644
--- a/src/main/game/game_state.cpp
+++ b/src/main/game/game_state.cpp
@@ -13,6 +13,7 @@
#include "game_state.h"
#include "../input-output/input/json-parsing/deal_parser.h"
+#include "../input-output/output/state_printer.h"
using namespace std;
using namespace rapidjson;
@@ -309,108 +310,12 @@ const vector<pile>& game_state::get_data() const {
return piles;
}
-
-//////////////
-// PRINTING //
-//////////////
-
-ostream& game_state::print(ostream& stream) const {
- if (rules.foundations) {
- print_header(stream, "Foundations");
- print_top_of_piles(stream, foundations);
- }
- if (rules.cells) {
- print_header(stream, "Cells");
- print_piles(stream, cells);
- }
- if (rules.tableau_pile_count > 0) {
- print_header(stream, "Tableau Piles");
- print_piles(stream, tableau_piles);
- }
- if (rules.stock_size > 0) {
- print_header(stream, "Stock | Waste");
- print_piles(stream, {stock, waste});
- }
- if (rules.hole) {
- print_header(stream, "Hole Card");
- print_top_of_pile(stream, hole);
- }
- return stream << "===================================";
-}
-
-void game_state::print_header(ostream& stream, const char* header) const {
- stream << "--- " << header << " ";
- size_t pad = 20 - strlen(header);
- for (size_t i = 0; i < pad; i++) {
- stream << '-';
- }
- stream << "\n";
-}
-
-void game_state::print_piles(ostream& stream, const vector<pile_ref>& pile_rs) const {
- bool empty_row = false;
- vector<card>::size_type row_idx = 0;
-
- // Loops through the rows in each pile, starting from the bottom, until
- // one is empty
- while (!empty_row) {
- // Loops through the current row to determine if it is empty
- empty_row = true;
- for (const pile_ref pile_r : pile_rs) {
- if (piles[pile_r].size() > row_idx) {
- empty_row = false;
- break;
- }
- }
- if (!empty_row || row_idx == 0) {
- // Loops through the current (non-empty) row, and outputs the values
- for (const pile_ref pile_r : pile_rs) {
- if (piles[pile_r].size() > row_idx) {
- stream << piles[pile_r][row_idx];
- } else if (row_idx == 0) {
- stream << "[]";
- }
- stream << "\t";
- }
-
- stream << "\n";
- row_idx++;
- }
- }
-}
-
-void game_state::print_pile(ostream& stream, const pile_ref pile_r) const {
- print_piles(stream, {pile_r});
-}
-
-void game_state::print_top_of_piles(ostream& stream, const vector<pile_ref>& vp) const {
- vector<pile_ref> top(vp);
-
- for (pile_ref p : top) {
- if (piles[p].empty()) {
- stream << "[]";
- } else {
- stream << piles[p].top_card();
- }
- stream << "\t";
- }
- stream << "\n";
-}
-
-void game_state::print_top_of_pile(ostream& stream, const pile_ref pile_r) const {
- print_top_of_piles(stream, {pile_r});
-}
-
bool operator==(const game_state& a, const game_state& b) {
return a.tableau_piles == b.tableau_piles
&& a.foundations == b.foundations
&& a.hole == b.hole;
}
-ostream& operator <<(ostream& stream, const game_state& gs) {
- return gs.print(stream);
-}
-
////////////////////
// HASH FUNCTIONS //
@@ -443,3 +348,12 @@ size_t hash_value(game_state const& gs) {
size_t hash_value(vector<pile> const& vp) {
return hash_range(begin(vp), end(vp));
}
+
+
+///////////
+// PRINT //
+///////////
+
+ostream& operator<< (ostream& str, const game_state& gs) {
+ return state_printer::print(str, gs);
+}
diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h
index 28d849e..30af8a6 100644
--- a/src/main/game/game_state.h
+++ b/src/main/game/game_state.h
@@ -9,7 +9,6 @@
#define PILE_REF_MAX (255)
#include <vector>
-#include <ostream>
#include <string>
#include <rapidjson/document.h>
@@ -23,6 +22,7 @@ class deal_parser;
class game_state {
friend class deal_parser;
+ friend class state_printer;
public:
// Defines types
typedef uint8_t pile_ref;
@@ -44,11 +44,9 @@ public:
bool is_solved() const;
const std::vector<pile>& get_data() const;
- // Prints
- std::ostream& print(std::ostream&) const;
-
friend bool operator==(const game_state&, const game_state&);
friend std::ostream& operator<< (std::ostream&, const game_state&);
+
friend std::size_t hash_value(game_state const&);
friend std::size_t hash_value(std::vector<pile> const&);
@@ -63,13 +61,6 @@ private:
bool is_valid_foundations_move(pile_ref, pile_ref) const;
bool is_valid_hole_move(pile_ref) const;
- // Prints
- void print_header(std::ostream&, const char*) const;
- void print_piles(std::ostream&, const std::vector<pile_ref>&) const;
- void print_pile(std::ostream&, pile_ref) const;
- void print_top_of_piles(std::ostream&, const std::vector<pile_ref>&) const;
- void print_top_of_pile(std::ostream&, pile_ref) const;
-
// References to piles
sol_rules rules;
std::vector<pile_ref> tableau_piles;
diff --git a/src/main/input-output/input/json-parsing/rules_parser.h b/src/main/input-output/input/json-parsing/rules_parser.h
index 1e28867..21dc635 100644
--- a/src/main/input-output/input/json-parsing/rules_parser.h
+++ b/src/main/input-output/input/json-parsing/rules_parser.h
@@ -5,7 +5,7 @@
#ifndef SOLVITAIRE_RULES_PARSER_H
#define SOLVITAIRE_RULES_PARSER_H
-#include "../../game/sol_rules.h"
+#include "../../../game/sol_rules.h"
class rules_parser {
public:
diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp
new file mode 100644
index 0000000..c9b8bc9
--- /dev/null
+++ b/src/main/input-output/output/state_printer.cpp
@@ -0,0 +1,96 @@
+//
+// Created by thecharlesblake on 1/3/18.
+//
+
+#include "state_printer.h"
+
+using namespace std;
+
+ostream& state_printer::print(ostream& stream, const game_state& gs) {
+ if (gs.rules.foundations) {
+ state_printer::print_header(stream, "Foundations");
+ state_printer::print_top_of_piles(stream, gs.foundations, gs);
+ }
+ if (gs.rules.cells) {
+ state_printer::print_header(stream, "Cells");
+ state_printer::print_piles(stream, gs.cells, gs);
+ }
+ if (gs.rules.tableau_pile_count > 0) {
+ state_printer::print_header(stream, "Tableau Piles");
+ state_printer::print_piles(stream, gs.tableau_piles, gs);
+ }
+ if (gs.rules.stock_size > 0) {
+ state_printer::print_header(stream, "Stock | Waste");
+ state_printer::print_piles(stream, {gs.stock, gs.waste}, gs);
+ }
+ if (gs.rules.hole) {
+ state_printer::print_header(stream, "Hole Card");
+ state_printer::print_top_of_pile(stream, gs.hole, gs);
+ }
+ return stream << "===================================";
+}
+
+void state_printer::print_header(ostream& stream, const char* header) {
+ stream << "--- " << header << " ";
+ size_t pad = 20 - strlen(header);
+ for (size_t i = 0; i < pad; i++) {
+ stream << '-';
+ }
+ stream << "\n";
+}
+
+void state_printer::print_piles(ostream& stream,
+ const vector<game_state::pile_ref>& pile_rs,
+ const game_state& gs) {
+ bool empty_row = false;
+ vector<card>::size_type row_idx = 0;
+
+ // Loops through the rows in each pile, starting from the bottom, until
+ // one is empty
+ while (!empty_row) {
+ // Loops through the current row to determine if it is empty
+ empty_row = true;
+ for (const game_state::pile_ref pile_r : pile_rs) {
+ if (gs.piles[pile_r].size() > row_idx) {
+ empty_row = false;
+ break;
+ }
+ }
+ if (!empty_row || row_idx == 0) {
+ // Loops through the current (non-empty) row, and outputs the values
+ for (const game_state::pile_ref pile_r : pile_rs) {
+ if (gs.piles[pile_r].size() > row_idx) {
+ stream << gs.piles[pile_r][row_idx];
+ } else if (row_idx == 0) {
+ stream << "[]";
+ }
+ stream << "\t";
+ }
+
+ stream << "\n";
+ row_idx++;
+ }
+ }
+}
+
+void state_printer::print_top_of_piles(ostream& stream,
+ const vector<game_state::pile_ref>& vp,
+ const game_state& gs) {
+ vector<game_state::pile_ref> top(vp);
+
+ for (game_state::pile_ref p : top) {
+ if (gs.piles[p].empty()) {
+ stream << "[]";
+ } else {
+ stream << gs.piles[p].top_card();
+ }
+ stream << "\t";
+ }
+ stream << "\n";
+}
+
+void state_printer::print_top_of_pile(ostream& stream,
+ const game_state::pile_ref pile_r,
+ const game_state& gs) {
+ print_top_of_piles(stream, {pile_r}, gs);
+} \ No newline at end of file
diff --git a/src/main/input-output/output/state_printer.h b/src/main/input-output/output/state_printer.h
new file mode 100644
index 0000000..c918bcd
--- /dev/null
+++ b/src/main/input-output/output/state_printer.h
@@ -0,0 +1,28 @@
+//
+// Created by thecharlesblake on 1/3/18.
+//
+
+#ifndef SOLVITAIRE_STATE_PRINT_H
+#define SOLVITAIRE_STATE_PRINT_H
+
+#include <ostream>
+
+#include "../../game/game_state.h"
+
+class state_printer {
+public:
+ static std::ostream& print(std::ostream&, const game_state&);
+
+private:
+ static void print_header(std::ostream&, const char*);
+ static void print_piles(std::ostream&,
+ const std::vector<game_state::pile_ref>&,
+ const game_state&);
+ static void print_top_of_piles(std::ostream&,
+ const std::vector<game_state::pile_ref>&,
+ const game_state&);
+ static void print_top_of_pile(std::ostream&, game_state::pile_ref,
+ const game_state&);
+};
+
+#endif //SOLVITAIRE_STATE_PRINT_H
diff --git a/src/main/main.cpp b/src/main/main.cpp
index 9ed7321..ac81a19 100644
--- a/src/main/main.cpp
+++ b/src/main/main.cpp
@@ -3,9 +3,9 @@
#include <rapidjson/document.h>
#include "input-output/input/command_line_helper.h"
-#include "input-output/output/log_helper.h"
#include "input-output/input/json-parsing/json_helper.h"
#include "input-output/input/json-parsing/rules_parser.h"
+#include "input-output/output/log_helper.h"
#include "solver/solver.h"
using namespace rapidjson;
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
index b7d6de1..e0fbbc6 100644
--- a/src/main/solver/solver.cpp
+++ b/src/main/solver/solver.cpp
@@ -6,7 +6,6 @@
#include <ostream>
#include <iostream>
#include <algorithm>
-#include <utility>
#include "solver.h"
#include "../input-output/output/log_helper.h"