aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/card.cpp20
-rw-r--r--src/main/card.h1
-rw-r--r--src/main/game_state.cpp115
-rw-r--r--src/main/game_state.h8
-rw-r--r--src/main/pile.cpp92
-rw-r--r--src/main/pile.h36
-rw-r--r--src/main/sol_rules.cpp6
-rw-r--r--src/main/sol_rules.h15
-rw-r--r--src/main/solver.cpp11
9 files changed, 243 insertions, 61 deletions
diff --git a/src/main/card.cpp b/src/main/card.cpp
index 0510304..583814d 100644
--- a/src/main/card.cpp
+++ b/src/main/card.cpp
@@ -40,13 +40,13 @@ card::card(const char* c) : rank(-1), suit(-1) {
s = tolower(s);
- if (s == 's') {
+ if (s == 'c') {
suit = 0;
- } else if (s == 'h') {
- suit = 1;
} else if (s == 'd') {
+ suit = 1;
+ } else if (s == 'h') {
suit = 2;
- } else if (s == 'c') {
+ } else if (s == 's') {
suit = 3;
}
}
@@ -61,14 +61,18 @@ int card::get_rank() const {
char card::get_suit() const {
switch (suit) {
- case 0: return 'S';
- case 1: return 'H';
- case 2: return 'D';
- case 3: return 'C';
+ case 0: return 'C';
+ case 1: return 'D';
+ case 2: return 'H';
+ case 3: return 'S';
default: return 'X';
}
}
+int card::get_suit_val() const {
+ return suit;
+}
+
std::ostream & operator<<(std::ostream & s, card const & c) {
std::string r;
if (c.get_rank() == 1) {
diff --git a/src/main/card.h b/src/main/card.h
index 1569a5b..7c28a28 100644
--- a/src/main/card.h
+++ b/src/main/card.h
@@ -14,6 +14,7 @@ public:
card(const char*);
int get_rank() const;
char get_suit() const;
+ int get_suit_val() const;
private:
int rank;
diff --git a/src/main/game_state.cpp b/src/main/game_state.cpp
index 45bea21..93ecb02 100644
--- a/src/main/game_state.cpp
+++ b/src/main/game_state.cpp
@@ -7,16 +7,23 @@
#include <numeric>
#include <random>
#include <algorithm>
+#include <functional>
#include <rapidjson/document.h>
#include "game_state.h"
+#include "pile.h"
using namespace rapidjson;
using namespace std;
+typedef sol_rules::build_order ord;
+typedef sol_rules::build_policy pol;
+
// Construct an initial game state from a JSON doc
-game_state::game_state(const Document& doc) : rules("simple-black-hole") {
+game_state::game_state(const Document& doc)
+ : rules("simple-black-hole"),
+ hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank) {
// Construct tableau piles
assert(doc.HasMember("tableau piles"));
const Value& json_tab_piles = doc["tableau piles"];
@@ -24,12 +31,12 @@ game_state::game_state(const Document& doc) : rules("simple-black-hole") {
int max_rank = 1;
for (auto& json_tab : json_tab_piles.GetArray()) {
- vector<card> tableau_pile;
+ pile tableau_pile(true, rules.build_order, pol::ANY_SUIT, false);
for (auto& json_card : json_tab.GetArray()) {
card c(json_card.GetString());
if (c.get_rank() > max_rank) max_rank = c.get_rank();
- tableau_pile.push_back(c);
+ tableau_pile.place(c);
}
tableau_piles.push_back(tableau_pile);
@@ -39,11 +46,13 @@ game_state::game_state(const Document& doc) : rules("simple-black-hole") {
assert(doc.HasMember("hole card"));
const Value& json_h_card = doc["hole card"];
assert(json_h_card.IsString());
- hole_card = json_h_card.GetString();
+ hole.place(json_h_card.GetString());
}
// Construct an initial game state from a seed
-game_state::game_state(int seed, const sol_rules& rules) : rules(rules) {
+game_state::game_state(int seed, const sol_rules& rules)
+ : rules(rules),
+ hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank) {
vector<card> deck = shuffled_deck(seed, rules.max_rank);
// Deal to the tableau piles (row-by-row)
@@ -52,17 +61,26 @@ game_state::game_state(int seed, const sol_rules& rules) : rules(rules) {
// If there is a hole card, don't deal the ace of spades
if (rules.hole && c == "AS") {
- hole_card = "AS";
+ hole.place("AS");
continue;
}
// For the first row we must create all the tableau pile vectors
if (i / rules.tableau_pile_count == 0) {
- tableau_piles.push_back(vector<card>());
+ pile p(true, rules.build_order, pol::ANY_SUIT, false);
+ tableau_piles.push_back(p);
}
// Add the randomly generated card to the tableau piles
- tableau_piles[i % rules.tableau_pile_count].push_back(c);
+ tableau_piles[i % rules.tableau_pile_count].place(c);
+ }
+
+ // If there are foundation piles, create the relevant pile vectors
+ if (rules.foundations) {
+ for (int i = 0; i < 4; i++) {
+ pile p(true, ord::ASCENDING, pol(i), false);
+ foundations.push_back(p);
+ }
}
}
@@ -93,38 +111,51 @@ vector<card> game_state::shuffled_deck(int seed, int max_rank = 13) {
return deck;
}
-
vector<game_state> game_state::get_next_legal_states() const {
+ // Generates vectors of references to all the piles from which
+ // cards can be removed, and to which they can be added
+ vector<pile&> can_remove;
+ vector<pile&> can_add;
+
+ for (pile p : tableau_piles) {
+ can_remove.push_back(p);
+ if (rules.build_order != ord::NO_BUILD) {
+ can_add.push_back(p);
+ }
+ }
+ if (rules.foundations) {
+ for (pile p : foundations) {
+ can_remove.push_back(p);
+ can_add.push_back(p);
+ }
+ }
+ if (rules.hole) {
+ can_add.push_back(hole);
+ }
+
+ // The next legal states
vector<game_state> next;
- // Searches through each column for a top card that can be moved to the hole
- bool all_tableaux_empty = true;
- for (vector<vector<card>>::size_type i = 0; i < tableau_piles.size(); i++) {
- vector<card> tableau_pile = tableau_piles[i];
+ for (pile &rem_pile : can_remove) {
+ if (rem_pile.empty()) continue;
- // Ignore empty piles
- if (tableau_pile.empty()) {
- continue;
- } else {
- all_tableaux_empty = false;
- }
- card top_card = tableau_pile.back();
-
- // If a card at the top of a tableau pile is adjacent to the hole card,
- // move it to the hole and add that state
- if (adjacent(top_card, hole_card)) {
- game_state s = *this;
- s.move_to_hole(i);
- next.push_back(s);
+ for (pile &add_pile : can_add) {
+ if (&add_pile != &rem_pile
+ && add_pile.can_place(rem_pile.top_card())) {
+
+ move(rem_pile, add_pile); // remove
+ game_state s = *this;
+ next.push_back(s);
+ move(add_pile, rem_pile); // restore
+ }
}
}
- // If there are no cards left in play, deal solved
- solved = all_tableaux_empty;
-
return next;
}
+
+
bool game_state::adjacent(const card& a, const card& b) const {
int x = a.get_rank();
int y = b.get_rank();
@@ -135,13 +166,13 @@ bool game_state::adjacent(const card& a, const card& b) const {
|| (x == rules.max_rank && y == 1);
}
-void game_state::move_to_hole(int tab_idx) {
- hole_card = tableau_piles[tab_idx].back();
- tableau_piles[tab_idx].pop_back();
-}
-
bool game_state::is_solved() const {
- return solved;
+ for (auto p : tableau_piles) {
+ if (!p.empty()) {
+ return false;
+ }
+ }
+ return true;
}
ostream& game_state::print(ostream& stream) const {
@@ -172,10 +203,10 @@ void game_state::print_header(ostream& stream, const char* header) const {
}
void game_state::print_foundations(ostream& stream) const {
- stream << foundations[0] << "\t"
- << foundations[1] << "\t"
- << foundations[2] << "\t"
- << foundations[3] << "\n";
+ stream << foundations[0].top_card() << "\t"
+ << foundations[1].top_card() << "\t"
+ << foundations[2].top_card() << "\t"
+ << foundations[3].top_card() << "\n";
};
void game_state::print_tableau_piles(ostream& stream) const {
@@ -185,7 +216,7 @@ void game_state::print_tableau_piles(ostream& stream) const {
while (!empty_row) {
empty_row = true;
- for (vector<card> tableau_pile : tableau_piles) {
+ for (pile tableau_pile : tableau_piles) {
if (tableau_pile.size() > row_idx) {
empty_row = false;
stream << tableau_pile[row_idx];
@@ -199,7 +230,7 @@ void game_state::print_tableau_piles(ostream& stream) const {
};
void game_state::print_hole(ostream& stream) const {
- stream << hole_card << "\n";
+ stream << hole.top_card() << "\n";
};
ostream& operator <<(ostream& stream, const game_state& gs) {
diff --git a/src/main/game_state.h b/src/main/game_state.h
index 974fd2a..c5d4f96 100644
--- a/src/main/game_state.h
+++ b/src/main/game_state.h
@@ -13,6 +13,7 @@
#include "card.h"
#include "sol_rules.h"
+#include "pile.h"
class game_state {
public:
@@ -35,11 +36,10 @@ private:
void print_tableau_piles(std::ostream&) const;
void print_hole(std::ostream&) const;
- std::vector<std::vector<card> > tableau_piles;
- std::vector<card> foundations;
+ std::vector<pile> tableau_piles;
+ std::vector<pile> foundations;
+ pile hole;
sol_rules rules;
- card hole_card;
- mutable bool solved;
};
diff --git a/src/main/pile.cpp b/src/main/pile.cpp
new file mode 100644
index 0000000..e19d96c
--- /dev/null
+++ b/src/main/pile.cpp
@@ -0,0 +1,92 @@
+//
+// Created by thecharlesblake on 10/28/17.
+//
+
+#include "pile.h"
+#include "sol_rules.h"
+#include "card.h"
+
+pile::pile(bool c, sol_rules::build_order bo, sol_rules::build_policy bp,
+ bool l, int m) : can_remove(c), build_order(bo),
+ build_policy(bp), build_order_loops(l),
+ max_rank(m) {}
+
+bool pile::can_place(const card& c) const {
+ typedef sol_rules::build_order ord;
+ typedef sol_rules::build_policy pol;
+
+ if (build_order == ord::NO_BUILD) {
+ return false;
+ }
+
+ if (empty()) {
+ if (build_policy < 4) {
+ return c.get_suit_val() == build_policy;
+ } else {
+ return true;
+ }
+ }
+
+ card top_card = top_card();
+
+ // Check suit
+ if (build_policy == pol::SAME_SUIT && c.get_suit_val() != top_card.get_suit_val()) {
+ return false;
+ }
+ if (build_policy < 4 && c.get_suit_val() != build_policy) {
+ return false;
+ }
+
+ int tc_r = top_card.get_rank();
+ int comp_r = c.get_rank();
+
+ if (build_order == ord::ASCENDING) {
+ return one_lt(tc_r, comp_r);
+ } else if (build_order == ord::DESCENDING) {
+ return one_lt(comp_r, tc_r);
+ } else if (build_order == ord::BOTH) {
+ return one_lt(tc_r, comp_r) || one_lt(comp_r, tc_r);
+ }
+}
+
+bool pile::one_lt(int a, int b) {
+ return (a == b - 1) || (loop && a == max_rank && b == 1);
+}
+
+void pile::place(const card& c) {
+ pile_vec.push_back(c);
+}
+
+card pile::top_card() const {
+ return pile_vec.back();
+}
+
+card pile::take() {
+ card c = top_card();
+ pile_vec.pop_back();
+ return c;
+}
+
+bool pile::can_remove() const {
+ return can_remove;
+}
+
+bool pile::empty() const {
+ return pile_vec.empty();
+}
+
+int pile::size() const {
+ return pile_vec.size();
+}
+
+card& pile::operator[] (std::vector::size_type i) {
+ return pile_vec[i];
+}
+card pile::operator[] (std::vector::size_type i) const {
+ return pile_vec[i];
+}
+
+
+void move(const pile& a, const pile& b) {
+ b.place(a.take());
+}
diff --git a/src/main/pile.h b/src/main/pile.h
new file mode 100644
index 0000000..669c540
--- /dev/null
+++ b/src/main/pile.h
@@ -0,0 +1,36 @@
+//
+// Created by thecharlesblake on 10/28/17.
+//
+
+#ifndef SOLVITAIRE_PILE_H
+#define SOLVITAIRE_PILE_H
+
+#import "sol_rules.h"
+
+class pile {
+public:
+ pile();
+ pile(bool, sol_rules::build_order, sol_rules::build_policy, bool, int m = 13);
+ bool can_place(const card&) const;
+ void place(const card&);
+ card take();
+ card top_card() const;
+ bool can_remove() const;
+ bool empty() const;
+ int size() const;
+ card& operator[] (std::vector::size_type);
+ card operator[] (std::vector::size_type) const;
+private:
+ vector<card> pile_vec;
+ const bool can_remove;
+ const bool build_order_loops;
+ const int max_rank;
+ const sol_rules::build_order build_order;
+ const sol_rules::build_policy build_policy;
+ bool one_lt(int, int);
+};
+
+void move(const pile&, const pile&);
+
+
+#endif //SOLVITAIRE_PILE_H
diff --git a/src/main/sol_rules.cpp b/src/main/sol_rules.cpp
index 75c79b1..7d01902 100644
--- a/src/main/sol_rules.cpp
+++ b/src/main/sol_rules.cpp
@@ -22,19 +22,19 @@ sol_rules::sol_rules(std::string sol_type) {
hole = false;
foundations = true;
- build_order = tab_build_order::DESCENDING;
+ build_order = build_order::DESCENDING;
switch(vs) {
case valid_sol::BLACK_HOLE:
tableau_pile_count = 17;
- build_order = tab_build_order::NO_BUILD;
+ build_order = build_order::NO_BUILD;
max_rank = 13;
hole = true;
foundations = false;
break;
case valid_sol::SIMPLE_BLACK_HOLE:
tableau_pile_count = 9;
- build_order = tab_build_order::NO_BUILD;
+ build_order = build_order::NO_BUILD;
max_rank = 7;
hole = true;
foundations = false;
diff --git a/src/main/sol_rules.h b/src/main/sol_rules.h
index 56701c7..2a442b8 100644
--- a/src/main/sol_rules.h
+++ b/src/main/sol_rules.h
@@ -14,9 +14,19 @@ struct sol_rules {
SIMPLE_BLACK_HOLE,
SPANISH_PATIENCE
};
- enum class tab_build_order {
+ enum class build_order {
NO_BUILD,
DESCENDING,
+ ASCENDING,
+ BOTH
+ };
+ enum class build_policy {
+ CLUBS,
+ DIAMONDS,
+ HEARTS,
+ SPADES,
+ ANY_SUIT,
+ SAME_SUIT
};
static std::vector<std::string> valid_sol_strs;
@@ -24,7 +34,7 @@ struct sol_rules {
sol_rules(std::string);
int tableau_pile_count;
- tab_build_order build_order;
+ build_order build_order;
int max_rank;
bool hole;
bool foundations;
@@ -33,5 +43,4 @@ private:
static valid_sol valid_sol_enum(const std::string&);
};
-
#endif //SOLVITAIRE_SOL_RULES_H
diff --git a/src/main/solver.cpp b/src/main/solver.cpp
index ac8ffb8..c706a01 100644
--- a/src/main/solver.cpp
+++ b/src/main/solver.cpp
@@ -5,6 +5,7 @@
#include <string>
#include <ostream>
#include <iostream>
+#include <algorithm>
#include "solver.h"
#include "log_helper.h"
@@ -34,9 +35,17 @@ bool solver::run() {
// Create new nodes for each of the children
vector<game_state> new_children = current.state.get_next_legal_states();
for (auto it = new_children.rbegin(); it != new_children.rend(); it++) {
+ game_state new_state = *it;
+
+ // If we have seen the state before, ignore it (loop detection)
+ if (find(begin(current.history), end(current.history), new_state)
+ != end(current.history)) {
+ continue;
+ }
+
node n = current;
n.history.push_back(n.state);
- n.state = *it;
+ n.state = new_state;
frontier.push_back(n);
}