From dfecd22e848447fbecdfb045a1477209e965e7ed Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 5 Feb 2018 15:31:39 +0000 Subject: Add "foundations removable" field to rules JSON --- resources/bakers-dozen.json | 3 ++- resources/default.json | 1 + resources/flower-garden.json | 3 ++- resources/simple-bakers-dozen.json | 3 ++- resources/simple-flower-garden.json | 3 ++- resources/simple-somerset.json | 3 ++- resources/simple-spanish-patience.json | 3 ++- resources/somerset.json | 3 ++- resources/spanish-patience.json | 3 ++- src/main/input-output/input/sol_preset_types.cpp | 25 ++++++++++++++++-------- 10 files changed, 34 insertions(+), 16 deletions(-) diff --git a/resources/bakers-dozen.json b/resources/bakers-dozen.json index 7f49b61..4f63936 100644 --- a/resources/bakers-dozen.json +++ b/resources/bakers-dozen.json @@ -2,5 +2,6 @@ "tableau piles": { "count": 13, "spaces policy": "no-build" - } + }, + "foundations removable": true } \ No newline at end of file diff --git a/resources/default.json b/resources/default.json index 3a9800d..9b28865 100644 --- a/resources/default.json +++ b/resources/default.json @@ -12,6 +12,7 @@ "hole": false, "foundations": true, "foundations initial card": false, + "foundations removable": false, "cells": 0, "stock size": 0, "stock deal type": "waste", diff --git a/resources/flower-garden.json b/resources/flower-garden.json index 6ba2fbc..92906fb 100644 --- a/resources/flower-garden.json +++ b/resources/flower-garden.json @@ -2,5 +2,6 @@ "tableau piles": { "count": 6 }, - "reserve size": 16 + "reserve size": 16, + "foundations removable": true } diff --git a/resources/simple-bakers-dozen.json b/resources/simple-bakers-dozen.json index 4bb0dc7..3f038fe 100644 --- a/resources/simple-bakers-dozen.json +++ b/resources/simple-bakers-dozen.json @@ -3,5 +3,6 @@ "count": 3, "spaces policy": "no-build" }, - "max rank": 3 + "max rank": 3, + "foundations removable": true } \ No newline at end of file diff --git a/resources/simple-flower-garden.json b/resources/simple-flower-garden.json index 7e6f329..eb7916c 100644 --- a/resources/simple-flower-garden.json +++ b/resources/simple-flower-garden.json @@ -3,5 +3,6 @@ "count": 3 }, "max rank": 3, - "reserve size": 3 + "reserve size": 3, + "foundations removable": true } diff --git a/resources/simple-somerset.json b/resources/simple-somerset.json index 309b217..555fddf 100644 --- a/resources/simple-somerset.json +++ b/resources/simple-somerset.json @@ -4,5 +4,6 @@ "build policy": "red-black", "diagonal deal": true }, - "max rank": 2 + "max rank": 2, + "foundations removable": true } \ No newline at end of file diff --git a/resources/simple-spanish-patience.json b/resources/simple-spanish-patience.json index a0c745e..2b27cb5 100644 --- a/resources/simple-spanish-patience.json +++ b/resources/simple-spanish-patience.json @@ -2,5 +2,6 @@ "tableau piles": { "count": 3 }, - "max rank": 2 + "max rank": 2, + "foundations removable": true } \ No newline at end of file diff --git a/resources/somerset.json b/resources/somerset.json index 7b218ef..fc3bdad 100644 --- a/resources/somerset.json +++ b/resources/somerset.json @@ -3,5 +3,6 @@ "count": 10, "build policy": "red-black", "diagonal deal": true - } + }, + "foundations removable": true } \ No newline at end of file diff --git a/resources/spanish-patience.json b/resources/spanish-patience.json index 2ffec07..32c1dab 100644 --- a/resources/spanish-patience.json +++ b/resources/spanish-patience.json @@ -1,5 +1,6 @@ { "tableau piles": { "count": 13 - } + }, + "foundations removable": true } \ No newline at end of file diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 8f0293f..eeb0edc 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -32,6 +32,7 @@ std::map sol_preset_types::mp = { "hole": false, "foundations": true, "foundations initial card": false, + "foundations removable": false, "cells": 0, "stock size": 0, "stock deal type": "waste", @@ -48,7 +49,8 @@ std::map sol_preset_types::mp = { { "tableau piles": { "count": 13 - } + }, + "foundations removable": true } )" @@ -61,7 +63,8 @@ std::map sol_preset_types::mp = { "tableau piles": { "count": 3 }, - "max rank": 2 + "max rank": 2, + "foundations removable": true } )" @@ -102,7 +105,8 @@ std::map sol_preset_types::mp = { "tableau piles": { "count": 13, "spaces policy": "no-build" - } + }, + "foundations removable": true } )" @@ -116,7 +120,8 @@ std::map sol_preset_types::mp = { "count": 3, "spaces policy": "no-build" }, - "max rank": 3 + "max rank": 3, + "foundations removable": true } )" @@ -192,7 +197,8 @@ std::map sol_preset_types::mp = { "count": 6 }, "reserve size": 16 -} +}, + "foundations removable": true )" }, @@ -206,7 +212,8 @@ std::map sol_preset_types::mp = { }, "max rank": 3, "reserve size": 3 -} +}, + "foundations removable": true )" }, @@ -252,7 +259,8 @@ std::map sol_preset_types::mp = { "count": 10, "build policy": "red-black", "diagonal deal": true - } + }, + "foundations removable": true } )" @@ -267,7 +275,8 @@ std::map sol_preset_types::mp = { "build policy": "red-black", "diagonal deal": true }, - "max rank": 2 + "max rank": 2, + "foundations removable": true } )" -- cgit v1.2.1 From 793b3c253cac90a748fa16c20d185505e5fe6808 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 5 Feb 2018 15:57:21 +0000 Subject: Fix json bug in sol presets --- src/main/input-output/input/sol_preset_types.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index eeb0edc..de0fd41 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -196,9 +196,9 @@ std::map sol_preset_types::mp = { "tableau piles": { "count": 6 }, - "reserve size": 16 -}, + "reserve size": 16, "foundations removable": true +} )" }, @@ -211,9 +211,9 @@ std::map sol_preset_types::mp = { "count": 3 }, "max rank": 3, - "reserve size": 3 -}, + "reserve size": 3, "foundations removable": true +} )" }, -- cgit v1.2.1 From f907ddd96ae6886b6c541d22c1f44628b64289d4 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 5 Feb 2018 15:57:39 +0000 Subject: Add foundations removable rule parsing --- src/main/game/sol_rules.h | 1 + src/main/input-output/input/json-parsing/rules_parser.cpp | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index 067e107..8ebb73d 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -37,6 +37,7 @@ struct sol_rules { bool hole; bool foundations; bool foundations_init_card; + bool foundations_removable; bool diagonal_deal; uint8_t cells; uint8_t stock_size; 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 7b9de84..395f2e7 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -181,6 +181,14 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } + if (d.HasMember("foundations removable")) { + if (d["foundations removable"].IsBool()) { + sr.foundations_removable = d["foundations removable"].GetBool(); + } else { + json_helper::json_parse_err("[foundations removable] must be a boolean"); + } + } + if (d.HasMember("cells")) { if (d["cells"].IsInt()) { sr.cells = static_cast(d["cells"].GetInt()); -- cgit v1.2.1 From 8dba2d3a176d1381cf9c3ed31192c31a468608f1 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 5 Feb 2018 15:58:09 +0000 Subject: Add removable foundations logic to legal moves gen --- src/main/game/game_state.legal_moves.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp index 3043587..fb08abb 100644 --- a/src/main/game/game_state.legal_moves.cpp +++ b/src/main/game/game_state.legal_moves.cpp @@ -24,6 +24,14 @@ vector game_state::get_legal_moves() const { // Never removes a card from the hole, the waste, or an empty pile if (rem_ref == hole || piles[rem_ref].empty()) continue; + // If the foundations aren't removable, also skip them + if (rules.foundations + && !rules.foundations_removable + && rem_ref >= foundations.front() + && rem_ref <= foundations.back()) { + continue; + } + // Stock cards can only be moved to the waste (assuming there is a waste) if (rules.stock_size > 0 && rem_ref == stock) { if (rules.stock_deal_t == sdt::WASTE) { -- cgit v1.2.1 From e5760f55abea5f2170cba605405cc780b3effd9a Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 14 Feb 2018 16:42:10 +0000 Subject: Resolve name ambiguity in card class --- src/main/game/card.cpp | 24 ++++++++++++++++-------- src/main/game/card.h | 18 +++++++++--------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 271bf73..a19760b 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -11,12 +11,20 @@ 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) : suit(s), rank(r) {} +card::card(suit_t s, rank_t r) : card_suit(s), card_rank(r) {} -card::card(const char* c) : suit(suit_from_str(c)), rank(rank_from_str(c)) {} +card::card(const char* c) : card_suit(suit_from_str(c)), card_rank(rank_from_str(c)) {} card::card() : card(0, 0) {} @@ -44,11 +52,11 @@ card::rank_t card::rank_from_str(const char* c) { } card::suit_t card::get_suit() const { - return suit; + return card_suit; } card::colour_t card::get_colour() const { - switch (suit) { + switch (card_suit) { case card::suit::Spades : case card::suit::Clubs : return card::colour::Black; default : return card::colour::Red; @@ -56,19 +64,19 @@ card::colour_t card::get_colour() const { } card::rank_t card::get_rank() const { - return rank; + return card_rank; } std::string card::to_string() const { string s; - switch(rank) { + 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(rank); + default: s += std::to_string(card_rank); } - switch(suit) { + switch(card_suit) { case card::suit::Spades : s += "S"; break; case card::suit::Hearts : s += "H"; break; case card::suit::Clubs : s += "C"; break; diff --git a/src/main/game/card.h b/src/main/game/card.h index 74f513d..8d88df8 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -16,15 +16,15 @@ public: typedef uint8_t colour_t; struct suit { - static const suit_t Clubs = 0; - static const suit_t Hearts = 1; - static const suit_t Spades = 2; - static const suit_t Diamonds = 3; + 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 = 0; - static const colour_t Red = 1; + static const colour_t Black; + static const colour_t Red; }; const static card divider; @@ -32,7 +32,7 @@ public: // A card can either be created using two int values (rank & suit), or // using a string (e.g. "AS") card(suit_t, rank_t); - explicit card(const char*); + card(const char*); card(); suit_t get_suit() const; @@ -45,8 +45,8 @@ private: static suit_t suit_from_str(const char* c); static rank_t rank_from_str(const char* c); - suit_t suit : 4; - rank_t rank : 4; + suit_t card_suit : 4; + rank_t card_rank : 4; }; bool operator==(const card&, const card&); -- cgit v1.2.1 From 86ce38efc5ad3bf4fe06ef4c515591545e6fe925 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 14 Feb 2018 16:42:38 +0000 Subject: Update game rep to deal with foundation dominance --- src/main/game/game_state.cpp | 123 ++++++++++++++++++++++++++++++- src/main/game/game_state.h | 10 +++ src/main/game/game_state.legal_moves.cpp | 45 +++++++++++ src/main/game/pile.cpp | 2 + src/main/game/pile.h | 1 + 5 files changed, 177 insertions(+), 4 deletions(-) diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index a6186ff..3c92aa4 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -14,6 +14,7 @@ #include "game_state.h" #include "../input-output/input/json-parsing/deal_parser.h" #include "../input-output/output/state_printer.h" +#include "../input-output/output/log_helper.h" using namespace std; using namespace rapidjson; @@ -40,6 +41,7 @@ game_state::game_state(const sol_rules& s_rules) : rules(s_rules) { for (uint8_t i = 0; i < 4*(rules.two_decks ? 2:1); i++) { piles.emplace_back(); foundations.push_back(static_cast(piles.size() - 1)); + auto_foundation_moves.push_back(true); } } @@ -205,11 +207,18 @@ vector game_state::gen_shuffled_deck(int seed, card::rank_t max_rank, return deck; } +const pile::size_type game_state::move::dominance_flag = pile::max_size_type; + game_state::move::move(pile_ref f, pile_ref t, pile::size_type i) : from(f), to(t), count(i) { assert(i >= 1); } +// Returns true is the move has been flagged as a special 'dominance' move +bool game_state::move::is_dominance() const { + return count == dominance_flag; +} + ///////////////////////// // MODIFYING FUNCTIONS // @@ -222,15 +231,20 @@ void game_state::make_move(const move m) { // Handles special stock-to-tableau-piles move if (rules.stock_deal_t == sdt::TABLEAU_PILES && m.from == stock) { for (pile_ref tab_pr = original_tableau_piles.front(); - tab_pr < original_tableau_piles.front() + m.count; + tab_pr < pile_ref(original_tableau_piles.front() + m.count); tab_pr++) { place_card(tab_pr, take_card(stock)); } } // If this is not a built-pile move - else if (m.count == 1) { + else if (m.count == 1 || m.is_dominance()) { place_card(m.to, take_card(m.from)); - } else { +#ifndef NO_AUTO_FOUNDATIONS + assess_auto_foundation_moves(m.to); +#endif + } + // If this is a built-pile move + else { assert(m.count <= rules.max_rank); // Adds the cards to the 'to' pile for (auto pile_idx = m.count; pile_idx-- > 0;) { @@ -257,8 +271,11 @@ void game_state::undo_move(const move m) { } } // If this is not a built-pile move - else if (m.count == 1) { + else if (m.count == 1 || m.is_dominance()) { place_card(m.from, take_card(m.to)); +#ifndef NO_AUTO_FOUNDATIONS + assess_auto_foundation_moves(m.from); +#endif } else { assert(m.count <= rules.max_rank); // Adds the cards to the 'from' pile @@ -361,6 +378,100 @@ void game_state::eval_pile_order(list& pile_lst, pile_ref changed_pr, #endif } +// Decides which 'auto foundation' booleans should be 'true' currently +void game_state::assess_auto_foundation_moves(pile_ref target_pile) { + // If there are no foundations, or this is not a foundation move, do nothing + if (!rules.foundations + || target_pile < foundations.front() + || target_pile > foundations.back() + || rules.foundations_removable) { + return; + } + + // Turns the 'auto' boolean for the target foundation false + auto_foundation_moves[target_pile - foundations.front()] = false; + + // Repeats the cycle which attempts to turn one of the auto_foundation_move + // bools true for each foundation, until no change is made in a full scan + bool change_made = true; + while (change_made) { + change_made = false; + // Cycles through each of the foundations with a false 'auto' boolean, + // attempting to turn them true + for (card::suit_t suit = 0; suit < 4; suit++) { + if (auto_foundation_moves[suit]) continue; + + pile_ref foundation_ref = *(begin(foundations) + suit); + + // Empty foundations are automatically true + if(piles[foundation_ref].empty()) { + auto_foundation_moves[suit] = true; + // Restarts the loop + change_made = true; + break; + } + card::rank_t foundation_rank = piles[foundation_ref].top_card().get_rank(); + + // Creates the 'reliant' set. These are the cards that rely on the + // foundation's 'next' card to be able to move in the tableau piles + vector reliant_set; + switch (rules.build_pol) { + case pol::SAME_SUIT: + reliant_set.emplace_back(suit, foundation_rank); + break; + case pol::RED_BLACK: + if (suit == card::suit::Clubs || suit == card::suit::Spades) { + reliant_set.emplace_back(card::suit::Hearts, foundation_rank); + reliant_set.emplace_back(card::suit::Diamonds, foundation_rank); + } else { + reliant_set.emplace_back(card::suit::Clubs, foundation_rank); + reliant_set.emplace_back(card::suit::Spades, foundation_rank); + } + break; + case pol::ANY_SUIT: + reliant_set.emplace_back(card::suit::Hearts, foundation_rank); + reliant_set.emplace_back(card::suit::Diamonds, foundation_rank); + reliant_set.emplace_back(card::suit::Clubs, foundation_rank); + reliant_set.emplace_back(card::suit::Spades, foundation_rank); + break; + case pol::NO_BUILD: + break; + } + + // For each card in the reliant set, the foundation can only be + // true if the reliant card is already up, or legally could be put + // up + bool change_auto_foundation = true; + for (card reliant_card : reliant_set) { + pile_ref reliant_foundation = + *(begin(foundations) + reliant_card.get_suit()); + card::rank_t reliant_found_rank = + piles[reliant_foundation].empty() + ? card::rank_t(0) + : piles[reliant_foundation].top_card().get_rank(); + + bool reliant_card_already_up = reliant_found_rank + >= reliant_card.get_rank(); + bool reliant_card_auto_up = + reliant_found_rank == reliant_card.get_rank() - 1 + && auto_foundation_moves[reliant_card.get_suit()]; + + if (!reliant_card_already_up && !reliant_card_auto_up) { + change_auto_foundation = false; + break; + } + } + + if (change_auto_foundation) { + auto_foundation_moves[suit] = true; + // Restarts the loop + change_made = true; + break; + } + } + } +} + //////////////////////// // INSPECT GAME STATE // @@ -408,6 +519,10 @@ bool game_state::is_solved() const { return solved; } +const std::vector& game_state::get_data() const { + return piles; +} + /////////// // PRINT // diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 9b0d7b5..2b68e8c 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -11,6 +11,7 @@ #include #include +#include #include "card.h" #include "pile.h" @@ -28,6 +29,9 @@ public: typedef uint8_t pile_ref; struct move { move(pile_ref, pile_ref, pile::size_type = 1); + bool is_dominance() const; + + const static pile::size_type dominance_flag; pile_ref from; pile_ref to; pile::size_type count; }; @@ -49,6 +53,8 @@ public: // Inspects state bool is_solved() const; + const std::vector& get_data() const; + boost::optional get_dominance_move() const; friend std::ostream& operator<< (std::ostream&, const game_state&); @@ -74,6 +80,8 @@ private: void add_empty_built_group_moves(std::vector&, pile_ref, pile_ref, card) const; + void assess_auto_foundation_moves(pile_ref); + // References to piles const sol_rules rules; std::list tableau_piles; @@ -90,6 +98,8 @@ private: std::vector original_cells; std::vector original_reserve; + std::vector auto_foundation_moves; + // The core piles std::vector piles; }; diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp index fb08abb..36c5554 100644 --- a/src/main/game/game_state.legal_moves.cpp +++ b/src/main/game/game_state.legal_moves.cpp @@ -2,9 +2,12 @@ // Created by thecharlesblake on 1/3/18. // +#include + #include "game_state.h" using namespace std; +using namespace boost; typedef sol_rules::build_policy pol; typedef sol_rules::spaces_policy s_pol; @@ -279,3 +282,45 @@ void game_state::add_empty_built_group_moves(vector& moves, ); } while (piles[rem_ref][card_idx++] != bg_high); } + +///////////////////// +// Dominance Moves // +///////////////////// + +// Returns a dominance move if one is available +optional game_state::get_dominance_move() const { +#ifndef NO_AUTO_FOUNDATIONS + // If there are 2 decks or no foundations, return + if (!rules.foundations || rules.two_decks || rules.foundations_removable) + return 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 or stock cards to the foundations + if ((pr >= foundations.front() && pr <= foundations.back()) + || (rules.hole && pr == hole) + || (rules.stock_size > 0 && pr == stock) + || (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 + // makes the move and restarts the search + 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() && + auto_foundation_moves[c.get_suit()]) { + + return move(pr, target_foundation, move::dominance_flag); + } + } +#endif + + return none; +} diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp index 367f7a0..382fa50 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -9,6 +9,8 @@ using namespace std; +const pile::size_type pile::max_size_type = 255; + pile::pile(std::vector pv) : pile_vec(pv) { } diff --git a/src/main/game/pile.h b/src/main/game/pile.h index 7257738..654f5be 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -17,6 +17,7 @@ class pile { friend class cached_game_state; public: typedef uint8_t size_type; + const static size_type max_size_type; pile() = default; pile(std::vector); -- cgit v1.2.1 From ce4be63b218aaf0ab87481e90fad1407f54bca1f Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 14 Feb 2018 16:43:11 +0000 Subject: Update solver logic: simpler & handles dominances --- src/main/solver/solver.cpp | 169 ++++++++++++++++++++++++++++----------------- src/main/solver/solver.h | 19 +++-- 2 files changed, 122 insertions(+), 66 deletions(-) diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 6dddfb4..7638d92 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -7,8 +7,6 @@ #include #include -#include - #include "solver.h" #include "../input-output/output/log_helper.h" @@ -16,86 +14,133 @@ using namespace std; using namespace boost; solver::solver(const game_state& gs) - : cache(gs), initial_state(gs), states_searched(0) {} + : cache(gs) + , init_state(gs) + , state(gs) + , states_searched(1) + , root(nullptr, game_state::move(0, 0)) + , current_node(&root) { +} -solver::node::node(const game_state::move m, - vector uc) - : move(m), unsearched_children(std::move(uc)) { +solver::node::node(node* p, const game_state::move m) + : parent(p), move(m) { } bool solver::run() { - //ProfilerStart("solvitaire"); - game_state state = initial_state; - - states_searched++; - LOG_DEBUG (state); - frontier.emplace_back(game_state::move(0,0), state.get_legal_moves()); - cache.insert(state); - - while (!frontier.empty()) { - node& current = frontier.back(); - - // If we have a solution, returns true - if (state.is_solved()) { - ProfilerStop(); - return true; - } - - // If the current node has no unsearched children - if (current.unsearched_children.empty()) { - // Undoes the move that led to this state - // (unless it's the null first move) - if (frontier.size() > 1) { - state.undo_move(current.move); - assert(cache.contains(state)); - LOG_DEBUG (state); - } - // Returns to the previous state - frontier.pop_back(); + bool states_exhausted = false; + while(!state.is_solved() && !states_exhausted) { +#ifndef NDEBUG + if (current_node->move.is_dominance()) + LOG_DEBUG("(dominance move)"); + LOG_DEBUG(state); +#endif + + // If there is a dominance move available, adds it to the search tree + // and repeats. Doesn't cache the state. + optional dominance_move = state.get_dominance_move(); + if (dominance_move) { + // Adds the dominance move as a child of the current search node + add_child(*dominance_move); } else { - game_state::move next_move = current.unsearched_children.back(); - current.unsearched_children.pop_back(); - - // Applies the first possible move in this state - state.make_move(next_move); - - // Insert the state into the global cache + // Caches the current state bool is_new_state = cache.insert(state); if (is_new_state) { - states_searched++; - LOG_DEBUG (state); - // If the state is new, creates a new node in the frontier to - // represent the move just made, and those that are now possible - frontier.emplace_back(next_move, state.get_legal_moves()); - } else { - // If we've seen the state before, undoes the move - state.undo_move(next_move); - assert(cache.contains(state)); + // Gets the legal moves in the current state + vector next_moves = state.get_legal_moves(); + + // If there are none, reverts to the last node with children + if (state.get_legal_moves().empty()) { + states_exhausted = revert_to_last_node_with_children(); + } else { + add_children(next_moves); + } } + // If the state is not a new one, reverts to the last node with children + else { + states_exhausted = revert_to_last_node_with_children(); + } + } + + // Sets the current node to one of its children + assert(states_exhausted == current_node->children.empty()); + if (!states_exhausted) { + current_node = ¤t_node->children.back(); + state.make_move(current_node->move); } + + states_searched++; + } + + return state.is_solved(); +} + +void solver::add_children(std::vector& moves) { + for (auto move : moves) { + add_child(move); } +} + +void solver::add_child(game_state::move move) { + current_node->children.emplace_back(current_node, move); +} + +// Called when the current node's children have been exhausted. Travels back up +// the search tree until it finds a node which still has children. Returns true +// unless all children have been exhausted. +bool solver::revert_to_last_node_with_children() { + if (!current_node->parent) + return true; + + state.undo_move(current_node->move); + +#ifndef NDEBUG + // Checks that the state after the undo is in the cache + // (as long as the move wasn't a dominance move) - ProfilerStop(); - return false; + if (!current_node->move.is_dominance()) { + assert(cache.contains(state)); + LOG_DEBUG("(undo move)"); + } else { + LOG_DEBUG("(undo dominance move)"); + } + LOG_DEBUG(state); +#endif + + // Reverts the current node to its parent and removes it + current_node = current_node->parent; + current_node->children.pop_back(); + + // If the current node now has no children, repeat + if (current_node->children.empty()) { + return revert_to_last_node_with_children(); + } else { + return false; + } } void solver::print_solution() const { std::flush(clog); std::flush(cout); - game_state state = initial_state; + + const node* n = &root.children.back(); + game_state state_copy = init_state; cout << "Solution:\n"; - cout << state << "\n"; + cout << state_copy << "\n"; // Ignores first move which is - bool first = true; - for (auto f_node : frontier) { - if (first) { - first = false; - continue; - } + while (!n->children.empty()) { // TODO print move as well - state.make_move(f_node.move); - cout << state << "\n"; + state_copy.make_move(n->move); + cout << state_copy << "\n"; + n = &n->children.back(); } cout << "States Searched: " << states_searched << "\n"; } + +int solver::get_states_searched() const { + return states_searched; +} + +const solver::node& solver::get_search_tree() const { + return root; +} diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 526bd53..9488dda 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -6,6 +6,7 @@ #define SOLVITAIRE_SOLVER_H #include +#include #include #include "../game/global_cache.h" @@ -16,20 +17,30 @@ public: global_cache cache; struct node { - explicit node(game_state::move, std::vector); + node(node*, game_state::move); + node* parent; const game_state::move move; - std::vector unsearched_children; + std::list children; }; explicit solver(const game_state&); bool run(); void print_solution() const; + int get_states_searched() const; + const node& get_search_tree() const; private: - const game_state initial_state; - std::vector frontier; + bool revert_to_last_node_with_children(); + void add_children(std::vector&); + void add_child(game_state::move); + + const game_state init_state; + game_state state; int states_searched; + + node root; + node* current_node; }; -- cgit v1.2.1 From 7902321ea7aec1cae38c01f681df81899fb1261d Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 14 Feb 2018 16:56:29 +0000 Subject: Refactor resources directory --- resources/alpha-star.json | 8 ------ resources/bakers-dozen.json | 7 ----- resources/black-hole.json | 8 ------ resources/canfield.json | 9 ------ .../deal-presets/ImpossibleFreeCell11982.json | 12 ++++++++ resources/default.json | 21 -------------- resources/flower-garden.json | 7 ----- resources/fortunes-favor.json | 8 ------ resources/free-cell.json | 6 ---- resources/input-file-schema.json | 32 ---------------------- resources/rules-presets/alpha-star.json | 8 ++++++ resources/rules-presets/bakers-dozen.json | 7 +++++ resources/rules-presets/black-hole.json | 8 ++++++ resources/rules-presets/canfield.json | 9 ++++++ resources/rules-presets/default.json | 21 ++++++++++++++ resources/rules-presets/flower-garden.json | 7 +++++ resources/rules-presets/fortunes-favor.json | 8 ++++++ resources/rules-presets/free-cell.json | 6 ++++ resources/rules-presets/input-file-schema.json | 32 ++++++++++++++++++++++ resources/rules-presets/simple-alpha-star.json | 9 ++++++ resources/rules-presets/simple-bakers-dozen.json | 8 ++++++ resources/rules-presets/simple-black-hole.json | 9 ++++++ resources/rules-presets/simple-canfield.json | 10 +++++++ resources/rules-presets/simple-flower-garden.json | 8 ++++++ resources/rules-presets/simple-fortunes-favor.json | 9 ++++++ resources/rules-presets/simple-free-cell.json | 8 ++++++ resources/rules-presets/simple-somerset.json | 9 ++++++ .../rules-presets/simple-spanish-patience.json | 7 +++++ resources/rules-presets/simple-spider.json | 9 ++++++ resources/rules-presets/somerset.json | 8 ++++++ resources/rules-presets/spanish-patience.json | 6 ++++ resources/rules-presets/spider.json | 8 ++++++ resources/simple-alpha-star.json | 9 ------ resources/simple-bakers-dozen.json | 8 ------ resources/simple-black-hole.json | 9 ------ resources/simple-canfield.json | 10 ------- resources/simple-flower-garden.json | 8 ------ resources/simple-fortunes-favor.json | 9 ------ resources/simple-free-cell.json | 8 ------ resources/simple-somerset.json | 9 ------ resources/simple-spanish-patience.json | 7 ----- resources/simple-spider.json | 9 ------ resources/somerset.json | 8 ------ resources/spanish-patience.json | 6 ---- resources/spider.json | 8 ------ 45 files changed, 226 insertions(+), 214 deletions(-) delete mode 100644 resources/alpha-star.json delete mode 100644 resources/bakers-dozen.json delete mode 100644 resources/black-hole.json delete mode 100644 resources/canfield.json create mode 100644 resources/deal-presets/ImpossibleFreeCell11982.json delete mode 100644 resources/default.json delete mode 100644 resources/flower-garden.json delete mode 100644 resources/fortunes-favor.json delete mode 100644 resources/free-cell.json delete mode 100644 resources/input-file-schema.json create mode 100644 resources/rules-presets/alpha-star.json create mode 100644 resources/rules-presets/bakers-dozen.json create mode 100644 resources/rules-presets/black-hole.json create mode 100644 resources/rules-presets/canfield.json create mode 100644 resources/rules-presets/default.json create mode 100644 resources/rules-presets/flower-garden.json create mode 100644 resources/rules-presets/fortunes-favor.json create mode 100644 resources/rules-presets/free-cell.json create mode 100644 resources/rules-presets/input-file-schema.json create mode 100644 resources/rules-presets/simple-alpha-star.json create mode 100644 resources/rules-presets/simple-bakers-dozen.json create mode 100644 resources/rules-presets/simple-black-hole.json create mode 100644 resources/rules-presets/simple-canfield.json create mode 100644 resources/rules-presets/simple-flower-garden.json create mode 100644 resources/rules-presets/simple-fortunes-favor.json create mode 100644 resources/rules-presets/simple-free-cell.json create mode 100644 resources/rules-presets/simple-somerset.json create mode 100644 resources/rules-presets/simple-spanish-patience.json create mode 100644 resources/rules-presets/simple-spider.json create mode 100644 resources/rules-presets/somerset.json create mode 100644 resources/rules-presets/spanish-patience.json create mode 100644 resources/rules-presets/spider.json delete mode 100644 resources/simple-alpha-star.json delete mode 100644 resources/simple-bakers-dozen.json delete mode 100644 resources/simple-black-hole.json delete mode 100644 resources/simple-canfield.json delete mode 100644 resources/simple-flower-garden.json delete mode 100644 resources/simple-fortunes-favor.json delete mode 100644 resources/simple-free-cell.json delete mode 100644 resources/simple-somerset.json delete mode 100644 resources/simple-spanish-patience.json delete mode 100644 resources/simple-spider.json delete mode 100644 resources/somerset.json delete mode 100644 resources/spanish-patience.json delete mode 100644 resources/spider.json diff --git a/resources/alpha-star.json b/resources/alpha-star.json deleted file mode 100644 index fde3ac4..0000000 --- a/resources/alpha-star.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "tableau piles": { - "count": 12, - "build policy": "same-suit", - "move built group": true - }, - "foundations initial card": true -} \ No newline at end of file diff --git a/resources/bakers-dozen.json b/resources/bakers-dozen.json deleted file mode 100644 index 4f63936..0000000 --- a/resources/bakers-dozen.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "tableau piles": { - "count": 13, - "spaces policy": "no-build" - }, - "foundations removable": true -} \ No newline at end of file diff --git a/resources/black-hole.json b/resources/black-hole.json deleted file mode 100644 index daab27c..0000000 --- a/resources/black-hole.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "tableau piles": { - "count": 17, - "build policy": "no-build" - }, - "hole": true, - "foundations": false -} \ No newline at end of file diff --git a/resources/canfield.json b/resources/canfield.json deleted file mode 100644 index 5f36a72..0000000 --- a/resources/canfield.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "tableau piles": { - "count": 4, - "build policy": "red-black" - }, - "stock size": 34, - "reserve size": 14, - "reserve stacked": true -} diff --git a/resources/deal-presets/ImpossibleFreeCell11982.json b/resources/deal-presets/ImpossibleFreeCell11982.json new file mode 100644 index 0000000..2ff12e2 --- /dev/null +++ b/resources/deal-presets/ImpossibleFreeCell11982.json @@ -0,0 +1,12 @@ +{ + "tableau piles": [ + ["AH","3D","KD","JC","6C","JD","KC"], + ["AS","3H","6H","5D","2C","7D","8D"], + ["4H","QS","5S","5C","10H","8H","2S"], + ["AC","QC","4D","8C","QH","9C","3S"], + ["2D","8S","9H","9D","6D","2H"], + ["6S","7H","JH","10D","10C","QD"], + ["10S","AD","9S","KH","4S","4C"], + ["JS","KS","3C","7C","7S","5H"] + ] +} diff --git a/resources/default.json b/resources/default.json deleted file mode 100644 index 9b28865..0000000 --- a/resources/default.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "tableau piles": { - "count": 8, - "build order": "descending", - "build policy": "any-suit", - "spaces policy": "any", - "diagonal deal": false, - "move built group": false - }, - "max rank": 13, - "two decks": false, - "hole": false, - "foundations": true, - "foundations initial card": false, - "foundations removable": false, - "cells": 0, - "stock size": 0, - "stock deal type": "waste", - "reserve size": 0, - "reserve stacked": false -} \ No newline at end of file diff --git a/resources/flower-garden.json b/resources/flower-garden.json deleted file mode 100644 index 92906fb..0000000 --- a/resources/flower-garden.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "tableau piles": { - "count": 6 - }, - "reserve size": 16, - "foundations removable": true -} diff --git a/resources/fortunes-favor.json b/resources/fortunes-favor.json deleted file mode 100644 index b791561..0000000 --- a/resources/fortunes-favor.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "tableau piles": { - "count": 12, - "build policy": "same-suit" - }, - "foundations initial card": true, - "stock size": 36 -} \ No newline at end of file diff --git a/resources/free-cell.json b/resources/free-cell.json deleted file mode 100644 index 12c1bdf..0000000 --- a/resources/free-cell.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "tableau piles": { - "build policy": "red-black" - }, - "cells": 4 -} \ No newline at end of file diff --git a/resources/input-file-schema.json b/resources/input-file-schema.json deleted file mode 100644 index 1b55e4f..0000000 --- a/resources/input-file-schema.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - "$schema": "http://json-schema.org/draft-04/schema#", - "definitions": { - "card": { - "type": "string", - "pattern": "^(?i)([A|[1-9]|1[0-3]|J|Q|K])([C|D|H|S])$" - } - }, - "type": "object", - "properties": { - "tableau piles": { - "type": "array", - "items": { - "type": "array", - "items": { - "$ref": "#/definitions/card" - } - } - }, - "hole card": { - "$ref": "#/definitions/card" - } - }, - "additionalProperties": false, - "anyOf": [ - { - "required": [ - "tableau piles" - ] - } - ] -} \ No newline at end of file diff --git a/resources/rules-presets/alpha-star.json b/resources/rules-presets/alpha-star.json new file mode 100644 index 0000000..fde3ac4 --- /dev/null +++ b/resources/rules-presets/alpha-star.json @@ -0,0 +1,8 @@ +{ + "tableau piles": { + "count": 12, + "build policy": "same-suit", + "move built group": true + }, + "foundations initial card": true +} \ No newline at end of file diff --git a/resources/rules-presets/bakers-dozen.json b/resources/rules-presets/bakers-dozen.json new file mode 100644 index 0000000..4f63936 --- /dev/null +++ b/resources/rules-presets/bakers-dozen.json @@ -0,0 +1,7 @@ +{ + "tableau piles": { + "count": 13, + "spaces policy": "no-build" + }, + "foundations removable": true +} \ No newline at end of file diff --git a/resources/rules-presets/black-hole.json b/resources/rules-presets/black-hole.json new file mode 100644 index 0000000..daab27c --- /dev/null +++ b/resources/rules-presets/black-hole.json @@ -0,0 +1,8 @@ +{ + "tableau piles": { + "count": 17, + "build policy": "no-build" + }, + "hole": true, + "foundations": false +} \ No newline at end of file diff --git a/resources/rules-presets/canfield.json b/resources/rules-presets/canfield.json new file mode 100644 index 0000000..5f36a72 --- /dev/null +++ b/resources/rules-presets/canfield.json @@ -0,0 +1,9 @@ +{ + "tableau piles": { + "count": 4, + "build policy": "red-black" + }, + "stock size": 34, + "reserve size": 14, + "reserve stacked": true +} diff --git a/resources/rules-presets/default.json b/resources/rules-presets/default.json new file mode 100644 index 0000000..9b28865 --- /dev/null +++ b/resources/rules-presets/default.json @@ -0,0 +1,21 @@ +{ + "tableau piles": { + "count": 8, + "build order": "descending", + "build policy": "any-suit", + "spaces policy": "any", + "diagonal deal": false, + "move built group": false + }, + "max rank": 13, + "two decks": false, + "hole": false, + "foundations": true, + "foundations initial card": false, + "foundations removable": false, + "cells": 0, + "stock size": 0, + "stock deal type": "waste", + "reserve size": 0, + "reserve stacked": false +} \ No newline at end of file diff --git a/resources/rules-presets/flower-garden.json b/resources/rules-presets/flower-garden.json new file mode 100644 index 0000000..92906fb --- /dev/null +++ b/resources/rules-presets/flower-garden.json @@ -0,0 +1,7 @@ +{ + "tableau piles": { + "count": 6 + }, + "reserve size": 16, + "foundations removable": true +} diff --git a/resources/rules-presets/fortunes-favor.json b/resources/rules-presets/fortunes-favor.json new file mode 100644 index 0000000..b791561 --- /dev/null +++ b/resources/rules-presets/fortunes-favor.json @@ -0,0 +1,8 @@ +{ + "tableau piles": { + "count": 12, + "build policy": "same-suit" + }, + "foundations initial card": true, + "stock size": 36 +} \ No newline at end of file diff --git a/resources/rules-presets/free-cell.json b/resources/rules-presets/free-cell.json new file mode 100644 index 0000000..12c1bdf --- /dev/null +++ b/resources/rules-presets/free-cell.json @@ -0,0 +1,6 @@ +{ + "tableau piles": { + "build policy": "red-black" + }, + "cells": 4 +} \ No newline at end of file diff --git a/resources/rules-presets/input-file-schema.json b/resources/rules-presets/input-file-schema.json new file mode 100644 index 0000000..1b55e4f --- /dev/null +++ b/resources/rules-presets/input-file-schema.json @@ -0,0 +1,32 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "definitions": { + "card": { + "type": "string", + "pattern": "^(?i)([A|[1-9]|1[0-3]|J|Q|K])([C|D|H|S])$" + } + }, + "type": "object", + "properties": { + "tableau piles": { + "type": "array", + "items": { + "type": "array", + "items": { + "$ref": "#/definitions/card" + } + } + }, + "hole card": { + "$ref": "#/definitions/card" + } + }, + "additionalProperties": false, + "anyOf": [ + { + "required": [ + "tableau piles" + ] + } + ] +} \ No newline at end of file diff --git a/resources/rules-presets/simple-alpha-star.json b/resources/rules-presets/simple-alpha-star.json new file mode 100644 index 0000000..9b15004 --- /dev/null +++ b/resources/rules-presets/simple-alpha-star.json @@ -0,0 +1,9 @@ +{ + "tableau piles": { + "count": 3, + "build policy": "same-suit", + "move built group": true + }, + "max rank": 5, + "foundations initial card": true +} \ No newline at end of file diff --git a/resources/rules-presets/simple-bakers-dozen.json b/resources/rules-presets/simple-bakers-dozen.json new file mode 100644 index 0000000..3f038fe --- /dev/null +++ b/resources/rules-presets/simple-bakers-dozen.json @@ -0,0 +1,8 @@ +{ + "tableau piles": { + "count": 3, + "spaces policy": "no-build" + }, + "max rank": 3, + "foundations removable": true +} \ No newline at end of file diff --git a/resources/rules-presets/simple-black-hole.json b/resources/rules-presets/simple-black-hole.json new file mode 100644 index 0000000..16ef52d --- /dev/null +++ b/resources/rules-presets/simple-black-hole.json @@ -0,0 +1,9 @@ +{ + "tableau piles": { + "count": 4, + "build policy": "no-build" + }, + "max rank": 5, + "hole": true, + "foundations": false +} diff --git a/resources/rules-presets/simple-canfield.json b/resources/rules-presets/simple-canfield.json new file mode 100644 index 0000000..62a73c3 --- /dev/null +++ b/resources/rules-presets/simple-canfield.json @@ -0,0 +1,10 @@ +{ + "tableau piles": { + "count": 3, + "build policy": "red-black" + }, + "max rank": 3, + "stock size": 1, + "reserve size": 2, + "reserve stacked": true +} \ No newline at end of file diff --git a/resources/rules-presets/simple-flower-garden.json b/resources/rules-presets/simple-flower-garden.json new file mode 100644 index 0000000..eb7916c --- /dev/null +++ b/resources/rules-presets/simple-flower-garden.json @@ -0,0 +1,8 @@ +{ + "tableau piles": { + "count": 3 + }, + "max rank": 3, + "reserve size": 3, + "foundations removable": true +} diff --git a/resources/rules-presets/simple-fortunes-favor.json b/resources/rules-presets/simple-fortunes-favor.json new file mode 100644 index 0000000..6f3cf61 --- /dev/null +++ b/resources/rules-presets/simple-fortunes-favor.json @@ -0,0 +1,9 @@ +{ + "tableau piles": { + "count": 3, + "build policy": "same-suit" + }, + "max rank": 4, + "foundations initial card": true, + "stock size": 3 +} \ No newline at end of file diff --git a/resources/rules-presets/simple-free-cell.json b/resources/rules-presets/simple-free-cell.json new file mode 100644 index 0000000..226daf0 --- /dev/null +++ b/resources/rules-presets/simple-free-cell.json @@ -0,0 +1,8 @@ +{ + "tableau piles": { + "count": 4, + "build policy": "red-black" + }, + "max rank": 4, + "cells": 1 +} \ No newline at end of file diff --git a/resources/rules-presets/simple-somerset.json b/resources/rules-presets/simple-somerset.json new file mode 100644 index 0000000..555fddf --- /dev/null +++ b/resources/rules-presets/simple-somerset.json @@ -0,0 +1,9 @@ +{ + "tableau piles": { + "count": 4, + "build policy": "red-black", + "diagonal deal": true + }, + "max rank": 2, + "foundations removable": true +} \ No newline at end of file diff --git a/resources/rules-presets/simple-spanish-patience.json b/resources/rules-presets/simple-spanish-patience.json new file mode 100644 index 0000000..2b27cb5 --- /dev/null +++ b/resources/rules-presets/simple-spanish-patience.json @@ -0,0 +1,7 @@ +{ + "tableau piles": { + "count": 3 + }, + "max rank": 2, + "foundations removable": true +} \ No newline at end of file diff --git a/resources/rules-presets/simple-spider.json b/resources/rules-presets/simple-spider.json new file mode 100644 index 0000000..b931efb --- /dev/null +++ b/resources/rules-presets/simple-spider.json @@ -0,0 +1,9 @@ +{ + "tableau piles": { + "count": 4 + }, + "two decks": true, + "max rank": 3, + "stock size": 12, + "stock deal type": "tableau piles" +} \ No newline at end of file diff --git a/resources/rules-presets/somerset.json b/resources/rules-presets/somerset.json new file mode 100644 index 0000000..fc3bdad --- /dev/null +++ b/resources/rules-presets/somerset.json @@ -0,0 +1,8 @@ +{ + "tableau piles": { + "count": 10, + "build policy": "red-black", + "diagonal deal": true + }, + "foundations removable": true +} \ No newline at end of file diff --git a/resources/rules-presets/spanish-patience.json b/resources/rules-presets/spanish-patience.json new file mode 100644 index 0000000..32c1dab --- /dev/null +++ b/resources/rules-presets/spanish-patience.json @@ -0,0 +1,6 @@ +{ + "tableau piles": { + "count": 13 + }, + "foundations removable": true +} \ No newline at end of file diff --git a/resources/rules-presets/spider.json b/resources/rules-presets/spider.json new file mode 100644 index 0000000..4d93a47 --- /dev/null +++ b/resources/rules-presets/spider.json @@ -0,0 +1,8 @@ +{ + "tableau piles": { + "count": 10 + }, + "two decks": true, + "stock size": 60, + "stock deal type": "tableau piles" +} \ No newline at end of file diff --git a/resources/simple-alpha-star.json b/resources/simple-alpha-star.json deleted file mode 100644 index 9b15004..0000000 --- a/resources/simple-alpha-star.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "tableau piles": { - "count": 3, - "build policy": "same-suit", - "move built group": true - }, - "max rank": 5, - "foundations initial card": true -} \ No newline at end of file diff --git a/resources/simple-bakers-dozen.json b/resources/simple-bakers-dozen.json deleted file mode 100644 index 3f038fe..0000000 --- a/resources/simple-bakers-dozen.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "tableau piles": { - "count": 3, - "spaces policy": "no-build" - }, - "max rank": 3, - "foundations removable": true -} \ No newline at end of file diff --git a/resources/simple-black-hole.json b/resources/simple-black-hole.json deleted file mode 100644 index 16ef52d..0000000 --- a/resources/simple-black-hole.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "tableau piles": { - "count": 4, - "build policy": "no-build" - }, - "max rank": 5, - "hole": true, - "foundations": false -} diff --git a/resources/simple-canfield.json b/resources/simple-canfield.json deleted file mode 100644 index 62a73c3..0000000 --- a/resources/simple-canfield.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "tableau piles": { - "count": 3, - "build policy": "red-black" - }, - "max rank": 3, - "stock size": 1, - "reserve size": 2, - "reserve stacked": true -} \ No newline at end of file diff --git a/resources/simple-flower-garden.json b/resources/simple-flower-garden.json deleted file mode 100644 index eb7916c..0000000 --- a/resources/simple-flower-garden.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "tableau piles": { - "count": 3 - }, - "max rank": 3, - "reserve size": 3, - "foundations removable": true -} diff --git a/resources/simple-fortunes-favor.json b/resources/simple-fortunes-favor.json deleted file mode 100644 index 6f3cf61..0000000 --- a/resources/simple-fortunes-favor.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "tableau piles": { - "count": 3, - "build policy": "same-suit" - }, - "max rank": 4, - "foundations initial card": true, - "stock size": 3 -} \ No newline at end of file diff --git a/resources/simple-free-cell.json b/resources/simple-free-cell.json deleted file mode 100644 index 226daf0..0000000 --- a/resources/simple-free-cell.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "tableau piles": { - "count": 4, - "build policy": "red-black" - }, - "max rank": 4, - "cells": 1 -} \ No newline at end of file diff --git a/resources/simple-somerset.json b/resources/simple-somerset.json deleted file mode 100644 index 555fddf..0000000 --- a/resources/simple-somerset.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "tableau piles": { - "count": 4, - "build policy": "red-black", - "diagonal deal": true - }, - "max rank": 2, - "foundations removable": true -} \ No newline at end of file diff --git a/resources/simple-spanish-patience.json b/resources/simple-spanish-patience.json deleted file mode 100644 index 2b27cb5..0000000 --- a/resources/simple-spanish-patience.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "tableau piles": { - "count": 3 - }, - "max rank": 2, - "foundations removable": true -} \ No newline at end of file diff --git a/resources/simple-spider.json b/resources/simple-spider.json deleted file mode 100644 index b931efb..0000000 --- a/resources/simple-spider.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "tableau piles": { - "count": 4 - }, - "two decks": true, - "max rank": 3, - "stock size": 12, - "stock deal type": "tableau piles" -} \ No newline at end of file diff --git a/resources/somerset.json b/resources/somerset.json deleted file mode 100644 index fc3bdad..0000000 --- a/resources/somerset.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "tableau piles": { - "count": 10, - "build policy": "red-black", - "diagonal deal": true - }, - "foundations removable": true -} \ No newline at end of file diff --git a/resources/spanish-patience.json b/resources/spanish-patience.json deleted file mode 100644 index 32c1dab..0000000 --- a/resources/spanish-patience.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "tableau piles": { - "count": 13 - }, - "foundations removable": true -} \ No newline at end of file diff --git a/resources/spider.json b/resources/spider.json deleted file mode 100644 index 4d93a47..0000000 --- a/resources/spider.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "tableau piles": { - "count": 10 - }, - "two decks": true, - "stock size": 60, - "stock deal type": "tableau piles" -} \ No newline at end of file -- cgit v1.2.1 From 53ff6d630627c066757bb7c83281ad5281b19091 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 14 Feb 2018 16:56:49 +0000 Subject: Improve solution printing --- src/main/main.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/main.cpp b/src/main/main.cpp index 78a3551..975e6c8 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -2,6 +2,8 @@ #include #include +#include + #include "input-output/input/command_line_helper.h" #include "input-output/input/json-parsing/json_helper.h" #include "input-output/input/json-parsing/rules_parser.h" @@ -92,10 +94,12 @@ void solve_input_files(const vector input_files, const sol_rules& rules) void solve_game(const game_state& gs) { solver solv(gs); bool solution = solv.run(); + ProfilerStop(); if (solution) { solv.print_solution(); } else { - cout << "Deal:\n" << gs << "\nNo Possible Solution\n"; + cout << "Deal:\n" << gs << "\nNo Possible Solution\nStates Searched: " + << solv.get_states_searched() << "\n"; } } -- cgit v1.2.1 From 18e63a362adeb4cd2c94efce72145b6ee976c29b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 14 Feb 2018 16:57:32 +0000 Subject: Update benchmark resources path & add no auto flag --- benchmark.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/benchmark.py b/benchmark.py index 10f45dd..db73c43 100644 --- a/benchmark.py +++ b/benchmark.py @@ -38,11 +38,16 @@ if (len(sys.argv) >= 2 flag = "-no-reduced-state" print("Running in no-reduced-state mode...") +if (len(sys.argv) >= 2 + and (sys.argv[1] == "--no-auto-foundations" or sys.argv[1] == "-af")): + flag = "-no-auto-foundations" + print("Running in no-auto-foundations mode...") + class SolitaireRulesGen(metaclass=ABCMeta): def __init__(self, name): self.name = name - self.baseJson = json.load(open('resources/' + name + '.json')) + self.baseJson = json.load(open('resources/rules-presets' + name + '.json')) def genRules(self, level): self.alterFieldsToChange(level) -- cgit v1.2.1 From 2a0cfd3af6d5dd5e4c7a49e713f13d65f1ad977a Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 14 Feb 2018 16:58:08 +0000 Subject: Add foundations dominance testing --- src/test/foundations_dominance_test.cpp | 29 +++++++++++++++++++++++++++++ src/test/test_helper.cpp | 32 ++++++++++++++++++++++++++++++++ src/test/test_helper.h | 4 ++++ 3 files changed, 65 insertions(+) create mode 100644 src/test/foundations_dominance_test.cpp diff --git a/src/test/foundations_dominance_test.cpp b/src/test/foundations_dominance_test.cpp new file mode 100644 index 0000000..b12a478 --- /dev/null +++ b/src/test/foundations_dominance_test.cpp @@ -0,0 +1,29 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include + +#include "test_helper.h" +#include "../main/game/game_state.h" +#include "../main/game/global_cache.h" + +typedef sol_rules::build_policy pol; + +TEST(FoundationsDominance, SameSuit) { + test_helper::run_foundations_dominance_test(pol::SAME_SUIT, { + "AC","2C","3C","4C","AH","2H","3H","4H","AS","2S","3S","4S","AD", + "2D","3D","4D"}); +} + +TEST(FoundationsDominance, RedBlack) { + test_helper::run_foundations_dominance_test(pol::RED_BLACK, { + "AC","2C","AH","2H","AS","3H","2S","AD","3C","4H","3S","2D","4C", + "4S","3D","4D"}); +} + +TEST(FoundationsDominance, AnySuit) { + test_helper::run_foundations_dominance_test(pol::ANY_SUIT, { + "AC","2C","AH","2H","AS","2S","AD","3C","3H","3S","2D","4C","4H", + "4S","3D","4D"}); +} diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 9ee1233..246de38 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -78,3 +78,35 @@ void test_helper::run_card_cache_test(sol_rules::build_policy bp, } } } + +void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy, + std::vector cards) { + sol_rules rules = rules_parser::from_preset("simple-free-cell"); + rules.build_pol = policy; + rules.foundations_removable = false; + rules.cells = 0; + + game_state gs = game_state(rules, std::initializer_list{ + {}, + {}, + {}, + {}, + {"4C","3C","2C","AC"}, + {"4H","3H","2H","AH"}, + {"4S","3S","2S","AS"}, + {"4D","3D","2D","AD"} + }); + + solver sol = solver(game_state(gs)); + sol.run(); + + solver::node const *n = &sol.get_search_tree(); + for (card c : cards) { + n = &n->children.back(); + ASSERT_TRUE(n->move.is_dominance()); + ASSERT_TRUE(gs.get_data()[n->move.from].top_card() == c); + ASSERT_TRUE(n->move.to >= 0 && n->move.to <= 4); + gs.make_move(n->move); + } + ASSERT_TRUE(n->children.empty()); +} diff --git a/src/test/test_helper.h b/src/test/test_helper.h index fe7f8c7..b0199af 100644 --- a/src/test/test_helper.h +++ b/src/test/test_helper.h @@ -8,11 +8,15 @@ #include #include "../main/game/sol_rules.h" +#include "../main/game/card.h" class test_helper { public: static bool is_solvable(const std::string&, const std::string&); static void run_card_cache_test(sol_rules::build_policy, bool, bool); + + static void run_foundations_dominance_test(sol_rules::build_policy policy, + std::vector cards); }; -- cgit v1.2.1 From 3896073ee04cbfd016344bc086c87fced34fafa1 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 14 Feb 2018 16:58:28 +0000 Subject: Update CMake file --- CMakeLists.txt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ba6b41a..0dace0a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,7 +50,8 @@ set(sources_test src/test/somerset_test.cpp src/test/alpha_star_test.cpp src/test/spider_test.cpp - src/test/global_cache_test.cpp) + src/test/global_cache_test.cpp + src/test/foundations_dominance_test.cpp) ############################################################################### ## Rapid JSON setup ########################################################### @@ -152,6 +153,11 @@ add_executable(solvitaire-no-reduced-state ${main} ${sources}) target_link_libraries(solvitaire-no-reduced-state LINK_PUBLIC ${Boost_LIBRARIES} -lprofiler) set_target_properties(solvitaire-no-reduced-state PROPERTIES COMPILE_FLAGS -DNO_REDUCED_STATE) +# add a separete executable for the 'no auto foundations' version of the solver +add_executable(solvitaire-no-auto-foundations ${main} ${sources}) +target_link_libraries(solvitaire-no-auto-foundations LINK_PUBLIC ${Boost_LIBRARIES} -lprofiler) +set_target_properties(solvitaire-no-auto-foundations PROPERTIES COMPILE_FLAGS -DNO_AUTO_FOUNDATIONS) + ############################################################################### ## testing #################################################################### -- cgit v1.2.1 From 46e31f8be960b9aa00e9bf9218cc712674e4455b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 14 Feb 2018 20:09:59 +0000 Subject: Add multiple runs test for foundations dominance --- src/test/foundations_dominance_test.cpp | 5 ++++ src/test/test_helper.cpp | 44 +++++++++++++++++++++++++++++++++ src/test/test_helper.h | 8 ++++++ 3 files changed, 57 insertions(+) diff --git a/src/test/foundations_dominance_test.cpp b/src/test/foundations_dominance_test.cpp index b12a478..48134f5 100644 --- a/src/test/foundations_dominance_test.cpp +++ b/src/test/foundations_dominance_test.cpp @@ -27,3 +27,8 @@ TEST(FoundationsDominance, AnySuit) { "AC","2C","AH","2H","AS","2S","AD","3C","3H","3S","2D","4C","4H", "4S","3D","4D"}); } + +TEST(FoundationsDominance, NoSolutionChange) { + test_helper::optimisation_validity_test( + test_helper::optimisation::AUTO_FOUNDATION_MOVES); +} \ No newline at end of file diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 246de38..37fc32d 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -110,3 +110,47 @@ void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy, } ASSERT_TRUE(n->children.empty()); } + +void test_helper::optimisation_validity_test(optimisation op) { + for (string preset : { + "simple-alpha-star", + "simple-bakers-dozen", + "simple-black-hole", + "simple-canfield", + "simple-flower-garden", + "simple-fortunes-favor", + "simple-free-cell", + "simple-somerset", + "simple-spanish-patience", + "simple-spider" + }) { + vector solutions_op, solutions_no_op; + + switch (op) { + case optimisation::AUTO_FOUNDATION_MOVES: +#define NO_AUTO_FOUNDATIONS + solutions_no_op = run_many_games(preset); +#undef NO_AUTO_FOUNDATIONS + solutions_op = run_many_games(preset); + break; + } + + ASSERT_TRUE(solutions_op == solutions_no_op) << preset; + } +} + +std::vector test_helper::run_many_games(std::string preset) { +#ifdef NDEBUG + const int run_count = 2000; +#else + const int run_count = 5; +#endif + + vector solutions; + for (int seed = 0; seed < run_count; seed++) { + solver sol(game_state(rules_parser::from_preset(preset), seed)); + solutions.push_back(sol.run()); + } + + return solutions; +} diff --git a/src/test/test_helper.h b/src/test/test_helper.h index b0199af..24acb5f 100644 --- a/src/test/test_helper.h +++ b/src/test/test_helper.h @@ -12,11 +12,19 @@ class test_helper { public: + enum class optimisation { + AUTO_FOUNDATION_MOVES + }; + static bool is_solvable(const std::string&, const std::string&); static void run_card_cache_test(sol_rules::build_policy, bool, bool); static void run_foundations_dominance_test(sol_rules::build_policy policy, std::vector cards); + + static void optimisation_validity_test(optimisation); + + static std::vector run_many_games(std::string); }; -- cgit v1.2.1 From d94650d5ec02b5efb4ee47cfa86501c21605ad49 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 15 Feb 2018 12:39:50 +0000 Subject: Fix benchmark path bug --- benchmark.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark.py b/benchmark.py index db73c43..efdddac 100644 --- a/benchmark.py +++ b/benchmark.py @@ -47,7 +47,7 @@ class SolitaireRulesGen(metaclass=ABCMeta): def __init__(self, name): self.name = name - self.baseJson = json.load(open('resources/rules-presets' + name + '.json')) + self.baseJson = json.load(open('resources/rules-presets/' + name + '.json')) def genRules(self, level): self.alterFieldsToChange(level) -- cgit v1.2.1 From 27ebe493056ee29045361f86eea20cd176ff9143 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 15 Feb 2018 12:44:14 +0000 Subject: Fix solver bug for instant-solve games --- src/main/solver/solver.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 7638d92..e55a4e3 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "solver.h" #include "../input-output/output/log_helper.h" @@ -23,15 +24,16 @@ solver::solver(const game_state& gs) } solver::node::node(node* p, const game_state::move m) - : parent(p), move(m) { + : parent(p), move(m), children() { } bool solver::run() { bool states_exhausted = false; while(!state.is_solved() && !states_exhausted) { #ifndef NDEBUG - if (current_node->move.is_dominance()) + if (current_node->move.is_dominance()) { LOG_DEBUG("(dominance move)"); + } LOG_DEBUG(state); #endif @@ -127,12 +129,14 @@ void solver::print_solution() const { cout << "Solution:\n"; cout << state_copy << "\n"; - // Ignores first move which is - while (!n->children.empty()) { - // TODO print move as well - state_copy.make_move(n->move); - cout << state_copy << "\n"; - n = &n->children.back(); + + if (states_searched > 1) { + while (!n->children.empty()) { + // TODO print move as well + state_copy.make_move(n->move); + cout << state_copy << "\n"; + n = &n->children.back(); + } } cout << "States Searched: " << states_searched << "\n"; } -- cgit v1.2.1 From 496d7a23cdaa0616b1453d54afb34a9108190f67 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 15 Feb 2018 12:44:42 +0000 Subject: Add simple auto-found + works w/ removable founds --- src/main/game/game_state.cpp | 119 ++++++++++--------------------- src/main/game/game_state.h | 6 +- src/main/game/game_state.legal_moves.cpp | 38 +++++++--- 3 files changed, 71 insertions(+), 92 deletions(-) diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 3c92aa4..322bfcc 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -29,7 +29,10 @@ typedef sol_rules::stock_deal_type sdt; // 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) : rules(s_rules) { +game_state::game_state(const sol_rules& s_rules) : rules(s_rules), + stock(255), + waste(255), + hole(255) { // If there is a hole, creates pile if (rules.hole) { piles.emplace_back(); @@ -240,7 +243,7 @@ void game_state::make_move(const move m) { else if (m.count == 1 || m.is_dominance()) { place_card(m.to, take_card(m.from)); #ifndef NO_AUTO_FOUNDATIONS - assess_auto_foundation_moves(m.to); + update_auto_foundation_moves(m.to); #endif } // If this is a built-pile move @@ -274,7 +277,7 @@ void game_state::undo_move(const move m) { else if (m.count == 1 || m.is_dominance()) { place_card(m.from, take_card(m.to)); #ifndef NO_AUTO_FOUNDATIONS - assess_auto_foundation_moves(m.from); + update_auto_foundation_moves(m.from); #endif } else { assert(m.count <= rules.max_rank); @@ -379,97 +382,49 @@ void game_state::eval_pile_order(list& pile_lst, pile_ref changed_pr, } // Decides which 'auto foundation' booleans should be 'true' currently -void game_state::assess_auto_foundation_moves(pile_ref target_pile) { +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 || target_pile < foundations.front() - || target_pile > foundations.back() - || rules.foundations_removable) { + || target_pile > foundations.back()) { return; } // Turns the 'auto' boolean for the target foundation false - auto_foundation_moves[target_pile - foundations.front()] = false; - - // Repeats the cycle which attempts to turn one of the auto_foundation_move - // bools true for each foundation, until no change is made in a full scan - bool change_made = true; - while (change_made) { - change_made = false; - // Cycles through each of the foundations with a false 'auto' boolean, - // attempting to turn them true - for (card::suit_t suit = 0; suit < 4; suit++) { - if (auto_foundation_moves[suit]) continue; - - pile_ref foundation_ref = *(begin(foundations) + suit); - - // Empty foundations are automatically true - if(piles[foundation_ref].empty()) { - auto_foundation_moves[suit] = true; - // Restarts the loop - change_made = true; - break; - } - card::rank_t foundation_rank = piles[foundation_ref].top_card().get_rank(); - - // Creates the 'reliant' set. These are the cards that rely on the - // foundation's 'next' card to be able to move in the tableau piles - vector reliant_set; - switch (rules.build_pol) { - case pol::SAME_SUIT: - reliant_set.emplace_back(suit, foundation_rank); - break; - case pol::RED_BLACK: - if (suit == card::suit::Clubs || suit == card::suit::Spades) { - reliant_set.emplace_back(card::suit::Hearts, foundation_rank); - reliant_set.emplace_back(card::suit::Diamonds, foundation_rank); - } else { - reliant_set.emplace_back(card::suit::Clubs, foundation_rank); - reliant_set.emplace_back(card::suit::Spades, foundation_rank); - } - break; - case pol::ANY_SUIT: - reliant_set.emplace_back(card::suit::Hearts, foundation_rank); - reliant_set.emplace_back(card::suit::Diamonds, foundation_rank); - reliant_set.emplace_back(card::suit::Clubs, foundation_rank); - reliant_set.emplace_back(card::suit::Spades, foundation_rank); - break; - case pol::NO_BUILD: - break; - } - // For each card in the reliant set, the foundation can only be - // true if the reliant card is already up, or legally could be put - // up - bool change_auto_foundation = true; - for (card reliant_card : reliant_set) { - pile_ref reliant_foundation = - *(begin(foundations) + reliant_card.get_suit()); - card::rank_t reliant_found_rank = - piles[reliant_foundation].empty() - ? card::rank_t(0) - : piles[reliant_foundation].top_card().get_rank(); - - bool reliant_card_already_up = reliant_found_rank - >= reliant_card.get_rank(); - bool reliant_card_auto_up = - reliant_found_rank == reliant_card.get_rank() - 1 - && auto_foundation_moves[reliant_card.get_suit()]; - - if (!reliant_card_already_up && !reliant_card_auto_up) { - change_auto_foundation = false; - break; - } - } + for (pile_ref pr : foundations) { + auto_foundation_moves[pr] = is_valid_auto_foundation_move(pr); + } +} - if (change_auto_foundation) { - auto_foundation_moves[suit] = true; - // Restarts the loop - change_made = true; - break; +bool game_state::is_valid_auto_foundation_move(pile_ref target_pile) const { + if (rules.build_pol == pol::ANY_SUIT || rules.build_pol == pol::RED_BLACK) { + 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 target_card = card(target_suit, target_rank); + + for (pile_ref pr : foundations) { + if (pr == target_pile) continue; + + card::suit_t foundation_suit(pr - foundations.front()); + card::rank_t foundation_rank = piles[pr].empty() ? card::rank_t(0) + : piles[pr].top_card().get_rank(); + card foundation_card = card(foundation_suit, foundation_rank); + int rank_diff = int(target_rank) + - int(foundation_card.get_rank()); + + if (rules.build_pol == pol::RED_BLACK + && foundation_card.get_colour() == target_card.get_colour()) { + if (rank_diff >= 3) { + return false; + } + } else if (rank_diff >= 2) { + return false; } } } + return true; } diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 2b68e8c..b403bc7 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -49,7 +49,7 @@ public: void place_card(pile_ref, card); card take_card(pile_ref); - std::vector get_legal_moves() const; + std::vector get_legal_moves(); // Inspects state bool is_solved() const; @@ -79,8 +79,10 @@ private: void add_built_group_move(std::vector&, pile_ref, pile_ref) const; void add_empty_built_group_moves(std::vector&, pile_ref, pile_ref, card) const; + bool dominance_blocks_foundation_move(pile_ref); - void assess_auto_foundation_moves(pile_ref); + void update_auto_foundation_moves(pile_ref); + bool is_valid_auto_foundation_move(pile_ref) const; // References to piles const sol_rules rules; diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp index 36c5554..c1a87e4 100644 --- a/src/main/game/game_state.legal_moves.cpp +++ b/src/main/game/game_state.legal_moves.cpp @@ -13,7 +13,7 @@ typedef sol_rules::build_policy pol; typedef sol_rules::spaces_policy s_pol; typedef sol_rules::stock_deal_type sdt; -vector game_state::get_legal_moves() const { +vector game_state::get_legal_moves() { // The next legal moves vector moves; @@ -25,14 +25,22 @@ vector game_state::get_legal_moves() const { // Cycles through each pile which we may be able to remove a card from for (pile_ref rem_ref = 0; rem_ref < piles.size(); rem_ref++) { // Never removes a card from the hole, the waste, or an empty pile - if (rem_ref == hole || piles[rem_ref].empty()) continue; + if ((rules.hole && rem_ref == hole) || piles[rem_ref].empty()) continue; - // If the foundations aren't removable, also skip them + // If we have a foundations card as the rem_ref if (rules.foundations - && !rules.foundations_removable && rem_ref >= foundations.front() && rem_ref <= foundations.back()) { - continue; + + // If the foundations are removable, checks to see if a dominance + // prevents us from removing from it. Otherwise, skips the pile ref + if (!rules.foundations_removable +#ifndef NO_AUTO_FOUNDATIONS + || dominance_blocks_foundation_move(rem_ref) +#endif + ) { + continue; + } } // Stock cards can only be moved to the waste (assuming there is a waste) @@ -291,7 +299,7 @@ void game_state::add_empty_built_group_moves(vector& moves, optional game_state::get_dominance_move() const { #ifndef NO_AUTO_FOUNDATIONS // If there are 2 decks or no foundations, return - if (!rules.foundations || rules.two_decks || rules.foundations_removable) + if (!rules.foundations || rules.two_decks) return none; // Cycles through the piles and sees if any cards can be automatically moved @@ -309,14 +317,13 @@ optional game_state::get_dominance_move() const { pile_ref target_foundation = foundations[c.get_suit()]; // If the card is the right rank and the auto-move boolean is true, then - // makes the move and restarts the search + // 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() && auto_foundation_moves[c.get_suit()]) { - return move(pr, target_foundation, move::dominance_flag); } } @@ -324,3 +331,18 @@ optional game_state::get_dominance_move() const { return none; } + +// 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; +} -- cgit v1.2.1