aboutsummaryrefslogtreecommitdiff
path: root/solvitaire/src/main/game
diff options
context:
space:
mode:
Diffstat (limited to 'solvitaire/src/main/game')
m---------solvitaire0
-rw-r--r--solvitaire/src/main/game/card.cpp152
-rw-r--r--solvitaire/src/main/game/card.h66
-rw-r--r--solvitaire/src/main/game/global_cache.cpp276
-rw-r--r--solvitaire/src/main/game/global_cache.h76
-rw-r--r--solvitaire/src/main/game/move.cpp25
-rw-r--r--solvitaire/src/main/game/move.h28
-rw-r--r--solvitaire/src/main/game/pile.cpp92
-rw-r--r--solvitaire/src/main/game/pile.h53
-rw-r--r--solvitaire/src/main/game/search-state/game_state.cpp764
-rw-r--r--solvitaire/src/main/game/search-state/game_state.dominance_moves.cpp192
-rw-r--r--solvitaire/src/main/game/search-state/game_state.h182
-rw-r--r--solvitaire/src/main/game/search-state/game_state.legal_moves.cpp640
-rw-r--r--solvitaire/src/main/game/search-state/game_state.pile_order.cpp80
-rw-r--r--solvitaire/src/main/game/sol_rules.cpp43
-rw-r--r--solvitaire/src/main/game/sol_rules.h95
16 files changed, 0 insertions, 2764 deletions
diff --git a/solvitaire b/solvitaire
new file mode 160000
+Subproject b835e10bfb21a73b4f89e09ddeb400f41538483
diff --git a/solvitaire/src/main/game/card.cpp b/solvitaire/src/main/game/card.cpp
deleted file mode 100644
index 60aaa74..0000000
--- a/solvitaire/src/main/game/card.cpp
+++ /dev/null
@@ -1,152 +0,0 @@
-//
-// Created by thecharlesblake on 10/11/17.
-//
-
-#include <sstream>
-
-#include <boost/functional/hash.hpp>
-
-#include "card.h"
-
-using namespace std;
-using namespace boost;
-
-const card::suit_t card::suit::Clubs = 0;
-const card::suit_t card::suit::Hearts = 1;
-const card::suit_t card::suit::Spades = 2;
-const card::suit_t card::suit::Diamonds = 3;
-
-const card::colour_t card::colour::Black = 0;
-const card::colour_t card::colour::Red = 1;
-
-// A card can either be created using two int values (rank & suit), or
-// using a string (e.g. "AS")
-
-card::card(suit_t s, rank_t r, bool fd) : card_rank(r), card_suit(s), face_down(fd) {}
-
-card::card(const char* c, bool face_down_possible) :
- card_rank(rank_from_str(c)), card_suit(suit_from_str(c)),
- face_down(face_down_from_str(c, face_down_possible)) {}
-
-card::card() : card(0, 0, false) {}
-
-card::suit_t card::suit_from_str(const char* c) {
- switch(tolower(c[strlen(c) - 1])) {
- case 'c': return suit::Clubs;
- case 'h': return suit::Hearts;
- case 's': return suit::Spades;
- case 'd': return suit::Diamonds;
- default: assert(false);
- return suit::Clubs;
- }
-}
-
-card::rank_t card::rank_from_str(const char* c) {
- switch(tolower(c[0])) {
- case 'a': return 1;
- case 'j': return 11;
- case 'q': return 12;
- case 'k': return 13;
- default:
- assert(stoi(c) >= 1 && stoi(c) <= 13);
- return static_cast<rank_t>(stoi(c));
- }
-}
-
-bool card::face_down_from_str(const char *c, bool possible) {
- if (!possible) return false;
-
- switch(c[strlen(c) - 1]) {
- case 'c': return true;
- case 'h': return true;
- case 's': return true;
- case 'd': return true;
- default: return false;
- }
-}
-
-card::suit_t card::get_suit() const {
- return card_suit;
-}
-
-card::colour_t card::get_colour() const {
- switch (card_suit) {
- case card::suit::Spades :
- case card::suit::Clubs : return card::colour::Black;
- default : return card::colour::Red;
- }
-}
-
-card::rank_t card::get_rank() const {
- return card_rank;
-}
-
-std::string card::to_string() const {
- if (face_down) return "##";
-
- string s;
- switch(card_rank) {
- case 1 : s += "A"; break;
- case 11: s += "J"; break;
- case 12: s += "Q"; break;
- case 13: s += "K"; break;
- default: s += std::to_string(card_rank);
- }
- switch(card_suit) {
- case card::suit::Spades : s += "S"; break;
- case card::suit::Hearts : s += "H"; break;
- case card::suit::Clubs : s += "C"; break;
- case card::suit::Diamonds: s += "D"; break;
- }
- return s;
-}
-
-bool card::is_face_down() const {
- return face_down;
-}
-
-void card::turn_face_up() {
- face_down = false;
-}
-
-void card::turn_face_down() {
- face_down = true;
-}
-
-const card card::divider = card();
-
-bool operator==(const card& l, const card& r) {
- return l.get_rank() == r.get_rank() && l.get_suit() == r.get_suit();
-}
-
-bool operator!=(const card& l, const card& r) {
- return !(l == r);
-}
-
-bool operator<(const card& l, const card& r) {
- if (l.get_rank() == r.get_rank())
- return l.get_suit() < r.get_suit();
- else
- return l.get_rank() < r.get_rank();
-}
-
-bool operator>(const card& l, const card& r) {
- if (l.get_rank() == r.get_rank())
- return l.get_suit() > r.get_suit();
- else
- return l.get_rank() > r.get_rank();
-}
-
-bool operator<=(const card& l, const card& r) {
- if (l.get_rank() == r.get_rank())
- return l.get_suit() <= r.get_suit();
- else
- return l.get_rank() <= r.get_rank();
-}
-
-bool operator>=(const card& l, const card& r) {
- if (l.get_rank() == r.get_rank())
- return l.get_suit() >= r.get_suit();
- else
- return l.get_rank() >= r.get_rank();
-}
diff --git a/solvitaire/src/main/game/card.h b/solvitaire/src/main/game/card.h
deleted file mode 100644
index 7885d66..0000000
--- a/solvitaire/src/main/game/card.h
+++ /dev/null
@@ -1,66 +0,0 @@
-//
-// Created by thecharlesblake on 10/11/17.
-//
-
-#ifndef SOLITAIRE_PIP_CARD_H
-#define SOLITAIRE_PIP_CARD_H
-
-#include <string>
-#include <map>
-
-class card {
-public:
- // Types & typedefs
- typedef uint8_t rank_t;
- typedef uint8_t suit_t;
- typedef uint8_t colour_t;
-
- struct suit {
- static const suit_t Clubs;
- static const suit_t Hearts;
- static const suit_t Spades;
- static const suit_t Diamonds;
- };
-
- struct colour {
- static const colour_t Black;
- static const colour_t Red;
- };
-
- const static card divider;
-
- // A card can either be created using two int values (rank & suit), or
- // using a string (e.g. "AS")
- card(suit_t, rank_t, bool face_down=false);
- card(const char*, bool face_down_possible=false);
- card();
-
- suit_t get_suit() const;
- colour_t get_colour() const;
- rank_t get_rank() const;
-
- bool is_face_down() const;
- void turn_face_up();
- void turn_face_down();
-
- std::string to_string() const;
-
-private:
- static suit_t suit_from_str(const char* c);
- static rank_t rank_from_str(const char* c);
- bool face_down_from_str(const char *c, bool possible);
-
- rank_t card_rank : 4;
- suit_t card_suit : 2;
- bool face_down : 1;
-};
-
-bool operator==(const card&, const card&);
-bool operator!=(const card&, const card&);
-bool operator<(const card&, const card&);
-bool operator>(const card&, const card&);
-bool operator<=(const card&, const card&);
-bool operator>=(const card&, const card&);
-
-
-#endif //SOLITAIRE_PIP_CARD_H
diff --git a/solvitaire/src/main/game/global_cache.cpp b/solvitaire/src/main/game/global_cache.cpp
deleted file mode 100644
index 079ed77..0000000
--- a/solvitaire/src/main/game/global_cache.cpp
+++ /dev/null
@@ -1,276 +0,0 @@
-//
-// Created by thecharlesblake on 1/10/18.
-//
-
-#include <algorithm>
-
-#include <boost/functional/hash.hpp>
-
-#include "global_cache.h"
-#include "../input-output/output/log_helper.h"
-#include "search-state/game_state.h"
-
-using namespace std;
-using namespace boost;
-
-typedef sol_rules::build_policy pol;
-typedef sol_rules::stock_deal_type sdt;
-typedef game_state::streamliner_options sos;
-
-typedef boost::multi_index::multi_index_container<
- cached_game_state,
- boost::multi_index::indexed_by<
- boost::multi_index::sequenced<>,
- boost::multi_index::hashed_unique<
- boost::multi_index::identity<cached_game_state>,
- hasher
- >
- >
-> item_list;
-
-
-///////////////////////
-// CACHED GAME STATE //
-///////////////////////
-
-cached_game_state::cached_game_state(const game_state& gs) : live(true) {
- data.reserve(52+18); // Enough for each card and up to 18 piles
-
- if (gs.rules.hole) {
- add_card(gs.piles[gs.hole].top_card(), gs);
- }
-
- for (pile::ref pr : gs.cells) {
- add_pile(pr, gs);
- }
- if (gs.rules.cells > 0) {
- add_card_divider();
- }
-
- if (gs.rules.stock_size > 0) {
- add_pile(gs.stock, gs);
-
- if (gs.rules.stock_deal_t == sdt::WASTE) {
- bool waste_deal_symmetry = gs.rules.stock_redeal
- && gs.piles[gs.waste].size() % gs.rules.stock_deal_count == 0;
-
- if (waste_deal_symmetry) {
- add_pile_in_reverse(gs.waste, gs);
- add_card_divider();
- } else {
- add_card_divider();
- add_pile_in_reverse(gs.waste, gs);
- }
- }
-
- add_card_divider();
- }
-
- for (pile::ref pr : gs.reserve) {
- add_pile(pr, gs);
- }
- if (gs.rules.reserve_size > 0) {
- add_card_divider();
- }
-
- for (pile::ref pr : gs.tableau_piles) {
- add_pile(pr, gs);
- add_card_divider();
- }
-
- for (pile::ref pr : gs.sequences) {
- add_pile(pr, gs);
- add_card_divider();
- }
-
- for (pile::ref pr : gs.accordion) {
- add_card(gs.piles[pr].top_card(), gs);
- }
-}
-
-
-void cached_game_state::add_pile(pile::ref pr, const game_state& gs) {
- for (card c : gs.piles[pr].pile_vec) {
- add_card(c, gs);
- }
-}
-
-void cached_game_state::add_pile_in_reverse(pile::ref pr, const game_state& gs) {
- for (auto i = gs.piles[pr].pile_vec.size(); i-->0;) {
- card c = gs.piles[pr].pile_vec[i];
- add_card(c, gs);
- }
-}
-
-void cached_game_state::add_card(card c, const game_state& gs) {
- auto& target = data;
-
- // If the game is a 'hole-based' game, or suit-reduction is on, reduces
- // the cached suit of the card where possible
-
- bool is_suit_symmetry = (gs.rules.foundations_present
- && (gs.stream_opts == sos::SUIT_SYMMETRY || gs.stream_opts == sos::BOTH))
- || gs.rules.hole;
-
- if (is_suit_symmetry) {
- switch (gs.rules.build_pol) {
- case pol::SAME_SUIT:
- target.emplace_back(c);
- break;
- case pol::RED_BLACK:
- target.emplace_back(c.get_colour(), c.get_rank(), c.is_face_down());
- break;
- default:
- target.emplace_back(0, c.get_rank(), c.is_face_down());
- break;
- }
- } else {
- target.emplace_back(c);
- }
-}
-
-void cached_game_state::add_card_divider() {
- data.emplace_back(0, 0);
-}
-
-bool operator==(const cached_game_state& a, const cached_game_state& b) {
- return a.data == b.data;
-}
-
-
-
-//////////////////
-// STATE HASHER //
-//////////////////
-
-hasher::hasher(const game_state& gs) : init_gs(gs) {
-}
-
-size_t hasher::operator()(const cached_game_state& cgs) const {
- size_t seed = 0;
-
- for (card d : cgs.data) {
- combine(seed, hash_value(d));
- }
- return seed;
-}
-
-std::size_t hasher::combine(std::size_t& seed, std::size_t value) const {
- return seed ^= value + 0x9e3779b9 + (seed<<6) + (seed>>2);
-}
-
-size_t hasher::hash_value(card const& c) const {
- boost::hash<uint8_t> boost_hasher;
-
- // If the game is a 'hole-based' game, or suit-reduction is enabled, hash
- // the reduced suit
- bool is_suit_symmetry = (init_gs.rules.foundations_present
- && (init_gs.stream_opts == sos::SUIT_SYMMETRY || init_gs.stream_opts == sos::BOTH))
- || init_gs.rules.hole;
-
- uint8_t suit_val;
- if (is_suit_symmetry) {
- switch (init_gs.rules.build_pol) {
- case pol::SAME_SUIT:
- suit_val = c.get_suit();
- break;
- case pol::RED_BLACK:
- suit_val = c.get_colour();
- break;
- default:
- suit_val = 0;
- }
- } else {
- suit_val = c.get_suit();
- }
-
- auto raw_val = static_cast<uint8_t>(suit_val * 13 + c.get_rank());
- return boost_hasher(raw_val);
-}
-
-
-///////////////
-// LRU CACHE //
-///////////////
-
-// Based on https://www.boost.org/doc/libs/1_67_0/libs/multi_index/example/serialization.cpp
-/* Boost.MultiIndex example of serialization of a MRU list.
- *
- * Copyright 2003-2008 Joaquin M Lopez Munoz.
- * Distributed under the Boost Software License, Version 1.0.
- * (See accompanying file LICENSE_1_0.txt or copy at
- * http://www.boost.org/LICENSE_1_0.txt)
- *
- * See http://www.boost.org/libs/multi_index for library home page.
- */
-
-item_list::ctor_args_list lru_cache::get_init_tuple(const game_state& gs) {
- return boost::make_tuple(
- item_list::nth_index<0>::type::ctor_args(),
- boost::make_tuple(
- size_t(0),
- multi_index::identity<cached_game_state>(),
- hasher(gs),
- equal_to<cached_game_state>()
- )
- );
-}
-
-lru_cache::lru_cache(const game_state& gs, uint64_t max_num_items_)
- : max_num_items(max_num_items_), cache(get_init_tuple(gs)), states_removed_from_cache(0) {
-}
-
-pair<item_list::iterator, bool> lru_cache::insert(const game_state& gs) {
- pair<item_list::iterator, bool> p = cache.push_front(cached_game_state(gs));
-
- if(!p.second){ /* duplicate item */
- cache.relocate(cache.begin(), p.first); /* put in front */
- } else if(cache.size() > max_num_items){ /* keep the length <= max_num_items */
-
- // If the least recently used node is 'live' (i.e. a parent), relocates
- // it to the head of the list until this is no longer the case
- for (uint64_t i = 0; prev(cache.end())->live; i++) {
- cache.relocate(cache.begin(), prev(cache.end()));
-
- if (i == max_num_items) {
-#ifndef NDEBUG
- LOG_ERROR("All items in cache are live and cache is full");
-#endif
- throw runtime_error("All items in cache are live and cache is full");
- }
- }
- cache.pop_back();
- states_removed_from_cache++;
- }
- return p;
-}
-
-bool lru_cache::contains(const game_state& gs) const {
- return cache.get<1>().count(cached_game_state(gs)) > 0;
-}
-
-void lru_cache::clear() {
- cache.clear();
-}
-
-item_list::size_type lru_cache::size() const {
- return cache.size();
-}
-
-item_list::size_type lru_cache::bucket_count() const {
- return cache.get<1>().bucket_count();
-}
-
-void lru_cache::set_non_live(item_list::iterator state_iter) {
-#ifndef NDEBUG
- bool succ =
-#endif
- cache.modify(state_iter, [](auto& v){ v.live = false; });
-#ifndef NDEBUG
- assert(succ);
-#endif
-}
-
-uint64_t lru_cache::get_states_removed_from_cache() const {
- return states_removed_from_cache;
-}
diff --git a/solvitaire/src/main/game/global_cache.h b/solvitaire/src/main/game/global_cache.h
deleted file mode 100644
index e9bb412..0000000
--- a/solvitaire/src/main/game/global_cache.h
+++ /dev/null
@@ -1,76 +0,0 @@
-//
-// Created by thecharlesblake on 1/10/18.
-//
-
-#ifndef SOLVITAIRE_GLOBAL_CACHE_H
-#define SOLVITAIRE_GLOBAL_CACHE_H
-
-#include <vector>
-#include <unordered_set>
-#include <boost/pool/pool.hpp>
-#include <boost/pool/pool_alloc.hpp>
-#include <boost/multi_index_container.hpp>
-#include <boost/multi_index/sequenced_index.hpp>
-#include <boost/multi_index/identity.hpp>
-#include <boost/multi_index/hashed_index.hpp>
-
-#include "sol_rules.h"
-#include "search-state/game_state.h"
-
-struct cached_game_state {
- typedef std::vector<card> state_data;
- typedef state_data::size_type size_type;
-
- explicit cached_game_state(const game_state&);
- void add_pile(pile::ref, const game_state&);
- void add_pile_in_reverse(pile::ref, const game_state&);
- void add_card(card, const game_state&);
- void add_card_divider();
-
- state_data data;
- bool live; // Is a parent in the current search tree
-};
-
-bool operator==(const cached_game_state&, const cached_game_state&);
-
-struct hasher {
- explicit hasher(const game_state&);
- std::size_t operator() (const cached_game_state&) const;
-
- std::size_t hash_value(const card&) const;
- std::size_t combine(std::size_t&, std::size_t) const;
-
- const game_state& init_gs;
-};
-
-class lru_cache {
-public:
- typedef boost::multi_index::multi_index_container<
- cached_game_state,
- boost::multi_index::indexed_by<
- boost::multi_index::sequenced<>,
- boost::multi_index::hashed_unique<
- boost::multi_index::identity<cached_game_state>,
- hasher
- >
- >
- > item_list;
-
- explicit lru_cache(const game_state&, uint64_t);
- std::pair<item_list::iterator, bool> insert(const game_state&);
- bool contains(const game_state&) const;
- void clear();
- item_list::size_type size() const;
- item_list::size_type bucket_count() const;
- void set_non_live(item_list::iterator);
- uint64_t get_states_removed_from_cache() const;
-
-private:
- static item_list::ctor_args_list get_init_tuple(const game_state&);
-
- uint64_t max_num_items;
- item_list cache;
- uint64_t states_removed_from_cache;
-};
-
-#endif //SOLVITAIRE_GLOBAL_CACHE_H
diff --git a/solvitaire/src/main/game/move.cpp b/solvitaire/src/main/game/move.cpp
deleted file mode 100644
index b349ddd..0000000
--- a/solvitaire/src/main/game/move.cpp
+++ /dev/null
@@ -1,25 +0,0 @@
-//
-// Created by thecharlesblake on 07/06/18.
-//
-
-#include "move.h"
-
-move::move(mtype ty, pile::ref f, pile::ref t, int8_t count_, bool reveal_move_, bool flip_waste_) :
- type(ty), from(f), to(t), count(count_), reveal_move(reveal_move_), flip_waste(flip_waste_) {
-#ifndef NDEBUG
- if (ty == mtype::regular) assert(count == 1);
- if (ty == mtype::stock_k_plus) assert(!reveal_move_);
-#endif
-}
-
-void move::make_reveal_move() {
- reveal_move = true;
-}
-
-bool operator==(const move& a, const move& b) {
- return (a.from == b.from )
- && (a.to == b.to )
- && (a.count == b.count)
- && (a.type == b.type )
- && (a.reveal_move == b.reveal_move);
-}
diff --git a/solvitaire/src/main/game/move.h b/solvitaire/src/main/game/move.h
deleted file mode 100644
index 8250609..0000000
--- a/solvitaire/src/main/game/move.h
+++ /dev/null
@@ -1,28 +0,0 @@
-//
-// Created by thecharlesblake on 07/06/18.
-//
-
-#ifndef SOLVITAIRE_MOVE_H
-#define SOLVITAIRE_MOVE_H
-
-#include "pile.h"
-
-struct move {
- enum class mtype : uint8_t {regular, dominance, built_group, stock_k_plus, stock_to_all_tableau, sequence, accordion, null};
-
- explicit move(mtype, pile::ref = 255 , pile::ref = 255, int8_t = 1, bool reveal_move_ = false, bool flip_waste_ = false);
- friend bool operator==(const move&, const move&);
-
- void make_reveal_move();
-
- mtype type;
- pile::ref from;
- pile::ref to;
- // For built piles the size of the pile, or for stock k-plus moves,
- // the number of cards to be moved from the stock to the waste before the waste card is played on the foundations
- int8_t count;
- bool reveal_move;
- bool flip_waste;
-};
-
-#endif //SOLVITAIRE_MOVE_H
diff --git a/solvitaire/src/main/game/pile.cpp b/solvitaire/src/main/game/pile.cpp
deleted file mode 100644
index 23a02af..0000000
--- a/solvitaire/src/main/game/pile.cpp
+++ /dev/null
@@ -1,92 +0,0 @@
-//
-// Created by thecharlesblake on 10/28/17.
-//
-
-#include <vector>
-#include <ostream>
-
-#include "pile.h"
-
-using namespace std;
-
-const pile::size_type pile::max_size_type = 255;
-
-pile::pile(std::vector<card> pv) : pile_vec(pv) {
-}
-
-pile::pile(std::initializer_list<card> il) : pile_vec(il) {
-}
-
-card pile::top_card() const {
- assert(!empty());
- return pile_vec.back();
-}
-
-bool pile::empty() const {
- return pile_vec.empty();
-}
-
-pile::size_type pile::size() const {
- return static_cast<uint8_t>(pile_vec.size());
-}
-
-card& pile::operator[] (size_type i) {
- return pile_vec[pile_vec.size() - 1 - i];
-}
-
-card pile::operator[] (size_type i) const {
- vector<card>::size_type s = pile_vec.size() - 1 - i;
- assert(s < pile_vec.size());
- return pile_vec[s];
-}
-
-void pile::place(const card c) {
- pile_vec.emplace_back(c);
-}
-
-card pile::take() {
- assert(!empty());
- card c = top_card();
- pile_vec.pop_back();
- return c;
-}
-
-bool operator==(const pile& a, const pile& b) {
- return a.pile_vec == b.pile_vec;
-}
-
-bool operator!=(const pile& a, const pile& b) {
- return a.pile_vec != b.pile_vec;
-}
-
-bool operator<(const pile& a, const pile& b) {
- if (a.size() == b.size()) {
- return a.pile_vec < b.pile_vec;
- } else {
- return a.size() < b.size();
- }
-}
-
-bool operator>(const pile& a, const pile& b) {
- if (a.size() == b.size()) {
- return a.pile_vec > b.pile_vec;
- } else {
- return a.size() > b.size();
- }
-}
-
-bool operator<=(const pile& a, const pile& b) {
- if (a.size() == b.size()) {
- return a.pile_vec <= b.pile_vec;
- } else {
- return a.size() <= b.size();
- }
-}
-
-bool operator>=(const pile& a, const pile& b) {
- if (a.size() == b.size()) {
- return a.pile_vec >= b.pile_vec;
- } else {
- return a.size() >= b.size();
- }
-}
diff --git a/solvitaire/src/main/game/pile.h b/solvitaire/src/main/game/pile.h
deleted file mode 100644
index 5ab2404..0000000
--- a/solvitaire/src/main/game/pile.h
+++ /dev/null
@@ -1,53 +0,0 @@
-//
-// Created by thecharlesblake on 10/28/17.
-//
-
-#ifndef SOLVITAIRE_PILE_H
-#define SOLVITAIRE_PILE_H
-
-#include <vector>
-#include <iterator>
-
-#include "sol_rules.h"
-#include "card.h"
-
-class pile {
- friend class hasher;
- friend class game_state;
- friend class cached_game_state;
-public:
- typedef uint8_t size_type;
- const static size_type max_size_type;
- typedef uint8_t ref;
-
- pile() = default;
- pile(std::vector<card>);
- pile(std::initializer_list<card>);
-
- // Inspect
- card top_card() const;
- bool empty() const;
- size_type size() 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 bool operator!=(const pile&, const pile&);
- friend bool operator<(const pile&, const pile&);
- friend bool operator>(const pile&, const pile&);
- friend bool operator<=(const pile&, const pile&);
- friend bool operator>=(const pile&, const pile&);
-
- // Modify
- void place(card);
- card take();
-
-private:
- // Underlying vector
- std::vector<card> pile_vec;
-};
-
-
-#endif //SOLVITAIRE_PILE_H
diff --git a/solvitaire/src/main/game/search-state/game_state.cpp b/solvitaire/src/main/game/search-state/game_state.cpp
deleted file mode 100644
index 6ea550c..0000000
--- a/solvitaire/src/main/game/search-state/game_state.cpp
+++ /dev/null
@@ -1,764 +0,0 @@
-//
-// Created by thecharlesblake on 10/24/17.
-//
-
-#include <vector>
-#include <ostream>
-#include <algorithm>
-#include <functional>
-
-#include <boost/functional/hash.hpp>
-#include <boost/optional.hpp>
-#include <boost/random.hpp>
-
-#include "game_state.h"
-#include "document.h"
-#include "../../input-output/input/json-parsing/deal_parser.h"
-#include "../../input-output/output/state_printer.h"
-#include "../../input-output/output/log_helper.h"
-#include "../move.h"
-#include "../sol_rules.h"
-
-using namespace rapidjson;
-using std::vector;
-using std::list;
-using std::find;
-using std::begin;
-using std::end;
-using std::rbegin;
-using std::rend;
-using std::runtime_error;
-using std::max;
-using std::ostream;
-using std::mt19937;
-using std::string;
-
-typedef sol_rules::build_policy pol;
-typedef sol_rules::stock_deal_type sdt;
-typedef sol_rules::face_up_policy fu;
-typedef game_state::streamliner_options sos;
-typedef sol_rules::foundations_init_type fit;
-
-//////////////////
-// 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, streamliner_options stream_opts_)
- : rules(s_rules)
- , stream_opts(stream_opts_)
- , foundations_base(card::rank_t(1))
- , stock(255)
- , waste(255)
- , hole (255) {
- // 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_present) {
- 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));
- auto_foundation_moves.push_back(true);
- }
- }
-
- // If there are cell piles, creates the relevant cell vectors
- if (rules.cells > 0) {
- for (uint8_t i = 0; i < rules.cells; i++) {
- piles.emplace_back();
- cells.push_back(static_cast<pile::ref>(piles.size() - 1));
- original_cells.push_back(static_cast<pile::ref>(piles.size() - 1));
- }
- }
-
- // If there is a stock, creates a pile
- if (rules.stock_size > 0) {
- piles.emplace_back();
- stock = 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);
- }
- }
-
- // 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));
- original_reserve.push_back(static_cast<pile::ref>(piles.size() - 1));
- }
- }
-
- // If there is a reserve, creates piles
- if (rules.accordion_size > 0) {
- uint8_t pile_count = rules.accordion_size;
- for (uint8_t i = 0; i < pile_count; i++) {
- piles.emplace_back();
- accordion.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));
- original_tableau_piles.push_back(static_cast<pile::ref>(piles.size() - 1));
- }
-
- // Creates the sequence piles
- for (uint8_t i = 0; i < rules.sequence_count; i++) {
- piles.emplace_back();
- sequences.push_back(static_cast<pile::ref>(piles.size() - 1));
- }
-}
-
-// Constructs an initial game state from a JSON doc
-game_state::game_state(const sol_rules& s_rules, const Document& doc, streamliner_options s_opts)
- : game_state(s_rules, s_opts) {
- deal_parser::parse(*this, doc);
-}
-
-// Constructs an initial game state from a seed
-game_state::game_state(const sol_rules& s_rules, int seed, streamliner_options s_opts)
- : game_state(s_rules, s_opts) {
- auto rng = mt19937(seed);
- vector<card> deck = gen_shuffled_deck(rules.max_rank, rules.two_decks, rng);
-
- // If there is a hole, moves the ace of spades to it
- if (rules.hole) {
- deck.erase(find(begin(deck), end(deck), card("AS")));
- place_card(hole, card("AS"));
- }
-
- // If the foundation base is random, selects a random rank and suit
- card::suit_t rand_suit = 0;
- if (!rules.foundations_base) {
- boost::random::uniform_int_distribution<card::rank_t> distr_rank(1, rules.max_rank);
- foundations_base = distr_rank(rng);
- boost::random::uniform_int_distribution<card::suit_t> distr_suit(0, 3);
- rand_suit = distr_suit(rng);
- }
-
- // If the foundations begin filled, then fills them
- uint8_t founds_to_fill = 0;
- switch (rules.foundations_init_cards) {
- case sol_rules::foundations_init_type::NONE:
- break;
- case sol_rules::foundations_init_type::ONE:
- founds_to_fill = 1; break;
- case sol_rules::foundations_init_type::ALL:
- founds_to_fill = static_cast<uint8_t>(4 * (rules.two_decks ? 2 : 1)); break;
- }
- for (uint8_t f_idx = 0; f_idx < founds_to_fill; f_idx++) {
- card c = card((f_idx + rand_suit) % uint8_t(4), foundations_base);
-
- deck.erase(find(begin(deck), end(deck), c));
- place_card(foundations[(f_idx + rand_suit)], c);
- }
-
- // If there are pre-filled cells, deals to them
- auto it = begin(original_cells);
- for (uint8_t i = 0; i < rules.cells_pre_filled; i++) {
- pile::ref r = *it;
- place_card(r, deck.back());
- deck.pop_back();
- it++;
- }
-
- // 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++) {
- place_card(stock, deck.back());
- deck.pop_back();
- }
- }
-
- // 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++) {
- int idx = rules.reserve_stacked ? 0 : i;
- place_card(original_reserve[idx], deck.back());
- deck.pop_back();
- }
- }
-
- // Deals to the sequence piles
- if (rules.sequence_count > 0) {
- for (int t = 0; !deck.empty(); t++) {
- // Adds the randomly generated card to the tableau piles
- auto s = t % sequences.size();
-
- card c = deck.back();
- if (c.get_rank() == 1) c = "AS"; // The dummy "gap" placeholder
-
- pile::ref seq_pile = sequences[s];
- place_card(seq_pile, c);
- deck.pop_back();
- }
- }
-
- // Deals to the accordion
- if (rules.accordion_size > 0) {
- for (auto pr : accordion) {
- place_card(pr, deck.back());
- deck.pop_back();
- }
- }
-
- // This only occurs during testing
- if (rules.tableau_pile_count == 0) return;
-
- // Deals to the tableau piles (row-by-row)
- for (int t = 0; !deck.empty(); t++) {
- card c = deck.back();
-
- // If only the top cards are face up, initially deals all face down
- if (rules.face_up == fu::TOP_CARDS) c.turn_face_down();
-
- // Adds the randomly generated card to the tableau piles
- auto p = t % original_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 / original_tableau_piles.size();
- if (rules.diagonal_deal && row_idx < original_tableau_piles.size()) {
- p = original_tableau_piles.size()-p-1;
- pile::ref tableau_pile = original_tableau_piles[p];
-
- if (p >= row_idx) {
- place_card(tableau_pile, c);
- deck.pop_back();
- }
- } else {
- pile::ref tableau_pile = original_tableau_piles[p];
- place_card(tableau_pile, c);
- deck.pop_back();
- }
- }
-
- // Now if necessary, turns the top cards face up
- if (rules.face_up == fu::TOP_CARDS)
- for (auto& p : piles)
- if (!p.empty())
- p[0].turn_face_up();
-
- // The size of all piles must equal the deck size
- int piles_sz = 0;
- for (auto& p : piles) piles_sz += p.size();
- if (piles_sz != rules.max_rank * (rules.two_decks ? 8:4)) {
- throw runtime_error("Error: incorrect number of cards in starting piles");
- }
-}
-
-game_state::game_state(const sol_rules& s_rules,
- std::initializer_list<std::initializer_list<string>> il)
- : game_state(s_rules, sos::NONE) {pile::ref pr = 0;
- for (auto& p_il : il) {
- for (const string& card_str : p_il) {
- card c(card_str.c_str(), s_rules.face_up == fu::TOP_CARDS);
- place_card(pr, c);
- }
- pr++;
- }
-
- // Sets foundations base appropriately
- if (rules.foundations_present && rules.foundations_base == boost::none) {
- for (auto f : foundations) {
- if (!piles[f].empty()) {
- foundations_base = piles[f].top_card().get_rank();
- return;
- }
- }
- }
-}
-
-game_state::game_state(const sol_rules& s_rules,
- std::vector<std::vector<string>> vec)
- : game_state(s_rules, sos::NONE) {pile::ref pr = 0;
- for (auto& p_vec : vec) {
- for (const string& card_str : p_vec) {
- card c(card_str.c_str(), s_rules.face_up == fu::TOP_CARDS);
- place_card(pr, c);
- }
- pr++;
- }
-
- // Sets foundations base appropriately
- if (rules.foundations_present && rules.foundations_base == boost::none) {
- for (auto f : foundations) {
- if (!piles[f].empty()) {
- foundations_base = piles[f].top_card().get_rank();
- return;
- }
- }
- }
-}
-
-game_state::game_state(const sol_rules& s_rules,
- std::vector<std::vector<card::suit_t>> suits,
- std::vector<std::vector<card::rank_t>> ranks,
- std::vector<std::vector<bool>> face_down)
- : game_state(s_rules, sos::NONE) {pile::ref pr = 0;
- assert(suits.size() == ranks.size());
- assert(ranks.size() == face_down.size());
- for (std::vector<std::vector<card::suit_t>>::size_type i = 0;
- i < suits.size(); ++i) {
- assert(suits[i].size() == ranks[i].size());
- assert(ranks[i].size() == face_down[i].size());
- for (std::vector<card::suit_t>::size_type j = 0; j < suits[i].size();
- ++j) {
- card c(suits[i][j], ranks[i][j], face_down[i][j]);
- place_card(pr, c);
- }
- pr++;
- }
-
- // Sets foundations base appropriately
- if (rules.foundations_present && rules.foundations_base == boost::none) {
- for (auto f : foundations) {
- if (!piles[f].empty()) {
- foundations_base = piles[f].top_card().get_rank();
- return;
- }
- }
- }
-}
-
-// Generates a randomly ordered vector of cards
-vector<card> game_state::gen_shuffled_deck(card::rank_t max_rank,
- bool two_decks, mt19937 rng) {
- vector<card> deck;
-
- for (int deck_count = 1; deck_count <= (two_decks ? 2 : 1); deck_count++) {
- for (card::rank_t rank = 1; rank <= max_rank; rank++) {
- for (card::suit_t suit = 0 ; suit < 4; suit++) {
- deck.emplace_back(suit, rank);
- }
- }
- }
-
- assert(deck.size() == pile::size_type(max_rank * (two_decks ? 8 : 4)));
-
- game_state::shuffle(begin(deck), end(deck), rng);
- return deck;
-}
-
-template<class RandomIt, class URBG>
-void game_state::shuffle(RandomIt first, RandomIt last, URBG&& g) {
- typedef typename std::iterator_traits<RandomIt>::difference_type diff_t;
- typedef boost::random::uniform_int_distribution<diff_t> distr_t;
- typedef typename distr_t::param_type param_t;
-
- distr_t D;
- diff_t n = last - first;
- for (diff_t i = n-1; i > 0; --i) {
- using std::swap;
- swap(first[i], first[D(g, param_t(0, i))]);
- }
-}
-
-
-
-////////////////////
-// ALTERING STATE //
-////////////////////
-
-void game_state::make_move(const move m) {
- switch (m.type) {
- case move::mtype::regular:
- case move::mtype::dominance:
- make_regular_move(m);
- break;
- case move::mtype::built_group:
- make_built_group_move(m);
- break;
- case move::mtype::stock_k_plus:
- make_stock_k_plus_move(m);
- break;
- case move::mtype::stock_to_all_tableau:
- make_stock_to_all_tableau_move(m);
- break;
- case move::mtype::sequence:
- make_sequence_move(m);
- break;
- case move::mtype::accordion:
- make_accordion_move(m);
- break;
- case move::mtype::null:
- assert(false);
- break;
- }
-
-#ifndef NDEBUG
- check_face_down_consistent();
-#endif
-}
-
-void game_state::undo_move(const move m) {
- switch (m.type) {
- case move::mtype::regular:
- case move::mtype::dominance:
- undo_regular_move(m);
- break;
- case move::mtype::built_group:
- undo_built_group_move(m);
- break;
- case move::mtype::stock_k_plus:
- undo_stock_k_plus_move(m);
- break;
- case move::mtype::stock_to_all_tableau:
- undo_stock_to_all_tableau_move(m);
- break;
- case move::mtype::sequence:
- undo_sequence_move(m);
- break;
- case move::mtype::accordion:
- undo_accordion_move(m);
- break;
- case move::mtype::null:
- assert(false);
- break;
- }
-
-#ifndef NDEBUG
- check_face_down_consistent();
-#endif
-}
-
-void game_state::make_regular_move(const move m) {
- assert(m.from < piles.size());
- assert(m.to < piles.size());
-
- place_card(m.to, take_card(m.from));
-
- if (m.reveal_move) {
- assert(!piles[m.from].empty());
- assert(piles[m.from][0].is_face_down());
- piles[m.from][0].turn_face_up();
- }
-
-#ifndef NO_AUTO_FOUNDATIONS
- update_auto_foundation_moves(m.to);
-#endif
-}
-
-void game_state::undo_regular_move(const move m) {
- assert(m.to < piles.size());
-
- if (m.reveal_move) {
- assert(!piles[m.from].empty());
- assert(!piles[m.from][0].is_face_down());
- piles[m.from][0].turn_face_down();
- }
-
- place_card(m.from, take_card(m.to));
-
-#ifndef NO_AUTO_FOUNDATIONS
- update_auto_foundation_moves(m.from);
-#endif
-}
-
-void game_state::make_built_group_move(move m) {
- assert(m.from < piles.size() );
- assert(m.to < piles.size() );
-
- // Adds the cards to the 'to' pile
- for (auto pile_idx = m.count; pile_idx-- > 0;) {
- place_card(m.to, piles[m.from][pile_idx]);
- }
-
- // Removes the cards from the 'from' pile
- for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) {
- take_card(m.from);
- }
-
- if (m.reveal_move) {
- assert(!piles[m.from].empty());
- assert(piles[m.from][0].is_face_down());
- piles[m.from][0].turn_face_up();
- }
-}
-
-void game_state::undo_built_group_move(move m) {
- assert(m.to < piles.size());
-
- if (m.reveal_move) {
- assert(!piles[m.from].empty());
- assert(!piles[m.from][0].is_face_down());
- piles[m.from][0].turn_face_down();
- }
-
- // Adds the cards to the 'from' pile
- for (auto pile_idx = m.count; pile_idx-- > 0;) {
- place_card(m.from, piles[m.to][pile_idx]);
- }
-
- // Removes the cards from the 'to' pile
- for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) {
- take_card(m.to);
- }
-}
-
-void game_state::make_stock_k_plus_move(const move m) {
-#ifndef NDEBUG
- assert(rules.stock_deal_t == sdt::WASTE);
- assert(m.from == stock);
- if (rules.stock_redeal) assert(m.count <= piles[stock].size() && m.count > -piles[waste].size());
- assert(!rules.stock_redeal || m.to != waste);
- auto sz_before = piles[stock].size() + piles[waste].size();
-#endif
-
- // Transfers count cards from the stock to the waste
- if (m.count > 0) {
- for (int i = 0; i < m.count; i++) {
- place_card(waste, take_card(stock));
- }
- } else {
- for (int i = 0; i > m.count; i--) {
- place_card(stock, take_card(waste));
- }
- }
-
- // Moves the card on top of the waste to the target pile
- place_card(m.to, take_card(waste));
-
- // Flips the waste back on to the empty stock (if necessary)
- if (m.flip_waste) {
- assert(rules.stock_redeal && piles[stock].empty());
- while (!piles[waste].empty()) {
- place_card(stock, take_card(waste));
- }
- }
-
-#ifndef NDEBUG
- auto sz_after = piles[stock].size() + piles[waste].size();
- assert(sz_before == sz_after + 1);
- assert(!(rules.stock_size > 0 && rules.stock_redeal && piles[stock].empty() && !piles[waste].empty()));
- assert(piles[stock].size() <= rules.stock_size);
-#endif
-}
-
-void game_state::undo_stock_k_plus_move(move m) {
-#ifndef NDEBUG
- assert(rules.stock_deal_t == sdt::WASTE);
- assert(m.from == stock);
- assert(!rules.stock_redeal || m.to != waste);
- auto sz_after = piles[stock].size() + piles[waste].size();
-#endif
-
- if (m.flip_waste) {
- assert(rules.stock_redeal && piles[waste].empty());
- while (!piles[stock].empty()) {
- place_card(waste, take_card(stock));
- }
- }
-
- place_card(waste, take_card(m.to));
-
- if (m.count > 0) {
- for (int i = 0; i < m.count; i++) {
- place_card(stock, take_card(waste));
- }
- } else {
- for (int i = 0; i > m.count; i--) {
- place_card(waste, take_card(stock));
- }
- }
-
-#ifndef NDEBUG
- auto sz_before = piles[stock].size() + piles[waste].size();
- assert(sz_before == sz_after + 1);
- if (rules.stock_redeal) assert(m.count <= piles[stock].size() && m.count > -piles[waste].size());
- assert(!(rules.stock_size > 0 && rules.stock_redeal && piles[stock].empty() && !piles[waste].empty()));
- assert(piles[stock].size() <= rules.stock_size);
-#endif
-}
-
-void game_state::make_stock_to_all_tableau_move(move m) {
- assert(rules.stock_deal_t == sdt::TABLEAU_PILES);
-
- for (pile::ref tab_pr = original_tableau_piles.front();
- tab_pr < pile::ref(original_tableau_piles.front() + m.count);
- tab_pr++) {
- place_card(tab_pr, take_card(stock));
- }
-}
-
-void game_state::undo_stock_to_all_tableau_move(move m) {
- assert(rules.stock_deal_t == sdt::TABLEAU_PILES);
-
- for (pile::ref tab_pr = original_tableau_piles.front() + m.count;
- tab_pr-- > original_tableau_piles.front();
- ) {
- place_card(stock, take_card(tab_pr));
- }
-}
-
-void game_state::make_sequence_move(const move m) {
- pile::ref from_seq_ref = m.from / rules.max_rank;
- pile::size_type from_card_idx = m.from % rules.max_rank;
- card from_card = piles[from_seq_ref][from_card_idx];
- piles[from_seq_ref][from_card_idx] = "AS";
-
- pile::ref to_seq_ref = m.to / rules.max_rank;
- pile::size_type to_card_idx = m.to % rules.max_rank;
- assert(piles[to_seq_ref][to_card_idx] == "AS");
- piles[to_seq_ref][to_card_idx] = from_card;
-}
-
-void game_state::undo_sequence_move(const move m) {
- pile::ref to_seq_ref = m.to / rules.max_rank;
- pile::size_type to_card_idx = m.to % rules.max_rank;
- card to_card = piles[to_seq_ref][to_card_idx];
- piles[to_seq_ref][to_card_idx] = "AS";
-
- pile::ref from_seq_ref = m.from / rules.max_rank;
- pile::size_type from_card_idx = m.from % rules.max_rank;
- assert(piles[from_seq_ref][from_card_idx] == "AS");
- piles[from_seq_ref][from_card_idx] = to_card;
-}
-
-void game_state::make_accordion_move(move m) {
- make_built_group_move(m);
- accordion.remove(m.from);
-}
-
-void game_state::undo_accordion_move(move m) {
- accordion.insert(upper_bound(begin(accordion), end(accordion), m.from), m.from);
- undo_built_group_move(m);
-}
-
-// Places a card on a pile and if it is on a tableau, cell or reserve pile,
-// reorders the pile refs so that the largest pile is first
-void game_state::place_card(pile::ref pr, card c) {
- piles[pr].place(c);
-
-#ifndef NO_PILE_SYMMETRY
- // If the stock deals to the tableau piles, there is no pile symmetry
- if (rules.stock_size == 0 || rules.stock_deal_t != sdt::TABLEAU_PILES) {
- eval_pile_order(pr, true);
- }
-#endif
-}
-
-// Same as above but for taking cards
-card game_state::take_card(pile::ref pr) {
- card c = piles[pr].take();
-#ifndef NO_PILE_SYMMETRY
- // If the stock deals to the tableau piles, there is no pile symmetry
- if (rules.stock_size == 0 || rules.stock_deal_t != sdt::TABLEAU_PILES) {
- eval_pile_order(pr, false);
- }
-#endif
- return c;
-}
-
-#ifndef NDEBUG
-void game_state::check_face_down_consistent() const {
- for (auto& p : original_tableau_piles) {
- if (piles[p].empty()) continue;
- // Makes sure the top cards of each tableau pile are face up
- assert(!piles[p].top_card().is_face_down());
-
- // Makes sure face down cards are never above face down ones
- bool seen_face_up = false;
- for (auto& c : piles[p].pile_vec) {
- seen_face_up = seen_face_up || !c.is_face_down();
- assert(!(c.is_face_down() && seen_face_up));
- }
- }
-}
-#endif
-
-
-////////////////////////
-// INSPECT GAME STATE //
-////////////////////////
-
-bool game_state::is_solved() const {
- bool solved = true;
- if (rules.hole) {
- solved = piles[hole].size()
- == rules.max_rank * 4 * (rules.two_decks ? 2 : 1);
- } else if (rules.foundations_present) {
- for (auto f : foundations) {
- if (piles[f].size() != rules.max_rank) {
- solved = false;
- }
- }
- } else if (rules.sequence_count > 0) {
- for (pile::ref i = 0; i < sequences.size() && solved; i++) {
- for (pile::ref j = piles[sequences[i]].size(); j-- > 2;) {
- if (!is_next_legal_card(rules.sequence_build_pol, piles[sequences[i]][j-1], piles[sequences[i]][j])) {
- solved = false;
- break;
- }
- }
- if (piles[sequences[i]].top_card() != "AS") solved = false;
- }
- } else if (rules.accordion_size > 0) {
- return accordion.size() == 1;
- } else {
- assert(false);
- }
-
- // Runs some alternative checks in debug mode to make sure the game state
- // is consistent
-#ifndef NDEBUG
- bool rest_empty = true;
- for (pile::ref pr = 0; pr < piles.size(); pr++) {
- // All piles other than the hole must be empty
- if (rules.hole) {
- if (pr != hole && !piles[pr].empty()) {
- rest_empty = false;
- break;
- }
- }
- // All piles other than the foundations must be empty
- else if (rules.foundations_present) {
- bool non_foundation_ref =
- pr < foundations.front()
- || pr >= foundations.front() + foundations.size();
- if (non_foundation_ref && !piles[pr].empty()) {
- rest_empty = false;
- break;
- }
- }
- }
- if (rules.sequence_count > 0) {
- for (auto seq : sequences) {
- assert(piles[seq].size() == rules.max_rank);
- }
- } else {
- assert(solved == rest_empty);
- }
-#endif
-
- return solved;
-}
-
-const std::vector<pile>& game_state::get_data() const {
- return piles;
-}
-
-
-///////////
-// PRINT //
-///////////
-
-ostream& operator<< (ostream& str, const game_state& gs) {
- return state_printer::print(str, gs);
-}
-
diff --git a/solvitaire/src/main/game/search-state/game_state.dominance_moves.cpp b/solvitaire/src/main/game/search-state/game_state.dominance_moves.cpp
deleted file mode 100644
index d0b4b43..0000000
--- a/solvitaire/src/main/game/search-state/game_state.dominance_moves.cpp
+++ /dev/null
@@ -1,192 +0,0 @@
-//
-// Created by thecharlesblake on 07/06/18.
-//
-
-#include "game_state.h"
-#include "../sol_rules.h"
-
-
-typedef sol_rules::build_policy pol;
-typedef game_state::streamliner_options sos;
-typedef sol_rules::stock_deal_type sdt;
-typedef sol_rules::spaces_policy s_pol;
-
-using std::max;
-using boost::optional;
-
-bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const {
- if (rules.foundations_only_comp_piles || rules.two_decks)
- return false;
- else if ( stream_opts == sos::AUTO_FOUNDATIONS
- || stream_opts == sos::BOTH
- || rules.build_pol == pol::NO_BUILD
- || rules.build_pol == pol::SAME_SUIT)
- return true;
-
- card::suit_t target_suit(target_pile - foundations.front());
- card::rank_t move_up_rank = piles[target_pile].empty()
- ? card::rank_t(1)
- : foundation_base_convert(piles[target_pile].top_card().get_rank() + card::rank_t(1));
-
- // The card to be moved up
- card move_up_card(target_suit, move_up_rank);
-
- // The max difference between the 'move up' card and the foundations of the
- // same colour and the other colour
- int same_rank_diff = 0;
- int other_rank_diff = 0;
- for (pile::ref pr : foundations) {
- if (pr == target_pile) continue;
-
- card::suit_t s(pr - foundations.front());
- card::rank_t r = piles[pr].empty() ? card::rank_t(0)
- : foundation_base_convert(piles[pr].top_card().get_rank());
- card c(s, r);
-
- auto rank_diff = int(move_up_card.get_rank() - r);
- if (move_up_card.get_suit() == c.get_suit()
- || (rules.build_pol == pol::RED_BLACK
- && move_up_card.get_colour() == c.get_colour())) {
- same_rank_diff = rank_diff;
- } else {
- other_rank_diff = max(other_rank_diff, rank_diff);
- }
- }
-
- bool other_within_1 = other_rank_diff <= 1;
- bool other_within_2 = other_rank_diff <= 2;
- bool same_within_3 = same_rank_diff <= 3;
-
- if (rules.build_pol == pol::RED_BLACK) {
- return other_within_1 || (other_within_2 && same_within_3);
- } else {
- assert(rules.build_pol == pol::ANY_SUIT);
- return other_within_2;
- }
-}
-
-// Returns a dominance move if one is available
-optional<move> game_state::get_dominance_move() const {
-#ifndef NO_AUTO_FOUNDATIONS
- if (rules.spaces_pol == s_pol::AUTO_RESERVE_THEN_WASTE) {
- optional<move> arm = auto_reserve_move();
- if (arm) return arm;
- } else if (rules.spaces_pol == s_pol::AUTO_WASTE_THEN_STOCK) {
- optional<move> awsm = auto_waste_stock_move();
- if (awsm) return awsm;
- }
-
- // If there are 2 decks or no foundations, return
- if (!rules.foundations_present || rules.two_decks)
- return boost::none;
-
- // Cycles through the piles and sees if any cards can be automatically moved
- // to the foundations
- for (pile::ref pr = 0; pr < piles.size(); pr++) {
- // Don't move foundation cards, hole, waste or stock cards to the foundations
- if ((pr >= foundations.front() && pr <= foundations.back())
- || (rules.hole && pr == hole)
- || (rules.stock_size > 0 && pr == stock)
- || (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && pr == waste)
- || (piles[pr].empty())) {
- continue;
- }
-
- card c = piles[pr].top_card();
- pile::ref target_foundation = foundations[c.get_suit()];
-
- // If the card is the right rank and the auto-move boolean is true, then
- // returns the move
- card::rank_t target_rank = piles[target_foundation].empty()
- ? card::rank_t(1)
- : foundation_base_convert(piles[target_foundation].top_card().get_rank() + card::rank_t(1));
- if (target_rank == foundation_base_convert(c.get_rank()) &&
- auto_foundation_moves[c.get_suit()]) {
- move m(move::mtype::dominance, pr, target_foundation);
-
- // If this dominance move reveals a card, adds this to the move
- if (piles[pr].size() > 1 && piles[pr][1].is_face_down())
- m.make_reveal_move();
-
- return m;
- }
- }
-#endif
-
- return boost::none;
-}
-
-optional<move> game_state::auto_reserve_move() const {
- if (rules.spaces_pol == s_pol::AUTO_RESERVE_THEN_WASTE && !piles[reserve.front()].empty()) {
- for (auto to : tableau_piles) {
- if (piles[to].empty()) {
- return move(move::mtype::dominance, reserve.front(), to);
- }
- }
- }
- return boost::none;
-}
-
-optional<move> game_state::auto_waste_stock_move() const {
- if (!piles[waste].empty()) {
- for (auto to : tableau_piles) {
- if (piles[to].empty()) {
- return move(move::mtype::dominance, waste, to);
- }
- }
- } else if (!piles[stock].empty()) {
- for (auto to : tableau_piles) {
- if (piles[to].empty()) {
- return move(move::mtype::dominance, stock, to);
- }
- }
- }
- return boost::none;
-}
-
-bool game_state::is_ordered_pile(pile::ref pr) const {
- if (piles[pr].size() != rules.max_rank) return false;
-
- card::suit_t s = piles[pr][0].get_suit();
- for (card::rank_t r = 0; r < rules.max_rank; r++) {
- if (piles[pr][r] != card(s, card::rank_t(r + 1))) return false;
- }
- return true;
-}
-
-// For games where the foundations can be removed from, this dominance blocks
-// the foundations being removed from if the card that were to be removed would
-// go 'automatically' up
-bool game_state::dominance_blocks_foundation_move(pile::ref target_pile) {
- assert(!piles[target_pile].empty());
-
- card target_card = piles[target_pile].top_card();
- piles[target_pile].take();
-
- bool blocked = is_valid_auto_foundation_move(target_pile);
- piles[target_pile].place(target_card);
-
- return blocked;
-}
-
-// Decides which 'auto foundation' booleans should be 'true' currently
-void game_state::update_auto_foundation_moves(pile::ref target_pile) {
- // If there are no foundations, or this is not a foundation move, do nothing
- if (!rules.foundations_present
- || target_pile < foundations.front()
- || target_pile > foundations.back()) {
- return;
- }
-
- // Turns the 'auto' boolean for the target foundation false
-
- for (pile::ref pr : foundations) {
- auto_foundation_moves[pr] = is_valid_auto_foundation_move(pr);
- }
-}
-
-card::rank_t game_state::foundation_base_convert(card::rank_t r) const {
- card::rank_t s = r - (foundations_base - card::rank_t(1));
- if (s < 1) s += rules.max_rank;
- return s;
-}
diff --git a/solvitaire/src/main/game/search-state/game_state.h b/solvitaire/src/main/game/search-state/game_state.h
deleted file mode 100644
index 1698122..0000000
--- a/solvitaire/src/main/game/search-state/game_state.h
+++ /dev/null
@@ -1,182 +0,0 @@
-//
-// Created by thecharlesblake on 10/24/17.
-//
-
-#ifndef SOLVITAIRE_GAME_STATE_H
-#define SOLVITAIRE_GAME_STATE_H
-
-#include <vector>
-#include <list>
-#include <string>
-#include <random>
-#include <functional>
-
-#include <boost/functional/hash.hpp>
-#include <boost/optional/optional.hpp>
-
-#include "document.h"
-#include "../card.h"
-#include "../pile.h"
-#include "../sol_rules.h"
-#include "../move.h"
-
-class game_state {
- friend class hasher;
- friend class global_cache;
- friend class cached_game_state;
- friend class deal_parser;
- friend class state_printer;
- friend class test_helper;
- friend class json_helper;
-public:
- enum class streamliner_options {NONE, AUTO_FOUNDATIONS, SUIT_SYMMETRY, BOTH};
-
- /* Constructors */
-
- // Creates a game state representation from a JSON doc
- game_state(const sol_rules&, const rapidjson::Document&, streamliner_options);
- // Does the same from a seed
- game_state(const sol_rules&, int seed, streamliner_options);
- // Does the same but with an initialiser list (useful for testing)
- game_state(const sol_rules&, std::initializer_list<std::initializer_list<std::string>>);
- // Does the same but with vectors
- game_state(const sol_rules&, std::vector<std::vector<std::string>>);
- // Does the same but with lots of vectors
- game_state(const sol_rules&, std::vector<std::vector<card::suit_t>>,
- std::vector<std::vector<card::rank_t>>,
- std::vector<std::vector<bool>>);
-
- /* Altering state */
-
- void make_move(move);
- void undo_move(move);
- void place_card(pile::ref, card);
- card take_card(pile::ref);
-
- /* Legal move generation */
-
- std::vector<move> get_legal_moves(move = move(move::mtype::regular));
- boost::optional<move> get_dominance_move() const;
-
- /* State inspection */
-
- bool is_solved() const;
- const std::vector<pile>& get_data() const;
-
- /* Printing */
-
- friend std::ostream& operator<< (std::ostream&, const game_state&);
-
-private:
- /* Constructors (& helper function) */
-
- explicit game_state(const sol_rules&, streamliner_options);
- static std::vector<card> gen_shuffled_deck(card::rank_t, bool, std::mt19937);
- template<class RandomIt, class URBG> static void shuffle(RandomIt, RandomIt, URBG&&);
-
- /* Pile order logic */
-
- void eval_pile_order(pile::ref, bool);
- void eval_pile_order(std::list<pile::ref>&, pile::ref, bool);
-
- /* Altering state */
-
- void make_regular_move(move move);
- void undo_regular_move(move move);
- void make_built_group_move(move move);
- void undo_built_group_move(move move);
- void make_stock_k_plus_move(move move);
- void undo_stock_k_plus_move(move move);
- void make_stock_to_all_tableau_move(move move);
- void undo_stock_to_all_tableau_move(move move);
- void make_sequence_move(move move);
- void undo_sequence_move(move move);
- void make_accordion_move(move move);
- void undo_accordion_move(move move);
-
-#ifndef NDEBUG
- void check_face_down_consistent() const;
-#endif
-
- /* Legal move generation */
-
- bool stock_can_deal_all_tableau() const;
- move get_stock_to_all_tableau_move() const;
-
- std::set<std::pair<int8_t, bool>, std::greater<>> generate_k_plus_moves_to_check() const;
- void add_stock_to_cell_move(std::vector<move>&, pile::ref) const;
- void add_stock_to_tableau_moves(std::vector<move>&) const;
- void add_stock_to_hole_foundation_moves(std::vector<move>&) const;
- card stock_card_from_count(int8_t) const;
- void add_foundation_complete_piles_moves(std::vector<move> &) const;
- void add_accordion_moves(std::vector<move>&) const;
-
- bool is_valid_tableau_move(pile::ref, pile::ref) const;
- bool is_valid_tableau_move(card, pile::ref) const;
- bool is_next_tableau_card(card, card) const;
- bool is_valid_foundations_move(pile::ref, pile::ref) const;
- bool is_valid_foundations_move(card, pile::ref) const;
- bool is_valid_hole_move(pile::ref) const;
- bool is_valid_hole_move(card) const;
-
- void add_valid_tableau_moves(std::vector<move>&, pile::ref) const;
- void add_built_group_moves(std::vector<move>&, bool) const;
- void add_built_group_moves(std::vector<move>&, pile::ref, pile::size_type, bool) const;
- void add_whole_pile_moves(std::vector<move>&) const;
- void add_whole_pile_moves(std::vector<move>&, pile::ref, pile::size_type) const;
- pile::size_type get_built_group_height(pile::ref) const;
- bool is_next_built_group_card(card, card) const;
- void add_empty_built_group_moves(std::vector<move>&, pile::ref, pile::ref, pile::size_type, bool, bool) const;
- void add_kings_only_built_group_move(std::vector<move>&, pile::ref, pile::ref, pile::size_type, bool) const;
- void add_non_empty_built_group_move(std::vector<move>&, pile::ref, pile::ref, pile::size_type, bool, bool) const;
- void add_sequence_moves(std::vector<move>&) const;
- bool tableau_space_and_auto_reserve() const;
-
- bool is_next_legal_card(sol_rules::build_policy, card, card) const;
- bool is_next_legal_card(std::vector<sol_rules::accordion_policy>, card, card) const;
- void turn_face_down_cards(std::vector<move>&) const;
-
- /* Auto-foundation moves */
-
- boost::optional<move> auto_reserve_move() const;
- boost::optional<move> auto_waste_stock_move() const;
- bool is_valid_auto_foundation_move(pile::ref) const;
- bool is_ordered_pile(pile::ref) const;
- bool dominance_blocks_foundation_move(pile::ref);
- void update_auto_foundation_moves(pile::ref);
- card::rank_t foundation_base_convert(card::rank_t) const;
-
- /* Game rules */
-
- const sol_rules rules;
- streamliner_options stream_opts;
- card::rank_t foundations_base;
-
- /* Pile references */
-
- std::list<pile::ref> tableau_piles;
- std::list<pile::ref> cells;
- pile::ref stock;
- pile::ref waste;
- std::list<pile::ref> reserve;
- std::vector<pile::ref> foundations;
- std::vector<pile::ref> sequences;
- std::list<pile::ref> accordion;
- pile::ref hole;
-
- /* Pile references of starting/original layout */
-
- std::vector<pile::ref> original_tableau_piles;
- std::vector<pile::ref> original_cells;
- std::vector<pile::ref> original_reserve;
-
- /* Auto-foundation moves state */
-
- std::vector<bool> auto_foundation_moves;
-
- /* Core piles */
-
- std::vector<pile> piles;
-};
-
-#endif //SOLVITAIRE_GAME_STATE_H
diff --git a/solvitaire/src/main/game/search-state/game_state.legal_moves.cpp b/solvitaire/src/main/game/search-state/game_state.legal_moves.cpp
deleted file mode 100644
index 63a4764..0000000
--- a/solvitaire/src/main/game/search-state/game_state.legal_moves.cpp
+++ /dev/null
@@ -1,640 +0,0 @@
-//
-// Created by thecharlesblake on 07/06/18.
-//
-
-#include "game_state.h"
-#include "../sol_rules.h"
-#include "../move.h"
-
-#include <boost/optional/optional.hpp>
-#include <iostream>
-
-using std::vector;
-using std::min;
-using std::list;
-using std::pair;
-using std::set;
-using std::greater;
-using namespace rapidjson;
-
-typedef sol_rules::build_policy pol;
-typedef sol_rules::spaces_policy s_pol;
-typedef sol_rules::accordion_policy acc_pol;
-typedef sol_rules::stock_deal_type sdt;
-typedef sol_rules::face_up_policy fu;
-typedef sol_rules::direction dir;
-typedef sol_rules::built_group_type bgt;
-
-////////////////////////////////
-// MAIN LEGAL MOVE GEN CYCLE ///
-////////////////////////////////
-
-// Note that the moves added last here, are tried first
-vector<move> game_state::get_legal_moves(move parent_move) {
- // Order:
- // Accordion moves
- // Foundations complete piles moves
- // Tableau / cells / reserve / stock-waste (redeal) to hole / foundation moves
- // Cell to tableau moves
- // Sequence to sequence moves
- // Tableau to tableau moves
- // Tableau built group moves
- // Stock-waste to tableau moves (redeal)
- // Reserve to tableau moves
- // Foundation to tableau moves
- // Stock-waste to tableau moves (no redeal)
- // Stock-waste (no redeal) to hole / foundation moves
- // Tableau / reserve / stock-waste to cell moves
- // Stock to all tableau moves
-
- vector<move> moves;
-
- // Stock to all tableau moves
- if (rules.stock_size > 0 && stock_can_deal_all_tableau())
- moves.emplace_back(get_stock_to_all_tableau_move());
-
- // Tableau / reserve / stock-waste to cell moves
- pile::ref empty_cell = 255;
- for (auto c : cells) {
- if (piles[c].empty()) empty_cell = c;
- }
- if (empty_cell != 255) {
- for (auto t : tableau_piles) {
- if (piles[t].empty() || parent_move.to == t) continue;
- else moves.emplace_back(move::mtype::regular, t, empty_cell);
- }
-
- for (auto r : reserve)
- if (!piles[r].empty())
- moves.emplace_back(move::mtype::regular, r, empty_cell);
-
- if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE)
- add_stock_to_cell_move(moves, empty_cell);
- }
-
- // Stock-waste (no redeal) to hole / foundation moves
- if (rules.hole || (rules.foundations_present && !rules.foundations_only_comp_piles)) {
- if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !rules.stock_redeal)
- add_stock_to_hole_foundation_moves(moves);
- }
-
- // Stock-waste to tableau moves (no redeal)
- if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !rules.stock_redeal)
- add_stock_to_tableau_moves(moves);
-
- // Foundation to tableau / empty cell moves
- if (rules.foundations_removable) {
- for (auto f : foundations) {
- if (piles[f].empty() || parent_move.to == f || dominance_blocks_foundation_move(f)) continue;
-
- add_valid_tableau_moves(moves, f);
-
- if (empty_cell != 255)
- moves.emplace_back(move::mtype::regular, f, empty_cell);
- }
- }
-
- // Reserve to tableau moves
- for (auto r : reserve) {
- if (piles[r].empty()) continue;
-
- add_valid_tableau_moves(moves, r);
- }
-
- // Stock-waste to tableau moves (redeal)
- if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && rules.stock_redeal)
- add_stock_to_tableau_moves(moves);
-
- // Tableau built group moves
- switch (rules.move_built_group) {
- case sol_rules::built_group_type::YES:
- add_built_group_moves(moves, false);
- break;
- case sol_rules::built_group_type::MAXIMAL_GROUP:
- add_built_group_moves(moves, true);
- break;
- case sol_rules::built_group_type::WHOLE_PILE:
- add_whole_pile_moves(moves);
- break;
- case sol_rules::built_group_type::NO:
- break;
- }
-
- // Tableau to tableau moves
- // If only whole pile moves are available, doesn't make regular ones
- if (rules.move_built_group != bgt::WHOLE_PILE) {
- for (auto t_from : tableau_piles) {
- // Forbids moves from empty piles, reversing parent moves, or from single-
- // card piles to empty piles
- if (piles[t_from].empty() || parent_move.to == t_from || tableau_space_and_auto_reserve()) continue;
-
- for (auto to : tableau_piles) {
- if (is_valid_tableau_move(t_from, to)
- && !(piles[t_from].size() == 1 && piles[to].empty())) {
- moves.emplace_back(move::mtype::regular, t_from, to);
- }
- }
- }
- }
-
- // Sequence to sequence moves
- if (rules.sequence_count > 0) {
- add_sequence_moves(moves);
- }
-
- // Cell to tableau moves
- for (auto c : cells) {
- if (piles[c].empty() || parent_move.to == c) continue;
-
- add_valid_tableau_moves(moves, c);
- }
-
- // Tableau / cells / reserve / stock-waste (redeal) to hole / foundation moves
- if (rules.hole || (rules.foundations_present && !rules.foundations_only_comp_piles)) {
- // Stock
- if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && rules.stock_redeal)
- add_stock_to_hole_foundation_moves(moves);
-
- list<pile::ref> from_piles = tableau_piles;
- if (rules.cells > 0) from_piles.insert(from_piles.end(), cells.begin(), cells.end());
- if (rules.reserve_size > 0) from_piles.insert(from_piles.end(), reserve.begin(), reserve.end());
-
- for (auto fp : from_piles) {
- if (piles[fp].empty() || (parent_move.to == fp && parent_move.type != move::mtype::dominance)) continue;
-
- for (auto f : foundations)
- if (is_valid_foundations_move(fp, f))
- moves.emplace_back(move::mtype::regular, fp, f);
- if (rules.hole && is_valid_hole_move(fp))
- moves.emplace_back(move::mtype::regular, fp, hole);
- }
- }
-
- if (rules.foundations_only_comp_piles) // i.e. Spider-type winning condition
- add_foundation_complete_piles_moves(moves);
-
- if (rules.tableau_pile_count > 0 && rules.face_up != fu::ALL)
- turn_face_down_cards(moves);
-
- if (rules.accordion_size > 0)
- add_accordion_moves(moves);
-
- return moves;
-}
-
-
-////////////////////////////////
-// REGULAR MOVE GEN FUNCTIONS //
-////////////////////////////////
-
-bool game_state::stock_can_deal_all_tableau() const {
- return rules.stock_deal_t == sdt::TABLEAU_PILES
- && !piles[stock].empty();
-}
-
-// This is a special kind of move which the solver handles differently.
-// We must supply the number of stock cards
-// that we will deal, so that the move can be undone in backtracking.
-move game_state::get_stock_to_all_tableau_move() const {
- pile::size_type stock_moves =
- piles[stock].size() >= tableau_piles.size()
- ? pile::size_type(tableau_piles.size())
- : piles[stock].size();
-
- assert(stock_moves > 0 && stock_moves <= piles[stock].size());
- return move(move::mtype::stock_to_all_tableau, 0, 0, stock_moves);
-}
-
-set<pair<int8_t, bool>, greater<>> game_state::generate_k_plus_moves_to_check() const {
- set<pair<int8_t, bool>, greater<>> stock_moves_to_check;
-
- // If the waste isn't empty, adds the move from the top of the current waste
- if (!piles[waste].empty()) stock_moves_to_check.insert(pair<int8_t, bool>(0, false));
- else if (piles[stock].empty()) return stock_moves_to_check;
-
- // Adds multiples of deal count (excluding last card)
- for (int8_t count = rules.stock_deal_count;
- count < piles[stock].size();
- count += rules.stock_deal_count)
- stock_moves_to_check.insert(pair<int8_t, bool>(count, false));
-
- // Adds last card
- bool flip_waste = rules.stock_redeal;
- stock_moves_to_check.insert(pair<int8_t, bool>(static_cast<int8_t>(piles[stock].size()), flip_waste));
-
- // If the stock can be redealt, searches through the waste then the stock again
- if (rules.stock_redeal)
- for (auto count = static_cast<int8_t>(-piles[waste].size() + rules.stock_deal_count);
- count < piles[stock].size();
- count += rules.stock_deal_count)
- stock_moves_to_check.insert(pair<int8_t, bool>(count, false));
-
- return stock_moves_to_check;
-}
-
-void game_state::add_stock_to_cell_move(std::vector<move>& moves, pile::ref empty_cell) const {
- for (auto k_plus_mv : generate_k_plus_moves_to_check())
- moves.emplace_back(move::mtype::stock_k_plus, stock, empty_cell, k_plus_mv.first, false, k_plus_mv.second);
-}
-
-void game_state::add_stock_to_tableau_moves(std::vector<move>& moves) const {
- // For each stock move to check, if it can be moved legally to one of the tableau
- // piles, adds this as a move
- for (auto k_plus_mv : generate_k_plus_moves_to_check()) {
- card from = stock_card_from_count(k_plus_mv.first);
-
- // Obeys the auto-reserve restriction unless the reserve is empty
- if (!piles[reserve.front()].empty() && tableau_space_and_auto_reserve()) return;
-
- for (auto t : tableau_piles) {
- if (is_valid_tableau_move(from, t)) {
- moves.emplace_back(move::mtype::stock_k_plus, stock, t, k_plus_mv.first, false, k_plus_mv.second);
- }
- }
- }
-}
-
-void game_state::add_stock_to_hole_foundation_moves(std::vector<move>& moves) const {
- for (auto k_plus_mv : generate_k_plus_moves_to_check()) {
- card from = stock_card_from_count(k_plus_mv.first);
-
- for (auto f : foundations)
- if (is_valid_foundations_move(from, f))
- moves.emplace_back(move::mtype::stock_k_plus, stock, f, k_plus_mv.first, false, k_plus_mv.second);
-
- if (rules.hole && is_valid_hole_move(from))
- moves.emplace_back(move::mtype::stock_k_plus, stock, hole, k_plus_mv.first, false, k_plus_mv.second);
- }
-}
-
-card game_state::stock_card_from_count(int8_t count) const {
- if (count > 0) {
- return piles[stock][count - 1];
- } else {
- return piles[waste][-count];
- }
-}
-
-void game_state::add_foundation_complete_piles_moves(std::vector<move>& moves) const {
- for (pile::ref tab_pr : tableau_piles) {
- if (is_ordered_pile(tab_pr)) {
-
- for (pile::ref found_pr : foundations) {
- if (piles[found_pr].empty()) {
- moves.emplace_back(move::mtype::built_group, tab_pr, found_pr, rules.max_rank);
- break;
- }
- }
- }
- }
-}
-
-bool game_state::is_valid_tableau_move(const pile::ref rem_ref,
- const pile::ref add_ref) const {
- if (rem_ref == add_ref || rules.build_pol == pol::NO_BUILD)
- return false;
-
- return is_valid_tableau_move(piles[rem_ref].top_card(), add_ref);
-}
-
-bool game_state::is_valid_tableau_move(const card rem_c,
- const pile::ref add_ref) const {
- if (piles[add_ref].empty()) {
- switch(rules.spaces_pol) {
- case sol_rules::spaces_policy::NO_BUILD:
- return false;
- case sol_rules::spaces_policy::KINGS:
- return rem_c.get_rank() == 13;
- default:
- return true;
- }
- } else {
- card add_c = piles[add_ref].top_card();
-
- return is_next_tableau_card(add_c, rem_c);
- }
-}
-
-bool game_state::is_next_tableau_card(card a, card b) const {
- return is_next_legal_card(rules.build_pol, a, b);
-}
-
-bool game_state::is_valid_foundations_move(const pile::ref rem_ref,
- const pile::ref add_ref) const {
- if (rem_ref == add_ref || rules.foundations_only_comp_piles) return false;
-
- return is_valid_foundations_move(piles[rem_ref].top_card(), add_ref);
-}
-
-bool game_state::is_valid_foundations_move(const card rem_c,
- const pile::ref add_ref) const {
- if (piles[add_ref].size() == rules.max_rank) return false;
-
- // Checks same suit policy
- uint8_t suit_idx = (add_ref - foundations.front()) % uint8_t(4);
- if (rem_c.get_suit() != suit_idx)
- return false;
-
- // Checks rank
- if (piles[add_ref].empty())
- return rem_c.get_rank() == foundations_base;
- else
- return rem_c.get_rank() == (piles[add_ref].top_card().get_rank() % rules.max_rank) + 1;
-}
-
-bool game_state::is_valid_hole_move(const pile::ref rem_ref) const {
- if (rem_ref == hole) return false;
- return is_valid_hole_move(piles[rem_ref].top_card());
-}
-
-bool game_state::is_valid_hole_move(const card c) const {
- card::rank_t rank = c.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 MOVE GEN FUNCTIONS //
-////////////////////////////////////
-
-void game_state::add_valid_tableau_moves(std::vector<move>& moves, pile::ref from) const {
- if (tableau_space_and_auto_reserve()) return;
-
- for (auto to : tableau_piles) {
- if (is_valid_tableau_move(from, to)) {
- moves.emplace_back(move::mtype::regular, from, to);
- }
- }
-}
-
-void game_state::add_built_group_moves(vector<move>& moves, bool only_maximal) const {
- assert(rules.built_group_pol != pol::NO_BUILD);
- if (tableau_space_and_auto_reserve()) return;
-
- // Cycles through each pile to see if it contains a built group
- 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;
-
- add_built_group_moves(moves, rem_ref, built_group_height, only_maximal);
- }
-}
-
-void game_state::add_built_group_moves(vector<move>& moves, pile::ref rem_ref, pile::size_type built_group_height,
- bool only_maximal) const {
- // We have found a built group. Cycles through each pile to see if it can be added
- for (auto add_ref : tableau_piles) {
- if (add_ref == rem_ref) continue;
-
- card bg_high = piles[rem_ref][built_group_height - 1];
- bool base_face_down =
- piles[rem_ref].size() > built_group_height
- && piles[rem_ref][built_group_height].is_face_down();
-
- if (piles[add_ref].empty()) {
- if (rules.spaces_pol == s_pol::ANY) {
- add_empty_built_group_moves(moves, rem_ref, add_ref, built_group_height, base_face_down, only_maximal);
- } else if (rules.spaces_pol == s_pol::KINGS && bg_high.get_rank() == 13) {
- add_kings_only_built_group_move(moves, rem_ref, add_ref, built_group_height, base_face_down);
- }
- } else {
- add_non_empty_built_group_move(moves, rem_ref, add_ref, built_group_height, base_face_down, only_maximal);
- }
- }
-}
-
-void game_state::add_whole_pile_moves(vector<move>& moves) const {
- assert(rules.built_group_pol != pol::NO_BUILD);
- if (tableau_space_and_auto_reserve()) return;
-
- // Cycles through each pile to see if it contains a whole-pile built group
- for (auto rem_ref : tableau_piles) {
- auto built_group_height = get_built_group_height(rem_ref);
-
- if (built_group_height == piles[rem_ref].size()) {
- add_whole_pile_moves(moves, rem_ref, built_group_height);
- }
- }
-}
-
-void game_state::add_whole_pile_moves(vector<move>& moves, pile::ref rem_ref, pile::size_type built_group_height) const {
- // We have found a built group. Cycles through each pile to see if it can be added
- for (auto add_ref : tableau_piles) {
- if (add_ref == rem_ref) continue;
-
- card bg_high = piles[rem_ref][built_group_height - 1];
-
- if (piles[add_ref].empty()) {
- if (rules.spaces_pol == s_pol::ANY || (rules.spaces_pol == s_pol::KINGS && bg_high.get_rank() == 13)) {
- moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, built_group_height);
- }
- } else if (is_next_built_group_card(piles[add_ref].top_card(), bg_high)) {
- moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, built_group_height);
- }
- }
-}
-
-// 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 {
- pile::size_type i = 1;
- while (i < piles[ref].size()
- && is_next_built_group_card(piles[ref][i], piles[ref][i-1])
- && !piles[ref][i].is_face_down())
- i++;
- return i;
-}
-
-bool game_state::is_next_built_group_card(card a, card b) const {
- return is_next_legal_card(rules.built_group_pol, a, b);
-}
-
-// Loops through each possible built group move to an empty pile and adds it to the list
-void game_state::add_empty_built_group_moves(vector<move>& moves, pile::ref rem_ref, pile::ref add_ref,
- pile::size_type built_group_height, bool base_face_down, bool only_maximal)
-const {
- auto start_idx = static_cast<pile::size_type>(only_maximal ? built_group_height - 1 : 1);
- for (pile::size_type card_idx = start_idx; card_idx < built_group_height; card_idx++) {
- bool is_reveal_move = card_idx + 1 == built_group_height && base_face_down;
- moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, card_idx + 1, is_reveal_move);
- }
-}
-
-void game_state::add_kings_only_built_group_move(vector<move>& moves, pile::ref rem_ref, pile::ref add_ref,
- pile::size_type built_group_height, bool base_face_down) const {
-
- moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, built_group_height, base_face_down);
-}
-
-void game_state::add_non_empty_built_group_move(vector<move>& moves, pile::ref rem_ref, pile::ref add_ref,
- pile::size_type built_group_height, bool base_face_down, bool only_maximal) const {
-
- auto start_idx = static_cast<pile::size_type>(only_maximal ? built_group_height - 1 : 1);
-
- // Given rank of add pile, get card of that rank - 1 in rem pile and see if next bg card
- // For each card going down from top card in rem pile, check if is next bg card
- for (pile::ref r = start_idx; r < built_group_height; r++) {
- if (is_next_built_group_card(piles[add_ref].top_card(), piles[rem_ref][r])) {
-
- bool is_reveal_move = r + 1 == built_group_height && base_face_down;
- moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, r + 1, is_reveal_move);
- break;
- }
- }
-}
-
-void game_state::add_sequence_moves(std::vector<move>& moves) const {
- // Note, the sequence moves are encoded by counting each card's location (1-52) and adding that into 'to' and 'from'
-
- // Finds the spaces
- vector<pair<uint8_t, uint8_t>> space_locations;
- for (pile::ref i = 0; i < sequences.size(); i++) {
-
- for (pile::ref j = piles[sequences[i]].size(); j --> 0 ;) {
- if (piles[sequences[i]][j] == "AS") {
- space_locations.emplace_back(i, j);
- }
- }
- }
-
- // Searches through the cards again to find valid moves into the spaces
- for (pile::ref i = 0; i < sequences.size(); i++) {
- for (pile::ref j = piles[sequences[i]].size(); j --> 0 ;) {
- pile::ref from_idx = i * piles[sequences[i]].size() + j; // Encoded index of card to be moved
- card from_card = piles[sequences[i]][j];
- if (from_card == "AS") continue;
-
- // Adds valid moves
- for (pair<uint8_t, uint8_t> space_loc : space_locations) {
- pile::ref space_idx = space_loc.first * piles[sequences[i]].size() + space_loc.second; // Encoded index of space
-
- if (rules.sequence_direction == dir::LEFT || rules.sequence_direction == dir::BOTH) {
- // Only twos in the far left column
- if (space_loc.second == piles[sequences[i]].size() - 1) {
- if (from_card.get_rank() == 2) {
- bool is_legal = false;
-
- if (rules.sequence_fixed_suit) {
- if (from_card.get_suit() == space_loc.first) {
- is_legal = true;
- }
- } else {
- is_legal = true;
- }
-
- if (is_legal) {
- moves.emplace_back(move::mtype::sequence, from_idx, space_idx);
- }
- }
- }
- // Otherwise must agree with left neighbour
- else {
- card neighbour_card = piles[sequences[space_loc.first]][space_loc.second + 1];
- if (neighbour_card != "AS" && is_next_legal_card(rules.sequence_build_pol, from_card, neighbour_card))
- moves.emplace_back(move::mtype::sequence, from_idx, space_idx);
- }
- }
-
- if (rules.sequence_direction == dir::RIGHT || rules.sequence_direction == dir::BOTH) {
- // Only max rank in the far right column
- if (space_loc.second == 0) {
- if (from_card.get_rank() == rules.max_rank)
- moves.emplace_back(move::mtype::sequence, from_idx, space_idx);
- }
- // Otherwise must agree with right neighbour
- else {
- card neighbour_card = piles[sequences[space_loc.first]][space_loc.second - 1];
- if (neighbour_card != "AS" && is_next_legal_card(rules.sequence_build_pol, neighbour_card, from_card))
- moves.emplace_back(move::mtype::sequence, from_idx, space_idx);
- }
- }
- }
- }
- }
-}
-
-void game_state::add_accordion_moves(vector<move>& moves) const {
- int idx = 0, idx_max = static_cast<int>(accordion.size());
- for (auto from_it = begin(accordion); from_it != end(accordion); from_it++, idx++) {
- for (pair<dir, uint8_t> move_rule : rules.accordion_moves) {
- auto to_it = from_it;
-
- if (move_rule.first == dir::LEFT && idx - move_rule.second >= 0) {
- for (uint8_t i = 0; i < move_rule.second; i++) to_it--;
- } else if (move_rule.first == dir::RIGHT && idx + move_rule.second < idx_max) {
- for (uint8_t i = 0; i < move_rule.second; i++) to_it++;
- } else {
- continue;
- }
-
- if (is_next_legal_card(rules.accordion_pol, piles[*to_it].top_card(), piles[*from_it].top_card()))
- moves.emplace_back(move::mtype::accordion, *from_it, *to_it, piles[*from_it].size());
- }
- }
-}
-
-// If auto-reserve is enabled and there is a space, returns
-bool game_state::tableau_space_and_auto_reserve() const {
- if (rules.spaces_pol == s_pol::AUTO_RESERVE_THEN_WASTE)
- for (auto to : tableau_piles)
- if (piles[to].empty()) return true;
- return false;
-}
-
-///////////////////////
-
-bool game_state::is_next_legal_card(pol p, card a, card b) const {
- // Checks build pol violations
- switch(p) {
- case sol_rules::build_policy::SAME_SUIT:
- if (b.get_suit() != a.get_suit()) return false;
- break;
- case sol_rules::build_policy::RED_BLACK:
- if (b.get_colour() == a.get_colour()) return false;
- break;
- default:;
- }
- // Checks rank
- auto final_rank = foundations_base == 1
- ? rules.max_rank
- : foundations_base - card::rank_t(1);
- if (b.get_rank() == final_rank) {
- return false;
- } else {
- return (b.get_rank() % rules.max_rank) + 1 == a.get_rank();
- }
-}
-
-bool game_state::is_next_legal_card(vector<acc_pol> vp, card a, card b) const {
- for (auto p : vp) {
- switch (p) {
- case sol_rules::accordion_policy::SAME_RANK:
- if (b.get_rank() == a.get_rank()) return true;
- break;
- case sol_rules::accordion_policy::SAME_SUIT:
- if (b.get_suit() == a.get_suit()) return true;
- break;
- case sol_rules::accordion_policy::RED_BLACK:
- if (b.get_colour() != a.get_colour()) return true;
- break;
- case sol_rules::accordion_policy::ANY_SUIT:
- return true;
- }
- }
- return false;
-}
-
-void game_state::turn_face_down_cards(vector<move>& moves) const {
- for (auto& m : moves) {
- bool is_tableau_move = m.from >= original_tableau_piles.front() && m.from <= original_tableau_piles.back();
- if (is_tableau_move && piles[m.from].size() > 1 && piles[m.from][1].is_face_down()) {
- m.make_reveal_move();
- }
- }
-}
diff --git a/solvitaire/src/main/game/search-state/game_state.pile_order.cpp b/solvitaire/src/main/game/search-state/game_state.pile_order.cpp
deleted file mode 100644
index 736cc89..0000000
--- a/solvitaire/src/main/game/search-state/game_state.pile_order.cpp
+++ /dev/null
@@ -1,80 +0,0 @@
-//
-// Created by thecharlesblake on 07/06/18.
-//
-
-#include "game_state.h"
-
-using std::list;
-using std::find;
-using std::rbegin;
-using std::rend;
-
-// Assesses whether the pile ref that was modified was a tableau, cell or
-// reserve pile, and if so makes the relevant function call
-void game_state::eval_pile_order(pile::ref pr, bool is_place) {
- if (!original_tableau_piles.empty()
- && pr >= original_tableau_piles[0]
- && pr < original_tableau_piles[0] + original_tableau_piles.size()) {
-
- eval_pile_order(tableau_piles, pr, is_place);
- } else if (!original_cells.empty()
- && pr >= original_cells[0]
- && pr < original_cells[0] + original_cells.size()) {
-
- eval_pile_order(cells, pr, is_place);
- } else if (!original_reserve.empty()
- && pr >= original_reserve[0]
- && pr < original_reserve[0] + original_reserve.size()) {
-
- eval_pile_order(reserve, pr, is_place);
- }
-}
-
-// Finds the pile ref in the list and evaluates whether it should be moved to
-// maintain the "pile order"
-void game_state::eval_pile_order(list<pile::ref>& pile_lst, pile::ref changed_pr,
- bool is_place) {
- // Finds the first pile that is larger/smaller than the ref pile
- // (the comp pile).
- // If the card has been placed, search for the ref from the right hand side
- // and if not from the lhs
- if (is_place) {
- auto changed_ref_iter = find(rbegin(pile_lst), rend(pile_lst), changed_pr);
- auto comp_iter = changed_ref_iter;
-
- while (++comp_iter != rend(pile_lst)
- && piles[*comp_iter] < piles[*changed_ref_iter]);
-
- if (comp_iter != ++changed_ref_iter) {
- // Places the ref pile before the comp pile
- pile_lst.insert(comp_iter.base(), changed_pr);
- pile_lst.erase(changed_ref_iter.base());
- }
- } else {
- auto changed_ref_iter = find(begin(pile_lst), end(pile_lst), changed_pr);
- auto comp_iter = changed_ref_iter;
-
- while (++comp_iter != end(pile_lst)
- && piles[*comp_iter] > piles[*changed_ref_iter]);
-
- if (comp_iter != ++changed_ref_iter) {
- // Places the ref pile before the comp pile
- pile_lst.insert(comp_iter, changed_pr);
- pile_lst.erase(--changed_ref_iter);
- }
- }
-
-#ifndef NDEBUG
- // Makes sure the piles are in order
- if (pile_lst.size() > 1) {
- auto prev = begin(pile_lst);
- auto cur = begin(pile_lst);
- cur++;
-
- while (cur != end(pile_lst)) {
- assert(piles[*prev] >= piles[*cur]);
- prev++; cur++;
- }
- }
-#endif
-}
diff --git a/solvitaire/src/main/game/sol_rules.cpp b/solvitaire/src/main/game/sol_rules.cpp
deleted file mode 100644
index 940b1a4..0000000
--- a/solvitaire/src/main/game/sol_rules.cpp
+++ /dev/null
@@ -1,43 +0,0 @@
-//
-// Created by thecharlesblake on 1/12/18.
-//
-
-#include "sol_rules.h"
-
-typedef sol_rules::build_policy pol;
-typedef sol_rules::spaces_policy s_pol;
-typedef sol_rules::stock_deal_type sdt;
-typedef sol_rules::direction dir;
-typedef sol_rules::face_up_policy fu;
-typedef sol_rules::built_group_type bgt;
-typedef sol_rules::foundations_init_type fit;
-
-// Note that the 'default' sol rules are set up by parsing the default json
-// rules description. This initialisation is really just for testing purposes.
-sol_rules::sol_rules() :
- tableau_pile_count(0),
- build_pol(pol::ANY_SUIT),
- spaces_pol(s_pol::ANY),
- move_built_group(bgt::NO),
- built_group_pol(pol::ANY_SUIT),
- two_decks(false),
- max_rank(13),
- hole(false),
- foundations_present(false),
- foundations_init_cards(fit::NONE),
- foundations_base(1),
- foundations_only_comp_piles(false),
- diagonal_deal(false),
- cells(0),
- cells_pre_filled(0),
- stock_size(0),
- stock_deal_t(sdt::TABLEAU_PILES),
- stock_deal_count(1),
- reserve_size(0),
- reserve_stacked(false),
- face_up(fu::ALL),
- sequence_count(0),
- sequence_direction(dir::LEFT),
- sequence_build_pol(pol::SAME_SUIT),
- accordion_size(0) {
-}
diff --git a/solvitaire/src/main/game/sol_rules.h b/solvitaire/src/main/game/sol_rules.h
deleted file mode 100644
index 6a6b575..0000000
--- a/solvitaire/src/main/game/sol_rules.h
+++ /dev/null
@@ -1,95 +0,0 @@
-//
-// Created by thecharlesblake on 10/28/17.
-//
-
-#ifndef SOLVITAIRE_SOL_RULES_H
-#define SOLVITAIRE_SOL_RULES_H
-
-#include <string>
-#include <vector>
-#include <boost/optional.hpp>
-
-#include "card.h"
-#include "../../../lib/rapidjson/document.h"
-
-struct sol_rules {
- sol_rules();
-
- // Tableau policies
- enum class build_policy {
- NO_BUILD,
- SAME_SUIT,
- RED_BLACK,
- ANY_SUIT
- };
- enum class spaces_policy {
- NO_BUILD,
- KINGS,
- ANY,
- AUTO_RESERVE_THEN_WASTE,
- AUTO_WASTE_THEN_STOCK
- };
- enum class accordion_policy {
- SAME_RANK,
- SAME_SUIT,
- RED_BLACK,
- ANY_SUIT
- };
- enum class stock_deal_type {
- WASTE,
- TABLEAU_PILES
- };
- enum class face_up_policy {
- ALL,
- TOP_CARDS
- };
- enum class direction {
- LEFT,
- RIGHT,
- BOTH
- };
- enum class built_group_type {
- YES,
- NO,
- WHOLE_PILE,
- MAXIMAL_GROUP
- };
- enum class foundations_init_type {
- NONE,
- ONE,
- ALL
- };
-
- uint8_t tableau_pile_count;
- build_policy build_pol;
- spaces_policy spaces_pol;
- built_group_type move_built_group;
- build_policy built_group_pol;
- bool two_decks;
- uint8_t max_rank;
- bool hole;
- bool foundations_present;
- foundations_init_type foundations_init_cards;
- boost::optional<card::rank_t> foundations_base;
- bool foundations_removable;
- bool foundations_only_comp_piles;
- bool diagonal_deal;
- uint8_t cells;
- uint8_t cells_pre_filled;
- uint8_t stock_size;
- stock_deal_type stock_deal_t;
- uint8_t stock_deal_count;
- bool stock_redeal;
- uint8_t reserve_size;
- bool reserve_stacked;
- face_up_policy face_up;
- uint8_t sequence_count;
- direction sequence_direction;
- build_policy sequence_build_pol;
- bool sequence_fixed_suit;
- uint8_t accordion_size;
- std::vector<std::pair<direction, uint8_t>> accordion_moves;
- std::vector<accordion_policy> accordion_pol;
-};
-
-#endif //SOLVITAIRE_SOL_RULES_H