aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharlie Blake2018-07-24 12:00:04 +0100
committerCharlie Blake2018-07-24 12:00:04 +0100
commit2acab3e51cabd8ba80738b1faf791980d2abf724 (patch)
treecb3841d736ff353c5d8aea6a4faf81861ef83f5c
parentSmall bug fixes inc found. base card & whole piles (diff)
downloadmetasol-2acab3e51cabd8ba80738b1faf791980d2abf724.tar.gz
metasol-2acab3e51cabd8ba80738b1faf791980d2abf724.zip

Fix canfield to pass all unit tests

-rw-r--r--CMakeLists.txt2
-rw-r--r--src/main/game/search-state/game_state.cpp10
-rw-r--r--src/main/game/search-state/game_state.dominance_moves.cpp (renamed from src/main/game/search-state/game_state.auto_foundation_moves.cpp)21
-rw-r--r--src/main/game/search-state/game_state.h3
-rw-r--r--src/main/game/search-state/game_state.legal_moves.cpp58
-rw-r--r--src/main/input-output/input/sol_preset_types.cpp4
-rw-r--r--src/test/integration_tests/canfield_test.cpp26
-rw-r--r--src/test/resources/canfield/ComplexSolvable.json10
-rw-r--r--src/test/resources/canfield/ComplexUnsolvable.json12
-rw-r--r--src/test/resources/canfield/SimpleSolvable.json4
-rw-r--r--src/test/resources/canfield/SimpleUnsolvable.json10
11 files changed, 105 insertions, 55 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 00fc75c..1815077 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -48,7 +48,7 @@ set(sources src/main/input-output/input/command_line_helper.cpp
lib/rapidjson/schema.h
lib/rapidjson/stringbuffer.h
src/main/game/search-state/game_state.legal_moves.cpp
- src/main/game/search-state/game_state.auto_foundation_moves.cpp
+ src/main/game/search-state/game_state.dominance_moves.cpp
src/main/game/search-state/game_state.pile_order.cpp
src/main/game/move.cpp
src/main/game/move.h src/main/evaluation/binomial_ci.cpp src/main/evaluation/binomial_ci.h)
diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp
index 78247bf..17fdc29 100644
--- a/src/main/game/search-state/game_state.cpp
+++ b/src/main/game/search-state/game_state.cpp
@@ -263,6 +263,16 @@ game_state::game_state(const sol_rules& s_rules,
}
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
diff --git a/src/main/game/search-state/game_state.auto_foundation_moves.cpp b/src/main/game/search-state/game_state.dominance_moves.cpp
index b2b55b3..b7e6e15 100644
--- a/src/main/game/search-state/game_state.auto_foundation_moves.cpp
+++ b/src/main/game/search-state/game_state.dominance_moves.cpp
@@ -9,9 +9,10 @@
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)
@@ -65,8 +66,13 @@ bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const {
}
// Returns a dominance move if one is available
-boost::optional<move> game_state::get_dominance_move() const {
+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;
+ }
+
// If there are 2 decks or no foundations, return
if (!rules.foundations_present || rules.two_decks)
return boost::none;
@@ -107,6 +113,17 @@ boost::optional<move> game_state::get_dominance_move() const {
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;
+}
+
bool game_state::is_ordered_pile(pile::ref pr) const {
if (piles[pr].size() != rules.max_rank) return false;
diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h
index f6fc666..d8d9f12 100644
--- a/src/main/game/search-state/game_state.h
+++ b/src/main/game/search-state/game_state.h
@@ -111,6 +111,7 @@ private:
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>&) const;
void add_built_group_moves(std::vector<move>&, pile::ref, pile::size_type) const;
void add_whole_pile_moves(std::vector<move>&) const;
@@ -121,6 +122,7 @@ private:
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) 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, bool) const;
bool is_next_legal_card(std::vector<sol_rules::accordion_policy>, card, card) const;
@@ -128,6 +130,7 @@ private:
/* Auto-foundation moves */
+ boost::optional<move> auto_reserve_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);
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 09a32fa..f97814a 100644
--- a/src/main/game/search-state/game_state.legal_moves.cpp
+++ b/src/main/game/search-state/game_state.legal_moves.cpp
@@ -74,9 +74,7 @@ vector<move> game_state::get_legal_moves(move parent_move) {
for (auto f : foundations) {
if (piles[f].empty() || parent_move.to == f || dominance_blocks_foundation_move(f)) continue;
- for (auto t : tableau_piles)
- if (is_valid_tableau_move(f, t))
- moves.emplace_back(move::mtype::regular, f, t);
+ add_valid_tableau_moves(moves, f);
if (empty_cell != 255)
moves.emplace_back(move::mtype::regular, f, empty_cell);
@@ -87,17 +85,14 @@ vector<move> game_state::get_legal_moves(move parent_move) {
for (auto r : reserve) {
if (piles[r].empty()) continue;
- for (auto t : tableau_piles) {
- if (is_valid_tableau_move(r, t)) {
- moves.emplace_back(move::mtype::regular, r, t);
- }
- }
+ add_valid_tableau_moves(moves, r);
}
- // Stock to tableau moves
+ // Stock-waste to tableau moves
if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE)
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);
@@ -115,11 +110,7 @@ vector<move> game_state::get_legal_moves(move parent_move) {
for (auto t_from : tableau_piles) {
if (piles[t_from].empty() || parent_move.to == t_from) continue;
- for (auto t_to : tableau_piles) {
- if (is_valid_tableau_move(t_from, t_to)) {
- moves.emplace_back(move::mtype::regular, t_from, t_to);
- }
- }
+ add_valid_tableau_moves(moves, t_from);
}
}
@@ -132,11 +123,7 @@ vector<move> game_state::get_legal_moves(move parent_move) {
for (auto c : cells) {
if (piles[c].empty() || parent_move.to == c) continue;
- for (auto t : tableau_piles) {
- if (is_valid_tableau_move(c, t)) {
- moves.emplace_back(move::mtype::regular, c, t);
- }
- }
+ add_valid_tableau_moves(moves, c);
}
// Tableau / cells / reserve / stock-waste to hole / foundation moves
@@ -233,9 +220,14 @@ void game_state::add_stock_to_tableau_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 t : tableau_piles)
- if (is_valid_tableau_move(from, t))
+ // 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);
+ }
+ }
}
}
@@ -313,6 +305,8 @@ bool game_state::is_valid_foundations_move(const pile::ref rem_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)
@@ -320,9 +314,9 @@ bool game_state::is_valid_foundations_move(const card rem_c,
// Checks rank
if (piles[add_ref].empty())
- return rem_c.get_rank() == rules.foundations_base;
+ return rem_c.get_rank() == foundations_base;
else
- return rem_c.get_rank() == (piles[add_ref].top_card().get_rank() + 1) % rules.max_rank;
+ 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 {
@@ -345,6 +339,16 @@ bool game_state::is_valid_hole_move(const card c) const {
// 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) const {
assert(rules.built_group_pol != pol::NO_BUILD);
@@ -383,6 +387,7 @@ void game_state::add_built_group_moves(vector<move>& moves, pile::ref rem_ref, p
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) {
@@ -534,6 +539,13 @@ void game_state::add_accordion_moves(vector<move>& moves) const {
}
}
+// 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;
+}
///////////////////////
diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp
index d3daf40..0aee219 100644
--- a/src/main/input-output/input/sol_preset_types.cpp
+++ b/src/main/input-output/input/sol_preset_types.cpp
@@ -290,7 +290,7 @@ map<string, string> sol_preset_types::mp = {
"redeal": true
},
"reserve": {
- "size": 14,
+ "size": 13,
"stacked": true
}
}
@@ -314,7 +314,7 @@ map<string, string> sol_preset_types::mp = {
"base card": "random"
},
"stock": {
- "size": 1,
+ "size": 6,
"deal count": 3,
"redeal": true
},
diff --git a/src/test/integration_tests/canfield_test.cpp b/src/test/integration_tests/canfield_test.cpp
index cdb6d98..006f4fb 100644
--- a/src/test/integration_tests/canfield_test.cpp
+++ b/src/test/integration_tests/canfield_test.cpp
@@ -97,18 +97,21 @@ TEST(Canfield, AutoReserve) {
sol_rules sr = rules_parser::from_preset("simple-canfield");
game_state gs(sr, string_il{
- {"3C"},{},{},{}, // Foundations
- {"3D"},{"2D"}, // Stock-waste
+ {"2C"},{},{},{}, // Foundations
+ {"3D","2D"},{}, // Stock-waste
{"3H"}, // Reserve
{"2H","3S","AH"},{},{"AC"} // Tableau piles
});
vector<move> actual_moves = gs.get_legal_moves();
- vector<move> exp_moves = {
- move(move::mtype::regular, 6, 8, 1)
- };
+ vector<move> exp_moves = {};
ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves;
+
+ boost::optional<move> actual_dom_move = gs.get_dominance_move();
+ boost::optional<move> exp_dom_move = move(move::mtype::dominance, 6, 8, 1);
+
+ ASSERT_EQ(exp_dom_move, actual_dom_move) << *actual_dom_move;
}
TEST(Canfield, AutoReserveThenWaste) {
@@ -116,16 +119,21 @@ TEST(Canfield, AutoReserveThenWaste) {
game_state gs(sr, string_il{
{"3C"},{},{},{}, // Foundations
- {"3D"},{"3H"}, // Stock-waste
+ {"2D"},{"2S"}, // Stock-waste
{}, // Reserve
- {"2H","3S","AH"},{},{"AC"} // Tableau piles
+ {"2H","3S","AH"},{},{"AD"} // Tableau piles
});
vector<move> actual_moves = gs.get_legal_moves();
vector<move> exp_moves = {
- move(move::mtype::stock_k_plus, 4, 8, 1),
- move(move::mtype::stock_k_plus, 4, 8, 2),
+ move(move::mtype::stock_k_plus, 4, 8, 0),
+ move(move::mtype::stock_k_plus, 4, 8, 1)
};
ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves;
+
+ boost::optional<move> actual_dom_move = gs.get_dominance_move();
+ boost::optional<move> exp_dom_move = boost::none;
+
+ ASSERT_EQ(exp_dom_move, actual_dom_move) << *actual_dom_move;
}
diff --git a/src/test/resources/canfield/ComplexSolvable.json b/src/test/resources/canfield/ComplexSolvable.json
index 7abae7a..2a4c252 100644
--- a/src/test/resources/canfield/ComplexSolvable.json
+++ b/src/test/resources/canfield/ComplexSolvable.json
@@ -1,10 +1,10 @@
{
"foundations": ["2C"],
"tableau piles": [
- ["AS"],
- ["AC"],
- ["3D"]
+ ["3D"],
+ ["3H"],
+ ["3S"]
],
- "stock": ["2H","3S"],
- "reserve": ["AH","2S","3H","AD","3C","2D"]
+ "reserve": ["AC","2S","3C"],
+ "stock": ["AH","2H","AD","AS","2D"]
} \ No newline at end of file
diff --git a/src/test/resources/canfield/ComplexUnsolvable.json b/src/test/resources/canfield/ComplexUnsolvable.json
index 3869722..297d06b 100644
--- a/src/test/resources/canfield/ComplexUnsolvable.json
+++ b/src/test/resources/canfield/ComplexUnsolvable.json
@@ -1,10 +1,10 @@
{
- "foundations": ["2S","2C"],
+ "foundations": ["2C"],
"tableau piles": [
- ["AS"],
- ["AC"],
- ["3D"]
+ ["AS","3D"],
+ ["AC","3H"],
+ ["3S"]
],
- "stock": ["AH"],
- "reserve": ["3H","2H","3S","AD","3C","2D"]
+ "reserve": ["AH","2S","3C"],
+ "stock": ["AD","2H","2D"]
} \ No newline at end of file
diff --git a/src/test/resources/canfield/SimpleSolvable.json b/src/test/resources/canfield/SimpleSolvable.json
index 450f5bd..3353b5c 100644
--- a/src/test/resources/canfield/SimpleSolvable.json
+++ b/src/test/resources/canfield/SimpleSolvable.json
@@ -5,6 +5,6 @@
["AC"],
["3D"]
],
- "stock": ["AH","3S"],
- "reserve": ["2H","AS","3H","2D","3C","AD"]
+ "reserve": ["AH","3S"],
+ "stock": ["2H","AS","3H","2D","3C","AD"]
} \ No newline at end of file
diff --git a/src/test/resources/canfield/SimpleUnsolvable.json b/src/test/resources/canfield/SimpleUnsolvable.json
index aefa7b8..440c358 100644
--- a/src/test/resources/canfield/SimpleUnsolvable.json
+++ b/src/test/resources/canfield/SimpleUnsolvable.json
@@ -1,10 +1,10 @@
{
"foundations": ["2C"],
"tableau piles": [
- ["AS"],
- ["AC"],
- ["AD"]
+ ["3D"],
+ ["3H"],
+ ["3S"]
],
- "stock": ["2S","3D"],
- "reserve": ["3H","2H","AH","3S","3C","2D"]
+ "reserve": ["2S","AS"],
+ "stock": ["AC","2H","3C","AH","AD","2D"]
} \ No newline at end of file