From db208b38d78571d68a5f92b7221175d2e67d09ed Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 16 Oct 2017 12:53:14 +0100 Subject: Move setup classes to legacy --- src/main/Card.cpp | 25 ---------- src/main/Card.h | 19 -------- src/main/legacy_card.cpp | 25 ++++++++++ src/main/legacy_card.h | 19 ++++++++ src/main/legacy_main.cpp | 121 +++++++++++++++++++++++++++++++++++++++++++++++ src/main/main.cpp | 121 ----------------------------------------------- 6 files changed, 165 insertions(+), 165 deletions(-) delete mode 100644 src/main/Card.cpp delete mode 100644 src/main/Card.h create mode 100644 src/main/legacy_card.cpp create mode 100644 src/main/legacy_card.h create mode 100644 src/main/legacy_main.cpp (limited to 'src') diff --git a/src/main/Card.cpp b/src/main/Card.cpp deleted file mode 100644 index ead2fd5..0000000 --- a/src/main/Card.cpp +++ /dev/null @@ -1,25 +0,0 @@ -// -// Created by thecharlesblake on 10/11/17. -// - -#include "Card.h" -#include - -Card::Card(const int r, const std::string s): rank(r), suit(s) {} - -std::ostream & operator<<(std::ostream & Str, Card const & c) { - std::string r; - if (c.rank == 1) { - r = "A"; - } else if (c.rank <= 10) { - r = std::to_string(c.rank); - } else if (c.rank == 11) { - r = "J"; - } else if (c.rank == 12) { - r = "Q"; - } else if (c.rank == 13) { - r = "K"; - } - - return Str << r << c.suit; -} \ No newline at end of file diff --git a/src/main/Card.h b/src/main/Card.h deleted file mode 100644 index 651df71..0000000 --- a/src/main/Card.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by thecharlesblake on 10/11/17. -// - -#ifndef SOLITAIRE_PIP_CARD_H -#define SOLITAIRE_PIP_CARD_H - -#include - -struct Card { - Card(const int, const std::string); - const int rank; - const std::string suit; -}; - -std::ostream & operator<<(std::ostream &, Card const &); - - -#endif //SOLITAIRE_PIP_CARD_H diff --git a/src/main/legacy_card.cpp b/src/main/legacy_card.cpp new file mode 100644 index 0000000..9481ecc --- /dev/null +++ b/src/main/legacy_card.cpp @@ -0,0 +1,25 @@ +// +// Created by thecharlesblake on 10/11/17. +// + +#include "legacy_card.h" +#include + +legacy_card::legacy_card(const int r, const std::string s): rank(r), suit(s) {} + +std::ostream & operator<<(std::ostream & Str, legacy_card const & c) { + std::string r; + if (c.rank == 1) { + r = "A"; + } else if (c.rank <= 10) { + r = std::to_string(c.rank); + } else if (c.rank == 11) { + r = "J"; + } else if (c.rank == 12) { + r = "Q"; + } else if (c.rank == 13) { + r = "K"; + } + + return Str << r << c.suit; +} \ No newline at end of file diff --git a/src/main/legacy_card.h b/src/main/legacy_card.h new file mode 100644 index 0000000..215e510 --- /dev/null +++ b/src/main/legacy_card.h @@ -0,0 +1,19 @@ +// +// Created by thecharlesblake on 10/11/17. +// + +#ifndef SOLITAIRE_PIP_CARD_H +#define SOLITAIRE_PIP_CARD_H + +#include + +struct legacy_card { + legacy_card(const int, const std::string); + const int rank; + const std::string suit; +}; + +std::ostream & operator<<(std::ostream &, legacy_card const &); + + +#endif //SOLITAIRE_PIP_CARD_H diff --git a/src/main/legacy_main.cpp b/src/main/legacy_main.cpp new file mode 100644 index 0000000..76940df --- /dev/null +++ b/src/main/legacy_main.cpp @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include +#include +#include "legacy_card.h" +#include + +#if defined NDEBUG + #define LOG_DEBUG(msg) +#else + #define LOG_DEBUG(msg) BOOST_LOG_TRIVIAL(debug) << msg +#endif +#define LOG_INFO(msg) BOOST_LOG_TRIVIAL(info) << msg +#define LOG_WARNING(msg) BOOST_LOG_TRIVIAL(warning) << msg +#define LOG_ERROR(msg) BOOST_LOG_ERROR(error) << msg + +using namespace std; + +bool search(vector* >, legacy_card* hole); + +int main() +{ + const int topRank = 4; + + vector* > tableaux; + + vector shuffleCards; + for (int r = 1; r <= topRank; r++) { + for (int s = 0; s < 4; s++) { + string suit; + if (s == 0) { + suit = "D"; + } else if (s == 1) { + suit = "S"; + } else if (s == 2) { + suit = "H"; + } else { + suit = "C"; + } + + legacy_card* c = new legacy_card(r, suit); + shuffleCards.push_back(c); + } + } + + auto rng = std::default_random_engine(2); // 3 has solution + std::shuffle(std::begin(shuffleCards), std::end(shuffleCards), rng); + + for (int i = 0; i < topRank * 2; i++) { + vector* tab = new vector(); + for (int j = 0; j < 2; j++) { + int idx = i + (topRank * 2 * j); + if (shuffleCards[idx]->suit == "S" && shuffleCards[idx]->rank == 1) { + continue; + } else { + tab->push_back(shuffleCards[idx]); + } + } + tableaux.push_back(tab); + } + + legacy_card c(2, string("H")); + LOG_DEBUG(c); + + cout << (search(tableaux, new legacy_card(1, "S")) ? "Solution Found" : "No Solution"); + + return 0; +} + +bool search(vector* > tableaux, legacy_card* hole) { + // If the tableaux are all empty, return true + + // Seaches through each tableau. If the top card is one away from + // the hole card, puts it in the hole and recurses. + // If that fails then put the card back and repeat for the next foundation + // If it succeeds, return true + + // If we get to the end of the function, return false + +#ifndef NDEBUG + string stateRep; + for (unsigned int i = 0; i < 2; i++) { + for (auto tableau : tableaux) { + if (tableau->size() > i) { + cout << *(*tableau)[i]; + } + cout << "\t"; + } + cout << "\n"; + } + cout << "Hole: " << *hole << "\n"; +#endif + + bool allTableauxEmpty = true; + for (unsigned int i = 0; i < tableaux.size(); i++) { + auto tableau = tableaux[i]; + + if (tableau->empty()) { + continue; + } else { + allTableauxEmpty = false; + } + + legacy_card* topCard = tableau->back(); + if (topCard->rank == hole->rank + 1 || topCard->rank == hole->rank - 1) { + tableau->pop_back(); + + bool success = search(tableaux, topCard); + + if (success) { + return true; + } else { + tableau->push_back(topCard); + } + } + } + + return allTableauxEmpty; +} \ No newline at end of file diff --git a/src/main/main.cpp b/src/main/main.cpp index 1b75252..e69de29 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -1,121 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "Card.h" -#include - -#if defined NDEBUG - #define LOG_DEBUG(msg) -#else - #define LOG_DEBUG(msg) BOOST_LOG_TRIVIAL(debug) << msg -#endif -#define LOG_INFO(msg) BOOST_LOG_TRIVIAL(info) << msg -#define LOG_WARNING(msg) BOOST_LOG_TRIVIAL(warning) << msg -#define LOG_ERROR(msg) BOOST_LOG_ERROR(error) << msg - -using namespace std; - -bool search(vector* >, Card* hole); - -int main() -{ - const int topRank = 4; - - vector* > tableaux; - - vector shuffleCards; - for (int r = 1; r <= topRank; r++) { - for (int s = 0; s < 4; s++) { - string suit; - if (s == 0) { - suit = "D"; - } else if (s == 1) { - suit = "S"; - } else if (s == 2) { - suit = "H"; - } else { - suit = "C"; - } - - Card* c = new Card(r, suit); - shuffleCards.push_back(c); - } - } - - auto rng = std::default_random_engine(2); // 3 has solution - std::shuffle(std::begin(shuffleCards), std::end(shuffleCards), rng); - - for (int i = 0; i < topRank * 2; i++) { - vector* tab = new vector(); - for (int j = 0; j < 2; j++) { - int idx = i + (topRank * 2 * j); - if (shuffleCards[idx]->suit == "S" && shuffleCards[idx]->rank == 1) { - continue; - } else { - tab->push_back(shuffleCards[idx]); - } - } - tableaux.push_back(tab); - } - - Card c(2, string("H")); - LOG_DEBUG(c); - - cout << (search(tableaux, new Card(1, "S")) ? "Solution Found" : "No Solution"); - - return 0; -} - -bool search(vector* > tableaux, Card* hole) { - // If the tableaux are all empty, return true - - // Seaches through each tableau. If the top card is one away from - // the hole card, puts it in the hole and recurses. - // If that fails then put the card back and repeat for the next foundation - // If it succeeds, return true - - // If we get to the end of the function, return false - -#ifndef NDEBUG - string stateRep; - for (unsigned int i = 0; i < 2; i++) { - for (auto tableau : tableaux) { - if (tableau->size() > i) { - cout << *(*tableau)[i]; - } - cout << "\t"; - } - cout << "\n"; - } - cout << "Hole: " << *hole << "\n"; -#endif - - bool allTableauxEmpty = true; - for (unsigned int i = 0; i < tableaux.size(); i++) { - auto tableau = tableaux[i]; - - if (tableau->empty()) { - continue; - } else { - allTableauxEmpty = false; - } - - Card* topCard = tableau->back(); - if (topCard->rank == hole->rank + 1 || topCard->rank == hole->rank - 1) { - tableau->pop_back(); - - bool success = search(tableaux, topCard); - - if (success) { - return true; - } else { - tableau->push_back(topCard); - } - } - } - - return allTableauxEmpty; -} \ No newline at end of file -- cgit v1.2.1