diff options
Diffstat (limited to 'src/main/game')
| -rw-r--r-- | src/main/game/card.cpp | 92 | ||||
| -rw-r--r-- | src/main/game/card.h | 31 | ||||
| -rw-r--r-- | src/main/game/game_state.cpp | 328 | ||||
| -rw-r--r-- | src/main/game/game_state.h | 56 | ||||
| -rw-r--r-- | src/main/game/pile.cpp | 145 | ||||
| -rw-r--r-- | src/main/game/pile.h | 55 | ||||
| -rw-r--r-- | src/main/game/sol_rules.cpp | 122 | ||||
| -rw-r--r-- | src/main/game/sol_rules.h | 60 |
8 files changed, 889 insertions, 0 deletions
diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp new file mode 100644 index 0000000..9f7dbdf --- /dev/null +++ b/src/main/game/card.cpp @@ -0,0 +1,92 @@ +// +// Created by thecharlesblake on 10/11/17. +// + +#include <sstream> + +#include "card.h" + +using namespace std; + +// A card can either be created using two int values (rank & suit), or +// using a string (e.g. "AS") + +card::card(int r, int s) : rank(r), suit(s) {} + +card::card(const char* c) : rank(-1), suit(-1) { + bool letter = false; + switch(tolower(c[0])) { + case 'a': rank = 1; + letter = true; + break; + case 'j': rank = 11; + letter = true; + break; + case 'q': rank = 12; + letter = true; + break; + case 'k': rank = 13; + letter = true; + break; + default: rank = stoi(c); + } + + char s; + if (rank >= 10 && !letter) { + s = c[2]; + } else { + s = c[1]; + } + + s = static_cast<char>(tolower(s)); + + if (s == 'c') { + suit = 0; + } else if (s == 'd') { + suit = 1; + } else if (s == 'h') { + suit = 2; + } else if (s == 's') { + suit = 3; + } +} + +bool operator==(const card& l, const card& r) { + return l.get_rank() == r.get_rank() && l.get_suit() == r.get_suit(); +} + +int card::get_rank() const { + return rank; +} + +char card::get_suit() const { + switch (suit) { + 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) { + s << "A"; + } else if (c.get_rank() <= 10) { + s << c.get_rank(); + } else if (c.get_rank() == 11) { + s << "J"; + } else if (c.get_rank() == 12) { + s << "Q"; + } else if (c.get_rank() == 13) { + s << "K"; + } + + s << c.get_suit(); + return s; +}
\ No newline at end of file diff --git a/src/main/game/card.h b/src/main/game/card.h new file mode 100644 index 0000000..1cac344 --- /dev/null +++ b/src/main/game/card.h @@ -0,0 +1,31 @@ +// +// Created by thecharlesblake on 10/11/17. +// + +#ifndef SOLITAIRE_PIP_CARD_H +#define SOLITAIRE_PIP_CARD_H + +#include <string> +#include <map> + +class card { +public: + // A card can either be created using two int values (rank & suit), or + // using a string (e.g. "AS") + card(int, int); + card(const char*); + + int get_rank() const; + char get_suit() const; + int get_suit_val() const; + +private: + int rank; + int suit; +}; + +std::ostream & operator<<(std::ostream &, card const &); +bool operator==(const card&, const card&); + + +#endif //SOLITAIRE_PIP_CARD_H diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp new file mode 100644 index 0000000..3336020 --- /dev/null +++ b/src/main/game/game_state.cpp @@ -0,0 +1,328 @@ +// +// Created by thecharlesblake on 10/24/17. +// + +#include <vector> +#include <ostream> +#include <random> +#include <algorithm> +#include <functional> + +#include <rapidjson/document.h> + +#include "game_state.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) + : reserve(true, ord::NO_BUILD, pol::ANY_SUIT, false), + stock(true, ord::NO_BUILD, pol::ANY_SUIT, false), + waste(true, ord::NO_BUILD, pol::ANY_SUIT, false), + hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank), + rules("simple-black-hole") { + // Construct tableau piles + assert(doc.HasMember("tableau piles")); + const Value& json_tab_piles = doc["tableau piles"]; + assert(json_tab_piles.IsArray()); + + int max_rank = 1; + for (auto& json_tab : json_tab_piles.GetArray()) { + pile tableau_pile(true, rules.build_ord, 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.place(c); + } + + tableau_piles.push_back(tableau_pile); + } + + // Assign hole card + assert(doc.HasMember("hole card")); + const Value& json_h_card = doc["hole card"]; + assert(json_h_card.IsString()); + hole.place(json_h_card.GetString()); +} + +// Construct an initial game state from a seed +game_state::game_state(int seed, const sol_rules& s_rules) : + reserve(pile::reserve_factory()), + stock(pile::stock_factory()), + waste(pile::waste_factory()), + hole(pile::hole_factory(s_rules.max_rank)), + rules(s_rules) { + + vector<card> deck = shuffled_deck(seed, rules.max_rank); + + // If there is a hole, move the ace of spades to it + if (rules.hole) { + deck.erase(find(begin(deck), end(deck), card("AS"))); + hole.place("AS"); + } + + // If there is a stock, deal to it and set up a waste pile too + if (rules.stock_size > 0) { + for (unsigned int i = 0; i < rules.stock_size; i++) { + stock.place(deck.back()); + deck.pop_back(); + } + } + + // If there is a reserve, deal to it + if (rules.reserve_size > 0) { + for (unsigned int i = 0; i < rules.reserve_size; i++) { + reserve.place(deck.back()); + deck.pop_back(); + } + } + + // Deal to the tableau piles (row-by-row) + unsigned int t = 0; + while (!deck.empty()) { + card c = deck.back(); + deck.pop_back(); + + // For the first row we must create all the tableau pile vectors + if (t / rules.tableau_pile_count == 0) { + tableau_piles.push_back(pile::tableau_factory(rules.build_ord)); + } + + // Add the randomly generated card to the tableau piles + tableau_piles[t % rules.tableau_pile_count].place(c); + t++; + } + + // If there are foundation piles, create the relevant pile vectors + if (rules.foundations) { + for (int i = 0; i < 4; i++) { + foundations.push_back(pile::foundation_factory(pol(i))); + } + } + + // If there are cell piles, create the relevant cell vectors + if (rules.cells > 0) { + for (unsigned int i = 0; i < rules.cells; i++) { + cells.push_back(pile::cell_factory()); + } + } +} + +// Generate a randomly ordered vector of cards +vector<card> game_state::shuffled_deck(int seed, int max_rank = 13) { + vector<int> values; + vector<int*> v_ptrs; + for (int i = 0; i < max_rank * 4; i++) { + values.push_back(i); + } + for (int i = 0; i < max_rank * 4; i++) { + v_ptrs.push_back(&values[i]); + } + + // Randomly shuffle the pointers + auto rng = default_random_engine(seed); + shuffle(begin(v_ptrs), end(v_ptrs), rng); + + vector<card> deck; + for (int *i : v_ptrs) { + int r = ((*i) % max_rank) + 1; + int s = (*i) / max_rank; + + deck.emplace_back(card(r, s)); + } + + return deck; +} + +const 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; + + // A mutable version of the current state, used to create the next legal + // states + game_state mut_state = *this; + + for (pile* p : mut_state.get_regular_pile_refs()) { + if (p->can_remove()) { + can_remove.push_back(p); + } + if (p->get_build_order() != ord::NO_BUILD) { + can_add.push_back(p); + } + } + + // The next legal states + vector<game_state> next; + + // Moving from and to the 'can remove' and 'can add' piles + for (pile* rem_pile : can_remove) { + if (rem_pile->empty()) continue; + + for (pile* add_pile : can_add) { + if (&add_pile != &rem_pile + && add_pile->can_place(rem_pile->top_card())) { + + game_state new_gs = move(mut_state, rem_pile, add_pile); + next.push_back(new_gs); + } + } + } + + // Dealing from the stock to the waste + if (!stock.empty()) { + game_state new_gs = move(mut_state, &mut_state.stock, &mut_state.waste); + next.push_back(new_gs); + } + + return next; +} + +const game_state game_state::move(game_state& gs, pile* from, pile* to) const { + to->place(from->take()); + const game_state new_gs(gs); + from->place(to->take()); + return new_gs; +} + + +bool game_state::is_solved() const { + for (auto p : foundations) { + if (p.size() != rules.max_rank) { + return false; + } + } + + return !rules.hole || hole.size() == rules.max_rank * 4; +} + +// Not the stock, because cards from it can't be moved to the rest of the piles! +vector<pile*> game_state::get_regular_pile_refs() { + vector<pile*> piles; + auto to_ptr = [](pile& p){return &p;}; + + if (rules.foundations) { + transform(begin(foundations), end(foundations), back_inserter(piles), + to_ptr); + } + if (rules.cells) { + transform(begin(cells), end(cells), back_inserter(piles), to_ptr); + } + transform(begin(tableau_piles), end(tableau_piles), back_inserter(piles), to_ptr); + if (rules.reserve_size > 0) { + piles.push_back(&reserve); + } + if (rules.stock_size > 0) { + piles.push_back(&waste); + } + if (rules.hole) { + piles.push_back(&hole); + } + return piles; +} + +ostream& game_state::print(ostream& stream) const { + if (rules.foundations) { + print_header(stream, "Foundations"); + print_top_of_piles(stream, foundations); + } + if (rules.cells) { + print_header(stream, "Cells"); + print_piles(stream, cells); + } + if (rules.reserve_size > 0) { + print_header(stream, "Reserve"); + print_pile(stream, reserve); + } + if (rules.tableau_pile_count > 0) { + print_header(stream, "Tableau Piles"); + print_piles(stream, tableau_piles); + } + if (rules.stock_size > 0) { + print_header(stream, "Stock | Waste"); + print_piles(stream, {stock, waste}); + } + if (rules.hole) { + print_header(stream, "Hole Card"); + print_top_of_pile(stream, hole); + } + return stream << "==================================="; +} + +void game_state::print_header(ostream& stream, const char* header) const { + stream << "--- " << header << " "; + int pad = 20 - strlen(header); + for (int i = 0; i < pad; i++) { + stream << '-'; + } + stream << "\n"; +} + +void game_state::print_piles(ostream& stream, + const vector<pile>& pile_vec) const { + bool empty_row = false; + vector<card>::size_type row_idx = 0; + + // Loops through the rows in each pile, starting from the bottom, until + // one is empty + while (!empty_row) { + // Loops throught the current row to determine if it is empty + empty_row = true; + for (const pile& tableau_pile : pile_vec) { + if (tableau_pile.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 pile& tableau_pile : pile_vec) { + if (tableau_pile.size() > row_idx) { + stream << tableau_pile[row_idx]; + } else if (row_idx == 0) { + stream << "[]"; + } + stream << "\t"; + } + + stream << "\n"; + row_idx++; + } + } +} + +void game_state::print_pile(ostream& stream, const pile &p) const { + print_piles(stream, {p}); +} + +void game_state::print_top_of_piles(ostream& stream, const vector<pile>& vp) const { + vector<pile> top(vp); + for (pile& p : top) { + if (!p.empty()) { + card t = p.top_card(); + p.clear(); + p.place(t); + } + } + print_piles(stream, top); +} + +void game_state::print_top_of_pile(ostream& stream, const pile& p) const { + print_top_of_piles(stream, {p}); +} + +bool operator==(const game_state& a, const game_state& b) { + return a.tableau_piles == b.tableau_piles + && a.foundations == b.foundations + && a.hole == b.hole; +} + +ostream& operator <<(ostream& stream, const game_state& gs) { + return gs.print(stream); +}
\ No newline at end of file diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h new file mode 100644 index 0000000..12dcda9 --- /dev/null +++ b/src/main/game/game_state.h @@ -0,0 +1,56 @@ +// +// Created by thecharlesblake on 10/24/17. +// + +#ifndef SOLVITAIRE_GAME_STATE_H +#define SOLVITAIRE_GAME_STATE_H + +#include <vector> +#include <ostream> +#include <string> + +#include <rapidjson/document.h> + +#include "card.h" +#include "sol_rules.h" +#include "pile.h" + +class game_state { +public: + // Create a game state representation from a JSON doc + game_state(const rapidjson::Document&); + // Do the same from a 'rules' object + game_state(int seed, const sol_rules&); + + const std::vector<game_state> get_next_legal_states() const; + bool is_solved() const; + + std::ostream& print(std::ostream&) const; + + friend bool operator==(const game_state&, const game_state&); + friend std::ostream& operator<< (std::ostream&, const game_state&); + +private: + static std::vector<card> shuffled_deck(int, int); + + const game_state move(game_state&, pile*, pile*) const; + std::vector<pile*> get_regular_pile_refs(); + + void print_header(std::ostream&, const char*) const; + void print_piles(std::ostream&, const std::vector<pile>&) const; + void print_pile(std::ostream&, const pile&) const; + void print_top_of_piles(std::ostream&, const std::vector<pile>&) const; + void print_top_of_pile(std::ostream&, const pile&) const; + + std::vector<pile> foundations; + std::vector<pile> cells; + std::vector<pile> tableau_piles; + pile reserve; + pile stock; + pile waste; + pile hole; + sol_rules rules; +}; + + +#endif //SOLVITAIRE_GAME_STATE_H diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp new file mode 100644 index 0000000..bebd1f6 --- /dev/null +++ b/src/main/game/pile.cpp @@ -0,0 +1,145 @@ +// +// Created by thecharlesblake on 10/28/17. +// + +#include <vector> +#include <ostream> + +#include "pile.h" + +using namespace std; + +typedef sol_rules::build_order ord; +typedef sol_rules::build_policy pol; + +//////////// +// Static // +//////////// + +pile pile::foundation_factory(pol p) { + return pile(true, ord::ASCENDING, p, false); +} +pile pile::cell_factory() { + return pile(true, ord::SINGLE_CARD, pol::ANY_SUIT, false); +} +pile pile::tableau_factory(ord o) { + return pile(true, o, pol::ANY_SUIT, false); +} +pile pile::reserve_factory() { + return pile(true, ord::NO_BUILD, pol::ANY_SUIT, false); +} +pile pile::stock_factory() { + return pile(true, ord::NO_BUILD, pol::ANY_SUIT, false); +} +pile pile::waste_factory() { + return pile(true, ord::NO_BUILD, pol::ANY_SUIT, false); +} +pile pile::hole_factory(int max_rank) { + return pile(false, ord::BOTH, pol::ANY_SUIT, true, max_rank); +} + +//////////////// +// Non-static // +//////////////// + +pile::pile(bool r, ord bo, pol bp, bool l, int mr) : + remove(r), + build_order(bo), + build_policy(bp), + build_order_loops(l), + max_rank(mr) {} + +bool pile::can_place(const card& c) const { + + if (build_order == ord::NO_BUILD) { + return false; + } else if (build_order == ord::SINGLE_CARD) { + return empty(); + } + + if (empty()) { + if (sol_rules::is_suit(build_policy)) { + return c.get_suit_val() == sol_rules::suit_val(build_policy) + && c.get_rank() == 1; + } else { + return true; + } + } + + card top_c = top_card(); + + // Check suit + if (build_policy == pol::SAME_SUIT && c.get_suit_val() + != top_c.get_suit_val()) { + return false; + } + if (sol_rules::is_suit(build_policy) && c.get_suit_val() + != sol_rules::suit_val(build_policy)) { + return false; + } + + int tc_r = top_c.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); + } else { + return false; + } +} + +bool pile::one_lt(int a, int b) const { + return (a == b - 1) || (build_order_loops && a == max_rank && b == 1); +} + + + +card pile::top_card() const { + return pile_vec.back(); +} + +bool pile::can_remove() const { + return remove; +} + +bool pile::empty() const { + return pile_vec.empty(); +} + +unsigned long pile::size() const { + return pile_vec.size(); +} + +ord pile::get_build_order() const { + return build_order; +} + +card& pile::operator[] (vector<card>::size_type i) { + return pile_vec[i]; +} +card pile::operator[] (vector<card>::size_type i) const { + return pile_vec[i]; +} + + +void pile::place(const card& c) { + pile_vec.push_back(c); +} + +card pile::take() { + card c = top_card(); + pile_vec.pop_back(); + return c; +} + +void pile::clear() { + pile_vec.clear(); +} + +bool operator==(const pile& a, const pile& b) { + return a.pile_vec == b.pile_vec; +} diff --git a/src/main/game/pile.h b/src/main/game/pile.h new file mode 100644 index 0000000..6482e0c --- /dev/null +++ b/src/main/game/pile.h @@ -0,0 +1,55 @@ +// +// Created by thecharlesblake on 10/28/17. +// + +#ifndef SOLVITAIRE_PILE_H +#define SOLVITAIRE_PILE_H + +#include <vector> + +#include "sol_rules.h" +#include "card.h" + +class pile { +public: + static pile foundation_factory(sol_rules::build_policy); + static pile cell_factory(); + static pile tableau_factory(sol_rules::build_order); + static pile reserve_factory(); + static pile stock_factory(); + static pile waste_factory(); + static pile hole_factory(int max_rank = 13); + + pile(bool, sol_rules::build_order, sol_rules::build_policy, bool, + int max_rank = 13); + + bool can_place(const card&) const; + bool can_remove() const; + card top_card() const; + bool empty() const; + unsigned long size() const; + sol_rules::build_order get_build_order() const; + + void place(const card&); + card take(); + void clear(); + + card& operator[] (std::vector<card>::size_type); + card operator[] (std::vector<card>::size_type) const; + + friend bool operator==(const pile&, const pile&); + +private: + bool one_lt(int, int) const; + + const bool remove; + const sol_rules::build_order build_order; + const sol_rules::build_policy build_policy; + const bool build_order_loops; + const int max_rank; + + std::vector<card> pile_vec; +}; + + +#endif //SOLVITAIRE_PILE_H diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp new file mode 100644 index 0000000..003528c --- /dev/null +++ b/src/main/game/sol_rules.cpp @@ -0,0 +1,122 @@ +// +// Created by thecharlesblake on 10/28/17. +// + +#include <string> +#include <vector> +#include <algorithm> +#include <assert.h> + +#include "sol_rules.h" + +using namespace std; + +//////////// +// Static // +//////////// + +typedef sol_rules::build_policy pol; + +vector<string> sol_rules::valid_sol_strs = { + "black-hole", + "simple-black-hole", + "spanish-patience", + "simple-spanish-patience", + "free-cell", + "simple-free-cell", + "canfield", + "simple-canfield" +}; + +bool sol_rules::is_suit(pol bp) { + return suit_val(bp) != -1; +} +int sol_rules::suit_val(pol bp) { + switch (bp) { + case pol::CLUBS: return 0; + case pol::DIAMONDS: return 1; + case pol::HEARTS: return 2; + case pol::SPADES: return 3; + default: return -1; + } +} + +//////////////// +// Non-static // +//////////////// + +sol_rules::sol_rules(std::string sol_type) { + valid_sol vs = valid_sol_enum(sol_type); + + // Default game values + + // tableau_pile_count = no obvious default + build_ord = build_order::DESCENDING; + build_pol = build_policy::ANY_SUIT; + max_rank = 13; + hole = false; + foundations = true; + cells = 0; + reserve_size = 0; + stock_size = 0; + + // Game-specific values + switch(vs) { + case valid_sol::BLACK_HOLE: + tableau_pile_count = 17; + build_ord = build_order::NO_BUILD; + hole = true; + foundations = false; + cells = 0; + break; + case valid_sol::SIMPLE_BLACK_HOLE: + tableau_pile_count = 9; + build_ord = build_order::NO_BUILD; + max_rank = 7; + hole = true; + foundations = false; + break; + case valid_sol::SPANISH_PATIENCE: + tableau_pile_count = 13; + break; + case valid_sol::SIMPLE_SPANISH_PATIENCE: + tableau_pile_count = 5; + max_rank = 5; + break; + case valid_sol::FREE_CELL: + tableau_pile_count = 8; + cells = 4; + break; + case valid_sol::SIMPLE_FREE_CELL: + tableau_pile_count = 3; + max_rank = 3; + cells = 1; + break; + case valid_sol::CANFIELD: + tableau_pile_count = 4; + build_pol = build_policy::RED_BLACK; + reserve_size = 14; + stock_size = 34; + break; + case valid_sol::SIMPLE_CANFIELD: + tableau_pile_count = 2; + build_pol = build_policy::RED_BLACK; + max_rank = 2; + reserve_size = 2; + stock_size = 2; + break; + default: + assert(false); + } +} + +sol_rules::valid_sol sol_rules::valid_sol_enum(const std::string& s) { + auto sol_type_it = + find(valid_sol_strs.begin(), valid_sol_strs.end(), s); + + assert(sol_type_it != valid_sol_strs.end()); + + long idx = sol_type_it - valid_sol_strs.begin(); + + return sol_rules::valid_sol(idx); +}
\ No newline at end of file diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h new file mode 100644 index 0000000..b73097d --- /dev/null +++ b/src/main/game/sol_rules.h @@ -0,0 +1,60 @@ +// +// Created by thecharlesblake on 10/28/17. +// + +#ifndef SOLVITAIRE_SOL_RULES_H +#define SOLVITAIRE_SOL_RULES_H + +#include <vector> +#include <string> + +class sol_rules { +public: + enum class valid_sol { + BLACK_HOLE, + SIMPLE_BLACK_HOLE, + SPANISH_PATIENCE, + SIMPLE_SPANISH_PATIENCE, + FREE_CELL, + SIMPLE_FREE_CELL, + CANFIELD, + SIMPLE_CANFIELD + }; + enum class build_order { + NO_BUILD, + DESCENDING, + ASCENDING, + BOTH, + SINGLE_CARD + }; + enum class build_policy { + CLUBS, + DIAMONDS, + HEARTS, + SPADES, + ANY_SUIT, + SAME_SUIT, + RED_BLACK + }; + + static std::vector<std::string> valid_sol_strs; + static bool is_suit(build_policy); + static int suit_val(build_policy); + + explicit sol_rules(std::string); + + unsigned int tableau_pile_count; + build_order build_ord; + build_policy build_pol; + unsigned int max_rank; + bool hole; + bool foundations; + unsigned int cells; + unsigned int reserve_size; + unsigned int stock_size; + +private: + static valid_sol valid_sol_enum(const std::string&); +}; + +#endif //SOLVITAIRE_SOL_RULES_H |
