aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/game/card.cpp4
-rw-r--r--src/main/game/card.h1
-rw-r--r--src/main/game/game_state.h21
-rw-r--r--src/main/game/game_state.legal_moves.cpp125
-rw-r--r--src/main/game/pile.cpp15
-rw-r--r--src/main/game/pile.h12
-rw-r--r--src/main/input-output/output/state_printer.cpp7
-rw-r--r--src/main/solver/solver.cpp2
8 files changed, 164 insertions, 23 deletions
diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp
index 74bdc31..4d9342d 100644
--- a/src/main/game/card.cpp
+++ b/src/main/game/card.cpp
@@ -69,6 +69,10 @@ 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);
+}
+
size_t hash_value(card const& c) {
boost::hash<unsigned char> hasher;
auto raw_val = static_cast<unsigned char>(
diff --git a/src/main/game/card.h b/src/main/game/card.h
index 8aaa50d..4cab774 100644
--- a/src/main/game/card.h
+++ b/src/main/game/card.h
@@ -36,6 +36,7 @@ private:
};
bool operator==(const card&, const card&);
+bool operator!=(const card&, const card&);
std::size_t hash_value(card const&);
diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h
index 6aa3918..f43348f 100644
--- a/src/main/game/game_state.h
+++ b/src/main/game/game_state.h
@@ -5,9 +5,6 @@
#ifndef SOLVITAIRE_GAME_STATE_H
#define SOLVITAIRE_GAME_STATE_H
-#define NULL_MOVE (game_state::move(PILE_REF_MAX, PILE_REF_MAX))
-#define PILE_REF_MAX (255)
-
#include <vector>
#include <string>
@@ -26,7 +23,10 @@ class game_state {
public:
// Defines types
typedef uint8_t pile_ref;
- typedef std::pair<pile_ref, pile_ref> move;
+ struct move {
+ move(pile_ref, pile_ref, pile::size_type = 1);
+ pile_ref from; pile_ref to; pile::size_type count;
+ };
// Constructors
// Creates a game state representation from a JSON doc
@@ -45,11 +45,16 @@ public:
const std::vector<pile>& get_data() const;
friend bool operator==(const game_state&, const game_state&);
- friend std::ostream& operator<< (std::ostream&, const game_state&);
+
+ // Static pile ref members/vars
+ static pile_ref PILE_REF_MAX;
+ static move null_move();
friend std::size_t hash_value(game_state const&);
friend std::size_t hash_value(std::vector<pile> const&);
+ friend std::ostream& operator<< (std::ostream&, const game_state&);
+
private:
static std::vector<card> gen_shuffled_deck(int, int);
@@ -60,6 +65,12 @@ private:
bool is_valid_tableau_move(pile_ref, pile_ref) const;
bool is_valid_foundations_move(pile_ref, pile_ref) const;
bool is_valid_hole_move(pile_ref) const;
+ void get_built_group_moves(std::vector<move>&) const;
+ pile::size_type get_built_group_height(pile_ref) const;
+ bool valid_built_group_move(card, card, card) const;
+ void add_built_group_move(std::vector<move>&, pile_ref, pile_ref) const;
+ void add_empty_built_group_moves(std::vector<move>&, pile_ref, pile_ref,
+ card) const;
// References to piles
sol_rules rules;
diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp
index 10a9639..56c1f06 100644
--- a/src/main/game/game_state.legal_moves.cpp
+++ b/src/main/game/game_state.legal_moves.cpp
@@ -57,6 +57,10 @@ vector<game_state::move> game_state::get_legal_moves() const {
// they cannot be added to
}
+ if (rules.move_built_group) {
+ get_built_group_moves(moves);
+ }
+
return moves;
}
@@ -129,3 +133,124 @@ bool game_state::is_valid_hole_move(const pile_ref rem_ref) const {
|| (rank == rules.max_rank && hole_rank == 1)
|| (rank == 1 && hole_rank == rules.max_rank);
}
+
+
+///////////////////////
+// BUILT-GROUP MOVES //
+///////////////////////
+
+void game_state::get_built_group_moves(vector<move>& moves) const {
+ assert(rules.build_pol != pol::NO_BUILD);
+
+ // Cycles through each pile to see if it contains a built group of size > 1
+ for (auto rem_ref : tableau_piles) {
+ if (piles[rem_ref].size() < 2) continue;
+
+ auto built_group_height = get_built_group_height(rem_ref);
+ if (built_group_height == 1) continue;
+
+ // We have found a built group, so cycles through each pile to see if
+ // this group can be added to it
+ for (auto add_ref : tableau_piles) {
+ if (add_ref == rem_ref) continue;
+
+ card bg_low = piles[rem_ref].top_card();
+ card bg_high = piles[rem_ref][built_group_height - 1];
+
+ // If the pile is empty, and we can move to empty piles, does so
+ if (piles[add_ref].empty()) {
+ if (rules.spaces_pol == s_pol::ANY) {
+ add_empty_built_group_moves(moves, rem_ref, add_ref, bg_high);
+ }
+ }
+ // Otherwise, checks to see if it is a valid move
+ else {
+ card add_card = piles[add_ref].top_card();
+
+ // Adds the move to the list of moves, as a special built group move
+ if (valid_built_group_move(bg_low, bg_high, add_card)) {
+ add_built_group_move(moves, rem_ref, add_ref);
+ }
+ }
+ }
+ }
+}
+
+// Finds the size of the built group at the top of a pile
+pile::size_type game_state::get_built_group_height(pile_ref ref) const {
+
+ // The index of the currently examined card, the current card, previous card
+ // and whether we currently have a valid built group.
+ pile::size_type group_card_idx = 0;
+ bool built_group = true;
+
+ do {
+ card prev_c = piles[ref][group_card_idx++];
+ card c = piles[ref][group_card_idx];
+
+ // If the build policy is same suit, makes sure group has same suit
+ if (rules.build_pol == sol_rules::build_policy::SAME_SUIT
+ && c.get_suit() != prev_c.get_suit()) {
+ built_group = false;
+ }
+ // If the build policy is red black, makes sure group alternates
+ // in correct phase
+ else if (rules.build_pol == sol_rules::build_policy::RED_BLACK
+ && c.get_colour() == prev_c.get_colour()) {
+ built_group = false;
+ }
+
+ // Makes sure the rank of the cards is sequential
+ built_group = built_group
+ && c.get_rank() == prev_c.get_rank()+1;
+
+ }
+ // Continues until group_card_idx points to a card outside the built
+ // Group
+ while (built_group && group_card_idx < piles[ref].size());
+
+ return group_card_idx;
+}
+
+bool game_state::valid_built_group_move(card bg_low, card bg_high, card add_card)
+const {
+ // The rank of the card we're moving the built group to must be greater than
+ // the second-from-top card of the built group, and less than or equal to the
+ // card that would come next at the top of the built pile
+ if (add_card.get_rank() <= bg_low.get_rank() + 1
+ || add_card.get_rank() > bg_high.get_rank() + 1) {
+ return false;
+ }
+
+ // Checks to see if the suits are correct
+ switch (rules.build_pol) {
+ case sol_rules::build_policy::SAME_SUIT:
+ return bg_low.get_suit() == add_card.get_suit();
+
+ case sol_rules::build_policy::RED_BLACK: {
+ bool same_suit = bg_low.get_suit() == add_card.get_suit();
+ bool even_diff = (add_card.get_rank() - bg_low.get_rank()) % 2 == 0;
+ return (same_suit && even_diff) || (!same_suit && !even_diff);
+ }
+ default: return true;
+ }
+}
+
+void game_state::add_built_group_move(vector<move>& moves, pile_ref rem_ref,
+ pile_ref add_ref) const {
+ moves.emplace_back(rem_ref, add_ref, 2);
+}
+
+void game_state::add_empty_built_group_moves(vector<move>& moves,
+ pile_ref rem_ref,
+ pile_ref add_ref,
+ card bg_high) const {
+ // Loops through each possible built group move to an empty pile and adds it
+ // to the list
+ pile::size_type card_idx = 1;
+ do {
+ moves.emplace_back(
+ rem_ref, add_ref, static_cast<pile_ref>(card_idx + 1)
+ );
+ } while (piles[rem_ref][card_idx++] != bg_high);
+}
diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp
index 3b53950..264bb49 100644
--- a/src/main/game/pile.cpp
+++ b/src/main/game/pile.cpp
@@ -21,15 +21,16 @@ bool pile::empty() const {
return pile_vec.empty();
}
-uint8_t pile::size() const {
+pile::size_type pile::size() const {
return static_cast<uint8_t>(pile_vec.size());
}
-card& pile::operator[] (vector<card>::size_type i) {
- return pile_vec[i];
+card& pile::operator[] (size_type i) {
+ return pile_vec[pile_vec.size() - 1 - i];
}
-card pile::operator[] (vector<card>::size_type i) const {
- return pile_vec[i];
+
+card pile::operator[] (size_type i) const {
+ return pile_vec[pile_vec.size() - 1 - i];
}
void pile::place(const card c) {
@@ -42,10 +43,6 @@ card pile::take() {
return c;
}
-void pile::clear() {
- pile_vec.clear();
-}
-
bool operator==(const pile& a, const pile& b) {
return a.pile_vec == b.pile_vec;
}
diff --git a/src/main/game/pile.h b/src/main/game/pile.h
index a51e0f4..f2600fe 100644
--- a/src/main/game/pile.h
+++ b/src/main/game/pile.h
@@ -6,25 +6,27 @@
#define SOLVITAIRE_PILE_H
#include <vector>
+#include <iterator>
#include "sol_rules.h"
#include "card.h"
class pile {
public:
+ typedef uint8_t size_type;
+
// Inspect
card top_card() const;
bool empty() const;
- uint8_t size() const;
+ size_type size() const;
// Modify
void place(card);
card take();
- void clear();
- // Index like array
- card& operator[] (std::vector<card>::size_type);
- card operator[] (std::vector<card>::size_type) const;
+ // Indexes the vector, with the top card as index 0
+ card& operator[] (size_type);
+ card operator[] (size_type) const;
friend bool operator==(const pile&, const pile&);
friend std::size_t hash_value(pile const&);
diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp
index 2c23496..cf9f09e 100644
--- a/src/main/input-output/output/state_printer.cpp
+++ b/src/main/input-output/output/state_printer.cpp
@@ -51,7 +51,7 @@ void state_printer::print_piles(ostream& stream,
const vector<game_state::pile_ref>& pile_rs,
const game_state& gs) {
bool empty_row = false;
- vector<card>::size_type row_idx = 0;
+ pile::size_type row_idx = 0;
// Loops through the rows in each pile, starting from the bottom, until
// one is empty
@@ -67,8 +67,9 @@ void state_printer::print_piles(ostream& stream,
if (!empty_row || row_idx == 0) {
// Loops through the current (non-empty) row, and outputs the values
for (const game_state::pile_ref pile_r : pile_rs) {
- if (gs.piles[pile_r].size() > row_idx) {
- print_card(stream, gs.piles[pile_r][row_idx]);
+ const pile& p = gs.piles[pile_r];
+ if (p.size() > row_idx) {
+ print_card(stream, p[p.size() - 1 - row_idx]);
} else if (row_idx == 0) {
stream << "[]";
}
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
index e0fbbc6..aaaf530 100644
--- a/src/main/solver/solver.cpp
+++ b/src/main/solver/solver.cpp
@@ -26,7 +26,7 @@ bool solver::run() {
states_searched++;
LOG_DEBUG (state);
- frontier.emplace_back(NULL_MOVE, state.get_legal_moves());
+ frontier.emplace_back(game_state::null_move(), state.get_legal_moves());
while (!frontier.empty()) {
node& current = frontier.back();