aboutsummaryrefslogtreecommitdiff
path: root/src/main/input-output/output
diff options
context:
space:
mode:
authorCharlie Blake2018-01-09 08:58:30 +0000
committerGitHub2018-01-09 08:58:30 +0000
commit93bfbeec309b027331f75d10864c99f19a958cfd (patch)
tree5ad08a4621dbc118480553d53666e3e507ce584e /src/main/input-output/output
parentMerge pull request #27 from thecharlesblake/efficient-implementation (diff)
parentAdd spider to benchmark script (diff)
downloadmetasol-93bfbeec309b027331f75d10864c99f19a958cfd.tar.gz
metasol-93bfbeec309b027331f75d10864c99f19a958cfd.zip

Merge pull request #28 from thecharlesblake/all-canonical-games

All canonical games

Diffstat (limited to 'src/main/input-output/output')
-rw-r--r--src/main/input-output/output/log_helper.h19
-rw-r--r--src/main/input-output/output/state_printer.cpp128
-rw-r--r--src/main/input-output/output/state_printer.h29
3 files changed, 176 insertions, 0 deletions
diff --git a/src/main/input-output/output/log_helper.h b/src/main/input-output/output/log_helper.h
new file mode 100644
index 0000000..71cc5f4
--- /dev/null
+++ b/src/main/input-output/output/log_helper.h
@@ -0,0 +1,19 @@
+//
+// Created by thecharlesblake on 10/16/17.
+//
+
+#ifndef SOLVITAIRE_LOG_HELPER_H
+#define SOLVITAIRE_LOG_HELPER_H
+
+#include <iostream>
+
+#if defined NDEBUG
+ #define LOG_DEBUG(msg)
+#else
+ #define LOG_DEBUG(msg) clog << "[debug]\n" << msg << "\n"
+#endif
+#define LOG_INFO(msg) clog << "[info] " << msg << "\n"
+#define LOG_WARNING clog(msg) << "[warning] " << msg << "\n"
+#define LOG_ERROR(msg) cerr << "[ERROR] " << msg << "\n"
+
+#endif //SOLVITAIRE_LOG_HELPER_H
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..bfb6c7d
--- /dev/null
+++ b/src/main/input-output/output/state_printer.cpp
@@ -0,0 +1,128 @@
+//
+// Created by thecharlesblake on 1/3/18.
+//
+
+#include "state_printer.h"
+
+using namespace std;
+
+typedef sol_rules::stock_deal_type sdt;
+
+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.reserve_size > 0) {
+ if (gs.rules.reserve_stacked) {
+ state_printer::print_header(stream, "Reserve (Stacked)");
+ } else {
+ state_printer::print_header(stream, "Reserve");
+ }
+ state_printer::print_piles(stream, gs.reserve, gs);
+ }
+ if (gs.rules.stock_size > 0) {
+ if (gs.rules.stock_deal_t == sdt::WASTE) {
+ state_printer::print_header(stream, "Stock | Waste");
+ state_printer::print_piles(stream, {gs.stock, gs.waste}, gs);
+ } else {
+ state_printer::print_header(stream, "Stock");
+ state_printer::print_piles(stream, {gs.stock}, 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;
+ pile::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) {
+ const pile& p = gs.piles[pile_r];
+ if (p.size() > row_idx) {
+ print_card(stream, p[p.size() - 1 - 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 {
+ print_card(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);
+}
+
+void state_printer::print_card(std::ostream& s, const card c) {
+ switch(c.get_rank()) {
+ case 1 : s << "A"; break;
+ case 11: s << "J"; break;
+ case 12: s << "Q"; break;
+ case 13: s << "K"; break;
+ default: s << int(c.get_rank());
+ }
+ switch(c.get_suit()) {
+ case card::suit_t::Spades : s << "S"; break;
+ case card::suit_t::Hearts : s << "H"; break;
+ case card::suit_t::Clubs : s << "C"; break;
+ case card::suit_t::Diamonds: s << "D"; break;
+ }
+}
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..ae63182
--- /dev/null
+++ b/src/main/input-output/output/state_printer.h
@@ -0,0 +1,29 @@
+//
+// 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&);
+ static void print_card(std::ostream&, card);
+};
+
+#endif //SOLVITAIRE_STATE_PRINT_H