diff options
| author | thecharlesblake | 2017-10-12 11:19:23 +0100 |
|---|---|---|
| committer | thecharlesblake | 2017-10-13 18:46:14 +0100 |
| commit | c04bec7b528aa3fe953f6a9b838c771d02c4f3b8 (patch) | |
| tree | 38474a6899cfe38213b787541e71f2af301c4c2e /src/main/main.cpp | |
| parent | Add logging macros (diff) | |
| download | metasol-c04bec7b528aa3fe953f6a9b838c771d02c4f3b8.tar.gz metasol-c04bec7b528aa3fe953f6a9b838c771d02c4f3b8.zip | |
Basic Black Hole solver
Diffstat (limited to 'src/main/main.cpp')
| -rw-r--r-- | src/main/main.cpp | 107 |
1 files changed, 99 insertions, 8 deletions
diff --git a/src/main/main.cpp b/src/main/main.cpp index 3b4b00b..1b75252 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -1,6 +1,10 @@ #include <string> #include <iostream> -#include <boost/foreach.hpp> +#include <vector> +#include <map> +#include <algorithm> +#include <random> +#include "Card.h" #include <boost/log/trivial.hpp> #if defined NDEBUG @@ -12,19 +16,106 @@ #define LOG_WARNING(msg) BOOST_LOG_TRIVIAL(warning) << msg #define LOG_ERROR(msg) BOOST_LOG_ERROR(error) << msg +using namespace std; + +bool search(vector<vector<Card*>* >, Card* hole); + int main() { + const int topRank = 4; + + vector<vector<Card*>* > tableaux; - LOG_DEBUG("Log debug"); - LOG_INFO("Log info"); - LOG_WARNING("Log warning"); + vector<Card*> 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); + } + } - std::string hello( "Hello, world!\n" ); + auto rng = std::default_random_engine(2); // 3 has solution + std::shuffle(std::begin(shuffleCards), std::end(shuffleCards), rng); - BOOST_FOREACH( char ch, hello ) - { - std::cout << ch; + for (int i = 0; i < topRank * 2; i++) { + vector<Card*>* tab = new vector<Card*>(); + 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<vector<Card*>* > 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 |
