aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharlie Blake2018-07-23 10:31:37 +0100
committerCharlie Blake2018-07-23 10:31:37 +0100
commit390a3cb4f15d84cde2312411b16203b70dbb2df5 (patch)
treee725ac0713cd44cc33ff252bb9c768ef819b73c8
parentAdd tableau wraps logic (diff)
downloadmetasol-390a3cb4f15d84cde2312411b16203b70dbb2df5.tar.gz
metasol-390a3cb4f15d84cde2312411b16203b70dbb2df5.zip

Add foundation wrap logic and fix top-of-waste bug

-rw-r--r--src/main/game/search-state/game_state.auto_foundation_moves.cpp30
-rw-r--r--src/main/game/search-state/game_state.cpp3
-rw-r--r--src/main/game/search-state/game_state.h4
-rw-r--r--src/main/game/search-state/game_state.legal_moves.cpp11
-rw-r--r--src/main/game/sol_rules.cpp1
-rw-r--r--src/main/input-output/input/json-parsing/deal_parser.cpp4
-rw-r--r--src/main/input-output/input/json-parsing/rules_parser.cpp10
7 files changed, 35 insertions, 28 deletions
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 b74948a..12a21f5 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
@@ -3,10 +3,12 @@
//
#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;
using std::max;
@@ -21,11 +23,12 @@ bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const {
return true;
card::suit_t target_suit(target_pile - foundations.front());
- card::rank_t target_rank = piles[target_pile].empty()
- ? card::rank_t(0)
- : piles[target_pile].top_card().get_rank();
+ 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, card::rank_t(target_rank + 1));
+ 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
@@ -36,7 +39,7 @@ bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const {
card::suit_t s(pr - foundations.front());
card::rank_t r = piles[pr].empty() ? card::rank_t(0)
- : piles[pr].top_card().get_rank();
+ : foundation_base_convert(piles[pr].top_card().get_rank());
card c(s, r);
auto rank_diff = int(move_up_card.get_rank() - r);
@@ -71,10 +74,11 @@ boost::optional<move> game_state::get_dominance_move() const {
// 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 or stock cards to the foundations
+ // 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;
}
@@ -85,10 +89,9 @@ boost::optional<move> game_state::get_dominance_move() const {
// 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) :
- piles[target_foundation].top_card().get_rank()
- + card::rank_t(1);
- if (target_rank == c.get_rank() &&
+ ? 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);
@@ -144,3 +147,10 @@ void game_state::update_auto_foundation_moves(pile::ref target_pile) {
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;
+ return r;
+}
diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp
index 81fdd96..dbb3ea1 100644
--- a/src/main/game/search-state/game_state.cpp
+++ b/src/main/game/search-state/game_state.cpp
@@ -4,7 +4,6 @@
#include <vector>
#include <ostream>
-#include <random>
#include <algorithm>
#include <functional>
@@ -631,7 +630,7 @@ bool game_state::is_solved() const {
} 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], false)) { // TODO
+ if (!is_next_legal_card(rules.sequence_build_pol, piles[sequences[i]][j-1], piles[sequences[i]][j], false)) {
solved = false;
break;
}
diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h
index 8b24346..f6fc666 100644
--- a/src/main/game/search-state/game_state.h
+++ b/src/main/game/search-state/game_state.h
@@ -8,6 +8,7 @@
#include <vector>
#include <list>
#include <string>
+#include <random>
#include <boost/functional/hash.hpp>
#include <boost/optional/optional.hpp>
@@ -62,7 +63,7 @@ private:
/* Constructors (& helper function) */
explicit game_state(const sol_rules&, streamliner_options);
- static std::vector<card> gen_shuffled_deck(card::rank_t, bool, mt19937);
+ 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 */
@@ -131,6 +132,7 @@ private:
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 */
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 868087e..93dc669 100644
--- a/src/main/game/search-state/game_state.legal_moves.cpp
+++ b/src/main/game/search-state/game_state.legal_moves.cpp
@@ -65,7 +65,7 @@ vector<move> game_state::get_legal_moves(move parent_move) {
if (!piles[r].empty())
moves.emplace_back(move::mtype::regular, r, empty_cell);
- if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty())
+ if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE)
add_stock_to_cell_move(moves, empty_cell);
}
@@ -95,7 +95,7 @@ vector<move> game_state::get_legal_moves(move parent_move) {
}
// Stock to tableau moves
- if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty())
+ if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE)
add_stock_to_tableau_moves(moves);
switch (rules.move_built_group) {
@@ -139,7 +139,7 @@ vector<move> game_state::get_legal_moves(move parent_move) {
// Tableau / cells / reserve / stock-waste 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 && !piles[stock].empty())
+ if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE)
add_stock_to_hole_foundation_moves(moves);
list<pile::ref> from_piles = tableau_piles;
@@ -196,7 +196,8 @@ set<pair<int8_t, bool>> game_state::generate_k_plus_moves_to_check() const {
set<pair<int8_t, bool>> stock_moves_to_check;
// If the waste isn't empty, adds the move from the top of the current waste
- if (piles[waste].size() != 0) stock_moves_to_check.insert(pair<int8_t, bool>(0, false));
+ 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;
@@ -398,7 +399,7 @@ void game_state::add_whole_pile_moves(vector<move>& moves, pile::ref rem_ref, pi
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) {
+ 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)) {
diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp
index 55c6757..2778241 100644
--- a/src/main/game/sol_rules.cpp
+++ b/src/main/game/sol_rules.cpp
@@ -20,6 +20,7 @@ sol_rules::sol_rules() :
spaces_pol(s_pol::ANY),
move_built_group(bgt::NO),
built_group_pol(pol::ANY_SUIT),
+ tableau_wraps(false),
two_decks(false),
max_rank(13),
hole(false),
diff --git a/src/main/input-output/input/json-parsing/deal_parser.cpp b/src/main/input-output/input/json-parsing/deal_parser.cpp
index e51313f..b80ac0e 100644
--- a/src/main/input-output/input/json-parsing/deal_parser.cpp
+++ b/src/main/input-output/input/json-parsing/deal_parser.cpp
@@ -209,10 +209,6 @@ void deal_parser::parse_reserve(game_state &gs, const Document& doc) {
// We treat a regular reserve like multiple single-card piles,
// but a stacked reserve as a single multiple-card pile
- if (json_reserve_piles.Size() != gs.rules.reserve_size) {
- json_helper::json_parse_err("Incorrect reserve size");
- }
-
for (pile::ref i = 0; i < json_card_arr.Size(); i++) {
assert(json_card_arr[i].IsString());
pile::ref pr = gs.original_reserve[0];
diff --git a/src/main/input-output/input/json-parsing/rules_parser.cpp b/src/main/input-output/input/json-parsing/rules_parser.cpp
index a6c84c5..209153f 100644
--- a/src/main/input-output/input/json-parsing/rules_parser.cpp
+++ b/src/main/input-output/input/json-parsing/rules_parser.cpp
@@ -5,6 +5,8 @@
#include <vector>
#include <algorithm>
+#include "../../../../../lib/rapidjson/error/en.h"
+
#include "rules_parser.h"
#include "json_helper.h"
#include "../sol_preset_types.h"
@@ -494,7 +496,6 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
"must be true");
}
- rules_schema_json(); // Throws an error if it fails
Document vd;
vd.Parse(rules_schema_json().c_str());
assert(!vd.HasParseError());
@@ -510,9 +511,6 @@ string rules_parser::rules_schema_json() {
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "JSON Schema representing a generic solitaire game",
- "definitions": {
- "rank": {"type": "string", "pattern": "^(([0-9]|1[0-3]|a|A|j|J|q|Q|k|K))$"},
- },
"type": "object",
"properties": {
"tableau piles": {
@@ -591,8 +589,8 @@ string rules_parser::rules_schema_json() {
"base card": {
"type": "string",
"oneOf":[
- {"$ref": "#/definitions/rank"},
- {"type": "string","enum": ["random"]}
+ {"pattern": "^(([0-9]|1[0-3]|a|A|j|J|q|Q|k|K))$"},
+ {"enum": ["random"]}
]
},
"removable": {