From 7f4712b292632becfc83bc0e887271b25fefd21a Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 13:05:31 +0000 Subject: Refactor io dir structure --- src/main/input-output/output/log_helper.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/input-output/output/log_helper.h (limited to 'src/main/input-output/output') 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 + +#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 -- cgit v1.2.1 From 4208d2570e74ab54d1878c72110bd380e7b197f2 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 15:21:47 +0000 Subject: Move game state printing into dedicated class --- src/main/input-output/output/state_printer.cpp | 96 ++++++++++++++++++++++++++ src/main/input-output/output/state_printer.h | 28 ++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/main/input-output/output/state_printer.cpp create mode 100644 src/main/input-output/output/state_printer.h (limited to 'src/main/input-output/output') 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& pile_rs, + const game_state& gs) { + bool empty_row = false; + vector::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& vp, + const game_state& gs) { + vector 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 + +#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&, + const game_state&); + static void print_top_of_piles(std::ostream&, + const std::vector&, + const game_state&); + static void print_top_of_pile(std::ostream&, game_state::pile_ref, + const game_state&); +}; + +#endif //SOLVITAIRE_STATE_PRINT_H -- cgit v1.2.1 From 66545273423eae0d8f945f7c3ac5871b6955ff94 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 15:27:47 +0000 Subject: Move card printing into dedicated class --- src/main/input-output/output/state_printer.cpp | 22 +++++++++++++++++++--- src/main/input-output/output/state_printer.h | 1 + 2 files changed, 20 insertions(+), 3 deletions(-) (limited to 'src/main/input-output/output') diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index c9b8bc9..88181de 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -60,7 +60,7 @@ void state_printer::print_piles(ostream& stream, // 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]; + print_card(stream, gs.piles[pile_r][row_idx]); } else if (row_idx == 0) { stream << "[]"; } @@ -82,7 +82,7 @@ void state_printer::print_top_of_piles(ostream& stream, if (gs.piles[p].empty()) { stream << "[]"; } else { - stream << gs.piles[p].top_card(); + print_card(stream, gs.piles[p].top_card()); } stream << "\t"; } @@ -93,4 +93,20 @@ 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 +} + +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 index c918bcd..ae63182 100644 --- a/src/main/input-output/output/state_printer.h +++ b/src/main/input-output/output/state_printer.h @@ -23,6 +23,7 @@ private: 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 -- cgit v1.2.1 From 2ac80451bc8a0d7a6830e1d9274f33138844b386 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 15:47:41 +0000 Subject: Add state printing for regular reserve --- src/main/input-output/output/state_printer.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/main/input-output/output') diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index 88181de..1828390 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -19,6 +19,10 @@ ostream& state_printer::print(ostream& stream, const game_state& gs) { state_printer::print_header(stream, "Tableau Piles"); state_printer::print_piles(stream, gs.tableau_piles, gs); } + if (gs.rules.reserve_size > 0) { + state_printer::print_header(stream, "Reserve"); + state_printer::print_piles(stream, gs.reserve, gs); + } if (gs.rules.stock_size > 0) { state_printer::print_header(stream, "Stock | Waste"); state_printer::print_piles(stream, {gs.stock, gs.waste}, gs); -- cgit v1.2.1 From 748dceab129dc57aa7a3e58ef538b9701398846c Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 15:56:46 +0000 Subject: Add state printing for stacked reserve (canfield) --- src/main/input-output/output/state_printer.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/main/input-output/output') diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index 1828390..2c23496 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -20,7 +20,11 @@ ostream& state_printer::print(ostream& stream, const game_state& gs) { state_printer::print_piles(stream, gs.tableau_piles, gs); } if (gs.rules.reserve_size > 0) { - state_printer::print_header(stream, "Reserve"); + 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) { -- cgit v1.2.1 From 067152f57ced18c5f706144909345125754e8664 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 8 Jan 2018 19:02:36 +0000 Subject: Add game logic for built pile (AA) and refactor --- src/main/input-output/output/state_printer.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/main/input-output/output') diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index 2c23496..cf9f09e 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -51,7 +51,7 @@ void state_printer::print_piles(ostream& stream, const vector& pile_rs, const game_state& gs) { bool empty_row = false; - vector::size_type row_idx = 0; + pile::size_type row_idx = 0; // Loops through the rows in each pile, starting from the bottom, until // one is empty @@ -67,8 +67,9 @@ void state_printer::print_piles(ostream& stream, 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) { - print_card(stream, gs.piles[pile_r][row_idx]); + 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 << "[]"; } -- cgit v1.2.1 From 077c56fbee736d9c597d9735229a1f625be46fe0 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 9 Jan 2018 08:56:01 +0000 Subject: Add state printing for stock-tableau deal --- src/main/input-output/output/state_printer.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/main/input-output/output') diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index cf9f09e..bfb6c7d 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -6,6 +6,8 @@ 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"); @@ -28,8 +30,13 @@ ostream& state_printer::print(ostream& stream, const game_state& gs) { state_printer::print_piles(stream, gs.reserve, 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.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"); -- cgit v1.2.1