aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorCharlie Blake2018-06-26 10:37:54 +0100
committerCharlie Blake2018-06-26 10:37:54 +0100
commit797400ea32ac1f6588e834fc1bcb42aa67fa6863 (patch)
tree16352c5245f2f94dadf31c3e510eedfc9254fc3d /src/main
parentAdd test to make sure cache encodes face up (diff)
downloadmetasol-797400ea32ac1f6588e834fc1bcb42aa67fa6863.tar.gz
metasol-797400ea32ac1f6588e834fc1bcb42aa67fa6863.zip

Finished klondike correct impl

Diffstat (limited to 'src/main')
-rw-r--r--src/main/game/card.cpp21
-rw-r--r--src/main/game/card.h3
-rw-r--r--src/main/game/move.cpp10
-rw-r--r--src/main/game/move.h5
-rw-r--r--src/main/game/search-state/game_state.auto_foundation_moves.cpp8
-rw-r--r--src/main/game/search-state/game_state.cpp60
-rw-r--r--src/main/game/search-state/game_state.h8
-rw-r--r--src/main/game/search-state/game_state.legal_moves.cpp31
8 files changed, 128 insertions, 18 deletions
diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp
index 12e577f..60aaa74 100644
--- a/src/main/game/card.cpp
+++ b/src/main/game/card.cpp
@@ -25,7 +25,8 @@ const card::colour_t card::colour::Red = 1;
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_possible) {} // TODO: CHANGE!
+ 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) {}
@@ -52,6 +53,18 @@ card::rank_t card::rank_from_str(const char* 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;
}
@@ -88,14 +101,16 @@ std::string card::to_string() const {
return s;
}
-bool card::is_face_up() const {
- return true;
+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();
diff --git a/src/main/game/card.h b/src/main/game/card.h
index ea808b4..7885d66 100644
--- a/src/main/game/card.h
+++ b/src/main/game/card.h
@@ -39,7 +39,7 @@ public:
colour_t get_colour() const;
rank_t get_rank() const;
- bool is_face_up() const;
+ bool is_face_down() const;
void turn_face_up();
void turn_face_down();
@@ -48,6 +48,7 @@ public:
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;
diff --git a/src/main/game/move.cpp b/src/main/game/move.cpp
index ff8269c..9e45c66 100644
--- a/src/main/game/move.cpp
+++ b/src/main/game/move.cpp
@@ -4,16 +4,22 @@
#include "move.h"
-move::move(mtype ty, pile::ref f, pile::ref t, pile::size_type i) : type(ty), from(f), to(t), count(i) {
+move::move(mtype ty, pile::ref f, pile::ref t, pile::size_type i, bool reveal_move_) :
+ type(ty), from(f), to(t), count(i), reveal_move(reveal_move_) {
#ifndef NDEBUG
if (ty == mtype::regular) assert(i == 1);
else assert(i >= 1);
#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.type == b.type )
+ && (a.reveal_move == b.reveal_move);
}
diff --git a/src/main/game/move.h b/src/main/game/move.h
index 1e3a9d1..8651d1e 100644
--- a/src/main/game/move.h
+++ b/src/main/game/move.h
@@ -10,13 +10,16 @@
struct move {
enum class mtype : uint8_t {regular, dominance, built_group, stock_to_waste, stock_to_tableau, redeal, null};
- explicit move(mtype, pile::ref = 255 , pile::ref = 255, pile::size_type = 1);
+ explicit move(mtype, pile::ref = 255 , pile::ref = 255, pile::size_type = 1, bool reveal_move_ = false);
friend bool operator==(const move&, const move&);
+ void make_reveal_move();
+
mtype type;
pile::ref from;
pile::ref to;
pile::size_type count;
+ bool reveal_move;
};
#endif //SOLVITAIRE_MOVE_H
diff --git a/src/main/game/search-state/game_state.auto_foundation_moves.cpp b/src/main/game/search-state/game_state.auto_foundation_moves.cpp
index 794c040..99b43a2 100644
--- a/src/main/game/search-state/game_state.auto_foundation_moves.cpp
+++ b/src/main/game/search-state/game_state.auto_foundation_moves.cpp
@@ -86,7 +86,13 @@ boost::optional<move> game_state::get_dominance_move() const {
+ card::rank_t(1);
if (target_rank == c.get_rank() &&
auto_foundation_moves[c.get_suit()]) {
- return move(move::mtype::dominance, pr, target_foundation);
+ 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
diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp
index 8a1298b..7870b1b 100644
--- a/src/main/game/search-state/game_state.cpp
+++ b/src/main/game/search-state/game_state.cpp
@@ -32,6 +32,7 @@ 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;
@@ -196,11 +197,11 @@ game_state::game_state(const sol_rules& s_rules, int seed, bool streamliners_)
}
game_state::game_state(const sol_rules& s_rules,
- std::initializer_list<pile> il)
- : game_state(s_rules, false) {
- pile::ref pr = 0;
- for (const pile& p : il) {
- for (const card c : p.pile_vec) {
+ std::initializer_list<std::initializer_list<string>> il)
+ : game_state(s_rules, false) {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++;
@@ -269,6 +270,10 @@ void game_state::make_move(const move m) {
assert(false);
break;
}
+
+#ifndef NDEBUG
+ check_face_down_consistent();
+#endif
}
void game_state::undo_move(const move m) {
@@ -293,6 +298,10 @@ void game_state::undo_move(const move m) {
assert(false);
break;
}
+
+#ifndef NDEBUG
+ check_face_down_consistent();
+#endif
}
void game_state::make_regular_move(const move m) {
@@ -301,6 +310,12 @@ void game_state::make_regular_move(const move m) {
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
@@ -309,6 +324,12 @@ void game_state::make_regular_move(const move m) {
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
@@ -331,6 +352,12 @@ void game_state::make_built_group_move(move m) {
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) {
@@ -338,6 +365,12 @@ void game_state::undo_built_group_move(move m) {
assert(m.to < piles.size());
assert(m.count <= rules.max_rank);
+ 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]);
@@ -427,6 +460,23 @@ card game_state::take_card(pile::ref pr) {
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 //
diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h
index f335de1..b8b0d03 100644
--- a/src/main/game/search-state/game_state.h
+++ b/src/main/game/search-state/game_state.h
@@ -32,7 +32,7 @@ public:
// Does the same from a seed
game_state(const sol_rules&, int seed, bool);
// Does the same but with an initialiser list (useful for testing)
- game_state(const sol_rules&, std::initializer_list<pile>);
+ game_state(const sol_rules&, std::initializer_list<std::initializer_list<std::string>>);
/* Altering state */
@@ -80,6 +80,10 @@ private:
void make_redeal_move();
void undo_redeal_move();
+#ifndef NDEBUG
+ void check_face_down_consistent() const;
+#endif
+
/* Legal move generation */
bool stock_can_deal_tableau() const;
@@ -107,6 +111,8 @@ private:
bool is_next_legal_card(sol_rules::build_policy, card, card) const;
+ void turn_face_down_cards(std::vector<move>&) const;
+
/* Auto-foundation moves */
bool is_valid_auto_foundation_move(pile::ref) const;
diff --git a/src/main/game/search-state/game_state.legal_moves.cpp b/src/main/game/search-state/game_state.legal_moves.cpp
index b7b910e..9ad8b1d 100644
--- a/src/main/game/search-state/game_state.legal_moves.cpp
+++ b/src/main/game/search-state/game_state.legal_moves.cpp
@@ -3,6 +3,7 @@
//
#include "game_state.h"
+#include "../sol_rules.h"
#include <boost/optional/optional.hpp>
@@ -14,6 +15,7 @@ using namespace rapidjson;
typedef sol_rules::build_policy pol;
typedef sol_rules::spaces_policy s_pol;
typedef sol_rules::stock_deal_type sdt;
+typedef sol_rules::face_up_policy fu;
////////////////////////////////
@@ -136,9 +138,11 @@ vector<move> game_state::get_legal_moves(move parent_move) {
if (rules.move_built_group)
add_built_group_moves(moves);
- if (rules.foundations_comp_piles) // I.E. Spider-type winning condition
+ if (rules.foundations_comp_piles) // i.e. Spider-type winning condition
add_foundation_complete_piles_moves(moves);
+ if (rules.face_up != fu::ALL)
+ turn_face_down_cards(moves);
/*
vector<move> moves;
@@ -185,6 +189,7 @@ vector<move> game_state::get_legal_moves(move parent_move) {
if (rules.foundations_comp_piles) // I.E. Spider-type winning condition
add_foundation_complete_piles_moves(moves);
*/
+
return moves;
}
@@ -352,8 +357,12 @@ void game_state::add_built_group_moves(vector<move> &moves) const {
if (rules.spaces_pol == s_pol::ANY || (rules.spaces_pol == s_pol::KINGS && bg_high.get_rank() == 13))
add_empty_built_group_moves(moves, rem_ref, add_ref, bg_high);
} 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);
+ if (is_next_built_group_card(piles[add_ref].top_card(), bg_high)) {
+ bool is_reveal_move =
+ piles[rem_ref].size() > built_group_height
+ && piles[rem_ref][built_group_height].is_face_down();
+ moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, built_group_height, is_reveal_move);
+ }
}
}
}
@@ -362,7 +371,9 @@ void game_state::add_built_group_moves(vector<move> &moves) const {
// 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]))
+ 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;
}
@@ -374,6 +385,9 @@ void game_state::add_empty_built_group_moves(vector<move>& moves, pile::ref rem_
do {
moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, static_cast<pile::size_type>(card_idx + 1));
} while (piles[rem_ref][card_idx++] != bg_high);
+
+ if (card_idx < piles[rem_ref].size() && piles[rem_ref][card_idx].is_face_down())
+ moves.back().make_reveal_move();
}
bool game_state::is_next_built_group_card(card a, card b) const {
@@ -394,3 +408,12 @@ bool game_state::is_next_legal_card(pol p, card a, card b) const {
// Checks rank
return b.get_rank() + 1 == a.get_rank();
}
+
+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();
+ }
+ }
+}