From 74d43eaf200d42a3060de0f7b458902d52274225 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 1 Jan 2018 17:56:54 +0000 Subject: Fix bug in solver - improper ref to vector element --- src/main/solver/solver.cpp | 19 ++++++++++--------- src/main/solver/solver.h | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 23ed693..694ad4e 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -18,7 +18,7 @@ solver::solver(const game_state& gs, const sol_rules& sr) : initial_state(gs), rules(sr), states_searched(0) {} solver::node::node(const game_state::move m, - vector &&uc) + vector uc) : move(m), unsearched_children(std::move(uc)) { } @@ -27,10 +27,10 @@ bool solver::run() { frontier.emplace_back(NULL_MOVE, state.get_legal_moves()); states_searched++; + LOG_DEBUG (state); while (!frontier.empty()) { node& current = frontier.back(); - LOG_DEBUG (state); // If we have a solution, returns true if (state.is_solved()) return true; @@ -45,23 +45,24 @@ bool solver::run() { // Returns to the previous state frontier.pop_back(); } else { + game_state::move next_move = current.unsearched_children[current.unsearched_children.size()-1]; + current.unsearched_children.pop_back(); + // Applies the first possible move in this state - state.make_move(current.unsearched_children.back()); + state.make_move(next_move); // Insert the state into the global cache bool is_new_state = global_cache.insert(state.get_data()).second; if (is_new_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(current.unsearched_children.back(), - state.get_legal_moves()); + frontier.emplace_back(next_move, state.get_legal_moves()); states_searched++; + LOG_DEBUG (state); } else { - // If we've seen the state before, undo the move - state.undo_move(current.unsearched_children.back()); + // If we've seen the state before, undoes the move + state.undo_move(next_move); } - // Removes the move just made from the list of possible moves - current.unsearched_children.pop_back(); } } diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 1ef841d..563a11e 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -20,7 +20,7 @@ public: > global_cache; struct node { - explicit node(const game_state::move, std::vector&&); + explicit node(game_state::move, std::vector); const game_state::move move; std::vector unsearched_children; }; -- cgit v1.2.1 From 9ad093dc713f5464181b1018e80ec359ca381bb3 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 2 Jan 2018 11:27:27 +0000 Subject: Add json rules for bakers dozen --- src/main/input-output/sol_preset_types.cpp | 74 +++++++++++++++------- src/test/bakers_dozen_test.cpp | 28 ++++++++ .../resources/bakers_dozen/ComplexSolvable.json | 7 ++ .../resources/bakers_dozen/ComplexUnsolvable.json | 7 ++ .../resources/bakers_dozen/SimpleSolvable.json | 7 ++ .../resources/bakers_dozen/SimpleUnsolvable.json | 7 ++ 6 files changed, 107 insertions(+), 23 deletions(-) create mode 100644 src/test/bakers_dozen_test.cpp create mode 100644 src/test/resources/bakers_dozen/ComplexSolvable.json create mode 100644 src/test/resources/bakers_dozen/ComplexUnsolvable.json create mode 100644 src/test/resources/bakers_dozen/SimpleSolvable.json create mode 100644 src/test/resources/bakers_dozen/SimpleUnsolvable.json (limited to 'src') diff --git a/src/main/input-output/sol_preset_types.cpp b/src/main/input-output/sol_preset_types.cpp index 169a8a5..2ba14d9 100644 --- a/src/main/input-output/sol_preset_types.cpp +++ b/src/main/input-output/sol_preset_types.cpp @@ -22,7 +22,8 @@ std::map sol_preset_types::mp = { "tableau piles": { "count": 8, "build order": "descending", - "build policy": "any-suit" + "build policy": "any-suit", + "spaces policy": "any" }, "max rank": 13, "hole": false, @@ -35,85 +36,112 @@ std::map sol_preset_types::mp = { }, { - "black-hole", + "spanish-patience", R"( { "tableau piles": { - "count": 17, - "build order": "no-build" + "count": 13 + } +} +)" + + }, + { + "simple-spanish-patience", + + R"( +{ + "tableau piles": { + "count": 3 }, - "hole": true, - "foundations": false + "max rank": 2 } )" }, { - "simple-black-hole", + "free-cell", + + R"( +{ + "tableau piles": { + "build policy": "red-black" + }, + "cells": 4 +} +)" + + }, + { + "simple-free-cell", R"( { "tableau piles": { "count": 4, - "build order": "no-build" + "build policy": "red-black" }, - "max rank": 5, - "hole": true, - "foundations": false + "max rank": 4, + "cells": 1 } )" }, { - "spanish-patience", + "bakers-dozen", R"( { "tableau piles": { - "count": 13 + "count": 13, + "spaces policy": "no-build" } } )" }, { - "simple-spanish-patience", + "simple-bakers-dozen", R"( { "tableau piles": { - "count": 3 + "count": 3, + "spaces policy": "no-build" }, - "max rank": 2 + "max rank": 3 } )" }, { - "free-cell", + "black-hole", R"( { "tableau piles": { - "build policy": "red-black" + "count": 17, + "build order": "no-build" }, - "cells": 4 + "hole": true, + "foundations": false } )" }, { - "simple-free-cell", + "simple-black-hole", R"( { "tableau piles": { "count": 4, - "build policy": "red-black" + "build order": "no-build" }, - "max rank": 4, - "cells": 1 + "max rank": 5, + "hole": true, + "foundations": false } )" diff --git a/src/test/bakers_dozen_test.cpp b/src/test/bakers_dozen_test.cpp new file mode 100644 index 0000000..8973a53 --- /dev/null +++ b/src/test/bakers_dozen_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#include "test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/spanish_patience/"; +const static std::string type = "simple-spanish-patience"; + +TEST(SpanishPatience, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(SpanishPatience, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(SpanishPatience, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(SpanishPatience, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/src/test/resources/bakers_dozen/ComplexSolvable.json b/src/test/resources/bakers_dozen/ComplexSolvable.json new file mode 100644 index 0000000..c48ce47 --- /dev/null +++ b/src/test/resources/bakers_dozen/ComplexSolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AS", "2S", "AH"], + ["AD", "2D", "2H"], + ["2C", "AC"] + ] +} diff --git a/src/test/resources/bakers_dozen/ComplexUnsolvable.json b/src/test/resources/bakers_dozen/ComplexUnsolvable.json new file mode 100644 index 0000000..ba9be59 --- /dev/null +++ b/src/test/resources/bakers_dozen/ComplexUnsolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AS", "2S", "AH"], + ["AD", "2D", "2H"], + ["AC", "2C"] + ] +} diff --git a/src/test/resources/bakers_dozen/SimpleSolvable.json b/src/test/resources/bakers_dozen/SimpleSolvable.json new file mode 100644 index 0000000..eacad0e --- /dev/null +++ b/src/test/resources/bakers_dozen/SimpleSolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["2S", "AS", "AD"], + ["2D", "2H", "AH"], + ["2C", "AC"] + ] +} diff --git a/src/test/resources/bakers_dozen/SimpleUnsolvable.json b/src/test/resources/bakers_dozen/SimpleUnsolvable.json new file mode 100644 index 0000000..99a3140 --- /dev/null +++ b/src/test/resources/bakers_dozen/SimpleUnsolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AS", "2S", "2D"], + ["AD", "AH", "2H"], + ["AC", "2C"] + ] +} -- cgit v1.2.1 From 19d51fbeb4298c3319e5c78f3f2c7f16adbe6388 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 2 Jan 2018 11:28:12 +0000 Subject: Add unit tests for bakers dozen --- src/test/bakers_dozen_test.cpp | 12 ++++++------ src/test/resources/bakers_dozen/ComplexSolvable.json | 8 ++++---- src/test/resources/bakers_dozen/ComplexUnsolvable.json | 8 ++++---- src/test/resources/bakers_dozen/SimpleSolvable.json | 6 +++--- src/test/resources/bakers_dozen/SimpleUnsolvable.json | 6 +++--- 5 files changed, 20 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/test/bakers_dozen_test.cpp b/src/test/bakers_dozen_test.cpp index 8973a53..0af0669 100644 --- a/src/test/bakers_dozen_test.cpp +++ b/src/test/bakers_dozen_test.cpp @@ -8,21 +8,21 @@ typedef test_helper th; -const static std::string path = "resources/spanish_patience/"; -const static std::string type = "simple-spanish-patience"; +const static std::string path = "resources/bakers_dozen/"; +const static std::string type = "simple-bakers-dozen"; -TEST(SpanishPatience, SimpleSolvable) { +TEST(BakersDozen, SimpleSolvable) { EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); } -TEST(SpanishPatience, ComplexSolvable) { +TEST(BakersDozen, ComplexSolvable) { EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); } -TEST(SpanishPatience, SimpleUnsolvable) { +TEST(BakersDozen, SimpleUnsolvable) { EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); } -TEST(SpanishPatience, ComplexUnsolvable) { +TEST(BakersDozen, ComplexUnsolvable) { EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); } diff --git a/src/test/resources/bakers_dozen/ComplexSolvable.json b/src/test/resources/bakers_dozen/ComplexSolvable.json index c48ce47..b953413 100644 --- a/src/test/resources/bakers_dozen/ComplexSolvable.json +++ b/src/test/resources/bakers_dozen/ComplexSolvable.json @@ -1,7 +1,7 @@ { "tableau piles": [ - ["AS", "2S", "AH"], - ["AD", "2D", "2H"], - ["2C", "AC"] + ["AC","2S","AS","3H"], + ["AD","2D","2H","AH"], + ["3D","3C","2C","3S"] ] -} +} \ No newline at end of file diff --git a/src/test/resources/bakers_dozen/ComplexUnsolvable.json b/src/test/resources/bakers_dozen/ComplexUnsolvable.json index ba9be59..a8247c4 100644 --- a/src/test/resources/bakers_dozen/ComplexUnsolvable.json +++ b/src/test/resources/bakers_dozen/ComplexUnsolvable.json @@ -1,7 +1,7 @@ { "tableau piles": [ - ["AS", "2S", "AH"], - ["AD", "2D", "2H"], - ["AC", "2C"] + ["AC","2S","AS","3C"], + ["AD","2D","2H","AH"], + ["3D","3H","2C","3S"] ] -} +} \ No newline at end of file diff --git a/src/test/resources/bakers_dozen/SimpleSolvable.json b/src/test/resources/bakers_dozen/SimpleSolvable.json index eacad0e..7c00a71 100644 --- a/src/test/resources/bakers_dozen/SimpleSolvable.json +++ b/src/test/resources/bakers_dozen/SimpleSolvable.json @@ -1,7 +1,7 @@ { "tableau piles": [ - ["2S", "AS", "AD"], - ["2D", "2H", "AH"], - ["2C", "AC"] + ["3S","2S","AS","AD"], + ["3H","2D","2H","AH"], + ["3D","3C","2C","AC"] ] } diff --git a/src/test/resources/bakers_dozen/SimpleUnsolvable.json b/src/test/resources/bakers_dozen/SimpleUnsolvable.json index 99a3140..0b39a8d 100644 --- a/src/test/resources/bakers_dozen/SimpleUnsolvable.json +++ b/src/test/resources/bakers_dozen/SimpleUnsolvable.json @@ -1,7 +1,7 @@ { "tableau piles": [ - ["AS", "2S", "2D"], - ["AD", "AH", "2H"], - ["AC", "2C"] + ["AS","2S","2D","3S"], + ["AD","AH","2H","3H"], + ["AC","2C","3C","3D"] ] } -- cgit v1.2.1 From 25d3f7591e2b7ca5303593e1da4bc880174e189a Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 2 Jan 2018 11:28:47 +0000 Subject: Add rules parsing for bakers dozen --- src/main/game/sol_rules.cpp | 23 ++++++++++++++++++++--- src/main/game/sol_rules.h | 9 +++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index 353c8db..87d556f 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -111,11 +111,11 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["tableau piles"].HasMember("build policy")) { if (d["tableau piles"]["build policy"].IsString()) { - string bo_str = d["tableau piles"]["build policy"].GetString(); + string bp_str = d["tableau piles"]["build policy"].GetString(); - if (bo_str == "any-suit") { + if (bp_str == "any-suit") { sr.build_pol = build_policy::ANY_SUIT; - } else if (bo_str == "red-black") { + } else if (bp_str == "red-black") { sr.build_pol = build_policy::RED_BLACK; } else { util::json_parse_err("[tableau piles][build policy] is invalid"); @@ -126,6 +126,23 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { } } + if (d["tableau piles"].HasMember("spaces policy")) { + if (d["tableau piles"]["spaces policy"].IsString()) { + string sp_str = d["tableau piles"]["spaces policy"].GetString(); + + if (sp_str == "any") { + sr.spaces_pol = spaces_policy::ANY; + } else if (sp_str == "no-build") { + sr.spaces_pol = spaces_policy::NO_BUILD; + } else { + util::json_parse_err("[tableau piles][spaces policy] is invalid"); + } + + } else { + util::json_parse_err("[tableau piles][spaces policy] must be an string"); + } + } + } else { util::json_parse_err("[tableau piles] must be an object"); } diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index a2f8fe9..96c10d5 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -43,16 +43,21 @@ public: RED_BLACK, N_A }; + enum class spaces_policy { + NO_BUILD, + ANY + }; static bool is_suit(build_policy); static card::suit_t suit_val(build_policy); - static const sol_rules from_preset(const std::string); - static const sol_rules from_file(const std::string); + static const sol_rules from_preset(std::string); + static const sol_rules from_file(std::string); uint8_t tableau_pile_count; build_order build_ord; build_policy build_pol; + spaces_policy spaces_pol; card::rank_t max_rank; bool hole; bool foundations; -- cgit v1.2.1 From f56486a83a03ab58fd3f455258de4caebe239322 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 2 Jan 2018 11:29:16 +0000 Subject: Add empty pile logic for bakers dozen --- src/main/game/game_state.cpp | 4 +++- src/main/game/pile.cpp | 13 +++++++++---- src/main/game/pile.h | 5 ++++- 3 files changed, 16 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index caabb18..319bf5d 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -20,6 +20,7 @@ using namespace boost; typedef sol_rules::build_order ord; typedef sol_rules::build_policy pol; +typedef sol_rules::spaces_policy s_pol; // A private constructor used by both of the public ones. Initializes all of the // piles and pile refs specified by the rules @@ -28,7 +29,8 @@ game_state::game_state(const sol_rules& s_rules) : // Creates the tableau piles for (uint8_t i = 0; i < rules.tableau_pile_count; i++) { - piles.push_back(pile::tableau_factory(rules.build_ord, rules.build_pol)); + piles.push_back(pile::tableau_factory(rules.build_ord, rules.build_pol, + rules.spaces_pol)); tableau_piles.push_back(static_cast(piles.size() - 1)); } diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp index 641174e..2b97591 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -14,13 +14,14 @@ using namespace boost; typedef sol_rules::build_order ord; typedef sol_rules::build_policy pol; +typedef sol_rules::spaces_policy s_pol; //////////// // Static // //////////// -pile pile::tableau_factory(ord o, pol p) { - return pile(o, p); +pile pile::tableau_factory(ord o, pol p, s_pol sp) { + return pile(o, p, sp); } pile pile::cell_factory() { return pile(ord::SINGLE_CARD, pol::N_A); @@ -38,16 +39,17 @@ pile pile::foundation_factory(pol p) { return pile(ord::ASCENDING, p); } pile pile::hole_factory(int max_rank) { - return pile(ord::BOTH, pol::ANY_SUIT, false, true, max_rank); + return pile(ord::BOTH, pol::ANY_SUIT, s_pol::ANY, false, true, max_rank); } //////////////// // Non-static // //////////////// -pile::pile(ord bo, pol bp, bool r, bool l, int mr) : +pile::pile(ord bo, pol bp, s_pol sp, bool r, bool l, int mr) : build_order(bo), build_policy(bp), + spaces_policy(sp), removable(r), build_order_loops(l), max_rank(mr) { @@ -63,6 +65,9 @@ bool pile::can_place(const card c) const { } if (empty()) { + if (spaces_policy == s_pol::NO_BUILD) { + return false; + } if (sol_rules::is_suit(build_policy)) { return c.get_suit() == sol_rules::suit_val(build_policy) && c.get_rank() == 1; diff --git a/src/main/game/pile.h b/src/main/game/pile.h index 978e32e..a064693 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -13,7 +13,8 @@ class pile { public: // Factory methods - static pile tableau_factory(sol_rules::build_order, sol_rules::build_policy); + static pile tableau_factory(sol_rules::build_order, sol_rules::build_policy, + sol_rules::spaces_policy); static pile cell_factory(); static pile reserve_factory(); static pile stock_factory(); @@ -24,6 +25,7 @@ public: // Constructors pile(sol_rules::build_order bo = sol_rules::build_order::DESCENDING, sol_rules::build_policy bp = sol_rules::build_policy::ANY_SUIT, + sol_rules::spaces_policy sp = sol_rules::spaces_policy::ANY, bool removable = true, bool loops = false, int max_rank = 13); @@ -54,6 +56,7 @@ private: // const pile properties const sol_rules::build_order build_order; const sol_rules::build_policy build_policy; + const sol_rules::spaces_policy spaces_policy; const bool removable; const bool build_order_loops; const int max_rank; -- cgit v1.2.1 From 77a7aeba7854d15b140cc6dc3c69b15ed5f3b936 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 12:30:14 +0000 Subject: Major refactor, putting pile logic into game_state Much of the logic that was in the pile class was beginning to seem out of place. A lot of the rules have fairly arbitrary interactons *between* piles, which weren't best represented in the pile classes themselves, which were getting unweildy and inefficient. Hence, much of this logic has been moved to the game state class. --- src/main/game/card.cpp | 10 + src/main/game/card.h | 2 + src/main/game/game_state.cpp | 246 ++++++++++++++------- src/main/game/game_state.h | 33 ++- src/main/game/pile.cpp | 110 +-------- src/main/game/pile.h | 34 +-- src/main/game/sol_rules.cpp | 35 +-- src/main/game/sol_rules.h | 32 +-- src/main/input-output/deal_parser.h | 1 - src/main/input-output/sol_preset_types.cpp | 4 +- src/main/solver/solver.cpp | 6 +- src/test/resources/black_hole/ComplexSolvable.json | 4 +- 12 files changed, 226 insertions(+), 291 deletions(-) (limited to 'src') diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 4ea9991..0f9809d 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -55,6 +55,16 @@ card::rank_t card::get_rank() const { return rank; } +card::suit_t card::to_suit(uint8_t v) { + switch (v) { + case 0: return suit_t::Clubs; + case 1: return suit_t::Diamonds; + case 2: return suit_t::Hearts; + case 3: return suit_t::Spades; + default: assert(false); return suit_t::Clubs; + } +} + bool operator==(const card& l, const card& r) { return l.get_rank() == r.get_rank() && l.get_suit() == r.get_suit(); } diff --git a/src/main/game/card.h b/src/main/game/card.h index cc5a993..9c43350 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -14,6 +14,8 @@ public: enum class colour_t {Black, Red}; typedef uint8_t rank_t; + static suit_t to_suit(uint8_t); + // A card can either be created using two int values (rank & suit), or // using a string (e.g. "AS") card(suit_t, rank_t); diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 319bf5d..14867f8 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -22,62 +22,53 @@ typedef sol_rules::build_order ord; typedef sol_rules::build_policy pol; typedef sol_rules::spaces_policy s_pol; + +////////////////// +// CONSTRUCTORS // +////////////////// + // 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) { + rules(s_rules), + stock(PILE_REF_MAX), + waste(PILE_REF_MAX), + hole(PILE_REF_MAX) { - // Creates the tableau piles - for (uint8_t i = 0; i < rules.tableau_pile_count; i++) { - piles.push_back(pile::tableau_factory(rules.build_ord, rules.build_pol, - rules.spaces_pol)); - tableau_piles.push_back(static_cast(piles.size() - 1)); + // If there is a hole, creates pile + if (rules.hole) { + piles.emplace_back(); + hole = static_cast(piles.size() - 1); } // If there are foundation piles, creates the relevant pile vectors if (rules.foundations) { for (uint8_t i = 0; i < 4; i++) { - piles.push_back(pile::foundation_factory(pol(i))); + piles.emplace_back(); foundations.push_back(static_cast(piles.size() - 1)); } } - // If there are cell piles, creates the relevant cell vectors - if (rules.cells > 0) { - for (uint8_t i = 0; i < rules.cells; i++) { - piles.push_back(pile::cell_factory()); - cells.push_back(static_cast(piles.size() - 1)); - } - } - - // If there is a reserve, creates a reserve pile - if (rules.reserve_size > 0) { - piles.push_back(pile::reserve_factory()); - reserve = static_cast(piles.size() - 1); - } - // If there is a stock & waste, creates piles if (rules.stock_size > 0) { - piles.push_back(pile::stock_factory()); + piles.emplace_back(); stock = static_cast(piles.size() - 1); - piles.push_back(pile::waste_factory()); + piles.emplace_back(); waste = static_cast(piles.size() - 1); } - if (rules.hole) { - piles.push_back(pile::hole_factory(s_rules.max_rank)); - hole = static_cast(piles.size() - 1); + // If there are cell piles, creates the relevant cell vectors + if (rules.cells > 0) { + for (uint8_t i = 0; i < rules.cells; i++) { + piles.emplace_back(); + cells.push_back(static_cast(piles.size() - 1)); + } } - for (pile_ref pr = 0; pr < piles.size(); pr++) { - if (rules.stock_size > 0 && pr == waste) continue; - - if (piles[pr].can_remove()) { - removable_piles.emplace_back(pr); - } - if (piles[pr].get_build_order() != ord::NO_BUILD) { - addable_piles.emplace_back(pr); - } + // Creates the tableau piles + for (uint8_t i = 0; i < rules.tableau_pile_count; i++) { + piles.emplace_back(); + tableau_piles.push_back(static_cast(piles.size() - 1)); } } @@ -96,7 +87,7 @@ game_state::game_state(const sol_rules& s_rules, int seed) : // If there is a hole, moves the ace of spades to it if (rules.hole) { deck.erase(find(begin(deck), end(deck), card("AS"))); - piles[hole].place(card("AS")); + piles[hole].place("AS"); } // If there is a stock, deals to it and set up a waste pile too @@ -107,14 +98,6 @@ game_state::game_state(const sol_rules& s_rules, int seed) : } } - // If there is a reserve, deals to it - if (rules.reserve_size > 0) { - for (unsigned int i = 0; i < rules.reserve_size; i++) { - piles[reserve].place(deck.back()); - deck.pop_back(); - } - } - // Deals to the tableau piles (row-by-row) for (int t = 0; !deck.empty(); t++) { card c = deck.back(); @@ -147,16 +130,16 @@ vector game_state::gen_shuffled_deck(int seed, int max_rank = 13) { card::suit_t s; switch ((*i) / max_rank) { case 0 : s = card::suit_t::Clubs; - break; + break; case 1 : s = card::suit_t::Diamonds; - break; + break; case 2 : s = card::suit_t::Hearts; - break; + break; case 3 : s = card::suit_t::Spades; - break; + break; default : assert(false); - s = card::suit_t::Clubs; - break; + s = card::suit_t::Clubs; + break; } deck.emplace_back(card(s, r)); @@ -165,6 +148,11 @@ vector game_state::gen_shuffled_deck(int seed, int max_rank = 13) { return deck; } + +///////////////////////// +// MODIFYING FUNCTIONS // +///////////////////////// + void game_state::make_move(const move m) { piles[m.second].place(piles[m.first].take()); } @@ -173,33 +161,140 @@ void game_state::undo_move(const move m) { piles[m.first].place(piles[m.second].take()); } + +/////////////////////////// +// LEGAL MOVE GENERATION // +/////////////////////////// + vector game_state::get_legal_moves() const { - // The next legal states + // The next legal moves vector moves; - // Dealing from the stock to the waste - if (rules.stock_size > 0 && !piles[stock].empty()) { - moves.emplace_back(stock, waste); - } + // 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 + || rem_ref == waste + || piles[rem_ref].empty()) continue; + + // Stock cards can only be moved to the waste + if (stock != PILE_REF_MAX && rem_ref == stock) { + moves.emplace_back(stock, waste); + continue; + } - // Moving from and to the 'can remove' and 'can add' piles - // Note traversal order. Solver evaluates backwards, so we reverse traversal - // order of removable piles, but not addable. This moves from tableau piles - // to foundations first if possible - for (auto rem_it = removable_piles.rbegin(); rem_it != removable_piles.rend(); ++rem_it) { - if (piles[*rem_it].empty()) continue; + // For each tableau pile, check if moving from the rem pile is a valid + // tableau move according to the rules + for (auto add_ref : tableau_piles) { + if (is_valid_tableau_move(rem_ref, add_ref)) { + moves.emplace_back(rem_ref, add_ref); + } + } - for (const pile_ref add_pile : addable_piles) { - if (add_pile != *rem_it && - piles[add_pile].can_place(piles[*rem_it].top_card())) { - moves.emplace_back(*rem_it, add_pile); + // Any card can be moved to a cell, so long as the cell isn't full + for (auto add_ref : cells) { + if (add_ref != rem_ref && piles[add_ref].empty()) { + moves.emplace_back(rem_ref, add_ref); } } + + // Any card can be added to the waste + if (waste != PILE_REF_MAX && waste != rem_ref) { + moves.emplace_back(rem_ref, waste); + } + + // For each foundation pile, check if moving from the rem pile is a valid + // move according to the rules + for (auto add_ref : foundations) { + if (is_valid_foundations_move(rem_ref, add_ref)) { + moves.emplace_back(rem_ref, add_ref); + } + } + + // Does the same for the hole + if (hole != PILE_REF_MAX && is_valid_hole_move(rem_ref)) { + moves.emplace_back(rem_ref, hole); + } } return moves; } +// If a tableau pile is empty and the space policy is +// no-build, do not add. Otherwise any card with the right +// suit/rank can be moved to a tableau +bool game_state::is_valid_tableau_move(const pile_ref rem_ref, + const pile_ref add_ref) const { + if (rem_ref == add_ref) return false; + + else if (rules.build_pol == pol::NO_BUILD) return false; + + else if (piles[add_ref].empty()) { + return rules.spaces_pol == s_pol::ANY; + } + + card rem_c = piles[rem_ref].top_card(); + card add_c = piles[add_ref].top_card(); + + // Checks violation of same suit policy + if (rules.build_pol == pol::SAME_SUIT + && rem_c.get_suit() != add_c.get_suit()) { + return false; + } + + // Checks violation of red-black suit policy + else if (rules.build_pol == pol::RED_BLACK + && rem_c.get_colour() == add_c.get_colour()) { + return false; + } + + // Checks rank + else if (rules.build_ord == ord::DESCENDING + && rem_c.get_rank() + 1 != add_c.get_rank()) { + return false; + } else if (rules.build_ord == ord::ASCENDING + && rem_c.get_rank() - 1 != add_c.get_rank()) { + return false; + } else { + return true; + } +} + +bool game_state::is_valid_foundations_move(const pile_ref rem_ref, + const pile_ref add_ref) const { + if (rem_ref == add_ref) return false; + + card rem_c = piles[rem_ref].top_card(); + + // Checks violation of same suit policy + if (rem_c.get_suit() != card::to_suit(add_ref - foundations.front())) { + return false; + } + + // Checks rank + else { + card::rank_t add_rank = piles[add_ref].empty() + ? card::rank_t(0) + : piles[add_ref].top_card().get_rank(); + return rem_c.get_rank() - 1 == add_rank; + } +} + +bool game_state::is_valid_hole_move(const pile_ref rem_ref) const { + if (rem_ref == hole) return false; + card::rank_t rank = piles[rem_ref].top_card().get_rank(); + card::rank_t hole_rank = piles[hole].top_card().get_rank(); + return rank + 1 == hole_rank + || rank - 1 == hole_rank + || (rank == rules.max_rank && hole_rank == 1) + || (rank == 1 && hole_rank == rules.max_rank); +} + + +//////////////////////// +// INSPECT GAME STATE // +//////////////////////// + bool game_state::is_solved() const { for (auto f : foundations) { if (piles[f].size() != rules.max_rank) { @@ -214,6 +309,11 @@ const vector& game_state::get_data() const { return piles; } + +////////////// +// PRINTING // +////////////// + ostream& game_state::print(ostream& stream) const { if (rules.foundations) { print_header(stream, "Foundations"); @@ -223,10 +323,6 @@ ostream& game_state::print(ostream& stream) const { print_header(stream, "Cells"); print_piles(stream, cells); } - if (rules.reserve_size > 0) { - print_header(stream, "Reserve"); - print_pile(stream, reserve); - } if (rules.tableau_pile_count > 0) { print_header(stream, "Tableau Piles"); print_piles(stream, tableau_piles); @@ -307,14 +403,19 @@ void game_state::print_top_of_pile(ostream& stream, const pile_ref pile_r) const bool operator==(const game_state& a, const game_state& b) { return a.tableau_piles == b.tableau_piles - && a.foundations == b.foundations - && a.hole == b.hole; + && a.foundations == b.foundations + && a.hole == b.hole; } ostream& operator <<(ostream& stream, const game_state& gs) { return gs.print(stream); } + +//////////////////// +// HASH FUNCTIONS // +//////////////////// + size_t hash_value(game_state const& gs) { size_t seed = 0; @@ -328,9 +429,6 @@ size_t hash_value(game_state const& gs) { hash_combine(seed, gs.tableau_piles); } - if (gs.rules.reserve_size > 0) { - hash_combine(seed, gs.piles[gs.reserve]); - } if (gs.rules.stock_size > 0) { hash_combine(seed, gs.piles[gs.stock]); hash_combine(seed, gs.piles[gs.waste]); diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 8adc9d6..28d849e 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -5,7 +5,8 @@ #ifndef SOLVITAIRE_GAME_STATE_H #define SOLVITAIRE_GAME_STATE_H -#define NULL_MOVE (game_state::move(255, 255)) +#define NULL_MOVE (game_state::move(PILE_REF_MAX, PILE_REF_MAX)) +#define PILE_REF_MAX (255) #include #include @@ -15,28 +16,35 @@ #include #include "card.h" -#include "sol_rules.h" #include "pile.h" +#include "sol_rules.h" class deal_parser; class game_state { friend class deal_parser; public: + // Defines types typedef uint8_t pile_ref; typedef std::pair move; - // Create a game state representation from a JSON doc + // Constructors + // Creates a game state representation from a JSON doc game_state(const sol_rules&, const rapidjson::Document&); - // Do the same from a 'rules' object + // Does the same from a 'rules' object game_state(const sol_rules&, int seed); + // Alters state void make_move(move); void undo_move(move); + std::vector get_legal_moves() const; + + // Inspects state bool is_solved() const; const std::vector& get_data() const; + // Prints std::ostream& print(std::ostream&) const; friend bool operator==(const game_state&, const game_state&); @@ -47,26 +55,31 @@ public: private: static std::vector gen_shuffled_deck(int, int); + // Private constructor explicit game_state(const sol_rules&); + // Used in get_legal_moves() + bool is_valid_tableau_move(pile_ref, pile_ref) const; + bool is_valid_foundations_move(pile_ref, pile_ref) const; + bool is_valid_hole_move(pile_ref) const; + + // Prints void print_header(std::ostream&, const char*) const; void print_piles(std::ostream&, const std::vector&) const; void print_pile(std::ostream&, pile_ref) const; void print_top_of_piles(std::ostream&, const std::vector&) const; void print_top_of_pile(std::ostream&, pile_ref) const; + // References to piles sol_rules rules; - std::vector foundations; - std::vector cells; std::vector tableau_piles; - pile_ref reserve; + std::vector cells; pile_ref stock; pile_ref waste; + std::vector foundations; pile_ref hole; - std::vector addable_piles; - std::vector removable_piles; - + // The core piles std::vector piles; }; diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp index 2b97591..3b53950 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -12,114 +12,11 @@ using namespace std; using namespace boost; -typedef sol_rules::build_order ord; -typedef sol_rules::build_policy pol; -typedef sol_rules::spaces_policy s_pol; - -//////////// -// Static // -//////////// - -pile pile::tableau_factory(ord o, pol p, s_pol sp) { - return pile(o, p, sp); -} -pile pile::cell_factory() { - return pile(ord::SINGLE_CARD, pol::N_A); -} -pile pile::reserve_factory() { - return pile(ord::NO_BUILD, pol::N_A); -} -pile pile::stock_factory() { - return pile(ord::NO_BUILD, pol::N_A); -} -pile pile::waste_factory() { - return pile(ord::NO_BUILD, pol::N_A); -} -pile pile::foundation_factory(pol p) { - return pile(ord::ASCENDING, p); -} -pile pile::hole_factory(int max_rank) { - return pile(ord::BOTH, pol::ANY_SUIT, s_pol::ANY, false, true, max_rank); -} - -//////////////// -// Non-static // -//////////////// - -pile::pile(ord bo, pol bp, s_pol sp, bool r, bool l, int mr) : - build_order(bo), - build_policy(bp), - spaces_policy(sp), - removable(r), - build_order_loops(l), - max_rank(mr) { -} - -bool pile::can_place(const card c) const { - if (build_order == ord::ANY) { - return true; - } else if (build_order == ord::NO_BUILD) { - return false; - } else if (build_order == ord::SINGLE_CARD) { - return empty(); - } - - if (empty()) { - if (spaces_policy == s_pol::NO_BUILD) { - return false; - } - if (sol_rules::is_suit(build_policy)) { - return c.get_suit() == sol_rules::suit_val(build_policy) - && c.get_rank() == 1; - } else { - return true; - } - } - - card top_c = top_card(); - - // Checks violation of same suit policy - if (build_policy == pol::SAME_SUIT && c.get_suit() != top_c.get_suit()) { - return false; - } - // Checks violation of specific suit policy - if (sol_rules::is_suit(build_policy) && c.get_suit() - != sol_rules::suit_val(build_policy)) { - return false; - } - // Checks violation of red-black suit policy - if (build_policy == pol::RED_BLACK && c.get_colour() == top_c.get_colour()) { - return false; - } - - int tc_r = top_c.get_rank(); - int comp_r = c.get_rank(); - - if (build_order == ord::ASCENDING) { - return one_lt(tc_r, comp_r); - } else if (build_order == ord::DESCENDING) { - return one_lt(comp_r, tc_r); - } else if (build_order == ord::BOTH) { - return one_lt(tc_r, comp_r) || one_lt(comp_r, tc_r); - } else { - return false; - } -} - -bool pile::one_lt(int a, int b) const { - return (a == b - 1) || (build_order_loops && a == max_rank && b == 1); -} - - - card pile::top_card() const { + assert(!empty()); return pile_vec.back(); } -bool pile::can_remove() const { - return removable; -} - bool pile::empty() const { return pile_vec.empty(); } @@ -128,10 +25,6 @@ uint8_t pile::size() const { return static_cast(pile_vec.size()); } -ord pile::get_build_order() const { - return build_order; -} - card& pile::operator[] (vector::size_type i) { return pile_vec[i]; } @@ -139,7 +32,6 @@ card pile::operator[] (vector::size_type i) const { return pile_vec[i]; } - void pile::place(const card c) { pile_vec.emplace_back(c); } diff --git a/src/main/game/pile.h b/src/main/game/pile.h index a064693..a51e0f4 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -12,34 +12,13 @@ class pile { public: - // Factory methods - static pile tableau_factory(sol_rules::build_order, sol_rules::build_policy, - sol_rules::spaces_policy); - static pile cell_factory(); - static pile reserve_factory(); - static pile stock_factory(); - static pile waste_factory(); - static pile foundation_factory(sol_rules::build_policy); - static pile hole_factory(int); - - // Constructors - pile(sol_rules::build_order bo = sol_rules::build_order::DESCENDING, - sol_rules::build_policy bp = sol_rules::build_policy::ANY_SUIT, - sol_rules::spaces_policy sp = sol_rules::spaces_policy::ANY, - bool removable = true, - bool loops = false, - int max_rank = 13); - // Inspect - bool can_place(const card) const; - bool can_remove() const; card top_card() const; bool empty() const; uint8_t size() const; - sol_rules::build_order get_build_order() const; // Modify - void place(const card); + void place(card); card take(); void clear(); @@ -49,18 +28,7 @@ public: friend bool operator==(const pile&, const pile&); friend std::size_t hash_value(pile const&); - private: - bool one_lt(int, int) const; - - // const pile properties - const sol_rules::build_order build_order; - const sol_rules::build_policy build_policy; - const sol_rules::spaces_policy spaces_policy; - const bool removable; - const bool build_order_loops; - const int max_rank; - // Underlying vector std::vector pile_vec; }; diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index 87d556f..a1567f7 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -22,25 +22,6 @@ using namespace rapidjson; typedef sol_rules::build_policy pol; -bool sol_rules::is_suit(pol bp) { - switch (bp) { - case pol::CLUBS : - case pol::DIAMONDS : - case pol::HEARTS : - case pol::SPADES : return true; - default : return false; - } -} -card::suit_t sol_rules::suit_val(pol bp) { - switch (bp) { - case pol::CLUBS : return card::suit_t::Clubs; - case pol::DIAMONDS : return card::suit_t::Diamonds; - case pol::HEARTS : return card::suit_t::Hearts; - case pol::SPADES : return card::suit_t::Spades; - default: assert(false); return card::suit_t::Spades; - } -} - const sol_rules sol_rules::from_file(const string rules_file) { sol_rules sr = get_default(); Document d = util::get_file_json(rules_file); @@ -96,8 +77,8 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["tableau piles"]["build order"].IsString()) { string bo_str = d["tableau piles"]["build order"].GetString(); - if (bo_str == "no-build") { - sr.build_ord = build_order::NO_BUILD; + if (bo_str == "ascending") { + sr.build_ord = build_order::ASCENDING; } else if (bo_str == "descending") { sr.build_ord = build_order::DESCENDING; } else { @@ -117,6 +98,10 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { sr.build_pol = build_policy::ANY_SUIT; } else if (bp_str == "red-black") { sr.build_pol = build_policy::RED_BLACK; + } else if (bp_str == "same-suit") { + sr.build_pol = build_policy::SAME_SUIT; + } else if (bp_str == "no-build") { + sr.build_pol = build_policy::NO_BUILD; } else { util::json_parse_err("[tableau piles][build policy] is invalid"); } @@ -180,14 +165,6 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { } } - if (d.HasMember("reserve size")) { - if (d["reserve size"].IsInt()) { - sr.reserve_size = static_cast(d["reserve size"].GetInt()); - } else { - util::json_parse_err("[reserve size] must be an integer"); - } - } - if (d.HasMember("stock size")) { if (d["stock size"].IsInt()) { sr.stock_size = static_cast(d["stock size"].GetInt()); diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index 96c10d5..3d4995d 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -15,42 +15,22 @@ class sol_rules { public: - enum class valid_sol { - BLACK_HOLE, - SIMPLE_BLACK_HOLE, - SPANISH_PATIENCE, - SIMPLE_SPANISH_PATIENCE, - FREE_CELL, - SIMPLE_FREE_CELL, - CANFIELD, - SIMPLE_CANFIELD - }; + // Tableau policies enum class build_order { - NO_BUILD, DESCENDING, - ASCENDING, - BOTH, - SINGLE_CARD, - ANY + ASCENDING }; enum class build_policy { - CLUBS, - DIAMONDS, - HEARTS, - SPADES, - ANY_SUIT, + NO_BUILD, SAME_SUIT, RED_BLACK, - N_A + ANY_SUIT }; enum class spaces_policy { NO_BUILD, ANY }; - static bool is_suit(build_policy); - static card::suit_t suit_val(build_policy); - static const sol_rules from_preset(std::string); static const sol_rules from_file(std::string); @@ -62,15 +42,11 @@ public: bool hole; bool foundations; uint8_t cells; - uint8_t reserve_size; uint8_t stock_size; private: static sol_rules get_default(); static void modify_sol_rules(sol_rules&, rapidjson::Document&); - static void parse_err(const std::string&); - - static valid_sol valid_sol_enum(const std::string&); }; #endif //SOLVITAIRE_SOL_RULES_H diff --git a/src/main/input-output/deal_parser.h b/src/main/input-output/deal_parser.h index 255116b..76a723a 100644 --- a/src/main/input-output/deal_parser.h +++ b/src/main/input-output/deal_parser.h @@ -11,7 +11,6 @@ #include #include -#include "../game/card.h" #include "../game/sol_rules.h" class game_state; diff --git a/src/main/input-output/sol_preset_types.cpp b/src/main/input-output/sol_preset_types.cpp index 2ba14d9..3c9d295 100644 --- a/src/main/input-output/sol_preset_types.cpp +++ b/src/main/input-output/sol_preset_types.cpp @@ -122,7 +122,7 @@ std::map sol_preset_types::mp = { { "tableau piles": { "count": 17, - "build order": "no-build" + "build policy": "no-build" }, "hole": true, "foundations": false @@ -137,7 +137,7 @@ std::map sol_preset_types::mp = { { "tableau piles": { "count": 4, - "build order": "no-build" + "build policy": "no-build" }, "max rank": 5, "hole": true, diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 694ad4e..66dcc07 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -25,9 +25,9 @@ solver::node::node(const game_state::move m, bool solver::run() { game_state state = initial_state; - frontier.emplace_back(NULL_MOVE, state.get_legal_moves()); states_searched++; LOG_DEBUG (state); + frontier.emplace_back(NULL_MOVE, state.get_legal_moves()); while (!frontier.empty()) { node& current = frontier.back(); @@ -54,11 +54,11 @@ bool solver::run() { // Insert the state into the global cache bool is_new_state = global_cache.insert(state.get_data()).second; 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()); - states_searched++; - LOG_DEBUG (state); } else { // If we've seen the state before, undoes the move state.undo_move(next_move); diff --git a/src/test/resources/black_hole/ComplexSolvable.json b/src/test/resources/black_hole/ComplexSolvable.json index 8d0249d..15488e9 100644 --- a/src/test/resources/black_hole/ComplexSolvable.json +++ b/src/test/resources/black_hole/ComplexSolvable.json @@ -1,8 +1,8 @@ { "tableau piles": [ ["5D","4D","3D","2D","AD"], - ["5C","4C","3C","2C","4H","5H"], - ["AC","AH","2H","3H"], + ["5C","4C","3C","2C","4H"], + ["AC","AH","2H","3H","5H"], ["2S","5S","4S","3S"] ], "hole": "AS" -- cgit v1.2.1 From fa40f6facc5c500cae8bf890e66a7bf5d490387e Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 12:39:06 +0000 Subject: Rename util to json_helper --- src/main/game/sol_rules.cpp | 32 ++++++++++++++++---------------- src/main/input-output/deal_parser.cpp | 6 +++--- src/main/input-output/json_helper.cpp | 35 +++++++++++++++++++++++++++++++++++ src/main/input-output/json_helper.h | 19 +++++++++++++++++++ src/main/main.cpp | 4 ++-- src/main/util/util.cpp | 35 ----------------------------------- src/main/util/util.h | 19 ------------------- src/test/test_helper.cpp | 4 ++-- 8 files changed, 77 insertions(+), 77 deletions(-) create mode 100644 src/main/input-output/json_helper.cpp create mode 100644 src/main/input-output/json_helper.h delete mode 100644 src/main/util/util.cpp delete mode 100644 src/main/util/util.h (limited to 'src') diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index a1567f7..28c2af7 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -9,7 +9,7 @@ #include "rapidjson/error/en.h" #include "sol_rules.h" -#include "../util/util.h" +#include "../input-output/json_helper.h" #include "../input-output/sol_preset_types.h" using namespace std; @@ -24,7 +24,7 @@ typedef sol_rules::build_policy pol; const sol_rules sol_rules::from_file(const string rules_file) { sol_rules sr = get_default(); - Document d = util::get_file_json(rules_file); + Document d = json_helper::get_file_json(rules_file); modify_sol_rules(sr, d); @@ -59,7 +59,7 @@ sol_rules sol_rules::get_default() { void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (!d.IsObject()) { - util::json_parse_err("JSON doc must be object"); + json_helper::json_parse_err("JSON doc must be object"); } if (d.HasMember("tableau piles")) { @@ -69,7 +69,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["tableau piles"]["count"].IsInt()) { sr.tableau_pile_count = static_cast(d["tableau piles"]["count"].GetInt()); } else { - util::json_parse_err("[tableau piles][count] must be an integer"); + json_helper::json_parse_err("[tableau piles][count] must be an integer"); } } @@ -82,11 +82,11 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { } else if (bo_str == "descending") { sr.build_ord = build_order::DESCENDING; } else { - util::json_parse_err("[tableau piles][build order] is invalid"); + json_helper::json_parse_err("[tableau piles][build order] is invalid"); } } else { - util::json_parse_err("[tableau piles][build order] must be an string"); + json_helper::json_parse_err("[tableau piles][build order] must be an string"); } } @@ -103,11 +103,11 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { } else if (bp_str == "no-build") { sr.build_pol = build_policy::NO_BUILD; } else { - util::json_parse_err("[tableau piles][build policy] is invalid"); + json_helper::json_parse_err("[tableau piles][build policy] is invalid"); } } else { - util::json_parse_err("[tableau piles][build policy] must be an string"); + json_helper::json_parse_err("[tableau piles][build policy] must be an string"); } } @@ -120,16 +120,16 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { } else if (sp_str == "no-build") { sr.spaces_pol = spaces_policy::NO_BUILD; } else { - util::json_parse_err("[tableau piles][spaces policy] is invalid"); + json_helper::json_parse_err("[tableau piles][spaces policy] is invalid"); } } else { - util::json_parse_err("[tableau piles][spaces policy] must be an string"); + json_helper::json_parse_err("[tableau piles][spaces policy] must be an string"); } } } else { - util::json_parse_err("[tableau piles] must be an object"); + json_helper::json_parse_err("[tableau piles] must be an object"); } } @@ -137,7 +137,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["max rank"].IsInt()) { sr.max_rank = static_cast(d["max rank"].GetInt()); } else { - util::json_parse_err("[max rank] must be an integer"); + json_helper::json_parse_err("[max rank] must be an integer"); } } @@ -145,7 +145,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["hole"].IsBool()) { sr.hole = d["hole"].GetBool(); } else { - util::json_parse_err("[hole] must be a boolean"); + json_helper::json_parse_err("[hole] must be a boolean"); } } @@ -153,7 +153,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["foundations"].IsBool()) { sr.foundations = d["foundations"].GetBool(); } else { - util::json_parse_err("[foundations] must be a boolean"); + json_helper::json_parse_err("[foundations] must be a boolean"); } } @@ -161,7 +161,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["cells"].IsInt()) { sr.cells = static_cast(d["cells"].GetInt()); } else { - util::json_parse_err("[cells] must be an integer"); + json_helper::json_parse_err("[cells] must be an integer"); } } @@ -169,7 +169,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["stock size"].IsInt()) { sr.stock_size = static_cast(d["stock size"].GetInt()); } else { - util::json_parse_err("[stock size] must be an integer"); + json_helper::json_parse_err("[stock size] must be an integer"); } } } diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp index c876e91..8637649 100644 --- a/src/main/input-output/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp @@ -4,7 +4,7 @@ #include "deal_parser.h" #include "../game/game_state.h" -#include "../util/util.h" +#include "json_helper.h" using namespace std; using namespace rapidjson; @@ -35,7 +35,7 @@ void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& assert(json_tab_piles.IsArray()); if (gs.tableau_piles.size() != gs.rules.tableau_pile_count) { - util::json_parse_err("Incorrect number of tableau piles"); + json_helper::json_parse_err("Incorrect number of tableau piles"); } for (auto p = std::make_pair(begin(json_tab_piles.GetArray()), begin(gs.tableau_piles)); @@ -67,7 +67,7 @@ void deal_parser::parse_cells(game_state &gs, const Document& doc) { if (json_cell_arr.Empty()) return; else if (gs.rules.cells != json_cell_arr.Size()) { - util::json_parse_err("Incorrect number of cells"); + json_helper::json_parse_err("Incorrect number of cells"); } for (auto p = std::make_pair(begin(json_cell_arr), begin(gs.cells)); diff --git a/src/main/input-output/json_helper.cpp b/src/main/input-output/json_helper.cpp new file mode 100644 index 0000000..89f4564 --- /dev/null +++ b/src/main/input-output/json_helper.cpp @@ -0,0 +1,35 @@ +// +// Created by thecharlesblake on 11/13/17. +// + +#include +#include + +#include "json_helper.h" + +using namespace std; +using namespace rapidjson; + +Document json_helper::get_file_json(const string& filename) { + // Reads the file into a string + std::ifstream ifstr(filename); + std::stringstream buf; + buf << ifstr.rdbuf(); + + if (ifstr.fail()) { + throw runtime_error("could not read file " + filename); + } else { + Document d; + + d.Parse(buf.str().c_str()); + if (d.HasParseError()) { + throw runtime_error(filename + " not valid json"); + } else { + return d; + } + } +} + +void json_helper::json_parse_err(const string& msg) { + throw runtime_error("Error in JSON doc: " + msg); +} diff --git a/src/main/input-output/json_helper.h b/src/main/input-output/json_helper.h new file mode 100644 index 0000000..5ab24bc --- /dev/null +++ b/src/main/input-output/json_helper.h @@ -0,0 +1,19 @@ +// +// Created by thecharlesblake on 11/13/17. +// + +#ifndef SOLVITAIRE_UTIL_H +#define SOLVITAIRE_UTIL_H + +#include + +#include + +class json_helper { +public: + static rapidjson::Document get_file_json(const std::string&); + static void json_parse_err(const std::string& msg); +}; + + +#endif //SOLVITAIRE_UTIL_H diff --git a/src/main/main.cpp b/src/main/main.cpp index e07e566..342cdd0 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -5,7 +5,7 @@ #include "input-output/command_line_helper.h" #include "solver/solver.h" #include "input-output/log_helper.h" -#include "util/util.h" +#include "input-output/json_helper.h" using namespace rapidjson; @@ -72,7 +72,7 @@ void solve_input_files(const vector input_files, const sol_rules& rules) for (const string& input_file : input_files) { try { // Reads in the input file to a json doc - const Document in_doc = util::get_file_json(input_file); + const Document in_doc = json_helper::get_file_json(input_file); // Attempts to create a game state object from the json game_state gs(rules, in_doc); diff --git a/src/main/util/util.cpp b/src/main/util/util.cpp deleted file mode 100644 index 7e7de8d..0000000 --- a/src/main/util/util.cpp +++ /dev/null @@ -1,35 +0,0 @@ -// -// Created by thecharlesblake on 11/13/17. -// - -#include -#include - -#include "util.h" - -using namespace std; -using namespace rapidjson; - -Document util::get_file_json(const string& filename) { - // Reads the file into a string - std::ifstream ifstr(filename); - std::stringstream buf; - buf << ifstr.rdbuf(); - - if (ifstr.fail()) { - throw runtime_error("could not read file " + filename); - } else { - Document d; - - d.Parse(buf.str().c_str()); - if (d.HasParseError()) { - throw runtime_error(filename + " not valid json"); - } else { - return d; - } - } -} - -void util::json_parse_err(const string& msg) { - throw runtime_error("Error in JSON doc: " + msg); -} diff --git a/src/main/util/util.h b/src/main/util/util.h deleted file mode 100644 index de2a94d..0000000 --- a/src/main/util/util.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by thecharlesblake on 11/13/17. -// - -#ifndef SOLVITAIRE_UTIL_H -#define SOLVITAIRE_UTIL_H - -#include - -#include - -class util { -public: - static rapidjson::Document get_file_json(const std::string&); - static void json_parse_err(const std::string& msg); -}; - - -#endif //SOLVITAIRE_UTIL_H diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 9c258a3..59e7584 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -7,13 +7,13 @@ #include "test_helper.h" #include "../main/game/game_state.h" #include "../main/solver/solver.h" -#include "../main/util/util.h" +#include "../main/input-output/json_helper.h" using namespace rapidjson; using namespace std; bool test_helper::is_solvable(const std::string& input_file, const std::string& preset_type) { - const Document in_doc = util::get_file_json(input_file); + const Document in_doc = json_helper::get_file_json(input_file); const sol_rules rules = sol_rules::from_preset(preset_type); game_state gs(rules, in_doc); -- cgit v1.2.1 From 758d919cb5d600a421636ce54df9f28b6fc832ba Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 12:51:47 +0000 Subject: Refactor sol_rules into seperate parser class --- src/main/game/sol_rules.cpp | 175 --------------------------------- src/main/game/sol_rules.h | 12 +-- src/main/input-output/rules_parser.cpp | 173 ++++++++++++++++++++++++++++++++ src/main/input-output/rules_parser.h | 21 ++++ src/main/main.cpp | 9 +- src/test/test_helper.cpp | 5 +- 6 files changed, 203 insertions(+), 192 deletions(-) delete mode 100644 src/main/game/sol_rules.cpp create mode 100644 src/main/input-output/rules_parser.cpp create mode 100644 src/main/input-output/rules_parser.h (limited to 'src') diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp deleted file mode 100644 index 28c2af7..0000000 --- a/src/main/game/sol_rules.cpp +++ /dev/null @@ -1,175 +0,0 @@ -// -// Created by thecharlesblake on 10/28/17. -// - -#include -#include -#include - -#include "rapidjson/error/en.h" - -#include "sol_rules.h" -#include "../input-output/json_helper.h" -#include "../input-output/sol_preset_types.h" - -using namespace std; -using namespace boost; -using namespace rapidjson; - -//////////// -// Static // -//////////// - -typedef sol_rules::build_policy pol; - -const sol_rules sol_rules::from_file(const string rules_file) { - sol_rules sr = get_default(); - Document d = json_helper::get_file_json(rules_file); - - modify_sol_rules(sr, d); - - return sr; -} - -const sol_rules sol_rules::from_preset(const string sol_type) { - sol_rules sr = get_default(); - - const string preset_json = sol_preset_types::get(sol_type); - - Document d; - d.Parse(preset_json.c_str()); - assert(!d.HasParseError()); - - modify_sol_rules(sr, d); - return sr; -} - -sol_rules sol_rules::get_default() { - sol_rules sr; - const string default_json = sol_preset_types::get("default"); - - Document d; - d.Parse(default_json.c_str()); - assert(!d.HasParseError()); - - modify_sol_rules(sr, d); - return sr; -} - -void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { - - if (!d.IsObject()) { - json_helper::json_parse_err("JSON doc must be object"); - } - - if (d.HasMember("tableau piles")) { - if (d["tableau piles"].IsObject()) { - - if (d["tableau piles"].HasMember("count")) { - if (d["tableau piles"]["count"].IsInt()) { - sr.tableau_pile_count = static_cast(d["tableau piles"]["count"].GetInt()); - } else { - json_helper::json_parse_err("[tableau piles][count] must be an integer"); - } - } - - if (d["tableau piles"].HasMember("build order")) { - if (d["tableau piles"]["build order"].IsString()) { - string bo_str = d["tableau piles"]["build order"].GetString(); - - if (bo_str == "ascending") { - sr.build_ord = build_order::ASCENDING; - } else if (bo_str == "descending") { - sr.build_ord = build_order::DESCENDING; - } else { - json_helper::json_parse_err("[tableau piles][build order] is invalid"); - } - - } else { - json_helper::json_parse_err("[tableau piles][build order] must be an string"); - } - } - - if (d["tableau piles"].HasMember("build policy")) { - if (d["tableau piles"]["build policy"].IsString()) { - string bp_str = d["tableau piles"]["build policy"].GetString(); - - if (bp_str == "any-suit") { - sr.build_pol = build_policy::ANY_SUIT; - } else if (bp_str == "red-black") { - sr.build_pol = build_policy::RED_BLACK; - } else if (bp_str == "same-suit") { - sr.build_pol = build_policy::SAME_SUIT; - } else if (bp_str == "no-build") { - sr.build_pol = build_policy::NO_BUILD; - } else { - json_helper::json_parse_err("[tableau piles][build policy] is invalid"); - } - - } else { - json_helper::json_parse_err("[tableau piles][build policy] must be an string"); - } - } - - if (d["tableau piles"].HasMember("spaces policy")) { - if (d["tableau piles"]["spaces policy"].IsString()) { - string sp_str = d["tableau piles"]["spaces policy"].GetString(); - - if (sp_str == "any") { - sr.spaces_pol = spaces_policy::ANY; - } else if (sp_str == "no-build") { - sr.spaces_pol = spaces_policy::NO_BUILD; - } else { - json_helper::json_parse_err("[tableau piles][spaces policy] is invalid"); - } - - } else { - json_helper::json_parse_err("[tableau piles][spaces policy] must be an string"); - } - } - - } else { - json_helper::json_parse_err("[tableau piles] must be an object"); - } - } - - if (d.HasMember("max rank")) { - if (d["max rank"].IsInt()) { - sr.max_rank = static_cast(d["max rank"].GetInt()); - } else { - json_helper::json_parse_err("[max rank] must be an integer"); - } - } - - if (d.HasMember("hole")) { - if (d["hole"].IsBool()) { - sr.hole = d["hole"].GetBool(); - } else { - json_helper::json_parse_err("[hole] must be a boolean"); - } - } - - if (d.HasMember("foundations")) { - if (d["foundations"].IsBool()) { - sr.foundations = d["foundations"].GetBool(); - } else { - json_helper::json_parse_err("[foundations] must be a boolean"); - } - } - - if (d.HasMember("cells")) { - if (d["cells"].IsInt()) { - sr.cells = static_cast(d["cells"].GetInt()); - } else { - json_helper::json_parse_err("[cells] must be an integer"); - } - } - - if (d.HasMember("stock size")) { - if (d["stock size"].IsInt()) { - sr.stock_size = static_cast(d["stock size"].GetInt()); - } else { - json_helper::json_parse_err("[stock size] must be an integer"); - } - } -} diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index 3d4995d..1e6dfa4 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -5,16 +5,13 @@ #ifndef SOLVITAIRE_SOL_RULES_H #define SOLVITAIRE_SOL_RULES_H -#include #include -#include #include #include "card.h" -class sol_rules { -public: +struct sol_rules { // Tableau policies enum class build_order { DESCENDING, @@ -31,9 +28,6 @@ public: ANY }; - static const sol_rules from_preset(std::string); - static const sol_rules from_file(std::string); - uint8_t tableau_pile_count; build_order build_ord; build_policy build_pol; @@ -43,10 +37,6 @@ public: bool foundations; uint8_t cells; uint8_t stock_size; - -private: - static sol_rules get_default(); - static void modify_sol_rules(sol_rules&, rapidjson::Document&); }; #endif //SOLVITAIRE_SOL_RULES_H diff --git a/src/main/input-output/rules_parser.cpp b/src/main/input-output/rules_parser.cpp new file mode 100644 index 0000000..81cdd20 --- /dev/null +++ b/src/main/input-output/rules_parser.cpp @@ -0,0 +1,173 @@ +// +// Created by thecharlesblake on 1/3/18. +// + +#include +#include + +#include "rules_parser.h" +#include "../input-output/json_helper.h" +#include "../input-output/sol_preset_types.h" + +using namespace std; +using namespace rapidjson; + +//////////// +// Static // +//////////// + +typedef sol_rules::build_order ord; +typedef sol_rules::build_policy pol; +typedef sol_rules::spaces_policy s_pol; + +const sol_rules rules_parser::from_file(const string rules_file) { + sol_rules sr = get_default(); + Document d = json_helper::get_file_json(rules_file); + + modify_sol_rules(sr, d); + + return sr; +} + +const sol_rules rules_parser::from_preset(const string sol_type) { + sol_rules sr = get_default(); + + const string preset_json = sol_preset_types::get(sol_type); + + Document d; + d.Parse(preset_json.c_str()); + assert(!d.HasParseError()); + + modify_sol_rules(sr, d); + return sr; +} + +sol_rules rules_parser::get_default() { + sol_rules sr; + const string default_json = sol_preset_types::get("default"); + + Document d; + d.Parse(default_json.c_str()); + assert(!d.HasParseError()); + + modify_sol_rules(sr, d); + return sr; +} + +void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { + + if (!d.IsObject()) { + json_helper::json_parse_err("JSON doc must be object"); + } + + if (d.HasMember("tableau piles")) { + if (d["tableau piles"].IsObject()) { + + if (d["tableau piles"].HasMember("count")) { + if (d["tableau piles"]["count"].IsInt()) { + sr.tableau_pile_count = static_cast(d["tableau piles"]["count"].GetInt()); + } else { + json_helper::json_parse_err("[tableau piles][count] must be an integer"); + } + } + + if (d["tableau piles"].HasMember("build order")) { + if (d["tableau piles"]["build order"].IsString()) { + string bo_str = d["tableau piles"]["build order"].GetString(); + + if (bo_str == "ascending") { + sr.build_ord = ord::ASCENDING; + } else if (bo_str == "descending") { + sr.build_ord = ord::DESCENDING; + } else { + json_helper::json_parse_err("[tableau piles][build order] is invalid"); + } + + } else { + json_helper::json_parse_err("[tableau piles][build order] must be an string"); + } + } + + if (d["tableau piles"].HasMember("build policy")) { + if (d["tableau piles"]["build policy"].IsString()) { + string bp_str = d["tableau piles"]["build policy"].GetString(); + + if (bp_str == "any-suit") { + sr.build_pol = pol::ANY_SUIT; + } else if (bp_str == "red-black") { + sr.build_pol = pol::RED_BLACK; + } else if (bp_str == "same-suit") { + sr.build_pol = pol::SAME_SUIT; + } else if (bp_str == "no-build") { + sr.build_pol = pol::NO_BUILD; + } else { + json_helper::json_parse_err("[tableau piles][build policy] is invalid"); + } + + } else { + json_helper::json_parse_err("[tableau piles][build policy] must be an string"); + } + } + + if (d["tableau piles"].HasMember("spaces policy")) { + if (d["tableau piles"]["spaces policy"].IsString()) { + string sp_str = d["tableau piles"]["spaces policy"].GetString(); + + if (sp_str == "any") { + sr.spaces_pol = s_pol::ANY; + } else if (sp_str == "no-build") { + sr.spaces_pol = s_pol::NO_BUILD; + } else { + json_helper::json_parse_err("[tableau piles][spaces policy] is invalid"); + } + + } else { + json_helper::json_parse_err("[tableau piles][spaces policy] must be an string"); + } + } + + } else { + json_helper::json_parse_err("[tableau piles] must be an object"); + } + } + + if (d.HasMember("max rank")) { + if (d["max rank"].IsInt()) { + sr.max_rank = static_cast(d["max rank"].GetInt()); + } else { + json_helper::json_parse_err("[max rank] must be an integer"); + } + } + + if (d.HasMember("hole")) { + if (d["hole"].IsBool()) { + sr.hole = d["hole"].GetBool(); + } else { + json_helper::json_parse_err("[hole] must be a boolean"); + } + } + + if (d.HasMember("foundations")) { + if (d["foundations"].IsBool()) { + sr.foundations = d["foundations"].GetBool(); + } else { + json_helper::json_parse_err("[foundations] must be a boolean"); + } + } + + if (d.HasMember("cells")) { + if (d["cells"].IsInt()) { + sr.cells = static_cast(d["cells"].GetInt()); + } else { + json_helper::json_parse_err("[cells] must be an integer"); + } + } + + if (d.HasMember("stock size")) { + if (d["stock size"].IsInt()) { + sr.stock_size = static_cast(d["stock size"].GetInt()); + } else { + json_helper::json_parse_err("[stock size] must be an integer"); + } + } +} diff --git a/src/main/input-output/rules_parser.h b/src/main/input-output/rules_parser.h new file mode 100644 index 0000000..a478a01 --- /dev/null +++ b/src/main/input-output/rules_parser.h @@ -0,0 +1,21 @@ +// +// Created by thecharlesblake on 1/3/18. +// + +#ifndef SOLVITAIRE_RULES_PARSER_H +#define SOLVITAIRE_RULES_PARSER_H + +#include "../game/sol_rules.h" + +class rules_parser { +public: + static const sol_rules from_file(std::string); + static const sol_rules from_preset(std::string); + +private: + static sol_rules get_default(); + static void modify_sol_rules(sol_rules&, rapidjson::Document&); +}; + + +#endif //SOLVITAIRE_RULES_PARSER_H diff --git a/src/main/main.cpp b/src/main/main.cpp index 342cdd0..5386da6 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -3,9 +3,10 @@ #include #include "input-output/command_line_helper.h" -#include "solver/solver.h" #include "input-output/log_helper.h" #include "input-output/json_helper.h" +#include "input-output/rules_parser.h" +#include "solver/solver.h" using namespace rapidjson; @@ -16,7 +17,7 @@ namespace po = boost::program_options; const optional gen_rules(command_line_helper&); void solve_random_game(int, const sol_rules&); -void solve_input_files(const vector, const sol_rules&); +void solve_input_files(vector, const sol_rules&); void solve_game(const game_state&, const sol_rules&); int main(int argc, const char* argv[]) { @@ -50,9 +51,9 @@ int main(int argc, const char* argv[]) { const optional gen_rules(command_line_helper& clh) { try { if (!clh.get_solitaire_type().empty()) { - return sol_rules::from_preset(clh.get_solitaire_type()); + return rules_parser::from_preset(clh.get_solitaire_type()); } else { - return sol_rules::from_file(clh.get_rules_file()); + return rules_parser::from_file(clh.get_rules_file()); } } catch (const runtime_error& error) { string errmsg = "Error in rules generation: "; diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 59e7584..117273c 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -8,16 +8,17 @@ #include "../main/game/game_state.h" #include "../main/solver/solver.h" #include "../main/input-output/json_helper.h" +#include "../main/input-output/rules_parser.h" using namespace rapidjson; using namespace std; bool test_helper::is_solvable(const std::string& input_file, const std::string& preset_type) { const Document in_doc = json_helper::get_file_json(input_file); - const sol_rules rules = sol_rules::from_preset(preset_type); + const sol_rules rules = rules_parser::from_preset(preset_type); game_state gs(rules, in_doc); solver sol(gs, rules); - return sol.run() ? true : false; + return sol.run(); } -- cgit v1.2.1 From c70a063f407534ac3d8997b9f1bc58d23c9360b8 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 13:03:26 +0000 Subject: Tidy up card class --- src/main/game/card.h | 4 +++- src/main/game/game_state.cpp | 2 +- src/main/input-output/deal_parser.cpp | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/main/game/card.h b/src/main/game/card.h index 9c43350..3a1c6b0 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -10,16 +10,18 @@ class card { public: + // Types & typedefs enum class suit_t {Clubs, Diamonds, Hearts, Spades}; enum class colour_t {Black, Red}; typedef uint8_t rank_t; + // Converts a number (0-3) to a suit (Clubs-Spades) static suit_t to_suit(uint8_t); // A card can either be created using two int values (rank & suit), or // using a string (e.g. "AS") card(suit_t, rank_t); - card(const char*); + explicit card(const char*); suit_t get_suit() const; colour_t get_colour() const; diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 14867f8..b45cc11 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -87,7 +87,7 @@ game_state::game_state(const sol_rules& s_rules, int seed) : // If there is a hole, moves the ace of spades to it if (rules.hole) { deck.erase(find(begin(deck), end(deck), card("AS"))); - piles[hole].place("AS"); + piles[hole].place(card("AS")); } // If there is a stock, deals to it and set up a waste pile too diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp index 8637649..1746603 100644 --- a/src/main/input-output/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp @@ -76,7 +76,7 @@ void deal_parser::parse_cells(game_state &gs, const Document& doc) { auto json_card = p.first; assert(json_card->IsString()); - gs.piles[*p.second].place(json_card->GetString()); + gs.piles[*p.second].place(card(json_card->GetString())); } } } -- cgit v1.2.1 From 7f4712b292632becfc83bc0e887271b25fefd21a Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 13:05:31 +0000 Subject: Refactor io dir structure --- src/main/game/game_state.cpp | 2 +- src/main/input-output/command_line_helper.cpp | 170 ------------------- src/main/input-output/command_line_helper.h | 40 ----- src/main/input-output/deal_parser.cpp | 82 ---------- src/main/input-output/deal_parser.h | 27 ---- .../input-output/input/command_line_helper.cpp | 170 +++++++++++++++++++ src/main/input-output/input/command_line_helper.h | 40 +++++ .../input/json-parsing/deal_parser.cpp | 82 ++++++++++ .../input-output/input/json-parsing/deal_parser.h | 27 ++++ .../input/json-parsing/json_helper.cpp | 35 ++++ .../input-output/input/json-parsing/json_helper.h | 19 +++ .../input/json-parsing/rules_parser.cpp | 173 ++++++++++++++++++++ .../input-output/input/json-parsing/rules_parser.h | 21 +++ src/main/input-output/input/sol_preset_types.cpp | 180 +++++++++++++++++++++ src/main/input-output/input/sol_preset_types.h | 19 +++ src/main/input-output/json_helper.cpp | 35 ---- src/main/input-output/json_helper.h | 19 --- src/main/input-output/log_helper.h | 19 --- src/main/input-output/output/log_helper.h | 19 +++ src/main/input-output/rules_parser.cpp | 173 -------------------- src/main/input-output/rules_parser.h | 21 --- src/main/input-output/sol_preset_types.cpp | 180 --------------------- src/main/input-output/sol_preset_types.h | 19 --- src/main/main.cpp | 8 +- src/main/solver/solver.cpp | 2 +- src/test/test_helper.cpp | 4 +- 26 files changed, 793 insertions(+), 793 deletions(-) delete mode 100644 src/main/input-output/command_line_helper.cpp delete mode 100644 src/main/input-output/command_line_helper.h delete mode 100644 src/main/input-output/deal_parser.cpp delete mode 100644 src/main/input-output/deal_parser.h create mode 100644 src/main/input-output/input/command_line_helper.cpp create mode 100644 src/main/input-output/input/command_line_helper.h create mode 100644 src/main/input-output/input/json-parsing/deal_parser.cpp create mode 100644 src/main/input-output/input/json-parsing/deal_parser.h create mode 100644 src/main/input-output/input/json-parsing/json_helper.cpp create mode 100644 src/main/input-output/input/json-parsing/json_helper.h create mode 100644 src/main/input-output/input/json-parsing/rules_parser.cpp create mode 100644 src/main/input-output/input/json-parsing/rules_parser.h create mode 100644 src/main/input-output/input/sol_preset_types.cpp create mode 100644 src/main/input-output/input/sol_preset_types.h delete mode 100644 src/main/input-output/json_helper.cpp delete mode 100644 src/main/input-output/json_helper.h delete mode 100644 src/main/input-output/log_helper.h create mode 100644 src/main/input-output/output/log_helper.h delete mode 100644 src/main/input-output/rules_parser.cpp delete mode 100644 src/main/input-output/rules_parser.h delete mode 100644 src/main/input-output/sol_preset_types.cpp delete mode 100644 src/main/input-output/sol_preset_types.h (limited to 'src') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index b45cc11..78a5253 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -12,7 +12,7 @@ #include #include "game_state.h" -#include "../input-output/deal_parser.h" +#include "../input-output/input/json-parsing/deal_parser.h" using namespace std; using namespace rapidjson; diff --git a/src/main/input-output/command_line_helper.cpp b/src/main/input-output/command_line_helper.cpp deleted file mode 100644 index 97751a4..0000000 --- a/src/main/input-output/command_line_helper.cpp +++ /dev/null @@ -1,170 +0,0 @@ -// -// Created by thecharlesblake on 10/16/17. -// - -#include -#include - -#include - -#include "command_line_helper.h" -#include "../game/sol_rules.h" -#include "log_helper.h" -#include "sol_preset_types.h" - -using namespace std; - -namespace po = boost::program_options; - -command_line_helper::command_line_helper() - : main_options("options") { - - main_options.add_options() - ("help", "produce help message") - ("type", po::value(), - "specify the type of the solitaire game to be solved. Must supply " - "either 'type' or 'rules' file") - ("rules", po::value(), - "the path to a JSON file describing the rules of the solitaire " - "to be solved. Must supply either 'type' or 'rules' file") - ("random", po::value(), "create and solve a random solitaire " - "deal based on a seed. Must supply either 'random' or list " - "of deals to be sold."); - - po::options_description hidden_options("Hidden options"); - hidden_options.add_options() - ("input-files", po::value>(), - "the file containing the deal to be solved"); - - cmdline_options.add(main_options).add(hidden_options); - - p.add("input-files", -1); -} - -// Returns true if we can continue solving the supplied solitaire(s) -bool command_line_helper::parse(int argc, const char* argv[]) { - po::variables_map vm; - // Attempts to parse cmdln options. Catches basic errors - try { - store(po::command_line_parser(argc, argv).options(cmdline_options) - .positional(p).run(), vm); - } catch(po::error& e) { - LOG_ERROR ("Error: " << e.what()); - return false; - } - - // Updates the variables map which the user's options are stored in - notify(vm); - - // Converts the variables map into flags and values - - help = (vm.count("help") != 0); - - if (vm.count("input-files")) { - input_files = vm["input-files"].as>(); - } - - if (vm.count("type")) { - solitaire_type = vm["type"].as(); - } - - if (vm.count("rules")) { - rules_file = vm["rules"].as(); - } - - if (vm.count("random")) { - random_deal = vm["random"].as(); - } else { - random_deal = -1; - } - - // Handle logic error scenarios - return assess_errors(); -} - -bool command_line_helper::assess_errors() { - if (help) { - print_help(); - return false; - } - - // The user must either supply input files or a random seed - - if (random_deal != -1 && !input_files.empty()) { - print_rand_plus_input_err(); - return false; - } - - if (input_files.empty() && random_deal == -1) { - print_no_input_error(); - return false; - } - - // The user must supply either a solitaire type or a rules file - if ((solitaire_type.empty() && rules_file.empty()) - || (!solitaire_type.empty() && !rules_file.empty())) { - print_sol_type_rules_error(); - return false; - } - - if (!solitaire_type.empty()) { - return assess_sol_type(); - } else { - return true; - } -} - -// Checks if the supplied solitaire type is in the list of valid solitaires -bool command_line_helper::assess_sol_type() { - if (sol_preset_types::is_valid_preset(solitaire_type)) { - return true; - - } else { - LOG_ERROR ("Error: Solitaire type is not valid: " << solitaire_type); - print_help(); - return false; - } -} - -void command_line_helper::print_help() { - LOG_ERROR ("Usage: solvitaire [options] input-file1 input-file2 ...\n" - << main_options); -} - -void command_line_helper::print_no_input_error() { - LOG_ERROR ("Error: User must supply input file(s) or the '--random' " - "option"); - print_help(); -} - -void command_line_helper::print_sol_type_rules_error() { - LOG_ERROR ("Error: User must supply either a solitaire type, or a 'rules' " - "file"); - print_help(); -} - -void command_line_helper::print_rand_plus_input_err() { - LOG_ERROR ("Error: User must supply input file(s) or the '--random' option, " - "not both"); - print_help(); -} - -const vector command_line_helper::get_input_files() { - return input_files; -} - -const string command_line_helper::get_solitaire_type() { - return solitaire_type; -} - -const string command_line_helper::get_rules_file() { - return rules_file; -} - -int command_line_helper::get_random_deal() { - return random_deal; -} - -bool command_line_helper::get_help() { - return help; -} diff --git a/src/main/input-output/command_line_helper.h b/src/main/input-output/command_line_helper.h deleted file mode 100644 index fd1468c..0000000 --- a/src/main/input-output/command_line_helper.h +++ /dev/null @@ -1,40 +0,0 @@ -// -// Created by thecharlesblake on 10/16/17. -// - -#ifndef SOLVITAIRE_COMMAND_LINE_HELPER_H -#define SOLVITAIRE_COMMAND_LINE_HELPER_H - -#include - -class command_line_helper { -public: - command_line_helper(); - - bool parse(int argc, const char* argv[]); - const std::vector get_input_files(); - const std::string get_solitaire_type(); - const std::string get_rules_file(); - int get_random_deal(); - bool get_help(); - -private: - bool assess_errors(); - bool assess_sol_type(); - void print_help(); - void print_no_input_error(); - void print_sol_type_rules_error(); - void print_rand_plus_input_err(); - - boost::program_options::options_description cmdline_options; - boost::program_options::options_description main_options; - boost::program_options::positional_options_description p; - - std::vector input_files; - std::string solitaire_type; - std::string rules_file; - int random_deal; - bool help; -}; - -#endif //SOLVITAIRE_COMMAND_LINE_HELPER_H diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp deleted file mode 100644 index 1746603..0000000 --- a/src/main/input-output/deal_parser.cpp +++ /dev/null @@ -1,82 +0,0 @@ -// -// Created by thecharlesblake on 10/21/17. -// - -#include "deal_parser.h" -#include "../game/game_state.h" -#include "json_helper.h" - -using namespace std; -using namespace rapidjson; - -typedef sol_rules::build_order ord; -typedef sol_rules::build_policy pol; - -void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { - // Construct tableau piles - if (gs.rules.tableau_pile_count > 0) { - parse_tableau_piles(gs, doc); - } - - // Construct hole card - if (gs.rules.hole) { - parse_hole(gs, doc); - } - - // Construct filled cells - if (gs.rules.cells) { - parse_cells(gs, doc); - } -} - -void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& doc) { - assert(doc.HasMember("tableau piles")); - const Value& json_tab_piles = doc["tableau piles"]; - assert(json_tab_piles.IsArray()); - - if (gs.tableau_piles.size() != gs.rules.tableau_pile_count) { - json_helper::json_parse_err("Incorrect number of tableau piles"); - } - - for (auto p = std::make_pair(begin(json_tab_piles.GetArray()), begin(gs.tableau_piles)); - p.second != end(gs.tableau_piles); - ++p.first, ++p.second) { - - for (auto& json_card : p.first->GetArray()) { - assert(json_card.IsString()); - gs.piles[*p.second].place(card(json_card.GetString())); - } - } -} - -void deal_parser::parse_hole(game_state &gs, const Document& doc) { - if (doc.HasMember("hole")) { - const Value &json_hole = doc["hole"]; - assert(json_hole.IsString()); - - gs.piles[gs.hole].place(card(json_hole.GetString())); - } -} - -void deal_parser::parse_cells(game_state &gs, const Document& doc) { - if (doc.HasMember("cells")) { - const Value &json_cells = doc["cells"]; - assert(json_cells.IsArray()); - - const auto json_cell_arr = json_cells.GetArray(); - - if (json_cell_arr.Empty()) return; - else if (gs.rules.cells != json_cell_arr.Size()) { - json_helper::json_parse_err("Incorrect number of cells"); - } - - for (auto p = std::make_pair(begin(json_cell_arr), begin(gs.cells)); - p.second != end(gs.cells); - ++p.first, ++p.second) { - - auto json_card = p.first; - assert(json_card->IsString()); - gs.piles[*p.second].place(card(json_card->GetString())); - } - } -} diff --git a/src/main/input-output/deal_parser.h b/src/main/input-output/deal_parser.h deleted file mode 100644 index 76a723a..0000000 --- a/src/main/input-output/deal_parser.h +++ /dev/null @@ -1,27 +0,0 @@ -// -// Created by thecharlesblake on 10/21/17. -// - -#ifndef SOLVITAIRE_DEAL_PARSER_H -#define SOLVITAIRE_DEAL_PARSER_H - -#include - -#include -#include -#include - -#include "../game/sol_rules.h" - -class game_state; - -class deal_parser { -public: - static void parse(game_state&, const rapidjson::Document&); -private: - static void parse_tableau_piles(game_state&, const rapidjson::Document&); - static void parse_hole(game_state&, const rapidjson::Document&); - static void parse_cells(game_state&, const rapidjson::Document&); -}; - -#endif //SOLVITAIRE_DEAL_PARSER_H diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp new file mode 100644 index 0000000..2391e3a --- /dev/null +++ b/src/main/input-output/input/command_line_helper.cpp @@ -0,0 +1,170 @@ +// +// Created by thecharlesblake on 10/16/17. +// + +#include +#include + +#include + +#include "command_line_helper.h" +#include "../../game/sol_rules.h" +#include "../output/log_helper.h" +#include "sol_preset_types.h" + +using namespace std; + +namespace po = boost::program_options; + +command_line_helper::command_line_helper() + : main_options("options") { + + main_options.add_options() + ("help", "produce help message") + ("type", po::value(), + "specify the type of the solitaire game to be solved. Must supply " + "either 'type' or 'rules' file") + ("rules", po::value(), + "the path to a JSON file describing the rules of the solitaire " + "to be solved. Must supply either 'type' or 'rules' file") + ("random", po::value(), "create and solve a random solitaire " + "deal based on a seed. Must supply either 'random' or list " + "of deals to be sold."); + + po::options_description hidden_options("Hidden options"); + hidden_options.add_options() + ("input-files", po::value>(), + "the file containing the deal to be solved"); + + cmdline_options.add(main_options).add(hidden_options); + + p.add("input-files", -1); +} + +// Returns true if we can continue solving the supplied solitaire(s) +bool command_line_helper::parse(int argc, const char* argv[]) { + po::variables_map vm; + // Attempts to parse cmdln options. Catches basic errors + try { + store(po::command_line_parser(argc, argv).options(cmdline_options) + .positional(p).run(), vm); + } catch(po::error& e) { + LOG_ERROR ("Error: " << e.what()); + return false; + } + + // Updates the variables map which the user's options are stored in + notify(vm); + + // Converts the variables map into flags and values + + help = (vm.count("help") != 0); + + if (vm.count("input-files")) { + input_files = vm["input-files"].as>(); + } + + if (vm.count("type")) { + solitaire_type = vm["type"].as(); + } + + if (vm.count("rules")) { + rules_file = vm["rules"].as(); + } + + if (vm.count("random")) { + random_deal = vm["random"].as(); + } else { + random_deal = -1; + } + + // Handle logic error scenarios + return assess_errors(); +} + +bool command_line_helper::assess_errors() { + if (help) { + print_help(); + return false; + } + + // The user must either supply input files or a random seed + + if (random_deal != -1 && !input_files.empty()) { + print_rand_plus_input_err(); + return false; + } + + if (input_files.empty() && random_deal == -1) { + print_no_input_error(); + return false; + } + + // The user must supply either a solitaire type or a rules file + if ((solitaire_type.empty() && rules_file.empty()) + || (!solitaire_type.empty() && !rules_file.empty())) { + print_sol_type_rules_error(); + return false; + } + + if (!solitaire_type.empty()) { + return assess_sol_type(); + } else { + return true; + } +} + +// Checks if the supplied solitaire type is in the list of valid solitaires +bool command_line_helper::assess_sol_type() { + if (sol_preset_types::is_valid_preset(solitaire_type)) { + return true; + + } else { + LOG_ERROR ("Error: Solitaire type is not valid: " << solitaire_type); + print_help(); + return false; + } +} + +void command_line_helper::print_help() { + LOG_ERROR ("Usage: solvitaire [options] input-file1 input-file2 ...\n" + << main_options); +} + +void command_line_helper::print_no_input_error() { + LOG_ERROR ("Error: User must supply input file(s) or the '--random' " + "option"); + print_help(); +} + +void command_line_helper::print_sol_type_rules_error() { + LOG_ERROR ("Error: User must supply either a solitaire type, or a 'rules' " + "file"); + print_help(); +} + +void command_line_helper::print_rand_plus_input_err() { + LOG_ERROR ("Error: User must supply input file(s) or the '--random' option, " + "not both"); + print_help(); +} + +const vector command_line_helper::get_input_files() { + return input_files; +} + +const string command_line_helper::get_solitaire_type() { + return solitaire_type; +} + +const string command_line_helper::get_rules_file() { + return rules_file; +} + +int command_line_helper::get_random_deal() { + return random_deal; +} + +bool command_line_helper::get_help() { + return help; +} diff --git a/src/main/input-output/input/command_line_helper.h b/src/main/input-output/input/command_line_helper.h new file mode 100644 index 0000000..fd1468c --- /dev/null +++ b/src/main/input-output/input/command_line_helper.h @@ -0,0 +1,40 @@ +// +// Created by thecharlesblake on 10/16/17. +// + +#ifndef SOLVITAIRE_COMMAND_LINE_HELPER_H +#define SOLVITAIRE_COMMAND_LINE_HELPER_H + +#include + +class command_line_helper { +public: + command_line_helper(); + + bool parse(int argc, const char* argv[]); + const std::vector get_input_files(); + const std::string get_solitaire_type(); + const std::string get_rules_file(); + int get_random_deal(); + bool get_help(); + +private: + bool assess_errors(); + bool assess_sol_type(); + void print_help(); + void print_no_input_error(); + void print_sol_type_rules_error(); + void print_rand_plus_input_err(); + + boost::program_options::options_description cmdline_options; + boost::program_options::options_description main_options; + boost::program_options::positional_options_description p; + + std::vector input_files; + std::string solitaire_type; + std::string rules_file; + int random_deal; + bool help; +}; + +#endif //SOLVITAIRE_COMMAND_LINE_HELPER_H diff --git a/src/main/input-output/input/json-parsing/deal_parser.cpp b/src/main/input-output/input/json-parsing/deal_parser.cpp new file mode 100644 index 0000000..0e89f43 --- /dev/null +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -0,0 +1,82 @@ +// +// Created by thecharlesblake on 10/21/17. +// + +#include "deal_parser.h" +#include "../../../game/game_state.h" +#include "json_helper.h" + +using namespace std; +using namespace rapidjson; + +typedef sol_rules::build_order ord; +typedef sol_rules::build_policy pol; + +void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { + // Construct tableau piles + if (gs.rules.tableau_pile_count > 0) { + parse_tableau_piles(gs, doc); + } + + // Construct hole card + if (gs.rules.hole) { + parse_hole(gs, doc); + } + + // Construct filled cells + if (gs.rules.cells) { + parse_cells(gs, doc); + } +} + +void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& doc) { + assert(doc.HasMember("tableau piles")); + const Value& json_tab_piles = doc["tableau piles"]; + assert(json_tab_piles.IsArray()); + + if (gs.tableau_piles.size() != gs.rules.tableau_pile_count) { + json_helper::json_parse_err("Incorrect number of tableau piles"); + } + + for (auto p = std::make_pair(begin(json_tab_piles.GetArray()), begin(gs.tableau_piles)); + p.second != end(gs.tableau_piles); + ++p.first, ++p.second) { + + for (auto& json_card : p.first->GetArray()) { + assert(json_card.IsString()); + gs.piles[*p.second].place(card(json_card.GetString())); + } + } +} + +void deal_parser::parse_hole(game_state &gs, const Document& doc) { + if (doc.HasMember("hole")) { + const Value &json_hole = doc["hole"]; + assert(json_hole.IsString()); + + gs.piles[gs.hole].place(card(json_hole.GetString())); + } +} + +void deal_parser::parse_cells(game_state &gs, const Document& doc) { + if (doc.HasMember("cells")) { + const Value &json_cells = doc["cells"]; + assert(json_cells.IsArray()); + + const auto json_cell_arr = json_cells.GetArray(); + + if (json_cell_arr.Empty()) return; + else if (gs.rules.cells != json_cell_arr.Size()) { + json_helper::json_parse_err("Incorrect number of cells"); + } + + for (auto p = std::make_pair(begin(json_cell_arr), begin(gs.cells)); + p.second != end(gs.cells); + ++p.first, ++p.second) { + + auto json_card = p.first; + assert(json_card->IsString()); + gs.piles[*p.second].place(card(json_card->GetString())); + } + } +} diff --git a/src/main/input-output/input/json-parsing/deal_parser.h b/src/main/input-output/input/json-parsing/deal_parser.h new file mode 100644 index 0000000..9db9e2b --- /dev/null +++ b/src/main/input-output/input/json-parsing/deal_parser.h @@ -0,0 +1,27 @@ +// +// Created by thecharlesblake on 10/21/17. +// + +#ifndef SOLVITAIRE_DEAL_PARSER_H +#define SOLVITAIRE_DEAL_PARSER_H + +#include + +#include +#include +#include + +#include "../../../game/sol_rules.h" + +class game_state; + +class deal_parser { +public: + static void parse(game_state&, const rapidjson::Document&); +private: + static void parse_tableau_piles(game_state&, const rapidjson::Document&); + static void parse_hole(game_state&, const rapidjson::Document&); + static void parse_cells(game_state&, const rapidjson::Document&); +}; + +#endif //SOLVITAIRE_DEAL_PARSER_H diff --git a/src/main/input-output/input/json-parsing/json_helper.cpp b/src/main/input-output/input/json-parsing/json_helper.cpp new file mode 100644 index 0000000..89f4564 --- /dev/null +++ b/src/main/input-output/input/json-parsing/json_helper.cpp @@ -0,0 +1,35 @@ +// +// Created by thecharlesblake on 11/13/17. +// + +#include +#include + +#include "json_helper.h" + +using namespace std; +using namespace rapidjson; + +Document json_helper::get_file_json(const string& filename) { + // Reads the file into a string + std::ifstream ifstr(filename); + std::stringstream buf; + buf << ifstr.rdbuf(); + + if (ifstr.fail()) { + throw runtime_error("could not read file " + filename); + } else { + Document d; + + d.Parse(buf.str().c_str()); + if (d.HasParseError()) { + throw runtime_error(filename + " not valid json"); + } else { + return d; + } + } +} + +void json_helper::json_parse_err(const string& msg) { + throw runtime_error("Error in JSON doc: " + msg); +} diff --git a/src/main/input-output/input/json-parsing/json_helper.h b/src/main/input-output/input/json-parsing/json_helper.h new file mode 100644 index 0000000..5ab24bc --- /dev/null +++ b/src/main/input-output/input/json-parsing/json_helper.h @@ -0,0 +1,19 @@ +// +// Created by thecharlesblake on 11/13/17. +// + +#ifndef SOLVITAIRE_UTIL_H +#define SOLVITAIRE_UTIL_H + +#include + +#include + +class json_helper { +public: + static rapidjson::Document get_file_json(const std::string&); + static void json_parse_err(const std::string& msg); +}; + + +#endif //SOLVITAIRE_UTIL_H diff --git a/src/main/input-output/input/json-parsing/rules_parser.cpp b/src/main/input-output/input/json-parsing/rules_parser.cpp new file mode 100644 index 0000000..200dbab --- /dev/null +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -0,0 +1,173 @@ +// +// Created by thecharlesblake on 1/3/18. +// + +#include +#include + +#include "rules_parser.h" +#include "json_helper.h" +#include "../sol_preset_types.h" + +using namespace std; +using namespace rapidjson; + +//////////// +// Static // +//////////// + +typedef sol_rules::build_order ord; +typedef sol_rules::build_policy pol; +typedef sol_rules::spaces_policy s_pol; + +const sol_rules rules_parser::from_file(const string rules_file) { + sol_rules sr = get_default(); + Document d = json_helper::get_file_json(rules_file); + + modify_sol_rules(sr, d); + + return sr; +} + +const sol_rules rules_parser::from_preset(const string sol_type) { + sol_rules sr = get_default(); + + const string preset_json = sol_preset_types::get(sol_type); + + Document d; + d.Parse(preset_json.c_str()); + assert(!d.HasParseError()); + + modify_sol_rules(sr, d); + return sr; +} + +sol_rules rules_parser::get_default() { + sol_rules sr; + const string default_json = sol_preset_types::get("default"); + + Document d; + d.Parse(default_json.c_str()); + assert(!d.HasParseError()); + + modify_sol_rules(sr, d); + return sr; +} + +void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { + + if (!d.IsObject()) { + json_helper::json_parse_err("JSON doc must be object"); + } + + if (d.HasMember("tableau piles")) { + if (d["tableau piles"].IsObject()) { + + if (d["tableau piles"].HasMember("count")) { + if (d["tableau piles"]["count"].IsInt()) { + sr.tableau_pile_count = static_cast(d["tableau piles"]["count"].GetInt()); + } else { + json_helper::json_parse_err("[tableau piles][count] must be an integer"); + } + } + + if (d["tableau piles"].HasMember("build order")) { + if (d["tableau piles"]["build order"].IsString()) { + string bo_str = d["tableau piles"]["build order"].GetString(); + + if (bo_str == "ascending") { + sr.build_ord = ord::ASCENDING; + } else if (bo_str == "descending") { + sr.build_ord = ord::DESCENDING; + } else { + json_helper::json_parse_err("[tableau piles][build order] is invalid"); + } + + } else { + json_helper::json_parse_err("[tableau piles][build order] must be an string"); + } + } + + if (d["tableau piles"].HasMember("build policy")) { + if (d["tableau piles"]["build policy"].IsString()) { + string bp_str = d["tableau piles"]["build policy"].GetString(); + + if (bp_str == "any-suit") { + sr.build_pol = pol::ANY_SUIT; + } else if (bp_str == "red-black") { + sr.build_pol = pol::RED_BLACK; + } else if (bp_str == "same-suit") { + sr.build_pol = pol::SAME_SUIT; + } else if (bp_str == "no-build") { + sr.build_pol = pol::NO_BUILD; + } else { + json_helper::json_parse_err("[tableau piles][build policy] is invalid"); + } + + } else { + json_helper::json_parse_err("[tableau piles][build policy] must be an string"); + } + } + + if (d["tableau piles"].HasMember("spaces policy")) { + if (d["tableau piles"]["spaces policy"].IsString()) { + string sp_str = d["tableau piles"]["spaces policy"].GetString(); + + if (sp_str == "any") { + sr.spaces_pol = s_pol::ANY; + } else if (sp_str == "no-build") { + sr.spaces_pol = s_pol::NO_BUILD; + } else { + json_helper::json_parse_err("[tableau piles][spaces policy] is invalid"); + } + + } else { + json_helper::json_parse_err("[tableau piles][spaces policy] must be an string"); + } + } + + } else { + json_helper::json_parse_err("[tableau piles] must be an object"); + } + } + + if (d.HasMember("max rank")) { + if (d["max rank"].IsInt()) { + sr.max_rank = static_cast(d["max rank"].GetInt()); + } else { + json_helper::json_parse_err("[max rank] must be an integer"); + } + } + + if (d.HasMember("hole")) { + if (d["hole"].IsBool()) { + sr.hole = d["hole"].GetBool(); + } else { + json_helper::json_parse_err("[hole] must be a boolean"); + } + } + + if (d.HasMember("foundations")) { + if (d["foundations"].IsBool()) { + sr.foundations = d["foundations"].GetBool(); + } else { + json_helper::json_parse_err("[foundations] must be a boolean"); + } + } + + if (d.HasMember("cells")) { + if (d["cells"].IsInt()) { + sr.cells = static_cast(d["cells"].GetInt()); + } else { + json_helper::json_parse_err("[cells] must be an integer"); + } + } + + if (d.HasMember("stock size")) { + if (d["stock size"].IsInt()) { + sr.stock_size = static_cast(d["stock size"].GetInt()); + } else { + json_helper::json_parse_err("[stock size] must be an integer"); + } + } +} diff --git a/src/main/input-output/input/json-parsing/rules_parser.h b/src/main/input-output/input/json-parsing/rules_parser.h new file mode 100644 index 0000000..1e28867 --- /dev/null +++ b/src/main/input-output/input/json-parsing/rules_parser.h @@ -0,0 +1,21 @@ +// +// Created by thecharlesblake on 1/3/18. +// + +#ifndef SOLVITAIRE_RULES_PARSER_H +#define SOLVITAIRE_RULES_PARSER_H + +#include "../../game/sol_rules.h" + +class rules_parser { +public: + static const sol_rules from_file(std::string); + static const sol_rules from_preset(std::string); + +private: + static sol_rules get_default(); + static void modify_sol_rules(sol_rules&, rapidjson::Document&); +}; + + +#endif //SOLVITAIRE_RULES_PARSER_H diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp new file mode 100644 index 0000000..3c9d295 --- /dev/null +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -0,0 +1,180 @@ +// +// Created by thecharlesblake on 11/14/17. +// + +#include "sol_preset_types.h" +using namespace std; + +string sol_preset_types::get(const string& s) { + return mp[s]; +} + +bool sol_preset_types::is_valid_preset(const string& s) { + return mp.count(s) > 0; +} + +std::map sol_preset_types::mp = { + { + "default", + + R"( +{ + "tableau piles": { + "count": 8, + "build order": "descending", + "build policy": "any-suit", + "spaces policy": "any" + }, + "max rank": 13, + "hole": false, + "foundations": true, + "cells": 0, + "reserve size": 0, + "stock size": 0 +} +)" + + }, + { + "spanish-patience", + + R"( +{ + "tableau piles": { + "count": 13 + } +} +)" + + }, + { + "simple-spanish-patience", + + R"( +{ + "tableau piles": { + "count": 3 + }, + "max rank": 2 +} +)" + + }, + { + "free-cell", + + R"( +{ + "tableau piles": { + "build policy": "red-black" + }, + "cells": 4 +} +)" + + }, + { + "simple-free-cell", + + R"( +{ + "tableau piles": { + "count": 4, + "build policy": "red-black" + }, + "max rank": 4, + "cells": 1 +} +)" + + }, + { + "bakers-dozen", + + R"( +{ + "tableau piles": { + "count": 13, + "spaces policy": "no-build" + } +} +)" + + }, + { + "simple-bakers-dozen", + + R"( +{ + "tableau piles": { + "count": 3, + "spaces policy": "no-build" + }, + "max rank": 3 +} +)" + + }, + { + "black-hole", + + R"( +{ + "tableau piles": { + "count": 17, + "build policy": "no-build" + }, + "hole": true, + "foundations": false +} +)" + + }, + { + "simple-black-hole", + + R"( +{ + "tableau piles": { + "count": 4, + "build policy": "no-build" + }, + "max rank": 5, + "hole": true, + "foundations": false +} +)" + + }, + { + "canfield", + + R"( +{ + "tableau piles": { + "count": 4, + "build policy": "red-black" + }, + "reserve size": 14, + "stock size": 34 +} +)" + + }, + { + "simple-canfield", + + R"( +{ + "tableau piles": { + "count": 2, + "build policy": "red-black" + }, + "max rank": 2, + "reserve size": 2, + "stock size": 2 +} +)" + + } +}; \ No newline at end of file diff --git a/src/main/input-output/input/sol_preset_types.h b/src/main/input-output/input/sol_preset_types.h new file mode 100644 index 0000000..02f602d --- /dev/null +++ b/src/main/input-output/input/sol_preset_types.h @@ -0,0 +1,19 @@ +// +// Created by thecharlesblake on 11/14/17. +// + +#ifndef SOLVITAIRE_SOL_PRESET_TYPES_H +#define SOLVITAIRE_SOL_PRESET_TYPES_H + +#include +#include + +class sol_preset_types { +public: + static std::string get(const std::string& s); + static bool is_valid_preset(const std::string& s); +private: + static std::map mp; +}; + +#endif //SOLVITAIRE_SOL_PRESET_TYPES_H diff --git a/src/main/input-output/json_helper.cpp b/src/main/input-output/json_helper.cpp deleted file mode 100644 index 89f4564..0000000 --- a/src/main/input-output/json_helper.cpp +++ /dev/null @@ -1,35 +0,0 @@ -// -// Created by thecharlesblake on 11/13/17. -// - -#include -#include - -#include "json_helper.h" - -using namespace std; -using namespace rapidjson; - -Document json_helper::get_file_json(const string& filename) { - // Reads the file into a string - std::ifstream ifstr(filename); - std::stringstream buf; - buf << ifstr.rdbuf(); - - if (ifstr.fail()) { - throw runtime_error("could not read file " + filename); - } else { - Document d; - - d.Parse(buf.str().c_str()); - if (d.HasParseError()) { - throw runtime_error(filename + " not valid json"); - } else { - return d; - } - } -} - -void json_helper::json_parse_err(const string& msg) { - throw runtime_error("Error in JSON doc: " + msg); -} diff --git a/src/main/input-output/json_helper.h b/src/main/input-output/json_helper.h deleted file mode 100644 index 5ab24bc..0000000 --- a/src/main/input-output/json_helper.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by thecharlesblake on 11/13/17. -// - -#ifndef SOLVITAIRE_UTIL_H -#define SOLVITAIRE_UTIL_H - -#include - -#include - -class json_helper { -public: - static rapidjson::Document get_file_json(const std::string&); - static void json_parse_err(const std::string& msg); -}; - - -#endif //SOLVITAIRE_UTIL_H diff --git a/src/main/input-output/log_helper.h b/src/main/input-output/log_helper.h deleted file mode 100644 index 71cc5f4..0000000 --- a/src/main/input-output/log_helper.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by thecharlesblake on 10/16/17. -// - -#ifndef SOLVITAIRE_LOG_HELPER_H -#define SOLVITAIRE_LOG_HELPER_H - -#include - -#if defined NDEBUG - #define LOG_DEBUG(msg) -#else - #define LOG_DEBUG(msg) clog << "[debug]\n" << msg << "\n" -#endif -#define LOG_INFO(msg) clog << "[info] " << msg << "\n" -#define LOG_WARNING clog(msg) << "[warning] " << msg << "\n" -#define LOG_ERROR(msg) cerr << "[ERROR] " << msg << "\n" - -#endif //SOLVITAIRE_LOG_HELPER_H diff --git a/src/main/input-output/output/log_helper.h b/src/main/input-output/output/log_helper.h new file mode 100644 index 0000000..71cc5f4 --- /dev/null +++ b/src/main/input-output/output/log_helper.h @@ -0,0 +1,19 @@ +// +// Created by thecharlesblake on 10/16/17. +// + +#ifndef SOLVITAIRE_LOG_HELPER_H +#define SOLVITAIRE_LOG_HELPER_H + +#include + +#if defined NDEBUG + #define LOG_DEBUG(msg) +#else + #define LOG_DEBUG(msg) clog << "[debug]\n" << msg << "\n" +#endif +#define LOG_INFO(msg) clog << "[info] " << msg << "\n" +#define LOG_WARNING clog(msg) << "[warning] " << msg << "\n" +#define LOG_ERROR(msg) cerr << "[ERROR] " << msg << "\n" + +#endif //SOLVITAIRE_LOG_HELPER_H diff --git a/src/main/input-output/rules_parser.cpp b/src/main/input-output/rules_parser.cpp deleted file mode 100644 index 81cdd20..0000000 --- a/src/main/input-output/rules_parser.cpp +++ /dev/null @@ -1,173 +0,0 @@ -// -// Created by thecharlesblake on 1/3/18. -// - -#include -#include - -#include "rules_parser.h" -#include "../input-output/json_helper.h" -#include "../input-output/sol_preset_types.h" - -using namespace std; -using namespace rapidjson; - -//////////// -// Static // -//////////// - -typedef sol_rules::build_order ord; -typedef sol_rules::build_policy pol; -typedef sol_rules::spaces_policy s_pol; - -const sol_rules rules_parser::from_file(const string rules_file) { - sol_rules sr = get_default(); - Document d = json_helper::get_file_json(rules_file); - - modify_sol_rules(sr, d); - - return sr; -} - -const sol_rules rules_parser::from_preset(const string sol_type) { - sol_rules sr = get_default(); - - const string preset_json = sol_preset_types::get(sol_type); - - Document d; - d.Parse(preset_json.c_str()); - assert(!d.HasParseError()); - - modify_sol_rules(sr, d); - return sr; -} - -sol_rules rules_parser::get_default() { - sol_rules sr; - const string default_json = sol_preset_types::get("default"); - - Document d; - d.Parse(default_json.c_str()); - assert(!d.HasParseError()); - - modify_sol_rules(sr, d); - return sr; -} - -void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { - - if (!d.IsObject()) { - json_helper::json_parse_err("JSON doc must be object"); - } - - if (d.HasMember("tableau piles")) { - if (d["tableau piles"].IsObject()) { - - if (d["tableau piles"].HasMember("count")) { - if (d["tableau piles"]["count"].IsInt()) { - sr.tableau_pile_count = static_cast(d["tableau piles"]["count"].GetInt()); - } else { - json_helper::json_parse_err("[tableau piles][count] must be an integer"); - } - } - - if (d["tableau piles"].HasMember("build order")) { - if (d["tableau piles"]["build order"].IsString()) { - string bo_str = d["tableau piles"]["build order"].GetString(); - - if (bo_str == "ascending") { - sr.build_ord = ord::ASCENDING; - } else if (bo_str == "descending") { - sr.build_ord = ord::DESCENDING; - } else { - json_helper::json_parse_err("[tableau piles][build order] is invalid"); - } - - } else { - json_helper::json_parse_err("[tableau piles][build order] must be an string"); - } - } - - if (d["tableau piles"].HasMember("build policy")) { - if (d["tableau piles"]["build policy"].IsString()) { - string bp_str = d["tableau piles"]["build policy"].GetString(); - - if (bp_str == "any-suit") { - sr.build_pol = pol::ANY_SUIT; - } else if (bp_str == "red-black") { - sr.build_pol = pol::RED_BLACK; - } else if (bp_str == "same-suit") { - sr.build_pol = pol::SAME_SUIT; - } else if (bp_str == "no-build") { - sr.build_pol = pol::NO_BUILD; - } else { - json_helper::json_parse_err("[tableau piles][build policy] is invalid"); - } - - } else { - json_helper::json_parse_err("[tableau piles][build policy] must be an string"); - } - } - - if (d["tableau piles"].HasMember("spaces policy")) { - if (d["tableau piles"]["spaces policy"].IsString()) { - string sp_str = d["tableau piles"]["spaces policy"].GetString(); - - if (sp_str == "any") { - sr.spaces_pol = s_pol::ANY; - } else if (sp_str == "no-build") { - sr.spaces_pol = s_pol::NO_BUILD; - } else { - json_helper::json_parse_err("[tableau piles][spaces policy] is invalid"); - } - - } else { - json_helper::json_parse_err("[tableau piles][spaces policy] must be an string"); - } - } - - } else { - json_helper::json_parse_err("[tableau piles] must be an object"); - } - } - - if (d.HasMember("max rank")) { - if (d["max rank"].IsInt()) { - sr.max_rank = static_cast(d["max rank"].GetInt()); - } else { - json_helper::json_parse_err("[max rank] must be an integer"); - } - } - - if (d.HasMember("hole")) { - if (d["hole"].IsBool()) { - sr.hole = d["hole"].GetBool(); - } else { - json_helper::json_parse_err("[hole] must be a boolean"); - } - } - - if (d.HasMember("foundations")) { - if (d["foundations"].IsBool()) { - sr.foundations = d["foundations"].GetBool(); - } else { - json_helper::json_parse_err("[foundations] must be a boolean"); - } - } - - if (d.HasMember("cells")) { - if (d["cells"].IsInt()) { - sr.cells = static_cast(d["cells"].GetInt()); - } else { - json_helper::json_parse_err("[cells] must be an integer"); - } - } - - if (d.HasMember("stock size")) { - if (d["stock size"].IsInt()) { - sr.stock_size = static_cast(d["stock size"].GetInt()); - } else { - json_helper::json_parse_err("[stock size] must be an integer"); - } - } -} diff --git a/src/main/input-output/rules_parser.h b/src/main/input-output/rules_parser.h deleted file mode 100644 index a478a01..0000000 --- a/src/main/input-output/rules_parser.h +++ /dev/null @@ -1,21 +0,0 @@ -// -// Created by thecharlesblake on 1/3/18. -// - -#ifndef SOLVITAIRE_RULES_PARSER_H -#define SOLVITAIRE_RULES_PARSER_H - -#include "../game/sol_rules.h" - -class rules_parser { -public: - static const sol_rules from_file(std::string); - static const sol_rules from_preset(std::string); - -private: - static sol_rules get_default(); - static void modify_sol_rules(sol_rules&, rapidjson::Document&); -}; - - -#endif //SOLVITAIRE_RULES_PARSER_H diff --git a/src/main/input-output/sol_preset_types.cpp b/src/main/input-output/sol_preset_types.cpp deleted file mode 100644 index 3c9d295..0000000 --- a/src/main/input-output/sol_preset_types.cpp +++ /dev/null @@ -1,180 +0,0 @@ -// -// Created by thecharlesblake on 11/14/17. -// - -#include "sol_preset_types.h" -using namespace std; - -string sol_preset_types::get(const string& s) { - return mp[s]; -} - -bool sol_preset_types::is_valid_preset(const string& s) { - return mp.count(s) > 0; -} - -std::map sol_preset_types::mp = { - { - "default", - - R"( -{ - "tableau piles": { - "count": 8, - "build order": "descending", - "build policy": "any-suit", - "spaces policy": "any" - }, - "max rank": 13, - "hole": false, - "foundations": true, - "cells": 0, - "reserve size": 0, - "stock size": 0 -} -)" - - }, - { - "spanish-patience", - - R"( -{ - "tableau piles": { - "count": 13 - } -} -)" - - }, - { - "simple-spanish-patience", - - R"( -{ - "tableau piles": { - "count": 3 - }, - "max rank": 2 -} -)" - - }, - { - "free-cell", - - R"( -{ - "tableau piles": { - "build policy": "red-black" - }, - "cells": 4 -} -)" - - }, - { - "simple-free-cell", - - R"( -{ - "tableau piles": { - "count": 4, - "build policy": "red-black" - }, - "max rank": 4, - "cells": 1 -} -)" - - }, - { - "bakers-dozen", - - R"( -{ - "tableau piles": { - "count": 13, - "spaces policy": "no-build" - } -} -)" - - }, - { - "simple-bakers-dozen", - - R"( -{ - "tableau piles": { - "count": 3, - "spaces policy": "no-build" - }, - "max rank": 3 -} -)" - - }, - { - "black-hole", - - R"( -{ - "tableau piles": { - "count": 17, - "build policy": "no-build" - }, - "hole": true, - "foundations": false -} -)" - - }, - { - "simple-black-hole", - - R"( -{ - "tableau piles": { - "count": 4, - "build policy": "no-build" - }, - "max rank": 5, - "hole": true, - "foundations": false -} -)" - - }, - { - "canfield", - - R"( -{ - "tableau piles": { - "count": 4, - "build policy": "red-black" - }, - "reserve size": 14, - "stock size": 34 -} -)" - - }, - { - "simple-canfield", - - R"( -{ - "tableau piles": { - "count": 2, - "build policy": "red-black" - }, - "max rank": 2, - "reserve size": 2, - "stock size": 2 -} -)" - - } -}; \ No newline at end of file diff --git a/src/main/input-output/sol_preset_types.h b/src/main/input-output/sol_preset_types.h deleted file mode 100644 index 02f602d..0000000 --- a/src/main/input-output/sol_preset_types.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by thecharlesblake on 11/14/17. -// - -#ifndef SOLVITAIRE_SOL_PRESET_TYPES_H -#define SOLVITAIRE_SOL_PRESET_TYPES_H - -#include -#include - -class sol_preset_types { -public: - static std::string get(const std::string& s); - static bool is_valid_preset(const std::string& s); -private: - static std::map mp; -}; - -#endif //SOLVITAIRE_SOL_PRESET_TYPES_H diff --git a/src/main/main.cpp b/src/main/main.cpp index 5386da6..9ed7321 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -2,10 +2,10 @@ #include #include -#include "input-output/command_line_helper.h" -#include "input-output/log_helper.h" -#include "input-output/json_helper.h" -#include "input-output/rules_parser.h" +#include "input-output/input/command_line_helper.h" +#include "input-output/output/log_helper.h" +#include "input-output/input/json-parsing/json_helper.h" +#include "input-output/input/json-parsing/rules_parser.h" #include "solver/solver.h" using namespace rapidjson; diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 66dcc07..b7d6de1 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -9,7 +9,7 @@ #include #include "solver.h" -#include "../input-output/log_helper.h" +#include "../input-output/output/log_helper.h" using namespace std; using namespace boost; diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 117273c..14218f7 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -7,8 +7,8 @@ #include "test_helper.h" #include "../main/game/game_state.h" #include "../main/solver/solver.h" -#include "../main/input-output/json_helper.h" -#include "../main/input-output/rules_parser.h" +#include "../main/input-output/input/json-parsing/json_helper.h" +#include "../main/input-output/input/json-parsing/rules_parser.h" using namespace rapidjson; using namespace std; -- cgit v1.2.1 From 4208d2570e74ab54d1878c72110bd380e7b197f2 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 15:21:47 +0000 Subject: Move game state printing into dedicated class --- src/main/game/game_state.cpp | 106 ++------------------- src/main/game/game_state.h | 13 +-- .../input-output/input/json-parsing/rules_parser.h | 2 +- src/main/input-output/output/state_printer.cpp | 96 +++++++++++++++++++ src/main/input-output/output/state_printer.h | 28 ++++++ src/main/main.cpp | 2 +- src/main/solver/solver.cpp | 1 - 7 files changed, 138 insertions(+), 110 deletions(-) create mode 100644 src/main/input-output/output/state_printer.cpp create mode 100644 src/main/input-output/output/state_printer.h (limited to 'src') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 78a5253..1a7310d 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -13,6 +13,7 @@ #include "game_state.h" #include "../input-output/input/json-parsing/deal_parser.h" +#include "../input-output/output/state_printer.h" using namespace std; using namespace rapidjson; @@ -309,108 +310,12 @@ const vector& game_state::get_data() const { return piles; } - -////////////// -// PRINTING // -////////////// - -ostream& game_state::print(ostream& stream) const { - if (rules.foundations) { - print_header(stream, "Foundations"); - print_top_of_piles(stream, foundations); - } - if (rules.cells) { - print_header(stream, "Cells"); - print_piles(stream, cells); - } - if (rules.tableau_pile_count > 0) { - print_header(stream, "Tableau Piles"); - print_piles(stream, tableau_piles); - } - if (rules.stock_size > 0) { - print_header(stream, "Stock | Waste"); - print_piles(stream, {stock, waste}); - } - if (rules.hole) { - print_header(stream, "Hole Card"); - print_top_of_pile(stream, hole); - } - return stream << "==================================="; -} - -void game_state::print_header(ostream& stream, const char* header) const { - stream << "--- " << header << " "; - size_t pad = 20 - strlen(header); - for (size_t i = 0; i < pad; i++) { - stream << '-'; - } - stream << "\n"; -} - -void game_state::print_piles(ostream& stream, const vector& pile_rs) const { - bool empty_row = false; - vector::size_type row_idx = 0; - - // Loops through the rows in each pile, starting from the bottom, until - // one is empty - while (!empty_row) { - // Loops through the current row to determine if it is empty - empty_row = true; - for (const pile_ref pile_r : pile_rs) { - if (piles[pile_r].size() > row_idx) { - empty_row = false; - break; - } - } - if (!empty_row || row_idx == 0) { - // Loops through the current (non-empty) row, and outputs the values - for (const pile_ref pile_r : pile_rs) { - if (piles[pile_r].size() > row_idx) { - stream << piles[pile_r][row_idx]; - } else if (row_idx == 0) { - stream << "[]"; - } - stream << "\t"; - } - - stream << "\n"; - row_idx++; - } - } -} - -void game_state::print_pile(ostream& stream, const pile_ref pile_r) const { - print_piles(stream, {pile_r}); -} - -void game_state::print_top_of_piles(ostream& stream, const vector& vp) const { - vector top(vp); - - for (pile_ref p : top) { - if (piles[p].empty()) { - stream << "[]"; - } else { - stream << piles[p].top_card(); - } - stream << "\t"; - } - stream << "\n"; -} - -void game_state::print_top_of_pile(ostream& stream, const pile_ref pile_r) const { - print_top_of_piles(stream, {pile_r}); -} - bool operator==(const game_state& a, const game_state& b) { return a.tableau_piles == b.tableau_piles && a.foundations == b.foundations && a.hole == b.hole; } -ostream& operator <<(ostream& stream, const game_state& gs) { - return gs.print(stream); -} - //////////////////// // HASH FUNCTIONS // @@ -443,3 +348,12 @@ size_t hash_value(game_state const& gs) { size_t hash_value(vector const& vp) { return hash_range(begin(vp), end(vp)); } + + +/////////// +// PRINT // +/////////// + +ostream& operator<< (ostream& str, const game_state& gs) { + return state_printer::print(str, gs); +} diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 28d849e..30af8a6 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -9,7 +9,6 @@ #define PILE_REF_MAX (255) #include -#include #include #include @@ -23,6 +22,7 @@ class deal_parser; class game_state { friend class deal_parser; + friend class state_printer; public: // Defines types typedef uint8_t pile_ref; @@ -44,11 +44,9 @@ public: bool is_solved() const; const std::vector& get_data() const; - // Prints - std::ostream& print(std::ostream&) const; - friend bool operator==(const game_state&, const game_state&); friend std::ostream& operator<< (std::ostream&, const game_state&); + friend std::size_t hash_value(game_state const&); friend std::size_t hash_value(std::vector const&); @@ -63,13 +61,6 @@ private: bool is_valid_foundations_move(pile_ref, pile_ref) const; bool is_valid_hole_move(pile_ref) const; - // Prints - void print_header(std::ostream&, const char*) const; - void print_piles(std::ostream&, const std::vector&) const; - void print_pile(std::ostream&, pile_ref) const; - void print_top_of_piles(std::ostream&, const std::vector&) const; - void print_top_of_pile(std::ostream&, pile_ref) const; - // References to piles sol_rules rules; std::vector tableau_piles; diff --git a/src/main/input-output/input/json-parsing/rules_parser.h b/src/main/input-output/input/json-parsing/rules_parser.h index 1e28867..21dc635 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.h +++ b/src/main/input-output/input/json-parsing/rules_parser.h @@ -5,7 +5,7 @@ #ifndef SOLVITAIRE_RULES_PARSER_H #define SOLVITAIRE_RULES_PARSER_H -#include "../../game/sol_rules.h" +#include "../../../game/sol_rules.h" class rules_parser { public: diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp new file mode 100644 index 0000000..c9b8bc9 --- /dev/null +++ b/src/main/input-output/output/state_printer.cpp @@ -0,0 +1,96 @@ +// +// Created by thecharlesblake on 1/3/18. +// + +#include "state_printer.h" + +using namespace std; + +ostream& state_printer::print(ostream& stream, const game_state& gs) { + if (gs.rules.foundations) { + state_printer::print_header(stream, "Foundations"); + state_printer::print_top_of_piles(stream, gs.foundations, gs); + } + if (gs.rules.cells) { + state_printer::print_header(stream, "Cells"); + state_printer::print_piles(stream, gs.cells, gs); + } + if (gs.rules.tableau_pile_count > 0) { + state_printer::print_header(stream, "Tableau Piles"); + state_printer::print_piles(stream, gs.tableau_piles, gs); + } + if (gs.rules.stock_size > 0) { + state_printer::print_header(stream, "Stock | Waste"); + state_printer::print_piles(stream, {gs.stock, gs.waste}, gs); + } + if (gs.rules.hole) { + state_printer::print_header(stream, "Hole Card"); + state_printer::print_top_of_pile(stream, gs.hole, gs); + } + return stream << "==================================="; +} + +void state_printer::print_header(ostream& stream, const char* header) { + stream << "--- " << header << " "; + size_t pad = 20 - strlen(header); + for (size_t i = 0; i < pad; i++) { + stream << '-'; + } + stream << "\n"; +} + +void state_printer::print_piles(ostream& stream, + const vector& pile_rs, + const game_state& gs) { + bool empty_row = false; + vector::size_type row_idx = 0; + + // Loops through the rows in each pile, starting from the bottom, until + // one is empty + while (!empty_row) { + // Loops through the current row to determine if it is empty + empty_row = true; + for (const game_state::pile_ref pile_r : pile_rs) { + if (gs.piles[pile_r].size() > row_idx) { + empty_row = false; + break; + } + } + if (!empty_row || row_idx == 0) { + // Loops through the current (non-empty) row, and outputs the values + for (const game_state::pile_ref pile_r : pile_rs) { + if (gs.piles[pile_r].size() > row_idx) { + stream << gs.piles[pile_r][row_idx]; + } else if (row_idx == 0) { + stream << "[]"; + } + stream << "\t"; + } + + stream << "\n"; + row_idx++; + } + } +} + +void state_printer::print_top_of_piles(ostream& stream, + const vector& vp, + const game_state& gs) { + vector top(vp); + + for (game_state::pile_ref p : top) { + if (gs.piles[p].empty()) { + stream << "[]"; + } else { + stream << gs.piles[p].top_card(); + } + stream << "\t"; + } + stream << "\n"; +} + +void state_printer::print_top_of_pile(ostream& stream, + const game_state::pile_ref pile_r, + const game_state& gs) { + print_top_of_piles(stream, {pile_r}, gs); +} \ No newline at end of file diff --git a/src/main/input-output/output/state_printer.h b/src/main/input-output/output/state_printer.h new file mode 100644 index 0000000..c918bcd --- /dev/null +++ b/src/main/input-output/output/state_printer.h @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 1/3/18. +// + +#ifndef SOLVITAIRE_STATE_PRINT_H +#define SOLVITAIRE_STATE_PRINT_H + +#include + +#include "../../game/game_state.h" + +class state_printer { +public: + static std::ostream& print(std::ostream&, const game_state&); + +private: + static void print_header(std::ostream&, const char*); + static void print_piles(std::ostream&, + const std::vector&, + const game_state&); + static void print_top_of_piles(std::ostream&, + const std::vector&, + const game_state&); + static void print_top_of_pile(std::ostream&, game_state::pile_ref, + const game_state&); +}; + +#endif //SOLVITAIRE_STATE_PRINT_H diff --git a/src/main/main.cpp b/src/main/main.cpp index 9ed7321..ac81a19 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -3,9 +3,9 @@ #include #include "input-output/input/command_line_helper.h" -#include "input-output/output/log_helper.h" #include "input-output/input/json-parsing/json_helper.h" #include "input-output/input/json-parsing/rules_parser.h" +#include "input-output/output/log_helper.h" #include "solver/solver.h" using namespace rapidjson; diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index b7d6de1..e0fbbc6 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include "solver.h" #include "../input-output/output/log_helper.h" -- cgit v1.2.1 From 66545273423eae0d8f945f7c3ac5871b6955ff94 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 15:27:47 +0000 Subject: Move card printing into dedicated class --- src/main/game/card.cpp | 17 ----------------- src/main/game/card.h | 1 - src/main/input-output/output/state_printer.cpp | 22 +++++++++++++++++++--- src/main/input-output/output/state_printer.h | 1 + 4 files changed, 20 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 0f9809d..74bdc31 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -69,23 +69,6 @@ bool operator==(const card& l, const card& r) { return l.get_rank() == r.get_rank() && l.get_suit() == r.get_suit(); } -std::ostream & operator<<(std::ostream & s, card const & c) { - switch(c.get_rank()) { - case 1 : s << "A"; break; - case 11: s << "J"; break; - case 12: s << "Q"; break; - case 13: s << "K"; break; - default: s << int(c.get_rank()); - } - switch(c.get_suit()) { - case card::suit_t::Spades : s << "S"; break; - case card::suit_t::Hearts : s << "H"; break; - case card::suit_t::Clubs : s << "C"; break; - case card::suit_t::Diamonds: s << "D"; break; - } - return s; -} - size_t hash_value(card const& c) { boost::hash hasher; auto raw_val = static_cast( diff --git a/src/main/game/card.h b/src/main/game/card.h index 3a1c6b0..8aaa50d 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -35,7 +35,6 @@ private: rank_t rank : 4; }; -std::ostream & operator<<(std::ostream &, card const &); bool operator==(const card&, const card&); std::size_t hash_value(card const&); diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index c9b8bc9..88181de 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -60,7 +60,7 @@ void state_printer::print_piles(ostream& stream, // Loops through the current (non-empty) row, and outputs the values for (const game_state::pile_ref pile_r : pile_rs) { if (gs.piles[pile_r].size() > row_idx) { - stream << gs.piles[pile_r][row_idx]; + print_card(stream, gs.piles[pile_r][row_idx]); } else if (row_idx == 0) { stream << "[]"; } @@ -82,7 +82,7 @@ void state_printer::print_top_of_piles(ostream& stream, if (gs.piles[p].empty()) { stream << "[]"; } else { - stream << gs.piles[p].top_card(); + print_card(stream, gs.piles[p].top_card()); } stream << "\t"; } @@ -93,4 +93,20 @@ void state_printer::print_top_of_pile(ostream& stream, const game_state::pile_ref pile_r, const game_state& gs) { print_top_of_piles(stream, {pile_r}, gs); -} \ No newline at end of file +} + +void state_printer::print_card(std::ostream& s, const card c) { + switch(c.get_rank()) { + case 1 : s << "A"; break; + case 11: s << "J"; break; + case 12: s << "Q"; break; + case 13: s << "K"; break; + default: s << int(c.get_rank()); + } + switch(c.get_suit()) { + case card::suit_t::Spades : s << "S"; break; + case card::suit_t::Hearts : s << "H"; break; + case card::suit_t::Clubs : s << "C"; break; + case card::suit_t::Diamonds: s << "D"; break; + } +} diff --git a/src/main/input-output/output/state_printer.h b/src/main/input-output/output/state_printer.h index c918bcd..ae63182 100644 --- a/src/main/input-output/output/state_printer.h +++ b/src/main/input-output/output/state_printer.h @@ -23,6 +23,7 @@ private: const game_state&); static void print_top_of_pile(std::ostream&, game_state::pile_ref, const game_state&); + static void print_card(std::ostream&, card); }; #endif //SOLVITAIRE_STATE_PRINT_H -- cgit v1.2.1 From 965de2889e866e481749f73fe638bd160a793da4 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 15:41:34 +0000 Subject: Move game_state legal move gen into dedicated file --- src/main/game/game_state.cpp | 130 ----------------------------- src/main/game/game_state.legal_moves.cpp | 135 +++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 130 deletions(-) create mode 100644 src/main/game/game_state.legal_moves.cpp (limited to 'src') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 1a7310d..11e74ff 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -82,7 +82,6 @@ game_state::game_state(const sol_rules& s_rules, const Document& doc) : // Constructs an initial game state from a seed game_state::game_state(const sol_rules& s_rules, int seed) : game_state(s_rules) { - vector deck = gen_shuffled_deck(seed, rules.max_rank); // If there is a hole, moves the ace of spades to it @@ -163,135 +162,6 @@ void game_state::undo_move(const move m) { } -/////////////////////////// -// LEGAL MOVE GENERATION // -/////////////////////////// - -vector game_state::get_legal_moves() const { - // The next legal moves - vector moves; - - // 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 - || rem_ref == waste - || piles[rem_ref].empty()) continue; - - // Stock cards can only be moved to the waste - if (stock != PILE_REF_MAX && rem_ref == stock) { - moves.emplace_back(stock, waste); - continue; - } - - // For each tableau pile, check if moving from the rem pile is a valid - // tableau move according to the rules - for (auto add_ref : tableau_piles) { - if (is_valid_tableau_move(rem_ref, add_ref)) { - moves.emplace_back(rem_ref, add_ref); - } - } - - // Any card can be moved to a cell, so long as the cell isn't full - for (auto add_ref : cells) { - if (add_ref != rem_ref && piles[add_ref].empty()) { - moves.emplace_back(rem_ref, add_ref); - } - } - - // Any card can be added to the waste - if (waste != PILE_REF_MAX && waste != rem_ref) { - moves.emplace_back(rem_ref, waste); - } - - // For each foundation pile, check if moving from the rem pile is a valid - // move according to the rules - for (auto add_ref : foundations) { - if (is_valid_foundations_move(rem_ref, add_ref)) { - moves.emplace_back(rem_ref, add_ref); - } - } - - // Does the same for the hole - if (hole != PILE_REF_MAX && is_valid_hole_move(rem_ref)) { - moves.emplace_back(rem_ref, hole); - } - } - - return moves; -} - -// If a tableau pile is empty and the space policy is -// no-build, do not add. Otherwise any card with the right -// suit/rank can be moved to a tableau -bool game_state::is_valid_tableau_move(const pile_ref rem_ref, - const pile_ref add_ref) const { - if (rem_ref == add_ref) return false; - - else if (rules.build_pol == pol::NO_BUILD) return false; - - else if (piles[add_ref].empty()) { - return rules.spaces_pol == s_pol::ANY; - } - - card rem_c = piles[rem_ref].top_card(); - card add_c = piles[add_ref].top_card(); - - // Checks violation of same suit policy - if (rules.build_pol == pol::SAME_SUIT - && rem_c.get_suit() != add_c.get_suit()) { - return false; - } - - // Checks violation of red-black suit policy - else if (rules.build_pol == pol::RED_BLACK - && rem_c.get_colour() == add_c.get_colour()) { - return false; - } - - // Checks rank - else if (rules.build_ord == ord::DESCENDING - && rem_c.get_rank() + 1 != add_c.get_rank()) { - return false; - } else if (rules.build_ord == ord::ASCENDING - && rem_c.get_rank() - 1 != add_c.get_rank()) { - return false; - } else { - return true; - } -} - -bool game_state::is_valid_foundations_move(const pile_ref rem_ref, - const pile_ref add_ref) const { - if (rem_ref == add_ref) return false; - - card rem_c = piles[rem_ref].top_card(); - - // Checks violation of same suit policy - if (rem_c.get_suit() != card::to_suit(add_ref - foundations.front())) { - return false; - } - - // Checks rank - else { - card::rank_t add_rank = piles[add_ref].empty() - ? card::rank_t(0) - : piles[add_ref].top_card().get_rank(); - return rem_c.get_rank() - 1 == add_rank; - } -} - -bool game_state::is_valid_hole_move(const pile_ref rem_ref) const { - if (rem_ref == hole) return false; - card::rank_t rank = piles[rem_ref].top_card().get_rank(); - card::rank_t hole_rank = piles[hole].top_card().get_rank(); - return rank + 1 == hole_rank - || rank - 1 == hole_rank - || (rank == rules.max_rank && hole_rank == 1) - || (rank == 1 && hole_rank == rules.max_rank); -} - - //////////////////////// // INSPECT GAME STATE // //////////////////////// diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp new file mode 100644 index 0000000..27d7c8f --- /dev/null +++ b/src/main/game/game_state.legal_moves.cpp @@ -0,0 +1,135 @@ +// +// Created by thecharlesblake on 1/3/18. +// + +#include "game_state.h" + +using namespace std; + +typedef sol_rules::build_order ord; +typedef sol_rules::build_policy pol; +typedef sol_rules::spaces_policy s_pol; + +vector game_state::get_legal_moves() const { + // The next legal moves + vector moves; + + // 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 + || rem_ref == waste + || piles[rem_ref].empty()) continue; + + // Stock cards can only be moved to the waste + if (stock != PILE_REF_MAX && rem_ref == stock) { + moves.emplace_back(stock, waste); + continue; + } + + // For each tableau pile, check if moving from the rem pile is a valid + // tableau move according to the rules + for (auto add_ref : tableau_piles) { + if (is_valid_tableau_move(rem_ref, add_ref)) { + moves.emplace_back(rem_ref, add_ref); + } + } + + // Any card can be moved to a cell, so long as the cell isn't full + for (auto add_ref : cells) { + if (add_ref != rem_ref && piles[add_ref].empty()) { + moves.emplace_back(rem_ref, add_ref); + } + } + + // Any card can be added to the waste + if (waste != PILE_REF_MAX && waste != rem_ref) { + moves.emplace_back(rem_ref, waste); + } + + // For each foundation pile, check if moving from the rem pile is a valid + // move according to the rules + for (auto add_ref : foundations) { + if (is_valid_foundations_move(rem_ref, add_ref)) { + moves.emplace_back(rem_ref, add_ref); + } + } + + // Does the same for the hole + if (hole != PILE_REF_MAX && is_valid_hole_move(rem_ref)) { + moves.emplace_back(rem_ref, hole); + } + } + + return moves; +} + +// If a tableau pile is empty and the space policy is +// no-build, do not add. Otherwise any card with the right +// suit/rank can be moved to a tableau +bool game_state::is_valid_tableau_move(const pile_ref rem_ref, + const pile_ref add_ref) const { + if (rem_ref == add_ref) return false; + + else if (rules.build_pol == pol::NO_BUILD) return false; + + else if (piles[add_ref].empty()) { + return rules.spaces_pol == s_pol::ANY; + } + + card rem_c = piles[rem_ref].top_card(); + card add_c = piles[add_ref].top_card(); + + // Checks violation of same suit policy + if (rules.build_pol == pol::SAME_SUIT + && rem_c.get_suit() != add_c.get_suit()) { + return false; + } + + // Checks violation of red-black suit policy + else if (rules.build_pol == pol::RED_BLACK + && rem_c.get_colour() == add_c.get_colour()) { + return false; + } + + // Checks rank + else if (rules.build_ord == ord::DESCENDING + && rem_c.get_rank() + 1 != add_c.get_rank()) { + return false; + } else if (rules.build_ord == ord::ASCENDING + && rem_c.get_rank() - 1 != add_c.get_rank()) { + return false; + } else { + return true; + } +} + +bool game_state::is_valid_foundations_move(const pile_ref rem_ref, + const pile_ref add_ref) const { + if (rem_ref == add_ref) return false; + + card rem_c = piles[rem_ref].top_card(); + + // Checks violation of same suit policy + if (rem_c.get_suit() != card::to_suit(add_ref - foundations.front())) { + return false; + } + + // Checks rank + else { + card::rank_t add_rank = piles[add_ref].empty() + ? card::rank_t(0) + : piles[add_ref].top_card().get_rank(); + return rem_c.get_rank() - 1 == add_rank; + } +} + +bool game_state::is_valid_hole_move(const pile_ref rem_ref) const { + if (rem_ref == hole) return false; + card::rank_t rank = piles[rem_ref].top_card().get_rank(); + card::rank_t hole_rank = piles[hole].top_card().get_rank(); + return rank + 1 == hole_rank + || rank - 1 == hole_rank + || (rank == rules.max_rank && hole_rank == 1) + || (rank == 1 && hole_rank == rules.max_rank); +} -- cgit v1.2.1 From 6ce3b5a142b3e357eeabde37368915e8ef916f3d Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 11:38:36 +0000 Subject: Add json rules for fortunes favor --- src/main/input-output/input/sol_preset_types.cpp | 22 ++++++++--------- src/test/fortunes_favor_test.cpp | 28 ++++++++++++++++++++++ .../resources/fortunes_favor/ComplexSolvable.json | 7 ++++++ .../fortunes_favor/ComplexUnsolvable.json | 7 ++++++ .../resources/fortunes_favor/SimpleSolvable.json | 7 ++++++ .../resources/fortunes_favor/SimpleUnsolvable.json | 7 ++++++ 6 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 src/test/fortunes_favor_test.cpp create mode 100644 src/test/resources/fortunes_favor/ComplexSolvable.json create mode 100644 src/test/resources/fortunes_favor/ComplexUnsolvable.json create mode 100644 src/test/resources/fortunes_favor/SimpleSolvable.json create mode 100644 src/test/resources/fortunes_favor/SimpleUnsolvable.json (limited to 'src') diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 3c9d295..96cbba9 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -147,32 +147,32 @@ std::map sol_preset_types::mp = { }, { - "canfield", + "fortunes-favor", R"( { "tableau piles": { - "count": 4, - "build policy": "red-black" + "count": 12, + "build policy": "same-suit" }, - "reserve size": 14, - "stock size": 34 + "foundations initial card": true, + "stock size": 36 } )" }, { - "simple-canfield", + "simple-fortunes-favor", R"( { "tableau piles": { - "count": 2, - "build policy": "red-black" + "count": 3, + "build policy": "same-suit" }, - "max rank": 2, - "reserve size": 2, - "stock size": 2 + "max rank": 4, + "foundations initial card": true, + "stock size": 3 } )" diff --git a/src/test/fortunes_favor_test.cpp b/src/test/fortunes_favor_test.cpp new file mode 100644 index 0000000..8973a53 --- /dev/null +++ b/src/test/fortunes_favor_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#include "test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/spanish_patience/"; +const static std::string type = "simple-spanish-patience"; + +TEST(SpanishPatience, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(SpanishPatience, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(SpanishPatience, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(SpanishPatience, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/src/test/resources/fortunes_favor/ComplexSolvable.json b/src/test/resources/fortunes_favor/ComplexSolvable.json new file mode 100644 index 0000000..c48ce47 --- /dev/null +++ b/src/test/resources/fortunes_favor/ComplexSolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AS", "2S", "AH"], + ["AD", "2D", "2H"], + ["2C", "AC"] + ] +} diff --git a/src/test/resources/fortunes_favor/ComplexUnsolvable.json b/src/test/resources/fortunes_favor/ComplexUnsolvable.json new file mode 100644 index 0000000..ba9be59 --- /dev/null +++ b/src/test/resources/fortunes_favor/ComplexUnsolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AS", "2S", "AH"], + ["AD", "2D", "2H"], + ["AC", "2C"] + ] +} diff --git a/src/test/resources/fortunes_favor/SimpleSolvable.json b/src/test/resources/fortunes_favor/SimpleSolvable.json new file mode 100644 index 0000000..eacad0e --- /dev/null +++ b/src/test/resources/fortunes_favor/SimpleSolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["2S", "AS", "AD"], + ["2D", "2H", "AH"], + ["2C", "AC"] + ] +} diff --git a/src/test/resources/fortunes_favor/SimpleUnsolvable.json b/src/test/resources/fortunes_favor/SimpleUnsolvable.json new file mode 100644 index 0000000..99a3140 --- /dev/null +++ b/src/test/resources/fortunes_favor/SimpleUnsolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AS", "2S", "2D"], + ["AD", "AH", "2H"], + ["AC", "2C"] + ] +} -- cgit v1.2.1 From 21a73053795c19a78de30304115a89d7b8c20e4b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 11:39:32 +0000 Subject: Add unit tests for fortunes favor --- src/test/fortunes_favor_test.cpp | 12 ++++++------ src/test/resources/fortunes_favor/ComplexSolvable.json | 9 +++++---- src/test/resources/fortunes_favor/ComplexUnsolvable.json | 9 +++++---- src/test/resources/fortunes_favor/SimpleSolvable.json | 9 +++++---- src/test/resources/fortunes_favor/SimpleUnsolvable.json | 9 +++++---- 5 files changed, 26 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/test/fortunes_favor_test.cpp b/src/test/fortunes_favor_test.cpp index 8973a53..eab2e7e 100644 --- a/src/test/fortunes_favor_test.cpp +++ b/src/test/fortunes_favor_test.cpp @@ -8,21 +8,21 @@ typedef test_helper th; -const static std::string path = "resources/spanish_patience/"; -const static std::string type = "simple-spanish-patience"; +const static std::string path = "resources/fortunes_favor/"; +const static std::string type = "simple-fortunes-favor"; -TEST(SpanishPatience, SimpleSolvable) { +TEST(FortunesFavor, SimpleSolvable) { EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); } -TEST(SpanishPatience, ComplexSolvable) { +TEST(FortunesFavor, ComplexSolvable) { EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); } -TEST(SpanishPatience, SimpleUnsolvable) { +TEST(FortunesFavor, SimpleUnsolvable) { EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); } -TEST(SpanishPatience, ComplexUnsolvable) { +TEST(FortunesFavor, ComplexUnsolvable) { EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); } diff --git a/src/test/resources/fortunes_favor/ComplexSolvable.json b/src/test/resources/fortunes_favor/ComplexSolvable.json index c48ce47..f00d600 100644 --- a/src/test/resources/fortunes_favor/ComplexSolvable.json +++ b/src/test/resources/fortunes_favor/ComplexSolvable.json @@ -1,7 +1,8 @@ { "tableau piles": [ - ["AS", "2S", "AH"], - ["AD", "2D", "2H"], - ["2C", "AC"] - ] + ["2C","2D","4H"], + ["4C","3D","3H"], + ["2S","3C","4D"] + ], + "stock": ["2H","3S","4S"] } diff --git a/src/test/resources/fortunes_favor/ComplexUnsolvable.json b/src/test/resources/fortunes_favor/ComplexUnsolvable.json index ba9be59..4f54b94 100644 --- a/src/test/resources/fortunes_favor/ComplexUnsolvable.json +++ b/src/test/resources/fortunes_favor/ComplexUnsolvable.json @@ -1,7 +1,8 @@ { "tableau piles": [ - ["AS", "2S", "AH"], - ["AD", "2D", "2H"], - ["AC", "2C"] - ] + ["2H","2D","4H"], + ["2C","3D","3H"], + ["2S","3C","4D"] + ], + "stock": ["4C","3S","4S"] } diff --git a/src/test/resources/fortunes_favor/SimpleSolvable.json b/src/test/resources/fortunes_favor/SimpleSolvable.json index eacad0e..0891746 100644 --- a/src/test/resources/fortunes_favor/SimpleSolvable.json +++ b/src/test/resources/fortunes_favor/SimpleSolvable.json @@ -1,7 +1,8 @@ { "tableau piles": [ - ["2S", "AS", "AD"], - ["2D", "2H", "AH"], - ["2C", "AC"] - ] + ["2C","2D","2H"], + ["3C","3D","3H"], + ["4C","4D","4H"] + ], + "stock": ["4S","3S","2S"] } diff --git a/src/test/resources/fortunes_favor/SimpleUnsolvable.json b/src/test/resources/fortunes_favor/SimpleUnsolvable.json index 99a3140..44d7359 100644 --- a/src/test/resources/fortunes_favor/SimpleUnsolvable.json +++ b/src/test/resources/fortunes_favor/SimpleUnsolvable.json @@ -1,7 +1,8 @@ { "tableau piles": [ - ["AS", "2S", "2D"], - ["AD", "AH", "2H"], - ["AC", "2C"] - ] + ["2C","2D","3C"], + ["2H","3D","3H"], + ["2S","4D","4H"] + ], + "stock": ["4C","3S","4S"] } -- cgit v1.2.1 From 0bf65cecb1a8b9d3c8269ad579ddd833dd7f3163 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 11:40:01 +0000 Subject: Add rules parsing for (FF) foundations init card --- src/main/game/sol_rules.h | 1 + src/main/input-output/input/json-parsing/rules_parser.cpp | 8 ++++++++ 2 files changed, 9 insertions(+) (limited to 'src') diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index 1e6dfa4..6afb238 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -35,6 +35,7 @@ struct sol_rules { card::rank_t max_rank; bool hole; bool foundations; + bool foundations_init_card; 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 200dbab..3c372ae 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -155,6 +155,14 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } + if (d.HasMember("foundations initial card")) { + if (d["foundations initial card"].IsBool()) { + sr.foundations_init_card = d["foundations initial card"].GetBool(); + } else { + json_helper::json_parse_err("[foundations initial card] must be a boolean"); + } + } + if (d.HasMember("cells")) { if (d["cells"].IsInt()) { sr.cells = static_cast(d["cells"].GetInt()); -- cgit v1.2.1 From 3f93158b539430a7cb08534fff19903acd56ff66 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 11:41:32 +0000 Subject: Add deal parsing for (FF) foundations init card --- .../input/json-parsing/deal_parser.cpp | 33 ++++++++++++++++++++++ .../input-output/input/json-parsing/deal_parser.h | 2 ++ 2 files changed, 35 insertions(+) (limited to 'src') 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 0e89f43..e7f8209 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -27,6 +27,16 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { if (gs.rules.cells) { parse_cells(gs, doc); } + + // Construct stock + if (gs.rules.stock_size > 0) { + parse_stock(gs, doc); + } + + // If the foundations begin with cards in them, fill them + if (gs.rules.foundations_init_card) { + fill_foundations(gs); + } } void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& doc) { @@ -80,3 +90,26 @@ void deal_parser::parse_cells(game_state &gs, const Document& doc) { } } } + +void deal_parser::parse_stock(game_state &gs, const Document& doc) { + if (doc.HasMember("stock")) { + const Value &json_stock = doc["stock"]; + assert(json_stock.IsArray()); + + if (gs.rules.stock_size != json_stock.Size()) { + json_helper::json_parse_err("Incorrect stock size"); + } + + for (const Value& json_card : json_stock.GetArray()) { + gs.piles[gs.stock].place(card(json_card.GetString())); + } + } +} + +void deal_parser::fill_foundations(game_state &gs) { + for (uint8_t f_idx = 0; f_idx < 4; f_idx++) { + gs.piles[gs.foundations[f_idx]].place( + card(card::to_suit(f_idx), 1) + ); + } +} diff --git a/src/main/input-output/input/json-parsing/deal_parser.h b/src/main/input-output/input/json-parsing/deal_parser.h index 9db9e2b..5496eb2 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.h +++ b/src/main/input-output/input/json-parsing/deal_parser.h @@ -22,6 +22,8 @@ private: static void parse_tableau_piles(game_state&, const rapidjson::Document&); static void parse_hole(game_state&, const rapidjson::Document&); static void parse_cells(game_state&, const rapidjson::Document&); + static void parse_stock(game_state&, const rapidjson::Document&); + static void fill_foundations(game_state&); }; #endif //SOLVITAIRE_DEAL_PARSER_H -- cgit v1.2.1 From 03178af360824a22d9b6487705f40ee1ca32acce Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 11:47:01 +0000 Subject: Update random deal gen with foundations init cards --- src/main/game/game_state.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 11e74ff..666b970 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -90,6 +90,16 @@ game_state::game_state(const sol_rules& s_rules, int seed) : piles[hole].place(card("AS")); } + // If the foundations begin filled, then fills them + if (rules.foundations_init_card) { + for (uint8_t f_idx = 0; f_idx < 4; f_idx++) { + card c = card(card::to_suit(f_idx), 1); + + deck.erase(find(begin(deck), end(deck), c)); + piles[foundations[f_idx]].place(c); + } + } + // If there is a stock, deals to it and set up a waste pile too if (rules.stock_size > 0) { for (unsigned int i = 0; i < rules.stock_size; i++) { @@ -167,13 +177,16 @@ void game_state::undo_move(const move m) { //////////////////////// bool game_state::is_solved() const { - for (auto f : foundations) { - if (piles[f].size() != rules.max_rank) { - return false; + if (rules.hole) { + return piles[hole].size() == rules.max_rank * 4; + } else { + for (auto f : foundations) { + if (piles[f].size() != rules.max_rank) { + return false; + } } + return true; } - - return !rules.hole || piles[hole].size() == rules.max_rank * 4; } const vector& game_state::get_data() const { -- cgit v1.2.1 From 4591423f6ac5f60486664370ffcc8e0ca81eeac7 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 11:48:21 +0000 Subject: Add legal move generation for waste pile --- src/main/game/game_state.legal_moves.cpp | 6 ------ 1 file changed, 6 deletions(-) (limited to 'src') diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp index 27d7c8f..9369734 100644 --- a/src/main/game/game_state.legal_moves.cpp +++ b/src/main/game/game_state.legal_moves.cpp @@ -18,7 +18,6 @@ vector game_state::get_legal_moves() const { 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 - || rem_ref == waste || piles[rem_ref].empty()) continue; // Stock cards can only be moved to the waste @@ -42,11 +41,6 @@ vector game_state::get_legal_moves() const { } } - // Any card can be added to the waste - if (waste != PILE_REF_MAX && waste != rem_ref) { - moves.emplace_back(rem_ref, waste); - } - // For each foundation pile, check if moving from the rem pile is a valid // move according to the rules for (auto add_ref : foundations) { -- cgit v1.2.1 From e28645b3a2b610c1c95ed8f7553e336354b7d17f Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 11:48:41 +0000 Subject: Add fortunes favor to benchmark script --- src/test/flower_garden_test.cpp | 28 ++++++++++++++++++++++ .../resources/flower_garden/ComplexSolvable.json | 8 +++++++ .../resources/flower_garden/ComplexUnsolvable.json | 8 +++++++ .../resources/flower_garden/SimpleSolvable.json | 8 +++++++ .../resources/flower_garden/SimpleUnsolvable.json | 8 +++++++ 5 files changed, 60 insertions(+) create mode 100644 src/test/flower_garden_test.cpp create mode 100644 src/test/resources/flower_garden/ComplexSolvable.json create mode 100644 src/test/resources/flower_garden/ComplexUnsolvable.json create mode 100644 src/test/resources/flower_garden/SimpleSolvable.json create mode 100644 src/test/resources/flower_garden/SimpleUnsolvable.json (limited to 'src') diff --git a/src/test/flower_garden_test.cpp b/src/test/flower_garden_test.cpp new file mode 100644 index 0000000..eab2e7e --- /dev/null +++ b/src/test/flower_garden_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#include "test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/fortunes_favor/"; +const static std::string type = "simple-fortunes-favor"; + +TEST(FortunesFavor, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(FortunesFavor, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(FortunesFavor, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(FortunesFavor, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/src/test/resources/flower_garden/ComplexSolvable.json b/src/test/resources/flower_garden/ComplexSolvable.json new file mode 100644 index 0000000..f00d600 --- /dev/null +++ b/src/test/resources/flower_garden/ComplexSolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["2C","2D","4H"], + ["4C","3D","3H"], + ["2S","3C","4D"] + ], + "stock": ["2H","3S","4S"] +} diff --git a/src/test/resources/flower_garden/ComplexUnsolvable.json b/src/test/resources/flower_garden/ComplexUnsolvable.json new file mode 100644 index 0000000..4f54b94 --- /dev/null +++ b/src/test/resources/flower_garden/ComplexUnsolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["2H","2D","4H"], + ["2C","3D","3H"], + ["2S","3C","4D"] + ], + "stock": ["4C","3S","4S"] +} diff --git a/src/test/resources/flower_garden/SimpleSolvable.json b/src/test/resources/flower_garden/SimpleSolvable.json new file mode 100644 index 0000000..0891746 --- /dev/null +++ b/src/test/resources/flower_garden/SimpleSolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["2C","2D","2H"], + ["3C","3D","3H"], + ["4C","4D","4H"] + ], + "stock": ["4S","3S","2S"] +} diff --git a/src/test/resources/flower_garden/SimpleUnsolvable.json b/src/test/resources/flower_garden/SimpleUnsolvable.json new file mode 100644 index 0000000..44d7359 --- /dev/null +++ b/src/test/resources/flower_garden/SimpleUnsolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["2C","2D","3C"], + ["2H","3D","3H"], + ["2S","4D","4H"] + ], + "stock": ["4C","3S","4S"] +} -- cgit v1.2.1 From 88ed4cae6e8174102ee21ce15b03aa40fa032c29 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 15:44:47 +0000 Subject: Add rules json for flower garden --- src/main/input-output/input/sol_preset_types.cpp | 32 ++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 96cbba9..5e0fcff 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -28,9 +28,10 @@ std::map sol_preset_types::mp = { "max rank": 13, "hole": false, "foundations": true, + "foundations initial card": false, "cells": 0, - "reserve size": 0, - "stock size": 0 + "stock size": 0, + "reserve size": 0 } )" @@ -174,6 +175,33 @@ std::map sol_preset_types::mp = { "foundations initial card": true, "stock size": 3 } +)" + + }, + { + "flower-garden", + + R"( +{ + "tableau piles": { + "count": 6 + }, + "reserve size": 16 +} +)" + + }, + { + "simple-flower-garden", + + R"( +{ + "tableau piles": { + "count": 3 + }, + "max rank": 3, + "reserve size": 3 +} )" } -- cgit v1.2.1 From 1a262ef15c0f00ec756aa40952a3281204636ad9 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 15:45:23 +0000 Subject: Add unit tests for flower garden --- src/test/flower_garden_test.cpp | 12 ++++++------ src/test/resources/flower_garden/ComplexSolvable.json | 8 ++++---- src/test/resources/flower_garden/ComplexUnsolvable.json | 8 ++++---- src/test/resources/flower_garden/SimpleSolvable.json | 8 ++++---- src/test/resources/flower_garden/SimpleUnsolvable.json | 10 +++++----- 5 files changed, 23 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/test/flower_garden_test.cpp b/src/test/flower_garden_test.cpp index eab2e7e..b1aee9e 100644 --- a/src/test/flower_garden_test.cpp +++ b/src/test/flower_garden_test.cpp @@ -8,21 +8,21 @@ typedef test_helper th; -const static std::string path = "resources/fortunes_favor/"; -const static std::string type = "simple-fortunes-favor"; +const static std::string path = "resources/flower_garden/"; +const static std::string type = "simple-flower-garden"; -TEST(FortunesFavor, SimpleSolvable) { +TEST(FlowerGarden, SimpleSolvable) { EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); } -TEST(FortunesFavor, ComplexSolvable) { +TEST(FlowerGarden, ComplexSolvable) { EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); } -TEST(FortunesFavor, SimpleUnsolvable) { +TEST(FlowerGarden, SimpleUnsolvable) { EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); } -TEST(FortunesFavor, ComplexUnsolvable) { +TEST(FlowerGarden, ComplexUnsolvable) { EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); } diff --git a/src/test/resources/flower_garden/ComplexSolvable.json b/src/test/resources/flower_garden/ComplexSolvable.json index f00d600..1f1a6e6 100644 --- a/src/test/resources/flower_garden/ComplexSolvable.json +++ b/src/test/resources/flower_garden/ComplexSolvable.json @@ -1,8 +1,8 @@ { "tableau piles": [ - ["2C","2D","4H"], - ["4C","3D","3H"], - ["2S","3C","4D"] + ["AH","2D","AC"], + ["AD","2C","3D"], + ["2H","AS","3H"] ], - "stock": ["2H","3S","4S"] + "reserve": ["3C","2S","3S"] } diff --git a/src/test/resources/flower_garden/ComplexUnsolvable.json b/src/test/resources/flower_garden/ComplexUnsolvable.json index 4f54b94..be0ca64 100644 --- a/src/test/resources/flower_garden/ComplexUnsolvable.json +++ b/src/test/resources/flower_garden/ComplexUnsolvable.json @@ -1,8 +1,8 @@ { "tableau piles": [ - ["2H","2D","4H"], - ["2C","3D","3H"], - ["2S","3C","4D"] + ["2H","2D","AC"], + ["AD","2C","3D"], + ["AH","AS","3H"] ], - "stock": ["4C","3S","4S"] + "reserve": ["3C","2S","3S"] } diff --git a/src/test/resources/flower_garden/SimpleSolvable.json b/src/test/resources/flower_garden/SimpleSolvable.json index 0891746..18c680d 100644 --- a/src/test/resources/flower_garden/SimpleSolvable.json +++ b/src/test/resources/flower_garden/SimpleSolvable.json @@ -1,8 +1,8 @@ { "tableau piles": [ - ["2C","2D","2H"], - ["3C","3D","3H"], - ["4C","4D","4H"] + ["3C","2C","AC"], + ["3D","2D","AD"], + ["3H","3S","AH"] ], - "stock": ["4S","3S","2S"] + "reserve": ["2H","2S","AS"] } diff --git a/src/test/resources/flower_garden/SimpleUnsolvable.json b/src/test/resources/flower_garden/SimpleUnsolvable.json index 44d7359..e57e9ca 100644 --- a/src/test/resources/flower_garden/SimpleUnsolvable.json +++ b/src/test/resources/flower_garden/SimpleUnsolvable.json @@ -1,8 +1,8 @@ { "tableau piles": [ - ["2C","2D","3C"], - ["2H","3D","3H"], - ["2S","4D","4H"] + ["AC","2C","3C"], + ["AD","2D","3D"], + ["AH","AS","3H"] ], - "stock": ["4C","3S","4S"] -} + "reserve": ["2H","2S","3S"] +} \ No newline at end of file -- cgit v1.2.1 From 6e458408c692f05f8d8033a29c38fe3fdc18723c Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 15:45:57 +0000 Subject: Add rules parsing for flower garden --- src/main/game/sol_rules.h | 1 + src/main/input-output/input/json-parsing/rules_parser.cpp | 8 ++++++++ 2 files changed, 9 insertions(+) (limited to 'src') diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index 6afb238..5c7713c 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -38,6 +38,7 @@ struct sol_rules { bool foundations_init_card; uint8_t cells; uint8_t stock_size; + uint8_t reserve_size; }; #endif //SOLVITAIRE_SOL_RULES_H 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 3c372ae..cc893fa 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -178,4 +178,12 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { json_helper::json_parse_err("[stock size] must be an integer"); } } + + if (d.HasMember("reserve size")) { + if (d["reserve size"].IsInt()) { + sr.reserve_size = static_cast(d["reserve size"].GetInt()); + } else { + json_helper::json_parse_err("[reserve size] must be an integer"); + } + } } -- cgit v1.2.1 From 75f3ac710731890af853e5c45def1b97d49e0a74 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 15:46:25 +0000 Subject: Add deal parsing for flower garden --- .../input/json-parsing/deal_parser.cpp | 23 ++++++++++++++++++++++ .../input-output/input/json-parsing/deal_parser.h | 1 + 2 files changed, 24 insertions(+) (limited to 'src') 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 e7f8209..8bcb3b6 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -33,6 +33,11 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { parse_stock(gs, doc); } + // Construct reserve + if (gs.rules.reserve_size > 0) { + parse_reserve(gs, doc); + } + // If the foundations begin with cards in them, fill them if (gs.rules.foundations_init_card) { fill_foundations(gs); @@ -106,6 +111,24 @@ void deal_parser::parse_stock(game_state &gs, const Document& doc) { } } +void deal_parser::parse_reserve(game_state &gs, const Document& doc) { + assert(doc.HasMember("reserve")); + const Value& json_reserve_piles = doc["reserve"]; + assert(json_reserve_piles.IsArray()); + + if (gs.reserve.size() != gs.rules.reserve_size) { + json_helper::json_parse_err("Incorrect reserve size"); + } + + for (auto p = std::make_pair(begin(json_reserve_piles.GetArray()), begin(gs.reserve)); + p.second != end(gs.reserve); + ++p.first, ++p.second) { + + assert(p.first->IsString()); + gs.piles[*p.second].place(card(p.first->GetString())); + } +} + void deal_parser::fill_foundations(game_state &gs) { for (uint8_t f_idx = 0; f_idx < 4; f_idx++) { gs.piles[gs.foundations[f_idx]].place( diff --git a/src/main/input-output/input/json-parsing/deal_parser.h b/src/main/input-output/input/json-parsing/deal_parser.h index 5496eb2..ecb596c 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.h +++ b/src/main/input-output/input/json-parsing/deal_parser.h @@ -23,6 +23,7 @@ private: static void parse_hole(game_state&, const rapidjson::Document&); static void parse_cells(game_state&, const rapidjson::Document&); static void parse_stock(game_state&, const rapidjson::Document&); + static void parse_reserve(game_state&, const rapidjson::Document&); static void fill_foundations(game_state&); }; -- cgit v1.2.1 From 4161e60c9d332826129efddd5dabc60e3393057d Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 15:47:00 +0000 Subject: Create game state setup for flower garden --- src/main/game/game_state.cpp | 24 ++++++++++++++++++++---- src/main/game/game_state.h | 1 + 2 files changed, 21 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 666b970..841e213 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -50,6 +50,14 @@ game_state::game_state(const sol_rules& s_rules) : } } + // If there are cell piles, creates the relevant cell vectors + if (rules.cells > 0) { + for (uint8_t i = 0; i < rules.cells; i++) { + piles.emplace_back(); + cells.push_back(static_cast(piles.size() - 1)); + } + } + // If there is a stock & waste, creates piles if (rules.stock_size > 0) { piles.emplace_back(); @@ -58,11 +66,11 @@ game_state::game_state(const sol_rules& s_rules) : waste = static_cast(piles.size() - 1); } - // If there are cell piles, creates the relevant cell vectors - if (rules.cells > 0) { - for (uint8_t i = 0; i < rules.cells; i++) { + // If there is a reserve, creates piles + if (rules.reserve_size > 0) { + for (uint8_t i = 0; i < rules.reserve_size; i++) { piles.emplace_back(); - cells.push_back(static_cast(piles.size() - 1)); + reserve.push_back(static_cast(piles.size() - 1)); } } @@ -108,6 +116,14 @@ game_state::game_state(const sol_rules& s_rules, int seed) : } } + // If there is a reserve, deals to it + if (rules.reserve_size > 0) { + for (unsigned int i = 0; i < rules.reserve_size; i++) { + piles[reserve[i]].place(deck.back()); + deck.pop_back(); + } + } + // Deals to the tableau piles (row-by-row) for (int t = 0; !deck.empty(); t++) { card c = deck.back(); diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 30af8a6..6aa3918 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -67,6 +67,7 @@ private: std::vector cells; pile_ref stock; pile_ref waste; + std::vector reserve; std::vector foundations; pile_ref hole; -- cgit v1.2.1 From 2ac80451bc8a0d7a6830e1d9274f33138844b386 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 15:47:41 +0000 Subject: Add state printing for regular reserve --- src/main/input-output/output/state_printer.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src') diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index 88181de..1828390 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -19,6 +19,10 @@ ostream& state_printer::print(ostream& stream, const game_state& gs) { state_printer::print_header(stream, "Tableau Piles"); state_printer::print_piles(stream, gs.tableau_piles, gs); } + if (gs.rules.reserve_size > 0) { + state_printer::print_header(stream, "Reserve"); + state_printer::print_piles(stream, gs.reserve, gs); + } if (gs.rules.stock_size > 0) { state_printer::print_header(stream, "Stock | Waste"); state_printer::print_piles(stream, {gs.stock, gs.waste}, gs); -- cgit v1.2.1 From 73b6928f29a231b1294144100e6cf099c8dd90a9 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 4 Jan 2018 15:48:01 +0000 Subject: Add legal move gen for reserve --- src/main/game/game_state.legal_moves.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp index 9369734..10a9639 100644 --- a/src/main/game/game_state.legal_moves.cpp +++ b/src/main/game/game_state.legal_moves.cpp @@ -17,8 +17,7 @@ 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 (rem_ref == hole || piles[rem_ref].empty()) continue; // Stock cards can only be moved to the waste if (stock != PILE_REF_MAX && rem_ref == stock) { @@ -53,6 +52,9 @@ vector game_state::get_legal_moves() const { if (hole != PILE_REF_MAX && is_valid_hole_move(rem_ref)) { moves.emplace_back(rem_ref, hole); } + + // Note that the stock and the reserve aren't mentioned here, as + // they cannot be added to } return moves; -- cgit v1.2.1 From 6f56c50d2678f1640f8c15ff5cfcff03af3f52b2 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 15:53:30 +0000 Subject: Add json rules for canfield --- src/main/input-output/input/sol_preset_types.cpp | 36 +++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 5e0fcff..8bff279 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -31,7 +31,8 @@ std::map sol_preset_types::mp = { "foundations initial card": false, "cells": 0, "stock size": 0, - "reserve size": 0 + "reserve size": 0, + "reserve stacked": false } )" @@ -202,6 +203,39 @@ std::map sol_preset_types::mp = { "max rank": 3, "reserve size": 3 } +)" + + }, + { + "canfield", + + R"( +{ + "tableau piles": { + "count": 4, + "build policy": "red-black" + }, + "stock size": 34, + "reserve size": 14, + "reserve stacked": true +} +)" + + }, + { + "simple-canfield", + + R"( +{ + "tableau piles": { + "count": 3, + "build policy": "red-black" + }, + "max rank": 3, + "stock size": 1, + "reserve size": 2, + "reserve stacked": true +} )" } -- cgit v1.2.1 From e9678bfc154aaf59c9c7c965d99fd50de8532cce Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 15:54:04 +0000 Subject: Add unit tests for canfield --- src/test/canfield_test.cpp | 28 ++++++++++++++++++++++ src/test/resources/canfield/ComplexSolvable.json | 9 +++++++ src/test/resources/canfield/ComplexUnsolvable.json | 9 +++++++ src/test/resources/canfield/SimpleSolvable.json | 9 +++++++ src/test/resources/canfield/SimpleUnsolvable.json | 9 +++++++ 5 files changed, 64 insertions(+) create mode 100644 src/test/canfield_test.cpp create mode 100644 src/test/resources/canfield/ComplexSolvable.json create mode 100644 src/test/resources/canfield/ComplexUnsolvable.json create mode 100644 src/test/resources/canfield/SimpleSolvable.json create mode 100644 src/test/resources/canfield/SimpleUnsolvable.json (limited to 'src') diff --git a/src/test/canfield_test.cpp b/src/test/canfield_test.cpp new file mode 100644 index 0000000..63fd615 --- /dev/null +++ b/src/test/canfield_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#include "test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/canfield/"; +const static std::string type = "simple-canfield"; + +TEST(Canfield, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(Canfield, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(Canfield, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(Canfield, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/src/test/resources/canfield/ComplexSolvable.json b/src/test/resources/canfield/ComplexSolvable.json new file mode 100644 index 0000000..b5c9253 --- /dev/null +++ b/src/test/resources/canfield/ComplexSolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["AC","2C","3C"], + ["AD","2D","3D"], + ["3S","AS","3H"] + ], + "stock": ["2H"], + "reserve": ["AH","2S"] +} \ No newline at end of file diff --git a/src/test/resources/canfield/ComplexUnsolvable.json b/src/test/resources/canfield/ComplexUnsolvable.json new file mode 100644 index 0000000..9391caf --- /dev/null +++ b/src/test/resources/canfield/ComplexUnsolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["AC","2H","2D"], + ["AD","3D","3C"], + ["2S","AS","3H"] + ], + "stock": ["2C"], + "reserve": ["AH","3S"] +} \ No newline at end of file diff --git a/src/test/resources/canfield/SimpleSolvable.json b/src/test/resources/canfield/SimpleSolvable.json new file mode 100644 index 0000000..4e998ea --- /dev/null +++ b/src/test/resources/canfield/SimpleSolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["3C","2C","AC"], + ["3D","2D","AD"], + ["3H","3S","AH"] + ], + "stock": ["2H"], + "reserve": ["2S","AS"] +} diff --git a/src/test/resources/canfield/SimpleUnsolvable.json b/src/test/resources/canfield/SimpleUnsolvable.json new file mode 100644 index 0000000..e8a1e3e --- /dev/null +++ b/src/test/resources/canfield/SimpleUnsolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["AC","2C","3C"], + ["AD","2D","3D"], + ["AH","AS","3H"] + ], + "stock": ["2H"], + "reserve": ["2S","3S"] +} \ No newline at end of file -- cgit v1.2.1 From e1eb16767fee2241aa27896332a01ed5987fdf8b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 15:54:31 +0000 Subject: Add rules parsing for stacked reserve (canfield) --- src/main/game/sol_rules.h | 1 + src/main/input-output/input/json-parsing/rules_parser.cpp | 8 ++++++++ 2 files changed, 9 insertions(+) (limited to 'src') diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index 5c7713c..11f169f 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -39,6 +39,7 @@ struct sol_rules { uint8_t cells; uint8_t stock_size; uint8_t reserve_size; + bool reserve_stacked; }; #endif //SOLVITAIRE_SOL_RULES_H 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 cc893fa..f997ba3 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -186,4 +186,12 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { json_helper::json_parse_err("[reserve size] must be an integer"); } } + + if (d.HasMember("reserve stacked")) { + if (d["reserve stacked"].IsBool()) { + sr.reserve_stacked = d["reserve stacked"].GetBool(); + } else { + json_helper::json_parse_err("[reserve stacked] must be a boolean"); + } + } } -- cgit v1.2.1 From dc5a834c0d2d2a25f3023f812f8d85376855f0be Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 15:55:59 +0000 Subject: Add deal parser for stacked reserve (canfield) --- .../input/json-parsing/deal_parser.cpp | 26 +++++++++++++++------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'src') 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 8bcb3b6..6f0694d 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -116,16 +116,26 @@ void deal_parser::parse_reserve(game_state &gs, const Document& doc) { const Value& json_reserve_piles = doc["reserve"]; assert(json_reserve_piles.IsArray()); - if (gs.reserve.size() != gs.rules.reserve_size) { - json_helper::json_parse_err("Incorrect reserve size"); + // We treat a regular reserve like multiple single-card piles, + // but a stacked reserve as a single multiple-card pile + if (gs.rules.reserve_stacked) { + if (json_reserve_piles.Size() != gs.rules.reserve_size + || json_reserve_piles.Size() != gs.rules.reserve_size) { + json_helper::json_parse_err("Incorrect reserve size"); + } + } else { + if (gs.reserve.size() != gs.rules.reserve_size + || json_reserve_piles.Size() != gs.rules.reserve_size) { + json_helper::json_parse_err("Incorrect reserve size"); + } } - for (auto p = std::make_pair(begin(json_reserve_piles.GetArray()), begin(gs.reserve)); - p.second != end(gs.reserve); - ++p.first, ++p.second) { - - assert(p.first->IsString()); - gs.piles[*p.second].place(card(p.first->GetString())); + const auto& json_card_arr = json_reserve_piles.GetArray(); + for (game_state::pile_ref i = 0; i < json_card_arr.Size(); i++) { + assert(json_card_arr[i].IsString()); + game_state::pile_ref pr = gs.reserve[0]; + if (!gs.rules.reserve_stacked) pr += i; + gs.piles[pr].place(card(json_card_arr[i].GetString())); } } -- cgit v1.2.1 From 6ff8eb79720d76110f2477c07ab95eac167d0389 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 15:56:18 +0000 Subject: Add game state setup for stacked reserve (canfield) --- src/main/game/game_state.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 841e213..8da5bac 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -68,7 +68,9 @@ game_state::game_state(const sol_rules& s_rules) : // If there is a reserve, creates piles if (rules.reserve_size > 0) { - for (uint8_t i = 0; i < rules.reserve_size; i++) { + uint8_t pile_count = rules.reserve_stacked ? + uint8_t(1) : rules.reserve_size; + for (uint8_t i = 0; i < pile_count; i++) { piles.emplace_back(); reserve.push_back(static_cast(piles.size() - 1)); } @@ -116,10 +118,13 @@ game_state::game_state(const sol_rules& s_rules, int seed) : } } - // If there is a reserve, deals to it + // If there is a reserve, deals to it. + // We treat a regular reserve like multiple single-card piles, + // but a stacked reserve as a single multiple-card pile. if (rules.reserve_size > 0) { for (unsigned int i = 0; i < rules.reserve_size; i++) { - piles[reserve[i]].place(deck.back()); + int idx = rules.reserve_stacked ? 0 : i; + piles[reserve[idx]].place(deck.back()); deck.pop_back(); } } -- cgit v1.2.1 From 748dceab129dc57aa7a3e58ef538b9701398846c Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 15:56:46 +0000 Subject: Add state printing for stacked reserve (canfield) --- src/main/input-output/output/state_printer.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index 1828390..2c23496 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -20,7 +20,11 @@ ostream& state_printer::print(ostream& stream, const game_state& gs) { state_printer::print_piles(stream, gs.tableau_piles, gs); } if (gs.rules.reserve_size > 0) { - state_printer::print_header(stream, "Reserve"); + if (gs.rules.reserve_stacked) { + state_printer::print_header(stream, "Reserve (Stacked)"); + } else { + state_printer::print_header(stream, "Reserve"); + } state_printer::print_piles(stream, gs.reserve, gs); } if (gs.rules.stock_size > 0) { -- cgit v1.2.1 From 5886a4e8be7fb5e15617a8e58f207ec008ca93e2 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 18:00:41 +0000 Subject: Add json rules for somerset --- src/main/input-output/input/sol_preset_types.cpp | 32 +++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 8bff279..89a5842 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -23,7 +23,8 @@ std::map sol_preset_types::mp = { "count": 8, "build order": "descending", "build policy": "any-suit", - "spaces policy": "any" + "spaces policy": "any", + "diagonal deal": true }, "max rank": 13, "hole": false, @@ -236,6 +237,35 @@ std::map sol_preset_types::mp = { "reserve size": 2, "reserve stacked": true } +)" + + }, + { + "somerset", + + R"( +{ + "tableau piles": { + "count": 10, + "build policy": "red-black", + "diagonal deal": true + } +} +)" + + }, + { + "simple-somerset", + + R"( +{ + "tableau piles": { + "count": 4, + "build policy": "red-black", + "diagonal deal": true + }, + "max rank": 2 +} )" } -- cgit v1.2.1 From cc9a9721d3a7bdbbcc055986c014a0665d5b3e63 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 18:01:17 +0000 Subject: Add unit tests for somerset --- src/test/resources/somerset/ComplexSolvable.json | 8 +++++++ src/test/resources/somerset/ComplexUnsolvable.json | 8 +++++++ src/test/resources/somerset/SimpleSolvable.json | 8 +++++++ src/test/resources/somerset/SimpleUnsolvable.json | 8 +++++++ src/test/somerset_test.cpp | 28 ++++++++++++++++++++++ 5 files changed, 60 insertions(+) create mode 100644 src/test/resources/somerset/ComplexSolvable.json create mode 100644 src/test/resources/somerset/ComplexUnsolvable.json create mode 100644 src/test/resources/somerset/SimpleSolvable.json create mode 100644 src/test/resources/somerset/SimpleUnsolvable.json create mode 100644 src/test/somerset_test.cpp (limited to 'src') diff --git a/src/test/resources/somerset/ComplexSolvable.json b/src/test/resources/somerset/ComplexSolvable.json new file mode 100644 index 0000000..b316c50 --- /dev/null +++ b/src/test/resources/somerset/ComplexSolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["2S","2C","AC"], + ["2D","AD"], + ["2H","AH"], + ["AS"] + ] +} diff --git a/src/test/resources/somerset/ComplexUnsolvable.json b/src/test/resources/somerset/ComplexUnsolvable.json new file mode 100644 index 0000000..09d7864 --- /dev/null +++ b/src/test/resources/somerset/ComplexUnsolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["AS","AC","2C"], + ["AD","2D"], + ["AH","2H"], + ["2S"] + ] +} diff --git a/src/test/resources/somerset/SimpleSolvable.json b/src/test/resources/somerset/SimpleSolvable.json new file mode 100644 index 0000000..b316c50 --- /dev/null +++ b/src/test/resources/somerset/SimpleSolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["2S","2C","AC"], + ["2D","AD"], + ["2H","AH"], + ["AS"] + ] +} diff --git a/src/test/resources/somerset/SimpleUnsolvable.json b/src/test/resources/somerset/SimpleUnsolvable.json new file mode 100644 index 0000000..09d7864 --- /dev/null +++ b/src/test/resources/somerset/SimpleUnsolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["AS","AC","2C"], + ["AD","2D"], + ["AH","2H"], + ["2S"] + ] +} diff --git a/src/test/somerset_test.cpp b/src/test/somerset_test.cpp new file mode 100644 index 0000000..22cfe5a --- /dev/null +++ b/src/test/somerset_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#include "test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/somerset/"; +const static std::string type = "simple-somerset"; + +TEST(Somerset, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(Somerset, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(Somerset, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(Somerset, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} -- cgit v1.2.1 From f481f8bb2f6ba7c34e36fb3a439500ebf0b42b35 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 18:03:09 +0000 Subject: Add diagonal deal to rules parser (somerset) --- src/main/game/sol_rules.h | 1 + src/main/input-output/input/json-parsing/rules_parser.cpp | 8 ++++++++ 2 files changed, 9 insertions(+) (limited to 'src') diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index 11f169f..e9d130b 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -36,6 +36,7 @@ struct sol_rules { bool hole; bool foundations; bool foundations_init_card; + bool diagonal_deal; uint8_t cells; uint8_t stock_size; uint8_t reserve_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 f997ba3..9d20a5a 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -163,6 +163,14 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } + if (d.HasMember("diagonal deal")) { + if (d["diagonal deal"].IsBool()) { + sr.diagonal_deal = d["diagonal deal"].GetBool(); + } else { + json_helper::json_parse_err("[diagonal deal] must be a boolean"); + } + } + if (d.HasMember("cells")) { if (d["cells"].IsInt()) { sr.cells = static_cast(d["cells"].GetInt()); -- cgit v1.2.1 From 1ffa7dcf0c8df73d5b2affe8066c1cbeb324538e Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 5 Jan 2018 18:03:29 +0000 Subject: Add diagonal deal to game setup (somerset) --- src/main/game/game_state.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 8da5bac..83b9672 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -132,11 +132,26 @@ game_state::game_state(const sol_rules& s_rules, int seed) : // Deals to the tableau piles (row-by-row) for (int t = 0; !deck.empty(); t++) { card c = deck.back(); - deck.pop_back(); // Add the randomly generated card to the tableau piles - pile_ref tableau_pile = tableau_piles[t % tableau_piles.size()]; - piles[tableau_pile].place(c); + auto p = t % tableau_piles.size(); + + // If we are doing a diagonal deal, each row should have one fewer card. + // Leftover cards are dealt normally in full rows. + auto row_idx = t / tableau_piles.size(); + if (rules.diagonal_deal && row_idx < tableau_piles.size()) { + p = tableau_piles.size()-p-1; + pile_ref tableau_pile = tableau_piles[p]; + + if (p >= row_idx) { + piles[tableau_pile].place(c); + deck.pop_back(); + } + } else { + pile_ref tableau_pile = tableau_piles[p]; + piles[tableau_pile].place(c); + deck.pop_back(); + } } } -- cgit v1.2.1 From 22b347e97a9da26a2cddd7b5a04e1a7a62069d23 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 8 Jan 2018 18:46:11 +0000 Subject: Add json rules for alpha star --- src/main/input-output/input/sol_preset_types.cpp | 34 +++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 89a5842..6805f8e 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -24,7 +24,8 @@ std::map sol_preset_types::mp = { "build order": "descending", "build policy": "any-suit", "spaces policy": "any", - "diagonal deal": true + "diagonal deal": true, + "move built group": false }, "max rank": 13, "hole": false, @@ -266,6 +267,37 @@ std::map sol_preset_types::mp = { }, "max rank": 2 } +)" + + }, + { + "alpha-star", + + R"( +{ + "tableau piles": { + "count": 12, + "build policy": "same-suit", + "move built group": true + }, + "foundations initial card": true +} +)" + + }, + { + "simple-alpha-star", + + R"( +{ + "tableau piles": { + "count": 3, + "build policy": "same-suit", + "move built group": true + }, + "max rank": 5, + "foundations initial card": true +} )" } -- cgit v1.2.1 From 02e332eb50cbf8115f28f3277c3938fe317fae65 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 8 Jan 2018 18:57:33 +0000 Subject: Add unit tests for alpha star --- src/test/alpha_star_test.cpp | 28 ++++++++++++++++++++++ src/test/resources/alpha_star/ComplexSolvable.json | 7 ++++++ .../resources/alpha_star/ComplexUnsolvable.json | 7 ++++++ src/test/resources/alpha_star/SimpleSolvable.json | 7 ++++++ .../resources/alpha_star/SimpleUnsolvable.json | 7 ++++++ 5 files changed, 56 insertions(+) create mode 100644 src/test/alpha_star_test.cpp create mode 100644 src/test/resources/alpha_star/ComplexSolvable.json create mode 100644 src/test/resources/alpha_star/ComplexUnsolvable.json create mode 100644 src/test/resources/alpha_star/SimpleSolvable.json create mode 100644 src/test/resources/alpha_star/SimpleUnsolvable.json (limited to 'src') diff --git a/src/test/alpha_star_test.cpp b/src/test/alpha_star_test.cpp new file mode 100644 index 0000000..eeff1be --- /dev/null +++ b/src/test/alpha_star_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#include "test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/alpha_star/"; +const static std::string type = "simple-alpha-star"; + +TEST(AlphaStar, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(AlphaStar, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(AlphaStar, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(AlphaStar, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/src/test/resources/alpha_star/ComplexSolvable.json b/src/test/resources/alpha_star/ComplexSolvable.json new file mode 100644 index 0000000..e7eedd5 --- /dev/null +++ b/src/test/resources/alpha_star/ComplexSolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["3S","2C","3C","5C","5H","4H"], + ["4S","4C","5S","2D","5D"], + ["2S","2H","3H","4D","3D"] + ] +} \ No newline at end of file diff --git a/src/test/resources/alpha_star/ComplexUnsolvable.json b/src/test/resources/alpha_star/ComplexUnsolvable.json new file mode 100644 index 0000000..a01f2b5 --- /dev/null +++ b/src/test/resources/alpha_star/ComplexUnsolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["2S","2C","3C","5C","5H","4H"], + ["4S","4C","5S","2D","5D"], + ["3S","2H","3H","4D","3D"] + ] +} diff --git a/src/test/resources/alpha_star/SimpleSolvable.json b/src/test/resources/alpha_star/SimpleSolvable.json new file mode 100644 index 0000000..d160e42 --- /dev/null +++ b/src/test/resources/alpha_star/SimpleSolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["5S","5C","4C","3C","2S","2C"], + ["5D","4D","3S","3D","2D"], + ["5H","4S","4H","3H","2H"] + ] +} diff --git a/src/test/resources/alpha_star/SimpleUnsolvable.json b/src/test/resources/alpha_star/SimpleUnsolvable.json new file mode 100644 index 0000000..1097473 --- /dev/null +++ b/src/test/resources/alpha_star/SimpleUnsolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["2S","2C","3C","4C","5S","5C"], + ["2D","3D","4S","4D","5D"], + ["2H","3S","3H","4H","5H"] + ] +} \ No newline at end of file -- cgit v1.2.1 From cc5cb4243d2015f218ed5258df47ec678175a486 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 8 Jan 2018 18:59:44 +0000 Subject: Add rules parsing for built pile (AS) --- src/main/game/sol_rules.h | 1 + .../input/json-parsing/rules_parser.cpp | 24 ++++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index e9d130b..3e0393d 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -32,6 +32,7 @@ struct sol_rules { build_order build_ord; build_policy build_pol; spaces_policy spaces_pol; + bool move_built_group; card::rank_t max_rank; bool hole; bool foundations; 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 9d20a5a..9ec2339 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -126,6 +126,22 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } + if (d["tableau piles"].HasMember("move built group")) { + if (d["tableau piles"]["move built group"].IsBool()) { + sr.move_built_group = d["tableau piles"]["move built group"].GetBool(); + } else { + json_helper::json_parse_err("[tableau piles][move built group] must be a boolean"); + } + } + + if (d["tableau piles"].HasMember("diagonal deal")) { + if (d["tableau piles"]["diagonal deal"].IsBool()) { + sr.diagonal_deal = d["tableau piles"]["diagonal deal"].GetBool(); + } else { + json_helper::json_parse_err("[tableau piles][diagonal deal] must be a boolean"); + } + } + } else { json_helper::json_parse_err("[tableau piles] must be an object"); } @@ -163,14 +179,6 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } - if (d.HasMember("diagonal deal")) { - if (d["diagonal deal"].IsBool()) { - sr.diagonal_deal = d["diagonal deal"].GetBool(); - } else { - json_helper::json_parse_err("[diagonal deal] must be a boolean"); - } - } - if (d.HasMember("cells")) { if (d["cells"].IsInt()) { sr.cells = static_cast(d["cells"].GetInt()); -- cgit v1.2.1 From cb9217a4675d11c300bc704aadf3af7a1809b68c Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 8 Jan 2018 19:01:07 +0000 Subject: Add game setup for built pile (AS) --- src/main/game/game_state.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 83b9672..ee02889 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -194,17 +194,48 @@ vector game_state::gen_shuffled_deck(int seed, int max_rank = 13) { return deck; } +game_state::move::move(pile_ref f, pile_ref t, pile::size_type i) + : from(f), to(t), count(i) { + assert(i >= 1); +} + ///////////////////////// // MODIFYING FUNCTIONS // ///////////////////////// void game_state::make_move(const move m) { - piles[m.second].place(piles[m.first].take()); + // If this is not a built-pile move + if (m.count == 1) { + piles[m.to].place(piles[m.from].take()); + } else { + // Adds the cards to the 'to' pile + for (auto pile_idx = m.count; pile_idx-- > 0;) { + piles[m.to].place(piles[m.from][pile_idx]); + } + + // Removes the cards from the 'from' pile + for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) { + piles[m.from].take(); + } + } } void game_state::undo_move(const move m) { - piles[m.first].place(piles[m.second].take()); + // If this is not a built-pile move + if (m.count == 1) { + piles[m.from].place(piles[m.to].take()); + } else { + // Adds the cards to the 'from' pile + for (auto pile_idx = m.count; pile_idx-- > 0;) { + piles[m.from].place(piles[m.to][pile_idx]); + } + + // Removes the cards from the 'to' pile + for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) { + piles[m.to].take(); + } + } } @@ -236,6 +267,17 @@ bool operator==(const game_state& a, const game_state& b) { } +////////////////////////////////// +// STATIC VARIABLES AND MEMBERS // +////////////////////////////////// + +game_state::pile_ref game_state::PILE_REF_MAX = 255; + +game_state::move game_state::null_move() { + return {PILE_REF_MAX, 0, PILE_REF_MAX}; +} + + //////////////////// // HASH FUNCTIONS // //////////////////// -- cgit v1.2.1 From 067152f57ced18c5f706144909345125754e8664 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 8 Jan 2018 19:02:36 +0000 Subject: Add game logic for built pile (AA) and refactor --- src/main/game/card.cpp | 4 + src/main/game/card.h | 1 + src/main/game/game_state.h | 21 ++++- src/main/game/game_state.legal_moves.cpp | 125 +++++++++++++++++++++++++ src/main/game/pile.cpp | 15 ++- src/main/game/pile.h | 12 ++- src/main/input-output/output/state_printer.cpp | 7 +- src/main/solver/solver.cpp | 2 +- 8 files changed, 164 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 74bdc31..4d9342d 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -69,6 +69,10 @@ bool operator==(const card& l, const card& r) { return l.get_rank() == r.get_rank() && l.get_suit() == r.get_suit(); } +bool operator!=(const card& l, const card& r) { + return !(l == r); +} + size_t hash_value(card const& c) { boost::hash hasher; auto raw_val = static_cast( diff --git a/src/main/game/card.h b/src/main/game/card.h index 8aaa50d..4cab774 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -36,6 +36,7 @@ private: }; bool operator==(const card&, const card&); +bool operator!=(const card&, const card&); std::size_t hash_value(card const&); diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 6aa3918..f43348f 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -5,9 +5,6 @@ #ifndef SOLVITAIRE_GAME_STATE_H #define SOLVITAIRE_GAME_STATE_H -#define NULL_MOVE (game_state::move(PILE_REF_MAX, PILE_REF_MAX)) -#define PILE_REF_MAX (255) - #include #include @@ -26,7 +23,10 @@ class game_state { public: // Defines types typedef uint8_t pile_ref; - typedef std::pair move; + struct move { + move(pile_ref, pile_ref, pile::size_type = 1); + pile_ref from; pile_ref to; pile::size_type count; + }; // Constructors // Creates a game state representation from a JSON doc @@ -45,11 +45,16 @@ public: const std::vector& get_data() const; friend bool operator==(const game_state&, const game_state&); - friend std::ostream& operator<< (std::ostream&, const game_state&); + + // Static pile ref members/vars + static pile_ref PILE_REF_MAX; + static move null_move(); friend std::size_t hash_value(game_state const&); friend std::size_t hash_value(std::vector const&); + friend std::ostream& operator<< (std::ostream&, const game_state&); + private: static std::vector gen_shuffled_deck(int, int); @@ -60,6 +65,12 @@ private: bool is_valid_tableau_move(pile_ref, pile_ref) const; bool is_valid_foundations_move(pile_ref, pile_ref) const; bool is_valid_hole_move(pile_ref) const; + void get_built_group_moves(std::vector&) const; + pile::size_type get_built_group_height(pile_ref) const; + bool valid_built_group_move(card, card, card) const; + void add_built_group_move(std::vector&, pile_ref, pile_ref) const; + void add_empty_built_group_moves(std::vector&, pile_ref, pile_ref, + card) const; // References to piles sol_rules rules; diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp index 10a9639..56c1f06 100644 --- a/src/main/game/game_state.legal_moves.cpp +++ b/src/main/game/game_state.legal_moves.cpp @@ -57,6 +57,10 @@ vector game_state::get_legal_moves() const { // they cannot be added to } + if (rules.move_built_group) { + get_built_group_moves(moves); + } + return moves; } @@ -129,3 +133,124 @@ bool game_state::is_valid_hole_move(const pile_ref rem_ref) const { || (rank == rules.max_rank && hole_rank == 1) || (rank == 1 && hole_rank == rules.max_rank); } + + +/////////////////////// +// BUILT-GROUP MOVES // +/////////////////////// + +void game_state::get_built_group_moves(vector& moves) const { + assert(rules.build_pol != pol::NO_BUILD); + + // Cycles through each pile to see if it contains a built group of size > 1 + for (auto rem_ref : tableau_piles) { + if (piles[rem_ref].size() < 2) continue; + + auto built_group_height = get_built_group_height(rem_ref); + if (built_group_height == 1) continue; + + // We have found a built group, so cycles through each pile to see if + // this group can be added to it + for (auto add_ref : tableau_piles) { + if (add_ref == rem_ref) continue; + + card bg_low = piles[rem_ref].top_card(); + card bg_high = piles[rem_ref][built_group_height - 1]; + + // If the pile is empty, and we can move to empty piles, does so + if (piles[add_ref].empty()) { + if (rules.spaces_pol == s_pol::ANY) { + add_empty_built_group_moves(moves, rem_ref, add_ref, bg_high); + } + } + // Otherwise, checks to see if it is a valid move + else { + card add_card = piles[add_ref].top_card(); + + // Adds the move to the list of moves, as a special built group move + if (valid_built_group_move(bg_low, bg_high, add_card)) { + add_built_group_move(moves, rem_ref, add_ref); + } + } + } + } +} + +// Finds the size of the built group at the top of a pile +pile::size_type game_state::get_built_group_height(pile_ref ref) const { + + // The index of the currently examined card, the current card, previous card + // and whether we currently have a valid built group. + pile::size_type group_card_idx = 0; + bool built_group = true; + + do { + card prev_c = piles[ref][group_card_idx++]; + card c = piles[ref][group_card_idx]; + + // If the build policy is same suit, makes sure group has same suit + if (rules.build_pol == sol_rules::build_policy::SAME_SUIT + && c.get_suit() != prev_c.get_suit()) { + built_group = false; + } + // If the build policy is red black, makes sure group alternates + // in correct phase + else if (rules.build_pol == sol_rules::build_policy::RED_BLACK + && c.get_colour() == prev_c.get_colour()) { + built_group = false; + } + + // Makes sure the rank of the cards is sequential + built_group = built_group + && c.get_rank() == prev_c.get_rank()+1; + + } + // Continues until group_card_idx points to a card outside the built + // Group + while (built_group && group_card_idx < piles[ref].size()); + + return group_card_idx; +} + +bool game_state::valid_built_group_move(card bg_low, card bg_high, card add_card) +const { + // The rank of the card we're moving the built group to must be greater than + // the second-from-top card of the built group, and less than or equal to the + // card that would come next at the top of the built pile + if (add_card.get_rank() <= bg_low.get_rank() + 1 + || add_card.get_rank() > bg_high.get_rank() + 1) { + return false; + } + + // Checks to see if the suits are correct + switch (rules.build_pol) { + case sol_rules::build_policy::SAME_SUIT: + return bg_low.get_suit() == add_card.get_suit(); + + case sol_rules::build_policy::RED_BLACK: { + bool same_suit = bg_low.get_suit() == add_card.get_suit(); + bool even_diff = (add_card.get_rank() - bg_low.get_rank()) % 2 == 0; + return (same_suit && even_diff) || (!same_suit && !even_diff); + } + default: return true; + } +} + +void game_state::add_built_group_move(vector& moves, pile_ref rem_ref, + pile_ref add_ref) const { + moves.emplace_back(rem_ref, add_ref, 2); +} + +void game_state::add_empty_built_group_moves(vector& moves, + pile_ref rem_ref, + pile_ref add_ref, + card bg_high) const { + // Loops through each possible built group move to an empty pile and adds it + // to the list + pile::size_type card_idx = 1; + do { + moves.emplace_back( + rem_ref, add_ref, static_cast(card_idx + 1) + ); + } while (piles[rem_ref][card_idx++] != bg_high); +} diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp index 3b53950..264bb49 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -21,15 +21,16 @@ bool pile::empty() const { return pile_vec.empty(); } -uint8_t pile::size() const { +pile::size_type pile::size() const { return static_cast(pile_vec.size()); } -card& pile::operator[] (vector::size_type i) { - return pile_vec[i]; +card& pile::operator[] (size_type i) { + return pile_vec[pile_vec.size() - 1 - i]; } -card pile::operator[] (vector::size_type i) const { - return pile_vec[i]; + +card pile::operator[] (size_type i) const { + return pile_vec[pile_vec.size() - 1 - i]; } void pile::place(const card c) { @@ -42,10 +43,6 @@ card pile::take() { return c; } -void pile::clear() { - pile_vec.clear(); -} - bool operator==(const pile& a, const pile& b) { return a.pile_vec == b.pile_vec; } diff --git a/src/main/game/pile.h b/src/main/game/pile.h index a51e0f4..f2600fe 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -6,25 +6,27 @@ #define SOLVITAIRE_PILE_H #include +#include #include "sol_rules.h" #include "card.h" class pile { public: + typedef uint8_t size_type; + // Inspect card top_card() const; bool empty() const; - uint8_t size() const; + size_type size() const; // Modify void place(card); card take(); - void clear(); - // Index like array - card& operator[] (std::vector::size_type); - card operator[] (std::vector::size_type) const; + // Indexes the vector, with the top card as index 0 + card& operator[] (size_type); + card operator[] (size_type) const; friend bool operator==(const pile&, const pile&); friend std::size_t hash_value(pile const&); diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index 2c23496..cf9f09e 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -51,7 +51,7 @@ void state_printer::print_piles(ostream& stream, const vector& pile_rs, const game_state& gs) { bool empty_row = false; - vector::size_type row_idx = 0; + pile::size_type row_idx = 0; // Loops through the rows in each pile, starting from the bottom, until // one is empty @@ -67,8 +67,9 @@ void state_printer::print_piles(ostream& stream, if (!empty_row || row_idx == 0) { // Loops through the current (non-empty) row, and outputs the values for (const game_state::pile_ref pile_r : pile_rs) { - if (gs.piles[pile_r].size() > row_idx) { - print_card(stream, gs.piles[pile_r][row_idx]); + const pile& p = gs.piles[pile_r]; + if (p.size() > row_idx) { + print_card(stream, p[p.size() - 1 - row_idx]); } else if (row_idx == 0) { stream << "[]"; } diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index e0fbbc6..aaaf530 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -26,7 +26,7 @@ bool solver::run() { states_searched++; LOG_DEBUG (state); - frontier.emplace_back(NULL_MOVE, state.get_legal_moves()); + frontier.emplace_back(game_state::null_move(), state.get_legal_moves()); while (!frontier.empty()) { node& current = frontier.back(); -- cgit v1.2.1 From ef291b15a21f2f6cfa98b2c4f2572de5a61a2fa9 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 9 Jan 2018 08:51:11 +0000 Subject: Add json rules for spider --- src/main/input-output/input/sol_preset_types.cpp | 35 +++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 6805f8e..8f0293f 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -24,15 +24,17 @@ std::map sol_preset_types::mp = { "build order": "descending", "build policy": "any-suit", "spaces policy": "any", - "diagonal deal": true, + "diagonal deal": false, "move built group": false }, "max rank": 13, + "two decks": false, "hole": false, "foundations": true, "foundations initial card": false, "cells": 0, "stock size": 0, + "stock deal type": "waste", "reserve size": 0, "reserve stacked": false } @@ -298,6 +300,37 @@ std::map sol_preset_types::mp = { "max rank": 5, "foundations initial card": true } +)" + + }, + { + "spider", + + R"( +{ + "tableau piles": { + "count": 10 + }, + "two decks": true, + "stock size": 60, + "stock deal type": "tableau piles" +} +)" + + }, + { + "simple-spider", + + R"( +{ + "tableau piles": { + "count": 4 + }, + "two decks": true, + "max rank": 3, + "stock size": 12, + "stock deal type": "tableau piles" +} )" } -- cgit v1.2.1 From 2a05e7357744331b7a0c316a72c01508f0199d6a Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 9 Jan 2018 08:51:43 +0000 Subject: Add unit tests for spider --- src/test/resources/spider/ComplexSolvable.json | 13 +++++++++++ src/test/resources/spider/ComplexUnsolvable.json | 13 +++++++++++ src/test/resources/spider/SimpleSolvable.json | 14 ++++++++++++ src/test/resources/spider/SimpleUnsolvable.json | 13 +++++++++++ src/test/spider_test.cpp | 28 ++++++++++++++++++++++++ 5 files changed, 81 insertions(+) create mode 100644 src/test/resources/spider/ComplexSolvable.json create mode 100644 src/test/resources/spider/ComplexUnsolvable.json create mode 100644 src/test/resources/spider/SimpleSolvable.json create mode 100644 src/test/resources/spider/SimpleUnsolvable.json create mode 100644 src/test/spider_test.cpp (limited to 'src') diff --git a/src/test/resources/spider/ComplexSolvable.json b/src/test/resources/spider/ComplexSolvable.json new file mode 100644 index 0000000..3591805 --- /dev/null +++ b/src/test/resources/spider/ComplexSolvable.json @@ -0,0 +1,13 @@ +{ + "tableau piles": [ + ["2D","AD","3D"], + ["AC","2C","3C"], + ["AS","2S","3S"], + ["AH","2H","3H"] + ], + "stock": [ + "2C","2D","2H","2S", + "AC","AD","AH","AS", + "3C","3D","3H","3S" + ] +} diff --git a/src/test/resources/spider/ComplexUnsolvable.json b/src/test/resources/spider/ComplexUnsolvable.json new file mode 100644 index 0000000..8fce03d --- /dev/null +++ b/src/test/resources/spider/ComplexUnsolvable.json @@ -0,0 +1,13 @@ +{ + "tableau piles": [ + ["2D","AD","3D"], + ["AC","AC","3C"], + ["AS","AS","3S"], + ["AH","AH","3H"] + ], + "stock": [ + "2C","2D","2H","2S", + "AC","2D","2H","2S", + "3C","3D","3H","3S" + ] +} diff --git a/src/test/resources/spider/SimpleSolvable.json b/src/test/resources/spider/SimpleSolvable.json new file mode 100644 index 0000000..f0b9b00 --- /dev/null +++ b/src/test/resources/spider/SimpleSolvable.json @@ -0,0 +1,14 @@ +{ + "tableau piles": [ + ["3C","2C","AC"], + ["3D","2D","AD"], + ["3H","2H","AH"], + ["3S","2S","AS"] + ], + "stock": [ + "3C","2C","AC", + "3D","2D","AD", + "3H","2H","AH", + "3S","2S","AS" + ] +} diff --git a/src/test/resources/spider/SimpleUnsolvable.json b/src/test/resources/spider/SimpleUnsolvable.json new file mode 100644 index 0000000..d11a6dd --- /dev/null +++ b/src/test/resources/spider/SimpleUnsolvable.json @@ -0,0 +1,13 @@ +{ + "tableau piles": [ + ["AC","AC","2C"], + ["AD","AD","2D"], + ["AH","AH","2H"], + ["AS","AS","2S"] + ], + "stock": [ + "3C","3D","3H","3S", + "2C","2D","2H","2S", + "3C","3D","3H","3S" + ] +} diff --git a/src/test/spider_test.cpp b/src/test/spider_test.cpp new file mode 100644 index 0000000..0e21f86 --- /dev/null +++ b/src/test/spider_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#include "test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/spider/"; +const static std::string type = "simple-spider"; + +TEST(Spider, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(Spider, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(Spider, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(Spider, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} -- cgit v1.2.1 From 9efeb027c72460e7d5b6e320819788b7f38d8035 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 9 Jan 2018 08:52:22 +0000 Subject: Add rules parsing for stock-tableau deal & 2 deck --- src/main/game/sol_rules.h | 6 ++++++ .../input/json-parsing/rules_parser.cpp | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) (limited to 'src') diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index 3e0393d..87266ee 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -27,12 +27,17 @@ struct sol_rules { NO_BUILD, ANY }; + enum class stock_deal_type { + WASTE, + TABLEAU_PILES + }; uint8_t tableau_pile_count; build_order build_ord; build_policy build_pol; spaces_policy spaces_pol; bool move_built_group; + bool two_decks; card::rank_t max_rank; bool hole; bool foundations; @@ -40,6 +45,7 @@ struct sol_rules { bool diagonal_deal; uint8_t cells; uint8_t stock_size; + stock_deal_type stock_deal_t; uint8_t reserve_size; bool reserve_stacked; }; 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 9ec2339..948eb39 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -19,6 +19,7 @@ using namespace rapidjson; typedef sol_rules::build_order ord; typedef sol_rules::build_policy pol; typedef sol_rules::spaces_policy s_pol; +typedef sol_rules::stock_deal_type sdt; const sol_rules rules_parser::from_file(const string rules_file) { sol_rules sr = get_default(); @@ -155,6 +156,14 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } + if (d.HasMember("two decks")) { + if (d["two decks"].IsBool()) { + sr.two_decks = d["two decks"].GetBool(); + } else { + json_helper::json_parse_err("[two decks] must be a boolean"); + } + } + if (d.HasMember("hole")) { if (d["hole"].IsBool()) { sr.hole = d["hole"].GetBool(); @@ -195,6 +204,18 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } + if (d.HasMember("stock deal type")) { + if (d["stock deal type"].IsString()) { + if(string(d["stock deal type"].GetString()) == "waste") { + sr.stock_deal_t = sdt::WASTE; + } else { + sr.stock_deal_t = sdt::TABLEAU_PILES; + } + } else { + json_helper::json_parse_err("[stock deal type] must be a string"); + } + } + if (d.HasMember("reserve size")) { if (d["reserve size"].IsInt()) { sr.reserve_size = static_cast(d["reserve size"].GetInt()); -- cgit v1.2.1 From b2edd1a2352ac4ded59f4c91579ab80eeba4db96 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 9 Jan 2018 08:54:12 +0000 Subject: Add game state setup for stock-tableau & 2 deck --- src/main/game/game_state.cpp | 53 +++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index ee02889..76e6488 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -14,14 +14,14 @@ #include "game_state.h" #include "../input-output/input/json-parsing/deal_parser.h" #include "../input-output/output/state_printer.h" +#include "sol_rules.h" using namespace std; using namespace rapidjson; using namespace boost; -typedef sol_rules::build_order ord; typedef sol_rules::build_policy pol; -typedef sol_rules::spaces_policy s_pol; +typedef sol_rules::stock_deal_type sdt; ////////////////// @@ -44,7 +44,7 @@ game_state::game_state(const sol_rules& s_rules) : // If there are foundation piles, creates the relevant pile vectors if (rules.foundations) { - for (uint8_t i = 0; i < 4; i++) { + for (uint8_t i = 0; i < 4*(rules.two_decks ? 2:1); i++) { piles.emplace_back(); foundations.push_back(static_cast(piles.size() - 1)); } @@ -58,12 +58,16 @@ game_state::game_state(const sol_rules& s_rules) : } } - // If there is a stock & waste, creates piles + // If there is a stock, creates a pile if (rules.stock_size > 0) { piles.emplace_back(); stock = static_cast(piles.size() - 1); - piles.emplace_back(); - waste = static_cast(piles.size() - 1); + + // If the stock deals to a waste, create the waste + if (rules.stock_deal_t == sdt::WASTE) { + piles.emplace_back(); + waste = static_cast(piles.size() - 1); + } } // If there is a reserve, creates piles @@ -92,7 +96,7 @@ game_state::game_state(const sol_rules& s_rules, const Document& doc) : // Constructs an initial game state from a seed game_state::game_state(const sol_rules& s_rules, int seed) : game_state(s_rules) { - vector deck = gen_shuffled_deck(seed, rules.max_rank); + vector deck = gen_shuffled_deck(seed, rules.max_rank, rules.two_decks); // If there is a hole, moves the ace of spades to it if (rules.hole) { @@ -102,8 +106,8 @@ game_state::game_state(const sol_rules& s_rules, int seed) : // If the foundations begin filled, then fills them if (rules.foundations_init_card) { - for (uint8_t f_idx = 0; f_idx < 4; f_idx++) { - card c = card(card::to_suit(f_idx), 1); + for (uint8_t f_idx = 0; f_idx < 4*(rules.two_decks ? 2:1); f_idx++) { + card c = card(card::to_suit(f_idx % uint8_t(4)), 1); deck.erase(find(begin(deck), end(deck), c)); piles[foundations[f_idx]].place(c); @@ -156,14 +160,16 @@ game_state::game_state(const sol_rules& s_rules, int seed) : } // Generates a randomly ordered vector of cards -vector game_state::gen_shuffled_deck(int seed, int max_rank = 13) { +vector game_state::gen_shuffled_deck(int seed, int max_rank, + bool two_decks) { vector values; vector v_ptrs; for (int i = 0; i < max_rank * 4; i++) { values.emplace_back(i); + if (two_decks) values.emplace_back(i); } - for (int i = 0; i < max_rank * 4; i++) { - v_ptrs.emplace_back(&values[i]); + for (auto& v : values) { + v_ptrs.emplace_back(&v); } // Randomly shuffle the pointers @@ -205,8 +211,16 @@ game_state::move::move(pile_ref f, pile_ref t, pile::size_type i) ///////////////////////// 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 = tableau_piles.front(); + tab_pr < tableau_piles.front() + m.count; + tab_pr++) { + piles[tab_pr].place(piles[stock].take()); + } + } // If this is not a built-pile move - if (m.count == 1) { + else if (m.count == 1) { piles[m.to].place(piles[m.from].take()); } else { // Adds the cards to the 'to' pile @@ -222,8 +236,16 @@ void game_state::make_move(const move m) { } void game_state::undo_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 = tableau_piles.front() + m.count; + tab_pr-- > tableau_piles.front(); + ) { + piles[stock].place(piles[tab_pr].take()); + } + } // If this is not a built-pile move - if (m.count == 1) { + else if (m.count == 1) { piles[m.from].place(piles[m.to].take()); } else { // Adds the cards to the 'from' pile @@ -245,7 +267,8 @@ void game_state::undo_move(const move m) { bool game_state::is_solved() const { if (rules.hole) { - return piles[hole].size() == rules.max_rank * 4; + return piles[hole].size() + == rules.max_rank * 4 * (rules.two_decks ? 2 : 1); } else { for (auto f : foundations) { if (piles[f].size() != rules.max_rank) { -- cgit v1.2.1 From 077c56fbee736d9c597d9735229a1f625be46fe0 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 9 Jan 2018 08:56:01 +0000 Subject: Add state printing for stock-tableau deal --- src/main/input-output/output/state_printer.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index cf9f09e..bfb6c7d 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -6,6 +6,8 @@ using namespace std; +typedef sol_rules::stock_deal_type sdt; + ostream& state_printer::print(ostream& stream, const game_state& gs) { if (gs.rules.foundations) { state_printer::print_header(stream, "Foundations"); @@ -28,8 +30,13 @@ ostream& state_printer::print(ostream& stream, const game_state& gs) { state_printer::print_piles(stream, gs.reserve, gs); } if (gs.rules.stock_size > 0) { - state_printer::print_header(stream, "Stock | Waste"); - state_printer::print_piles(stream, {gs.stock, gs.waste}, gs); + if (gs.rules.stock_deal_t == sdt::WASTE) { + state_printer::print_header(stream, "Stock | Waste"); + state_printer::print_piles(stream, {gs.stock, gs.waste}, gs); + } else { + state_printer::print_header(stream, "Stock"); + state_printer::print_piles(stream, {gs.stock}, gs); + } } if (gs.rules.hole) { state_printer::print_header(stream, "Hole Card"); -- cgit v1.2.1 From 89bf6ecb16fa6cdf66707f0590a0c1370bf19c4d Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 9 Jan 2018 08:56:24 +0000 Subject: Add logic for stock-tableau deal & 2 deck --- src/main/game/game_state.h | 3 ++- src/main/game/game_state.legal_moves.cpp | 26 +++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index f43348f..8f7bf57 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -56,12 +56,13 @@ public: friend std::ostream& operator<< (std::ostream&, const game_state&); private: - static std::vector gen_shuffled_deck(int, int); + static std::vector gen_shuffled_deck(int, int, bool); // Private constructor explicit game_state(const sol_rules&); // Used in get_legal_moves() + move get_stock_tableau_move() const; bool is_valid_tableau_move(pile_ref, pile_ref) const; bool is_valid_foundations_move(pile_ref, pile_ref) const; bool is_valid_hole_move(pile_ref) const; diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp index 56c1f06..cbc052a 100644 --- a/src/main/game/game_state.legal_moves.cpp +++ b/src/main/game/game_state.legal_moves.cpp @@ -9,19 +9,27 @@ using namespace std; typedef sol_rules::build_order ord; 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 { // The next legal moves vector moves; + // If we have a stock that can deal to the tableau piles, add this move + if (rules.stock_deal_t == sdt::TABLEAU_PILES && !piles[stock].empty()) { + moves.emplace_back(get_stock_tableau_move()); + } + // 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; - // Stock cards can only be moved to the waste + // Stock cards can only be moved to the waste (assuming there is a waste) if (stock != PILE_REF_MAX && rem_ref == stock) { - moves.emplace_back(stock, waste); + if (waste != PILE_REF_MAX) { + moves.emplace_back(stock, waste); + } continue; } @@ -64,6 +72,17 @@ vector game_state::get_legal_moves() const { return moves; } +// This is a special kind of move which the solver handles differently. Hence +// the arbitrary second param. We must supply the number of stock cards +// that we will deal, so that the move can be undone in backtracking +game_state::move game_state::get_stock_tableau_move() const { + pile::size_type stock_moves = + piles[stock].size() >= tableau_piles.size() + ? pile::size_type(tableau_piles.size()) + : piles[stock].size(); + return {stock, PILE_REF_MAX, stock_moves}; +} + // If a tableau pile is empty and the space policy is // no-build, do not add. Otherwise any card with the right // suit/rank can be moved to a tableau @@ -111,7 +130,8 @@ bool game_state::is_valid_foundations_move(const pile_ref rem_ref, card rem_c = piles[rem_ref].top_card(); // Checks violation of same suit policy - if (rem_c.get_suit() != card::to_suit(add_ref - foundations.front())) { + uint8_t suit_idx = (add_ref - foundations.front()) % uint8_t(4); + if (rem_c.get_suit() != card::to_suit(suit_idx)) { return false; } -- cgit v1.2.1