diff options
Diffstat (limited to 'src/main/game')
| -rw-r--r-- | src/main/game/card.cpp | 27 | ||||
| -rw-r--r-- | src/main/game/card.h | 8 | ||||
| -rw-r--r-- | src/main/game/game_state.cpp | 336 | ||||
| -rw-r--r-- | src/main/game/game_state.h | 63 | ||||
| -rw-r--r-- | src/main/game/game_state.legal_moves.cpp | 276 | ||||
| -rw-r--r-- | src/main/game/pile.cpp | 118 | ||||
| -rw-r--r-- | src/main/game/pile.h | 41 | ||||
| -rw-r--r-- | src/main/game/sol_rules.cpp | 181 | ||||
| -rw-r--r-- | src/main/game/sol_rules.h | 60 |
9 files changed, 535 insertions, 575 deletions
diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 4ea9991..4d9342d 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -55,25 +55,22 @@ card::rank_t card::get_rank() const { return rank; } +card::suit_t card::to_suit(uint8_t v) { + switch (v) { + case 0: return suit_t::Clubs; + case 1: return suit_t::Diamonds; + case 2: return suit_t::Hearts; + case 3: return suit_t::Spades; + default: assert(false); return suit_t::Clubs; + } +} + bool operator==(const card& l, const card& r) { return l.get_rank() == r.get_rank() && l.get_suit() == r.get_suit(); } -std::ostream & operator<<(std::ostream & s, card const & 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; - } - return s; +bool operator!=(const card& l, const card& r) { + return !(l == r); } size_t hash_value(card const& c) { diff --git a/src/main/game/card.h b/src/main/game/card.h index cc5a993..4cab774 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -10,14 +10,18 @@ class card { public: + // Types & typedefs enum class suit_t {Clubs, Diamonds, Hearts, Spades}; enum class colour_t {Black, Red}; typedef uint8_t rank_t; + // Converts a number (0-3) to a suit (Clubs-Spades) + static suit_t to_suit(uint8_t); + // A card can either be created using two int values (rank & suit), or // using a string (e.g. "AS") card(suit_t, rank_t); - card(const char*); + explicit card(const char*); suit_t get_suit() const; colour_t get_colour() const; @@ -31,8 +35,8 @@ private: rank_t rank : 4; }; -std::ostream & operator<<(std::ostream &, card const &); bool operator==(const card&, const card&); +bool operator!=(const card&, const card&); std::size_t hash_value(card const&); diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index caabb18..76e6488 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -12,30 +12,40 @@ #include <boost/functional/hash.hpp> #include "game_state.h" -#include "../input-output/deal_parser.h" +#include "../input-output/input/json-parsing/deal_parser.h" +#include "../input-output/output/state_printer.h" +#include "sol_rules.h" using namespace std; using namespace rapidjson; using namespace boost; -typedef sol_rules::build_order ord; typedef sol_rules::build_policy pol; +typedef sol_rules::stock_deal_type sdt; + + +////////////////// +// CONSTRUCTORS // +////////////////// // A private constructor used by both of the public ones. Initializes all of the // piles and pile refs specified by the rules game_state::game_state(const sol_rules& s_rules) : - rules(s_rules) { + rules(s_rules), + stock(PILE_REF_MAX), + waste(PILE_REF_MAX), + hole(PILE_REF_MAX) { - // Creates the tableau piles - for (uint8_t i = 0; i < rules.tableau_pile_count; i++) { - piles.push_back(pile::tableau_factory(rules.build_ord, rules.build_pol)); - tableau_piles.push_back(static_cast<pile_ref>(piles.size() - 1)); + // If there is a hole, creates pile + if (rules.hole) { + piles.emplace_back(); + hole = static_cast<pile_ref>(piles.size() - 1); } // If there are foundation piles, creates the relevant pile vectors if (rules.foundations) { - for (uint8_t i = 0; i < 4; i++) { - piles.push_back(pile::foundation_factory(pol(i))); + for (uint8_t i = 0; i < 4*(rules.two_decks ? 2:1); i++) { + piles.emplace_back(); foundations.push_back(static_cast<pile_ref>(piles.size() - 1)); } } @@ -43,40 +53,38 @@ game_state::game_state(const sol_rules& s_rules) : // If there are cell piles, creates the relevant cell vectors if (rules.cells > 0) { for (uint8_t i = 0; i < rules.cells; i++) { - piles.push_back(pile::cell_factory()); + piles.emplace_back(); cells.push_back(static_cast<pile_ref>(piles.size() - 1)); } } - // If there is a reserve, creates a reserve pile - if (rules.reserve_size > 0) { - piles.push_back(pile::reserve_factory()); - reserve = static_cast<pile_ref>(piles.size() - 1); - } - - // If there is a stock & waste, creates piles + // If there is a stock, creates a pile if (rules.stock_size > 0) { - piles.push_back(pile::stock_factory()); + piles.emplace_back(); stock = static_cast<pile_ref>(piles.size() - 1); - piles.push_back(pile::waste_factory()); - waste = static_cast<pile_ref>(piles.size() - 1); - } - if (rules.hole) { - piles.push_back(pile::hole_factory(s_rules.max_rank)); - hole = static_cast<pile_ref>(piles.size() - 1); + // If the stock deals to a waste, create the waste + if (rules.stock_deal_t == sdt::WASTE) { + piles.emplace_back(); + waste = static_cast<pile_ref>(piles.size() - 1); + } } - for (pile_ref pr = 0; pr < piles.size(); pr++) { - if (rules.stock_size > 0 && pr == waste) continue; - - if (piles[pr].can_remove()) { - removable_piles.emplace_back(pr); - } - if (piles[pr].get_build_order() != ord::NO_BUILD) { - addable_piles.emplace_back(pr); + // If there is a reserve, creates piles + if (rules.reserve_size > 0) { + uint8_t pile_count = rules.reserve_stacked ? + uint8_t(1) : rules.reserve_size; + for (uint8_t i = 0; i < pile_count; i++) { + piles.emplace_back(); + reserve.push_back(static_cast<pile_ref>(piles.size() - 1)); } } + + // Creates the tableau piles + for (uint8_t i = 0; i < rules.tableau_pile_count; i++) { + piles.emplace_back(); + tableau_piles.push_back(static_cast<pile_ref>(piles.size() - 1)); + } } // Constructs an initial game state from a JSON doc @@ -88,8 +96,7 @@ game_state::game_state(const sol_rules& s_rules, const Document& doc) : // Constructs an initial game state from a seed game_state::game_state(const sol_rules& s_rules, int seed) : game_state(s_rules) { - - vector<card> deck = gen_shuffled_deck(seed, rules.max_rank); + vector<card> deck = gen_shuffled_deck(seed, rules.max_rank, rules.two_decks); // If there is a hole, moves the ace of spades to it if (rules.hole) { @@ -97,6 +104,16 @@ game_state::game_state(const sol_rules& s_rules, int seed) : piles[hole].place(card("AS")); } + // If the foundations begin filled, then fills them + if (rules.foundations_init_card) { + for (uint8_t f_idx = 0; f_idx < 4*(rules.two_decks ? 2:1); f_idx++) { + card c = card(card::to_suit(f_idx % uint8_t(4)), 1); + + deck.erase(find(begin(deck), end(deck), c)); + piles[foundations[f_idx]].place(c); + } + } + // If there is a stock, deals to it and set up a waste pile too if (rules.stock_size > 0) { for (unsigned int i = 0; i < rules.stock_size; i++) { @@ -105,10 +122,13 @@ game_state::game_state(const sol_rules& s_rules, int seed) : } } - // If there is a reserve, deals to it + // If there is a reserve, deals to it. + // We treat a regular reserve like multiple single-card piles, + // but a stacked reserve as a single multiple-card pile. if (rules.reserve_size > 0) { for (unsigned int i = 0; i < rules.reserve_size; i++) { - piles[reserve].place(deck.back()); + int idx = rules.reserve_stacked ? 0 : i; + piles[reserve[idx]].place(deck.back()); deck.pop_back(); } } @@ -116,23 +136,40 @@ game_state::game_state(const sol_rules& s_rules, int seed) : // Deals to the tableau piles (row-by-row) for (int t = 0; !deck.empty(); t++) { card c = deck.back(); - deck.pop_back(); // Add the randomly generated card to the tableau piles - pile_ref tableau_pile = tableau_piles[t % tableau_piles.size()]; - piles[tableau_pile].place(c); + auto p = t % tableau_piles.size(); + + // If we are doing a diagonal deal, each row should have one fewer card. + // Leftover cards are dealt normally in full rows. + auto row_idx = t / tableau_piles.size(); + if (rules.diagonal_deal && row_idx < tableau_piles.size()) { + p = tableau_piles.size()-p-1; + pile_ref tableau_pile = tableau_piles[p]; + + if (p >= row_idx) { + piles[tableau_pile].place(c); + deck.pop_back(); + } + } else { + pile_ref tableau_pile = tableau_piles[p]; + piles[tableau_pile].place(c); + deck.pop_back(); + } } } // Generates a randomly ordered vector of cards -vector<card> game_state::gen_shuffled_deck(int seed, int max_rank = 13) { +vector<card> game_state::gen_shuffled_deck(int seed, int max_rank, + bool two_decks) { vector<int> values; vector<int*> v_ptrs; for (int i = 0; i < max_rank * 4; i++) { values.emplace_back(i); + if (two_decks) values.emplace_back(i); } - for (int i = 0; i < max_rank * 4; i++) { - v_ptrs.emplace_back(&values[i]); + for (auto& v : values) { + v_ptrs.emplace_back(&v); } // Randomly shuffle the pointers @@ -145,16 +182,16 @@ vector<card> game_state::gen_shuffled_deck(int seed, int max_rank = 13) { card::suit_t s; switch ((*i) / max_rank) { case 0 : s = card::suit_t::Clubs; - break; + break; case 1 : s = card::suit_t::Diamonds; - break; + break; case 2 : s = card::suit_t::Hearts; - break; + break; case 3 : s = card::suit_t::Spades; - break; + break; default : assert(false); - s = card::suit_t::Clubs; - break; + s = card::suit_t::Clubs; + break; } deck.emplace_back(card(s, r)); @@ -163,156 +200,111 @@ vector<card> game_state::gen_shuffled_deck(int seed, int max_rank = 13) { return deck; } -void game_state::make_move(const move m) { - piles[m.second].place(piles[m.first].take()); +game_state::move::move(pile_ref f, pile_ref t, pile::size_type i) + : from(f), to(t), count(i) { + assert(i >= 1); } -void game_state::undo_move(const move m) { - piles[m.first].place(piles[m.second].take()); -} -vector<game_state::move> game_state::get_legal_moves() const { - // The next legal states - vector<move> moves; +///////////////////////// +// MODIFYING FUNCTIONS // +///////////////////////// - // Dealing from the stock to the waste - if (rules.stock_size > 0 && !piles[stock].empty()) { - moves.emplace_back(stock, waste); - } - - // Moving from and to the 'can remove' and 'can add' piles - // Note traversal order. Solver evaluates backwards, so we reverse traversal - // order of removable piles, but not addable. This moves from tableau piles - // to foundations first if possible - for (auto rem_it = removable_piles.rbegin(); rem_it != removable_piles.rend(); ++rem_it) { - if (piles[*rem_it].empty()) continue; - - for (const pile_ref add_pile : addable_piles) { - if (add_pile != *rem_it && - piles[add_pile].can_place(piles[*rem_it].top_card())) { - moves.emplace_back(*rem_it, add_pile); - } +void game_state::make_move(const move m) { + // Handles special stock-to-tableau-piles move + if (rules.stock_deal_t == sdt::TABLEAU_PILES && m.from == stock) { + for (pile_ref tab_pr = tableau_piles.front(); + tab_pr < tableau_piles.front() + m.count; + tab_pr++) { + piles[tab_pr].place(piles[stock].take()); } } - - return moves; -} - -bool game_state::is_solved() const { - for (auto f : foundations) { - if (piles[f].size() != rules.max_rank) { - return false; + // If this is not a built-pile move + else if (m.count == 1) { + piles[m.to].place(piles[m.from].take()); + } else { + // Adds the cards to the 'to' pile + for (auto pile_idx = m.count; pile_idx-- > 0;) { + piles[m.to].place(piles[m.from][pile_idx]); } - } - - return !rules.hole || piles[hole].size() == rules.max_rank * 4; -} -const vector<pile>& game_state::get_data() const { - 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); + // Removes the cards from the 'from' pile + for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) { + piles[m.from].take(); + } } - return stream << "==================================="; } -void game_state::print_header(ostream& stream, const char* header) const { - stream << "--- " << header << " "; - size_t pad = 20 - strlen(header); - for (size_t i = 0; i < pad; i++) { - stream << '-'; +void game_state::undo_move(const move m) { + // Handles special stock-to-tableau-piles move + if (rules.stock_deal_t == sdt::TABLEAU_PILES && m.from == stock) { + for (pile_ref tab_pr = tableau_piles.front() + m.count; + tab_pr-- > tableau_piles.front(); + ) { + piles[stock].place(piles[tab_pr].take()); + } } - stream << "\n"; -} - -void game_state::print_piles(ostream& stream, const vector<pile_ref>& pile_rs) 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 through the current row to determine if it is empty - empty_row = true; - for (const pile_ref pile_r : pile_rs) { - if (piles[pile_r].size() > row_idx) { - empty_row = false; - break; - } + // If this is not a built-pile move + else if (m.count == 1) { + piles[m.from].place(piles[m.to].take()); + } else { + // Adds the cards to the 'from' pile + for (auto pile_idx = m.count; pile_idx-- > 0;) { + piles[m.from].place(piles[m.to][pile_idx]); } - if (!empty_row || row_idx == 0) { - // Loops through the current (non-empty) row, and outputs the values - for (const pile_ref pile_r : pile_rs) { - if (piles[pile_r].size() > row_idx) { - stream << piles[pile_r][row_idx]; - } else if (row_idx == 0) { - stream << "[]"; - } - stream << "\t"; - } - stream << "\n"; - row_idx++; + // Removes the cards from the 'to' pile + for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) { + piles[m.to].take(); } } } -void game_state::print_pile(ostream& stream, const pile_ref pile_r) const { - print_piles(stream, {pile_r}); -} -void game_state::print_top_of_piles(ostream& stream, const vector<pile_ref>& vp) const { - vector<pile_ref> top(vp); +//////////////////////// +// INSPECT GAME STATE // +//////////////////////// - for (pile_ref p : top) { - if (piles[p].empty()) { - stream << "[]"; - } else { - stream << piles[p].top_card(); +bool game_state::is_solved() const { + if (rules.hole) { + return piles[hole].size() + == rules.max_rank * 4 * (rules.two_decks ? 2 : 1); + } else { + for (auto f : foundations) { + if (piles[f].size() != rules.max_rank) { + return false; + } } - stream << "\t"; + return true; } - stream << "\n"; } -void game_state::print_top_of_pile(ostream& stream, const pile_ref pile_r) const { - print_top_of_piles(stream, {pile_r}); +const vector<pile>& game_state::get_data() const { + return piles; } 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; + && a.foundations == b.foundations + && a.hole == b.hole; } -ostream& operator <<(ostream& stream, const game_state& gs) { - return gs.print(stream); + +////////////////////////////////// +// STATIC VARIABLES AND MEMBERS // +////////////////////////////////// + +game_state::pile_ref game_state::PILE_REF_MAX = 255; + +game_state::move game_state::null_move() { + return {PILE_REF_MAX, 0, PILE_REF_MAX}; } + +//////////////////// +// HASH FUNCTIONS // +//////////////////// + size_t hash_value(game_state const& gs) { size_t seed = 0; @@ -326,9 +318,6 @@ size_t hash_value(game_state const& gs) { hash_combine(seed, gs.tableau_piles); } - if (gs.rules.reserve_size > 0) { - hash_combine(seed, gs.piles[gs.reserve]); - } if (gs.rules.stock_size > 0) { hash_combine(seed, gs.piles[gs.stock]); hash_combine(seed, gs.piles[gs.waste]); @@ -343,3 +332,12 @@ size_t hash_value(game_state const& gs) { size_t hash_value(vector<pile> const& vp) { return hash_range(begin(vp), end(vp)); } + + +/////////// +// PRINT // +/////////// + +ostream& operator<< (ostream& str, const game_state& gs) { + return state_printer::print(str, gs); +} diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 8adc9d6..8f7bf57 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -5,68 +5,85 @@ #ifndef SOLVITAIRE_GAME_STATE_H #define SOLVITAIRE_GAME_STATE_H -#define NULL_MOVE (game_state::move(255, 255)) - #include <vector> -#include <ostream> #include <string> #include <rapidjson/document.h> #include <boost/functional/hash.hpp> #include "card.h" -#include "sol_rules.h" #include "pile.h" +#include "sol_rules.h" class deal_parser; class game_state { friend class deal_parser; + friend class state_printer; public: + // Defines types typedef uint8_t pile_ref; - typedef std::pair<pile_ref, pile_ref> move; + struct move { + move(pile_ref, pile_ref, pile::size_type = 1); + pile_ref from; pile_ref to; pile::size_type count; + }; - // Create a game state representation from a JSON doc + // Constructors + // Creates a game state representation from a JSON doc game_state(const sol_rules&, const rapidjson::Document&); - // Do the same from a 'rules' object + // Does the same from a 'rules' object game_state(const sol_rules&, int seed); + // Alters state void make_move(move); void undo_move(move); + std::vector<move> get_legal_moves() const; + + // Inspects state bool is_solved() const; const std::vector<pile>& get_data() 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&); + + // Static pile ref members/vars + static pile_ref PILE_REF_MAX; + static move null_move(); + friend std::size_t hash_value(game_state const&); friend std::size_t hash_value(std::vector<pile> const&); + friend std::ostream& operator<< (std::ostream&, const game_state&); + private: - static std::vector<card> gen_shuffled_deck(int, int); + static std::vector<card> gen_shuffled_deck(int, int, bool); + // Private constructor explicit game_state(const sol_rules&); - void print_header(std::ostream&, const char*) const; - void print_piles(std::ostream&, const std::vector<pile_ref>&) const; - void print_pile(std::ostream&, pile_ref) const; - void print_top_of_piles(std::ostream&, const std::vector<pile_ref>&) const; - void print_top_of_pile(std::ostream&, pile_ref) const; - + // Used in get_legal_moves() + move get_stock_tableau_move() const; + bool is_valid_tableau_move(pile_ref, pile_ref) const; + bool is_valid_foundations_move(pile_ref, pile_ref) const; + bool is_valid_hole_move(pile_ref) const; + void get_built_group_moves(std::vector<move>&) const; + pile::size_type get_built_group_height(pile_ref) const; + bool valid_built_group_move(card, card, card) const; + void add_built_group_move(std::vector<move>&, pile_ref, pile_ref) const; + void add_empty_built_group_moves(std::vector<move>&, pile_ref, pile_ref, + card) const; + + // References to piles sol_rules rules; - std::vector<pile_ref> foundations; - std::vector<pile_ref> cells; std::vector<pile_ref> tableau_piles; - pile_ref reserve; + std::vector<pile_ref> cells; pile_ref stock; pile_ref waste; + std::vector<pile_ref> reserve; + std::vector<pile_ref> foundations; pile_ref hole; - std::vector<pile_ref> addable_piles; - std::vector<pile_ref> removable_piles; - + // The core piles std::vector<pile> piles; }; diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp new file mode 100644 index 0000000..cbc052a --- /dev/null +++ b/src/main/game/game_state.legal_moves.cpp @@ -0,0 +1,276 @@ +// +// Created by thecharlesblake on 1/3/18. +// + +#include "game_state.h" + +using namespace std; + +typedef sol_rules::build_order ord; +typedef sol_rules::build_policy pol; +typedef sol_rules::spaces_policy s_pol; +typedef sol_rules::stock_deal_type sdt; + +vector<game_state::move> game_state::get_legal_moves() const { + // The next legal moves + vector<move> moves; + + // If we have a stock that can deal to the tableau piles, add this move + if (rules.stock_deal_t == sdt::TABLEAU_PILES && !piles[stock].empty()) { + moves.emplace_back(get_stock_tableau_move()); + } + + // Cycles through each pile which we may be able to remove a card from + for (pile_ref rem_ref = 0; rem_ref < piles.size(); rem_ref++) { + // Never removes a card from the hole, the waste, or an empty pile + if (rem_ref == hole || piles[rem_ref].empty()) continue; + + // Stock cards can only be moved to the waste (assuming there is a waste) + if (stock != PILE_REF_MAX && rem_ref == stock) { + if (waste != PILE_REF_MAX) { + moves.emplace_back(stock, waste); + } + continue; + } + + // For each tableau pile, check if moving from the rem pile is a valid + // tableau move according to the rules + for (auto add_ref : tableau_piles) { + if (is_valid_tableau_move(rem_ref, add_ref)) { + moves.emplace_back(rem_ref, add_ref); + } + } + + // Any card can be moved to a cell, so long as the cell isn't full + for (auto add_ref : cells) { + if (add_ref != rem_ref && piles[add_ref].empty()) { + moves.emplace_back(rem_ref, add_ref); + } + } + + // For each foundation pile, check if moving from the rem pile is a valid + // move according to the rules + for (auto add_ref : foundations) { + if (is_valid_foundations_move(rem_ref, add_ref)) { + moves.emplace_back(rem_ref, add_ref); + } + } + + // Does the same for the hole + if (hole != PILE_REF_MAX && is_valid_hole_move(rem_ref)) { + moves.emplace_back(rem_ref, hole); + } + + // Note that the stock and the reserve aren't mentioned here, as + // they cannot be added to + } + + if (rules.move_built_group) { + get_built_group_moves(moves); + } + + return moves; +} + +// This is a special kind of move which the solver handles differently. Hence +// the arbitrary second param. We must supply the number of stock cards +// that we will deal, so that the move can be undone in backtracking +game_state::move game_state::get_stock_tableau_move() const { + pile::size_type stock_moves = + piles[stock].size() >= tableau_piles.size() + ? pile::size_type(tableau_piles.size()) + : piles[stock].size(); + return {stock, PILE_REF_MAX, stock_moves}; +} + +// If a tableau pile is empty and the space policy is +// no-build, do not add. Otherwise any card with the right +// suit/rank can be moved to a tableau +bool game_state::is_valid_tableau_move(const pile_ref rem_ref, + const pile_ref add_ref) const { + if (rem_ref == add_ref) return false; + + else if (rules.build_pol == pol::NO_BUILD) return false; + + else if (piles[add_ref].empty()) { + return rules.spaces_pol == s_pol::ANY; + } + + card rem_c = piles[rem_ref].top_card(); + card add_c = piles[add_ref].top_card(); + + // Checks violation of same suit policy + if (rules.build_pol == pol::SAME_SUIT + && rem_c.get_suit() != add_c.get_suit()) { + return false; + } + + // Checks violation of red-black suit policy + else if (rules.build_pol == pol::RED_BLACK + && rem_c.get_colour() == add_c.get_colour()) { + return false; + } + + // Checks rank + else if (rules.build_ord == ord::DESCENDING + && rem_c.get_rank() + 1 != add_c.get_rank()) { + return false; + } else if (rules.build_ord == ord::ASCENDING + && rem_c.get_rank() - 1 != add_c.get_rank()) { + return false; + } else { + return true; + } +} + +bool game_state::is_valid_foundations_move(const pile_ref rem_ref, + const pile_ref add_ref) const { + if (rem_ref == add_ref) return false; + + card rem_c = piles[rem_ref].top_card(); + + // Checks violation of same suit policy + uint8_t suit_idx = (add_ref - foundations.front()) % uint8_t(4); + if (rem_c.get_suit() != card::to_suit(suit_idx)) { + return false; + } + + // Checks rank + else { + card::rank_t add_rank = piles[add_ref].empty() + ? card::rank_t(0) + : piles[add_ref].top_card().get_rank(); + return rem_c.get_rank() - 1 == add_rank; + } +} + +bool game_state::is_valid_hole_move(const pile_ref rem_ref) const { + if (rem_ref == hole) return false; + card::rank_t rank = piles[rem_ref].top_card().get_rank(); + card::rank_t hole_rank = piles[hole].top_card().get_rank(); + return rank + 1 == hole_rank + || rank - 1 == hole_rank + || (rank == rules.max_rank && hole_rank == 1) + || (rank == 1 && hole_rank == rules.max_rank); +} + + +/////////////////////// +// BUILT-GROUP MOVES // +/////////////////////// + +void game_state::get_built_group_moves(vector<move>& moves) const { + assert(rules.build_pol != pol::NO_BUILD); + + // Cycles through each pile to see if it contains a built group of size > 1 + for (auto rem_ref : tableau_piles) { + if (piles[rem_ref].size() < 2) continue; + + auto built_group_height = get_built_group_height(rem_ref); + if (built_group_height == 1) continue; + + // We have found a built group, so cycles through each pile to see if + // this group can be added to it + for (auto add_ref : tableau_piles) { + if (add_ref == rem_ref) continue; + + card bg_low = piles[rem_ref].top_card(); + card bg_high = piles[rem_ref][built_group_height - 1]; + + // If the pile is empty, and we can move to empty piles, does so + if (piles[add_ref].empty()) { + if (rules.spaces_pol == s_pol::ANY) { + add_empty_built_group_moves(moves, rem_ref, add_ref, bg_high); + } + } + // Otherwise, checks to see if it is a valid move + else { + card add_card = piles[add_ref].top_card(); + + // Adds the move to the list of moves, as a special built group move + if (valid_built_group_move(bg_low, bg_high, add_card)) { + add_built_group_move(moves, rem_ref, add_ref); + } + } + } + } +} + +// Finds the size of the built group at the top of a pile +pile::size_type game_state::get_built_group_height(pile_ref ref) const { + + // The index of the currently examined card, the current card, previous card + // and whether we currently have a valid built group. + pile::size_type group_card_idx = 0; + bool built_group = true; + + do { + card prev_c = piles[ref][group_card_idx++]; + card c = piles[ref][group_card_idx]; + + // If the build policy is same suit, makes sure group has same suit + if (rules.build_pol == sol_rules::build_policy::SAME_SUIT + && c.get_suit() != prev_c.get_suit()) { + built_group = false; + } + // If the build policy is red black, makes sure group alternates + // in correct phase + else if (rules.build_pol == sol_rules::build_policy::RED_BLACK + && c.get_colour() == prev_c.get_colour()) { + built_group = false; + } + + // Makes sure the rank of the cards is sequential + built_group = built_group + && c.get_rank() == prev_c.get_rank()+1; + + } + // Continues until group_card_idx points to a card outside the built + // Group + while (built_group && group_card_idx < piles[ref].size()); + + return group_card_idx; +} + +bool game_state::valid_built_group_move(card bg_low, card bg_high, card add_card) +const { + // The rank of the card we're moving the built group to must be greater than + // the second-from-top card of the built group, and less than or equal to the + // card that would come next at the top of the built pile + if (add_card.get_rank() <= bg_low.get_rank() + 1 + || add_card.get_rank() > bg_high.get_rank() + 1) { + return false; + } + + // Checks to see if the suits are correct + switch (rules.build_pol) { + case sol_rules::build_policy::SAME_SUIT: + return bg_low.get_suit() == add_card.get_suit(); + + case sol_rules::build_policy::RED_BLACK: { + bool same_suit = bg_low.get_suit() == add_card.get_suit(); + bool even_diff = (add_card.get_rank() - bg_low.get_rank()) % 2 == 0; + return (same_suit && even_diff) || (!same_suit && !even_diff); + } + default: return true; + } +} + +void game_state::add_built_group_move(vector<move>& moves, pile_ref rem_ref, + pile_ref add_ref) const { + moves.emplace_back(rem_ref, add_ref, 2); +} + +void game_state::add_empty_built_group_moves(vector<move>& moves, + pile_ref rem_ref, + pile_ref add_ref, + card bg_high) const { + // Loops through each possible built group move to an empty pile and adds it + // to the list + pile::size_type card_idx = 1; + do { + moves.emplace_back( + rem_ref, add_ref, static_cast<pile_ref>(card_idx + 1) + ); + } while (piles[rem_ref][card_idx++] != bg_high); +} diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp index 641174e..264bb49 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -12,129 +12,27 @@ using namespace std; using namespace boost; -typedef sol_rules::build_order ord; -typedef sol_rules::build_policy pol; - -//////////// -// Static // -//////////// - -pile pile::tableau_factory(ord o, pol p) { - return pile(o, p); -} -pile pile::cell_factory() { - return pile(ord::SINGLE_CARD, pol::N_A); -} -pile pile::reserve_factory() { - return pile(ord::NO_BUILD, pol::N_A); -} -pile pile::stock_factory() { - return pile(ord::NO_BUILD, pol::N_A); -} -pile pile::waste_factory() { - return pile(ord::NO_BUILD, pol::N_A); -} -pile pile::foundation_factory(pol p) { - return pile(ord::ASCENDING, p); -} -pile pile::hole_factory(int max_rank) { - return pile(ord::BOTH, pol::ANY_SUIT, false, true, max_rank); -} - -//////////////// -// Non-static // -//////////////// - -pile::pile(ord bo, pol bp, bool r, bool l, int mr) : - build_order(bo), - build_policy(bp), - removable(r), - build_order_loops(l), - max_rank(mr) { -} - -bool pile::can_place(const card c) const { - if (build_order == ord::ANY) { - return true; - } else 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() == sol_rules::suit_val(build_policy) - && c.get_rank() == 1; - } else { - return true; - } - } - - card top_c = top_card(); - - // Checks violation of same suit policy - if (build_policy == pol::SAME_SUIT && c.get_suit() != top_c.get_suit()) { - return false; - } - // Checks violation of specific suit policy - if (sol_rules::is_suit(build_policy) && c.get_suit() - != sol_rules::suit_val(build_policy)) { - return false; - } - // Checks violation of red-black suit policy - if (build_policy == pol::RED_BLACK && c.get_colour() == top_c.get_colour()) { - 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 { + assert(!empty()); return pile_vec.back(); } -bool pile::can_remove() const { - return removable; -} - bool pile::empty() const { return pile_vec.empty(); } -uint8_t pile::size() const { +pile::size_type pile::size() const { return static_cast<uint8_t>(pile_vec.size()); } -ord pile::get_build_order() const { - return build_order; +card& pile::operator[] (size_type i) { + return pile_vec[pile_vec.size() - 1 - i]; } -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]; +card pile::operator[] (size_type i) const { + return pile_vec[pile_vec.size() - 1 - i]; } - void pile::place(const card c) { pile_vec.emplace_back(c); } @@ -145,10 +43,6 @@ card pile::take() { 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 index 978e32e..f2600fe 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -6,58 +6,31 @@ #define SOLVITAIRE_PILE_H #include <vector> +#include <iterator> #include "sol_rules.h" #include "card.h" class pile { public: - // Factory methods - static pile tableau_factory(sol_rules::build_order, sol_rules::build_policy); - static pile cell_factory(); - static pile reserve_factory(); - static pile stock_factory(); - static pile waste_factory(); - static pile foundation_factory(sol_rules::build_policy); - static pile hole_factory(int); - - // Constructors - pile(sol_rules::build_order bo = sol_rules::build_order::DESCENDING, - sol_rules::build_policy bp = sol_rules::build_policy::ANY_SUIT, - bool removable = true, - bool loops = false, - int max_rank = 13); + typedef uint8_t size_type; // Inspect - bool can_place(const card) const; - bool can_remove() const; card top_card() const; bool empty() const; - uint8_t size() const; - sol_rules::build_order get_build_order() const; + size_type size() const; // Modify - void place(const card); + void place(card); card take(); - void clear(); - // Index like array - card& operator[] (std::vector<card>::size_type); - card operator[] (std::vector<card>::size_type) const; + // Indexes the vector, with the top card as index 0 + card& operator[] (size_type); + card operator[] (size_type) const; friend bool operator==(const pile&, const pile&); friend std::size_t hash_value(pile const&); - private: - bool one_lt(int, int) const; - - // const pile properties - const sol_rules::build_order build_order; - const sol_rules::build_policy build_policy; - const bool removable; - const bool build_order_loops; - const int max_rank; - // Underlying vector std::vector<card> pile_vec; }; diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp deleted file mode 100644 index 353c8db..0000000 --- a/src/main/game/sol_rules.cpp +++ /dev/null @@ -1,181 +0,0 @@ -// -// Created by thecharlesblake on 10/28/17. -// - -#include <string> -#include <vector> -#include <algorithm> - -#include "rapidjson/error/en.h" - -#include "sol_rules.h" -#include "../util/util.h" -#include "../input-output/sol_preset_types.h" - -using namespace std; -using namespace boost; -using namespace rapidjson; - -//////////// -// Static // -//////////// - -typedef sol_rules::build_policy pol; - -bool sol_rules::is_suit(pol bp) { - switch (bp) { - case pol::CLUBS : - case pol::DIAMONDS : - case pol::HEARTS : - case pol::SPADES : return true; - default : return false; - } -} -card::suit_t sol_rules::suit_val(pol bp) { - switch (bp) { - case pol::CLUBS : return card::suit_t::Clubs; - case pol::DIAMONDS : return card::suit_t::Diamonds; - case pol::HEARTS : return card::suit_t::Hearts; - case pol::SPADES : return card::suit_t::Spades; - default: assert(false); return card::suit_t::Spades; - } -} - -const sol_rules sol_rules::from_file(const string rules_file) { - sol_rules sr = get_default(); - Document d = util::get_file_json(rules_file); - - modify_sol_rules(sr, d); - - return sr; -} - -const sol_rules sol_rules::from_preset(const string sol_type) { - sol_rules sr = get_default(); - - const string preset_json = sol_preset_types::get(sol_type); - - Document d; - d.Parse(preset_json.c_str()); - assert(!d.HasParseError()); - - modify_sol_rules(sr, d); - return sr; -} - -sol_rules sol_rules::get_default() { - sol_rules sr; - const string default_json = sol_preset_types::get("default"); - - Document d; - d.Parse(default_json.c_str()); - assert(!d.HasParseError()); - - modify_sol_rules(sr, d); - return sr; -} - -void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { - - if (!d.IsObject()) { - util::json_parse_err("JSON doc must be object"); - } - - if (d.HasMember("tableau piles")) { - if (d["tableau piles"].IsObject()) { - - if (d["tableau piles"].HasMember("count")) { - if (d["tableau piles"]["count"].IsInt()) { - sr.tableau_pile_count = static_cast<uint8_t>(d["tableau piles"]["count"].GetInt()); - } else { - util::json_parse_err("[tableau piles][count] must be an integer"); - } - } - - if (d["tableau piles"].HasMember("build order")) { - if (d["tableau piles"]["build order"].IsString()) { - string bo_str = d["tableau piles"]["build order"].GetString(); - - if (bo_str == "no-build") { - sr.build_ord = build_order::NO_BUILD; - } else if (bo_str == "descending") { - sr.build_ord = build_order::DESCENDING; - } else { - util::json_parse_err("[tableau piles][build order] is invalid"); - } - - } else { - util::json_parse_err("[tableau piles][build order] must be an string"); - } - } - - if (d["tableau piles"].HasMember("build policy")) { - if (d["tableau piles"]["build policy"].IsString()) { - string bo_str = d["tableau piles"]["build policy"].GetString(); - - if (bo_str == "any-suit") { - sr.build_pol = build_policy::ANY_SUIT; - } else if (bo_str == "red-black") { - sr.build_pol = build_policy::RED_BLACK; - } else { - util::json_parse_err("[tableau piles][build policy] is invalid"); - } - - } else { - util::json_parse_err("[tableau piles][build policy] must be an string"); - } - } - - } else { - util::json_parse_err("[tableau piles] must be an object"); - } - } - - if (d.HasMember("max rank")) { - if (d["max rank"].IsInt()) { - sr.max_rank = static_cast<card::rank_t>(d["max rank"].GetInt()); - } else { - util::json_parse_err("[max rank] must be an integer"); - } - } - - if (d.HasMember("hole")) { - if (d["hole"].IsBool()) { - sr.hole = d["hole"].GetBool(); - } else { - util::json_parse_err("[hole] must be a boolean"); - } - } - - if (d.HasMember("foundations")) { - if (d["foundations"].IsBool()) { - sr.foundations = d["foundations"].GetBool(); - } else { - util::json_parse_err("[foundations] must be a boolean"); - } - } - - if (d.HasMember("cells")) { - if (d["cells"].IsInt()) { - sr.cells = static_cast<uint8_t>(d["cells"].GetInt()); - } else { - util::json_parse_err("[cells] must be an integer"); - } - } - - if (d.HasMember("reserve size")) { - if (d["reserve size"].IsInt()) { - sr.reserve_size = static_cast<uint8_t>(d["reserve size"].GetInt()); - } else { - util::json_parse_err("[reserve size] must be an integer"); - } - } - - if (d.HasMember("stock size")) { - if (d["stock size"].IsInt()) { - sr.stock_size = static_cast<uint8_t>(d["stock size"].GetInt()); - } else { - util::json_parse_err("[stock size] must be an integer"); - } - } -} diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index a2f8fe9..87266ee 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -5,67 +5,49 @@ #ifndef SOLVITAIRE_SOL_RULES_H #define SOLVITAIRE_SOL_RULES_H -#include <vector> #include <string> -#include <boost/optional.hpp> #include <rapidjson/document.h> #include "card.h" -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 - }; +struct sol_rules { + // Tableau policies enum class build_order { - NO_BUILD, DESCENDING, - ASCENDING, - BOTH, - SINGLE_CARD, - ANY + ASCENDING }; enum class build_policy { - CLUBS, - DIAMONDS, - HEARTS, - SPADES, - ANY_SUIT, + NO_BUILD, SAME_SUIT, RED_BLACK, - N_A + ANY_SUIT + }; + enum class spaces_policy { + NO_BUILD, + ANY + }; + enum class stock_deal_type { + WASTE, + TABLEAU_PILES }; - - static bool is_suit(build_policy); - static card::suit_t suit_val(build_policy); - - static const sol_rules from_preset(const std::string); - static const sol_rules from_file(const std::string); uint8_t tableau_pile_count; build_order build_ord; build_policy build_pol; + spaces_policy spaces_pol; + bool move_built_group; + bool two_decks; card::rank_t max_rank; bool hole; bool foundations; + bool foundations_init_card; + bool diagonal_deal; uint8_t cells; - uint8_t reserve_size; uint8_t stock_size; - -private: - static sol_rules get_default(); - static void modify_sol_rules(sol_rules&, rapidjson::Document&); - static void parse_err(const std::string&); - - static valid_sol valid_sol_enum(const std::string&); + stock_deal_type stock_deal_t; + uint8_t reserve_size; + bool reserve_stacked; }; #endif //SOLVITAIRE_SOL_RULES_H |
