diff options
38 files changed, 531 insertions, 1136 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index cc4200c..aa8eddc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,7 +69,6 @@ set(sources_test src/test/unit_tests/global_cache_test.cpp src/test/unit_tests/foundations_dominance_test.cpp src/test/unit_tests/deal_parser_test.cpp - src/test/unit_tests/rules_parser_test.cpp src/test/unit_tests/card_test.cpp src/test/unit_tests/pile_test.cpp src/test/unit_tests/legal_move_gen_test.cpp diff --git a/benchmark.py b/benchmark.py deleted file mode 100644 index ce50b49..0000000 --- a/benchmark.py +++ /dev/null @@ -1,238 +0,0 @@ -import json -import math -import os -import sys -import atexit -from subprocess import run, TimeoutExpired, CalledProcessError, DEVNULL -from abc import ABCMeta, abstractmethod -from prettytable import PrettyTable - -tempRulesFilename = "temp_rules.json" -timeoutsLimit = 4 -timeoutSeconds = 1 - -runtype = "release" -if (len(sys.argv) >= 2 - and (sys.argv[1] == "--debug" or sys.argv[1] == "-d")): - runtype = "debug" - print("Running in debug mode...") - -flag = "" -if (len(sys.argv) >= 2 - and (sys.argv[1] == "--no-card-symmetry" or sys.argv[1] == "-cs")): - flag = "-no-card-symmetry" - print("Running in no-card-symmetry mode...") - -if (len(sys.argv) >= 2 - and (sys.argv[1] == "--no-pile-symmetry" or sys.argv[1] == "-ps")): - flag = "-no-pile-symmetry" - print("Running in no-pile-symmetry mode...") - -if (len(sys.argv) >= 2 - and (sys.argv[1] == "--no-symmetry" or sys.argv[1] == "-ps")): - flag = "-no-symmetry" - print("Running in no-symmetry mode...") - -if (len(sys.argv) >= 2 - and (sys.argv[1] == "--no-reduced-state" or sys.argv[1] == "-rc")): - flag = "-no-reduced-state" - print("Running in no-reduced-state mode...") - -if (len(sys.argv) >= 2 - and (sys.argv[1] == "--no-auto-foundations" or sys.argv[1] == "-af")): - flag = "-no-auto-foundations" - print("Running in no-auto-foundations mode...") - -class SolitaireRulesGen(metaclass=ABCMeta): - - def __init__(self, name): - self.name = name - self.baseJson = json.load(open('resources/rules-presets/' + name + '.json')) - - def genRules(self, level): - self.alterFieldsToChange(level) - with open(tempRulesFilename, "w") as tempOut: - json.dump(self.baseJson, tempOut) - - @abstractmethod - def alterFieldsToChange(self, level): - pass - - def __str__(self): - return json.dumps(self.baseJson, indent=4) - -class SpanishPatienceRulesGen(SolitaireRulesGen): - - def __init__(self): - super().__init__("spanish-patience") - - def alterFieldsToChange(self, level): - self.baseJson["max rank"] = level - self.baseJson["tableau piles"]["count"] = level - -class FreeCellRulesGen(SolitaireRulesGen): - - def __init__(self): - super().__init__("free-cell") - - def alterFieldsToChange(self, level): - self.baseJson["max rank"] = level - self.baseJson["tableau piles"]["count"] = math.ceil(level * 0.61) - self.baseJson["cells"] = math.ceil(level * 0.3) - -class BlackHoleRulesGen(SolitaireRulesGen): - - def __init__(self): - super().__init__("black-hole") - - def alterFieldsToChange(self, level): - self.baseJson["max rank"] = level - self.baseJson["tableau piles"]["count"] = math.ceil((4*level - 1)/3) - -class BakersDozenRulesGen(SolitaireRulesGen): - - def __init__(self): - super().__init__("bakers-dozen") - - def alterFieldsToChange(self, level): - self.baseJson["max rank"] = level - self.baseJson["tableau piles"]["count"] = level - -class FortunesFavorRulesGen(SolitaireRulesGen): - - def __init__(self): - super().__init__("fortunes-favor") - - def alterFieldsToChange(self, level): - # Because aces start up we must skip lv 1 - if (level == 1): level = 2; - self.baseJson["max rank"] = level - self.baseJson["tableau piles"]["count"] = math.ceil(12*level/13) - self.baseJson["stock size"] = level * 4 - 4 - self.baseJson["tableau piles"]["count"] - -class FlowerGardenRulesGen(SolitaireRulesGen): - - def __init__(self): - super().__init__("flower-garden") - - def alterFieldsToChange(self, level): - self.baseJson["max rank"] = level - self.baseJson["tableau piles"]["count"] = math.ceil(6*level/13) - self.baseJson["reserve size"] = math.ceil(16*level/13) - -class CanfieldRulesGen(SolitaireRulesGen): - - def __init__(self): - super().__init__("canfield") - - def alterFieldsToChange(self, level): - self.baseJson["max rank"] = level - cardsNotInTableau = (level - 1) * 4 - reserveSize = math.ceil(cardsNotInTableau * 0.7) - stockSize = (level - 1) * 4 - reserveSize - self.baseJson["reserve size"] = reserveSize - self.baseJson["stock size"] = stockSize - -class SomersetRulesGen(SolitaireRulesGen): - - def __init__(self): - super().__init__("somerset") - - def alterFieldsToChange(self, level): - self.baseJson["max rank"] = level - self.baseJson["tableau piles"]["count"] = math.ceil(10*level/13) - -class AlphaStarRulesGen(SolitaireRulesGen): - - def __init__(self): - super().__init__("alpha-star") - - def alterFieldsToChange(self, level): - self.baseJson["max rank"] = level - self.baseJson["tableau piles"]["count"] = math.ceil(12*level/13) - -class SpiderRulesGen(SolitaireRulesGen): - - def __init__(self): - super().__init__("spider") - - def alterFieldsToChange(self, level): - self.baseJson["max rank"] = level - self.baseJson["tableau piles"]["count"] = math.ceil(10*level/13) - self.baseJson["stock size"] = math.ceil(50*level/13) - -def cleanup(): - os.remove(tempRulesFilename) - run("pkill solvitaire", shell=True) - -atexit.register(cleanup) - -summaryResults = [] - -# Loops through each canonical solitaire -for rulesGen in [ - BlackHoleRulesGen() - ,SpanishPatienceRulesGen() - ,FreeCellRulesGen() - ,SomersetRulesGen() - ,FortunesFavorRulesGen() - ,FlowerGardenRulesGen() - ,CanfieldRulesGen() - ,BakersDozenRulesGen() - ,AlphaStarRulesGen() - ,SpiderRulesGen() -]: - - # Loops through the levels - for level in range(1, 14): - - # Calls the solitaire rules generator function with the level - rulesGen.genRules(level) - - # Runs the solver with the rules and 20 random seeds. If more than 4 - # don't return in 5 seconds, output that this is the max level - timeouts = 0 - for attempt in range(20): - try: - run("./cmake-build-" + runtype + "/bin/solvitaire" + flag - + " --custom-rules " + tempRulesFilename + " --random " - + str(attempt + 1), - shell=True, - check=True, - stdout=DEVNULL, - stderr=DEVNULL, - timeout=timeoutSeconds) - except TimeoutExpired: - timeouts += 1 - except CalledProcessError: - print("Error running " + rulesGen.name + " at level " - + str(level) + " with seed " + str(attempt + 1) + "!") - print(str(rulesGen) + "\n") - sys.exit() - except: - print("Unknown error in script") - sys.exit() - - run("pkill solvitaire", shell=True) - - if timeouts > timeoutsLimit: - break - - if timeouts > timeoutsLimit: - print("Could not solve " + rulesGen.name + " at level " + str(level) - + " within time constraint\n") - print("Game description at level " + str(level) + ":") - print(str(rulesGen) + "\n") - summaryResults.append((rulesGen.name, level-1)) - break - else: - print("Solved " + rulesGen.name + " at level " + str(level) - + " ...") - if level == 13: - summaryResults.append((rulesGen.name, level)) - -# Prints results summary table -t = PrettyTable(['Level', 'Solitaire']) -for (name, level) in summaryResults: - t.add_row([level, name]) -print(t) diff --git a/resources/rules-presets/alpha-star.json b/resources/rules-presets/alpha-star.json deleted file mode 100644 index fde3ac4..0000000 --- a/resources/rules-presets/alpha-star.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "tableau piles": { - "count": 12, - "build policy": "same-suit", - "move built group": true - }, - "foundations initial card": true -}
\ No newline at end of file diff --git a/resources/rules-presets/bakers-dozen.json b/resources/rules-presets/bakers-dozen.json deleted file mode 100644 index 4f63936..0000000 --- a/resources/rules-presets/bakers-dozen.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "tableau piles": { - "count": 13, - "spaces policy": "no-build" - }, - "foundations removable": true -}
\ No newline at end of file diff --git a/resources/rules-presets/black-hole.json b/resources/rules-presets/black-hole.json deleted file mode 100644 index daab27c..0000000 --- a/resources/rules-presets/black-hole.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "tableau piles": { - "count": 17, - "build policy": "no-build" - }, - "hole": true, - "foundations": false -}
\ No newline at end of file diff --git a/resources/rules-presets/canfield.json b/resources/rules-presets/canfield.json deleted file mode 100644 index 5f36a72..0000000 --- a/resources/rules-presets/canfield.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "tableau piles": { - "count": 4, - "build policy": "red-black" - }, - "stock size": 34, - "reserve size": 14, - "reserve stacked": true -} diff --git a/resources/rules-presets/default.json b/resources/rules-presets/default.json deleted file mode 100644 index 47032d8..0000000 --- a/resources/rules-presets/default.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "tableau piles": { - "count": 8, - "build order": "descending", - "build policy": "any-suit", - "spaces policy": "any", - "diagonal deal": false, - "move built group": false, - "move built group policy": "same-as-build" - }, - "max rank": 13, - "two decks": false, - "hole": false, - "foundations": true, - "foundations initial card": false, - "foundations removable": false, - "foundations complete piles": false, - "cells": 0, - "stock size": 0, - "stock deal type": "waste", - "reserve size": 0, - "reserve stacked": false -}
\ No newline at end of file diff --git a/resources/rules-presets/flower-garden.json b/resources/rules-presets/flower-garden.json deleted file mode 100644 index 92906fb..0000000 --- a/resources/rules-presets/flower-garden.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "tableau piles": { - "count": 6 - }, - "reserve size": 16, - "foundations removable": true -} diff --git a/resources/rules-presets/fortunes-favor.json b/resources/rules-presets/fortunes-favor.json deleted file mode 100644 index b791561..0000000 --- a/resources/rules-presets/fortunes-favor.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "tableau piles": { - "count": 12, - "build policy": "same-suit" - }, - "foundations initial card": true, - "stock size": 36 -}
\ No newline at end of file diff --git a/resources/rules-presets/free-cell.json b/resources/rules-presets/free-cell.json deleted file mode 100644 index 12c1bdf..0000000 --- a/resources/rules-presets/free-cell.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "tableau piles": { - "build policy": "red-black" - }, - "cells": 4 -}
\ No newline at end of file diff --git a/resources/rules-presets/input-file-schema.json b/resources/rules-presets/input-file-schema.json deleted file mode 100644 index 1b55e4f..0000000 --- a/resources/rules-presets/input-file-schema.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - "$schema": "http://json-schema.org/draft-04/schema#", - "definitions": { - "card": { - "type": "string", - "pattern": "^(?i)([A|[1-9]|1[0-3]|J|Q|K])([C|D|H|S])$" - } - }, - "type": "object", - "properties": { - "tableau piles": { - "type": "array", - "items": { - "type": "array", - "items": { - "$ref": "#/definitions/card" - } - } - }, - "hole card": { - "$ref": "#/definitions/card" - } - }, - "additionalProperties": false, - "anyOf": [ - { - "required": [ - "tableau piles" - ] - } - ] -}
\ No newline at end of file diff --git a/resources/rules-presets/simple-alpha-star.json b/resources/rules-presets/simple-alpha-star.json deleted file mode 100644 index c80a82b..0000000 --- a/resources/rules-presets/simple-alpha-star.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "tableau piles": { - "count": 3, - "build policy": "same-suit", - "move built group": true - }, - "max rank": 6, - "foundations initial card": true -}
\ No newline at end of file diff --git a/resources/rules-presets/simple-bakers-dozen.json b/resources/rules-presets/simple-bakers-dozen.json deleted file mode 100644 index 3f038fe..0000000 --- a/resources/rules-presets/simple-bakers-dozen.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "tableau piles": { - "count": 3, - "spaces policy": "no-build" - }, - "max rank": 3, - "foundations removable": true -}
\ No newline at end of file diff --git a/resources/rules-presets/simple-black-hole.json b/resources/rules-presets/simple-black-hole.json deleted file mode 100644 index 16ef52d..0000000 --- a/resources/rules-presets/simple-black-hole.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "tableau piles": { - "count": 4, - "build policy": "no-build" - }, - "max rank": 5, - "hole": true, - "foundations": false -} diff --git a/resources/rules-presets/simple-canfield.json b/resources/rules-presets/simple-canfield.json deleted file mode 100644 index 62a73c3..0000000 --- a/resources/rules-presets/simple-canfield.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "tableau piles": { - "count": 3, - "build policy": "red-black" - }, - "max rank": 3, - "stock size": 1, - "reserve size": 2, - "reserve stacked": true -}
\ No newline at end of file diff --git a/resources/rules-presets/simple-flower-garden.json b/resources/rules-presets/simple-flower-garden.json deleted file mode 100644 index eb7916c..0000000 --- a/resources/rules-presets/simple-flower-garden.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "tableau piles": { - "count": 3 - }, - "max rank": 3, - "reserve size": 3, - "foundations removable": true -} diff --git a/resources/rules-presets/simple-fortunes-favor.json b/resources/rules-presets/simple-fortunes-favor.json deleted file mode 100644 index 6f3cf61..0000000 --- a/resources/rules-presets/simple-fortunes-favor.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "tableau piles": { - "count": 3, - "build policy": "same-suit" - }, - "max rank": 4, - "foundations initial card": true, - "stock size": 3 -}
\ No newline at end of file diff --git a/resources/rules-presets/simple-free-cell.json b/resources/rules-presets/simple-free-cell.json deleted file mode 100644 index 226daf0..0000000 --- a/resources/rules-presets/simple-free-cell.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "tableau piles": { - "count": 4, - "build policy": "red-black" - }, - "max rank": 4, - "cells": 1 -}
\ No newline at end of file diff --git a/resources/rules-presets/simple-somerset.json b/resources/rules-presets/simple-somerset.json deleted file mode 100644 index 555fddf..0000000 --- a/resources/rules-presets/simple-somerset.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "tableau piles": { - "count": 4, - "build policy": "red-black", - "diagonal deal": true - }, - "max rank": 2, - "foundations removable": true -}
\ No newline at end of file diff --git a/resources/rules-presets/simple-spanish-patience.json b/resources/rules-presets/simple-spanish-patience.json deleted file mode 100644 index 2b27cb5..0000000 --- a/resources/rules-presets/simple-spanish-patience.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "tableau piles": { - "count": 3 - }, - "max rank": 2, - "foundations removable": true -}
\ No newline at end of file diff --git a/resources/rules-presets/simple-spider.json b/resources/rules-presets/simple-spider.json deleted file mode 100644 index 5982441..0000000 --- a/resources/rules-presets/simple-spider.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "tableau piles": { - "count": 4, - "build policy": "any-suit", - "move built group": true, - "move built group policy": "same-suit" - }, - "foundations complete piles": true, - "two decks": true, - "max rank": 3, - "stock size": 12, - "stock deal type": "tableau piles" -}
\ No newline at end of file diff --git a/resources/rules-presets/somerset.json b/resources/rules-presets/somerset.json deleted file mode 100644 index fc3bdad..0000000 --- a/resources/rules-presets/somerset.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "tableau piles": { - "count": 10, - "build policy": "red-black", - "diagonal deal": true - }, - "foundations removable": true -}
\ No newline at end of file diff --git a/resources/rules-presets/spanish-patience.json b/resources/rules-presets/spanish-patience.json deleted file mode 100644 index 32c1dab..0000000 --- a/resources/rules-presets/spanish-patience.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "tableau piles": { - "count": 13 - }, - "foundations removable": true -}
\ No newline at end of file diff --git a/resources/rules-presets/spider.json b/resources/rules-presets/spider.json deleted file mode 100644 index 6d08fba..0000000 --- a/resources/rules-presets/spider.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "tableau piles": { - "count": 10, - "build policy": "any-suit", - "move built group": true, - "move built group policy": "same-suit" - }, - "foundations complete piles": true, - "two decks": true, - "stock size": 50, - "stock deal type": "tableau piles" -}
\ No newline at end of file diff --git a/src/main/game/search-state/game_state.auto_foundation_moves.cpp b/src/main/game/search-state/game_state.auto_foundation_moves.cpp index 43f8911..b74948a 100644 --- a/src/main/game/search-state/game_state.auto_foundation_moves.cpp +++ b/src/main/game/search-state/game_state.auto_foundation_moves.cpp @@ -12,7 +12,7 @@ using std::max; bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const { - if (rules.foundations_comp_piles) + if (rules.foundations_only_comp_piles) return false; else if ( stream_opts == sos::AUTO_FOUNDATIONS || stream_opts == sos::BOTH @@ -65,7 +65,7 @@ bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const { boost::optional<move> game_state::get_dominance_move() const { #ifndef NO_AUTO_FOUNDATIONS // If there are 2 decks or no foundations, return - if (!rules.foundations || rules.two_decks) + if (!rules.foundations_present || rules.two_decks) return boost::none; // Cycles through the piles and sees if any cards can be automatically moved @@ -132,7 +132,7 @@ bool game_state::dominance_blocks_foundation_move(pile::ref target_pile) { // Decides which 'auto foundation' booleans should be 'true' currently void game_state::update_auto_foundation_moves(pile::ref target_pile) { // If there are no foundations, or this is not a foundation move, do nothing - if (!rules.foundations + if (!rules.foundations_present || target_pile < foundations.front() || target_pile > foundations.back()) { return; diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index a1d60bf..169c96e 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -58,7 +58,7 @@ game_state::game_state(const sol_rules& s_rules, streamliner_options stream_opts } // If there are foundation piles, creates the relevant pile vectors - if (rules.foundations) { + if (rules.foundations_present) { for (uint8_t i = 0; i < 4*(rules.two_decks ? 2:1); i++) { piles.emplace_back(); foundations.push_back(static_cast<pile::ref>(piles.size() - 1)); @@ -389,9 +389,8 @@ void game_state::make_stock_k_plus_move(const move m) { // Index of the card to be moved auto count = m.count % (piles[stock].size() + piles[waste].size()); - for (int i = 0; i <= count; i++) { - if (i < count) place_card(waste, take_card(stock)); - else place_card(m.to, take_card(stock)); + for (int i = 0; i < count; i++) { + place_card(waste, take_card(stock)); if (rules.stock_redeal) { // If the stock is empty, flips it @@ -402,6 +401,11 @@ void game_state::make_stock_k_plus_move(const move m) { } } + place_card(m.to, take_card(stock)); + if (rules.stock_redeal && piles[stock].empty() && count != 0) + while (!piles[waste].empty()) + place_card(stock, take_card(waste)); + #ifndef NDEBUG auto sz_after = piles[stock].size() + piles[waste].size(); assert(sz_before == sz_after + 1); @@ -420,7 +424,12 @@ void game_state::undo_stock_k_plus_move(move m) { auto count = m.count % (piles[stock].size() + piles[waste].size() + 1); - for (int i = count; i >= 0; i--) { + if (rules.stock_redeal && piles[waste].empty() && count != 0) + while (!piles[stock].empty()) + place_card(waste, take_card(stock)); + place_card(stock, take_card(m.to)); + + for (int i = 0; i < count; i++) { if (rules.stock_redeal) { // If the waste is empty, flips it // (invariant: no search state has an empty stock if rules.stock_size > 0 && rules.stock_redeal) @@ -429,8 +438,7 @@ void game_state::undo_stock_k_plus_move(move m) { place_card(waste, take_card(stock)); } - if (i < count) place_card(stock, take_card(waste)); - else place_card(stock, take_card(m.to )); + place_card(stock, take_card(waste)); } #ifndef NDEBUG @@ -514,7 +522,7 @@ bool game_state::is_solved() const { solved = piles[hole].size() == rules.max_rank * 4 * (rules.two_decks ? 2 : 1); } else { - assert(rules.foundations); + assert(rules.foundations_present); for (auto f : foundations) { if (piles[f].size() != rules.max_rank) { solved = false; diff --git a/src/main/game/search-state/game_state.legal_moves.cpp b/src/main/game/search-state/game_state.legal_moves.cpp index 7dd75d8..f68df0b 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -119,7 +119,7 @@ vector<move> game_state::get_legal_moves(move parent_move) { } // Tableau / cells / reserve / stock-waste to hole / foundation moves - if (rules.hole || (rules.foundations && !rules.foundations_comp_piles)) { + if (rules.hole || (rules.foundations_present && !rules.foundations_only_comp_piles)) { // Stock if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty()) add_stock_to_hole_foundation_moves(moves); @@ -139,7 +139,7 @@ vector<move> game_state::get_legal_moves(move parent_move) { } } - if (rules.foundations_comp_piles) // i.e. Spider-type winning condition + if (rules.foundations_only_comp_piles) // i.e. Spider-type winning condition add_foundation_complete_piles_moves(moves); if (rules.tableau_pile_count > 0 && rules.face_up != fu::ALL) @@ -190,6 +190,7 @@ set<pile::size_type> game_state::generate_stock_moves_to_check() const { for (auto i = piles[stock].size() - 1 + rules.stock_deal_count; i < piles[stock].size() + stock_waste_sz - 1; i += rules.stock_deal_count) { + if (i == stock_waste_sz - 1) continue; // No need to add top of waste card as it is a regular move stock_moves_to_check.insert(static_cast<pile::size_type>(i % stock_waste_sz)); } } @@ -308,7 +309,7 @@ bool game_state::is_next_tableau_card(card a, card b) const { bool game_state::is_valid_foundations_move(const pile::ref rem_ref, const pile::ref add_ref) const { - if (rem_ref == add_ref || rules.foundations_comp_piles) return false; + if (rem_ref == add_ref || rules.foundations_only_comp_piles) return false; return is_valid_foundations_move(piles[rem_ref].top_card(), add_ref); } diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index 4cef7c1..d50d852 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -19,9 +19,9 @@ sol_rules::sol_rules() : two_decks(false), max_rank(13), hole(false), - foundations(false), + foundations_present(false), foundations_init_card(false), - foundations_comp_piles(false), + foundations_only_comp_piles(false), diagonal_deal(false), cells(0), stock_size(0), diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index a7b9ae6..8075821 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -41,10 +41,10 @@ struct sol_rules { bool two_decks; uint8_t max_rank; bool hole; - bool foundations; + bool foundations_present; bool foundations_init_card; bool foundations_removable; - bool foundations_comp_piles; + bool foundations_only_comp_piles; bool diagonal_deal; uint8_t cells; uint8_t stock_size; 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 bc31cbc..5a6dff6 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -52,7 +52,7 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { } // If the foundations begin with cards in them, fill them - if (gs.rules.foundations) { + if (gs.rules.foundations_present) { bool supplied_foundations = parse_foundations(gs, doc); if (!supplied_foundations && gs.rules.foundations_init_card) { 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 29e755b..c47ed2b 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -186,22 +186,42 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } - if (d.HasMember("max rank")) { - if (d["max rank"].IsInt()) { - sr.max_rank = static_cast<card::rank_t>(d["max rank"].GetInt()); - if (sr.max_rank > 13) { - json_helper::json_parse_err("[max rank] must be a valid rank"); + if (d.HasMember("foundations")) { + if (d["foundations"].IsObject()) { + if (d["foundations"].HasMember("present")) { + if (d["foundations"]["present"].IsBool()) { + sr.foundations_present = d["foundations"]["present"].GetBool(); + } else { + json_helper::json_parse_err("[foundations][present] must be a boolean"); + } + } + + if (d["foundations"].HasMember("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["foundations"].HasMember("removable")) { + if (d["foundations"]["removable"].IsBool()) { + sr.foundations_removable = d["foundations"]["removable"].GetBool(); + } else { + json_helper::json_parse_err("[foundations][removable] must be a boolean"); + } + } + + if (d["foundations"].HasMember("complete piles")) { + if (d["foundations"]["complete piles"].IsBool()) { + sr.foundations_only_comp_piles = d["foundations"]["complete piles"].GetBool(); + } else { + json_helper::json_parse_err("[foundations][complete piles] must be a boolean"); + } } - } else { - json_helper::json_parse_err("[max rank] must be an integer"); - } - } - 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"); + json_helper::json_parse_err("[foundations] must be an object"); } } @@ -213,38 +233,6 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } - 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("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("foundations removable")) { - if (d["foundations removable"].IsBool()) { - sr.foundations_removable = d["foundations removable"].GetBool(); - } else { - json_helper::json_parse_err("[foundations removable] must be a boolean"); - } - } - - if (d.HasMember("foundations complete piles")) { - if (d["foundations complete piles"].IsBool()) { - sr.foundations_comp_piles = d["foundations complete piles"].GetBool(); - } else { - json_helper::json_parse_err("[foundations complete piles] must be a boolean"); - } - } - if (d.HasMember("cells")) { if (d["cells"].IsInt()) { sr.cells = static_cast<uint8_t>(d["cells"].GetInt()); @@ -253,61 +241,92 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } - if (d.HasMember("stock size")) { - if (d["stock size"].IsInt()) { - sr.stock_size = static_cast<uint8_t>(d["stock size"].GetInt()); - } else { - json_helper::json_parse_err("[stock size] must be an integer"); - } - } + if (d.HasMember("stock")) { + if (d["stock"].IsObject()) { + if (d["stock"].HasMember("size")) { + if (d["stock"]["size"].IsInt()) { + sr.stock_size = static_cast<uint8_t>(d["stock"]["size"].GetInt()); + } else { + json_helper::json_parse_err("[stock][size] must be an integer"); + } + } - 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; + if (d["stock"].HasMember("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["stock"].HasMember("deal count")) { + if (d["stock"]["deal count"].IsInt()) { + sr.stock_deal_count = static_cast<uint8_t>(d["stock"]["deal count"].GetInt()); + } else { + json_helper::json_parse_err("[stock][deal count] must be an integer"); + } } - } else { - json_helper::json_parse_err("[stock deal type] must be a string"); - } - } - if (d.HasMember("stock deal count")) { - if (d["stock deal count"].IsInt()) { - sr.stock_deal_count = static_cast<uint8_t>(d["stock deal count"].GetInt()); + if (d["stock"].HasMember("redeal")) { + if (d["stock"]["redeal"].IsBool()) { + sr.stock_redeal = static_cast<uint8_t>(d["stock"]["redeal"].GetBool()); + } else { + json_helper::json_parse_err("[stock][redeal] must be a boolean"); + } + } } else { - json_helper::json_parse_err("[stock deal count] must be an integer"); + json_helper::json_parse_err("[stock] must be an object"); } } - if (d.HasMember("stock redeal")) { - if (d["stock redeal"].IsBool()) { - sr.stock_redeal = static_cast<uint8_t>(d["stock redeal"].GetBool()); + if (d.HasMember("reserve")) { + if (d["reserve"].IsObject()) { + if (d["reserve"].HasMember("size")) { + if (d["reserve"]["size"].IsInt()) { + sr.reserve_size = static_cast<uint8_t>(d["reserve"]["size"].GetInt()); + } else { + json_helper::json_parse_err("[size] must be an integer"); + } + } + + if (d["reserve"].HasMember("stacked")) { + if (d["reserve"]["stacked"].IsBool()) { + sr.reserve_stacked = d["reserve"]["stacked"].GetBool(); + } else { + json_helper::json_parse_err("[stacked] must be a boolean"); + } + } } else { - json_helper::json_parse_err("[stock redeal] must be a boolean"); + json_helper::json_parse_err("[reserve] must be an object"); } } - if (d.HasMember("reserve size")) { - if (d["reserve size"].IsInt()) { - sr.reserve_size = static_cast<uint8_t>(d["reserve size"].GetInt()); + if (d.HasMember("max rank")) { + if (d["max rank"].IsInt()) { + sr.max_rank = static_cast<card::rank_t>(d["max rank"].GetInt()); + if (sr.max_rank > 13) { + json_helper::json_parse_err("[max rank] must be a valid rank"); + } } else { - json_helper::json_parse_err("[reserve size] must be an integer"); + json_helper::json_parse_err("[max rank] must be an integer"); } } - if (d.HasMember("reserve stacked")) { - if (d["reserve stacked"].IsBool()) { - sr.reserve_stacked = d["reserve stacked"].GetBool(); + if (d.HasMember("two decks")) { + if (d["two decks"].IsBool()) { + sr.two_decks = d["two decks"].GetBool(); } else { - json_helper::json_parse_err("[reserve stacked] must be a boolean"); + json_helper::json_parse_err("[two decks] must be a boolean"); } } int solution_types = 0; if (sr.hole) solution_types++; - if (sr.foundations) solution_types++; + if (sr.foundations_present) solution_types++; if (solution_types != 1) { json_helper::json_parse_err("one and only one of [hole] and [foundations] " "must be true"); @@ -329,36 +348,130 @@ string rules_parser::rules_schema_json() { { "$schema": "http://json-schema.org/draft-04/schema#", "description": "JSON Schema representing a generic solitaire game", - - "type": "object", "properties": { - + "type": "object", + "properties": { "tableau piles": { - "type": "object", "properties": { - "count": {"type": "integer", "minimum": 0}, - "build policy": {"type": "string", "enum": ["any-suit", "red-black", "same-suit", "no-build"]}, - "spaces policy": {"type": "string", "enum": ["any", "no-build", "kings"]}, - "diagonal deal": {"type": "boolean"}, - "move built group": {"type": "boolean"}, - "move built group policy": {"type": "string", "enum": ["same-as-build", "any-suit", "red-black", "same-suit", "no-build"]}, - "face up cards": {"type": "string", "enum": ["all", "top"]} - }, "additionalProperties": false + "type": "object", + "properties": { + "count": { + "type": "integer", + "minimum": 0 + }, + "build policy": { + "type": "string", + "enum": [ + "any-suit", + "red-black", + "same-suit", + "no-build" + ] + }, + "spaces policy": { + "type": "string", + "enum": [ + "any", + "no-build", + "kings" + ] + }, + "diagonal deal": { + "type": "boolean" + }, + "move built group": { + "type": "boolean" + }, + "move built group policy": { + "type": "string", + "enum": [ + "same-as-build", + "any-suit", + "red-black", + "same-suit", + "no-build" + ] + }, + "face up cards": { + "type": "string", + "enum": [ + "all", + "top" + ] + } + }, + "additionalProperties": false + }, + "foundations": { + "type": "object", + "properties": { + "present": { + "type": "boolean" + }, + "initial card": { + "type": "boolean" + }, + "removable": { + "type": "boolean" + }, + "only complete pile moves": { + "type": "boolean" + } + }, + "additionalProperties": false + }, + "hole": { + "type": "boolean" + }, + "cells": { + "type": "integer", + "minimum": 0 }, - "max rank": {"type": "integer", "minimum": 1, "maximum": 13}, - "two decks": {"type": "boolean"}, - "hole": {"type": "boolean"}, - "foundations": {"type": "boolean"}, - "foundations initial card": {"type": "boolean"}, - "foundations removable": {"type": "boolean"}, - "foundations complete piles": {"type": "boolean"}, - "cells": {"type": "integer", "minimum": 0}, - "stock size": {"type": "integer", "minimum": 0}, - "stock deal type": {"type": "string", "enum": ["waste", "tableau piles"]}, - "stock deal count": {"type": "integer", "minimum": 1}, - "stock redeal": {"type": "boolean"}, - "reserve size": {"type": "integer", "minimum": 0}, - "reserve stacked": {"type": "boolean"} - - }, "additionalProperties": false + "stock": { + "type": "object", + "properties": { + "size": { + "type": "integer", + "minimum": 0 + }, + "deal type": { + "type": "string", + "enum": [ + "waste", + "tableau piles" + ] + }, + "deal count": { + "type": "integer", + "minimum": 1 + }, + "redeal": { + "type": "boolean" + } + }, + "additionalProperties": false + }, + "reserve": { + "type": "object", + "properties": { + "size": { + "type": "integer", + "minimum": 0 + }, + "stacked": { + "type": "boolean" + } + }, + "additionalProperties": false + }, + "max rank": { + "type": "integer", + "minimum": 1, + "maximum": 13 + }, + "two decks": { + "type": "boolean" + } + }, + "additionalProperties": false } )"; return schema_json; diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index e540a51..fdd353a 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -29,20 +29,26 @@ map<string, string> sol_preset_types::mp = { "move built group policy": "same-as-build", "face up cards": "all" }, - "max rank": 13, - "two decks": false, + "foundations": { + "present": true, + "initial card": false, + "removable": false, + "only complete pile moves": false + }, "hole": false, - "foundations": true, - "foundations initial card": false, - "foundations removable": false, - "foundations complete piles": false, "cells": 0, - "stock size": 0, - "stock deal type": "waste", - "stock deal count": 1, - "stock redeal": false, - "reserve size": 0, - "reserve stacked": false + "stock": { + "size": 0, + "deal type": "waste", + "deal count": 1, + "redeal": false + }, + "reserve": { + "size": 0, + "stacked": false + }, + "max rank": 13, + "two decks": false } )" @@ -55,7 +61,9 @@ map<string, string> sol_preset_types::mp = { "tableau piles": { "count": 13 }, - "foundations removable": true + "foundations": { + "removable": true + } } )" @@ -68,8 +76,10 @@ map<string, string> sol_preset_types::mp = { "tableau piles": { "count": 3 }, - "max rank": 2, - "foundations removable": true + "foundations": { + "removable": true + }, + "max rank": 2 } )" @@ -96,8 +106,8 @@ map<string, string> sol_preset_types::mp = { "count": 4, "build policy": "red-black" }, - "max rank": 4, - "cells": 1 + "cells": 1, + "max rank": 4 } )" @@ -111,7 +121,9 @@ map<string, string> sol_preset_types::mp = { "count": 13, "spaces policy": "no-build" }, - "foundations removable": true + "foundations": { + "removable": true + } } )" @@ -125,8 +137,10 @@ map<string, string> sol_preset_types::mp = { "count": 3, "spaces policy": "no-build" }, - "max rank": 3, - "foundations removable": true + "foundations": { + "removable": true + }, + "max rank": 3 } )" @@ -140,8 +154,10 @@ map<string, string> sol_preset_types::mp = { "count": 17, "build policy": "no-build" }, - "hole": true, - "foundations": false + "foundations": { + "present": false + }, + "hole": true } )" @@ -155,9 +171,11 @@ map<string, string> sol_preset_types::mp = { "count": 4, "build policy": "no-build" }, - "max rank": 5, + "foundations": { + "present": false + }, "hole": true, - "foundations": false + "max rank": 5 } )" @@ -171,8 +189,12 @@ map<string, string> sol_preset_types::mp = { "count": 12, "build policy": "same-suit" }, - "foundations initial card": true, - "stock size": 36 + "foundations": { + "initial card": true + }, + "stock": { + "size": 36 + } } )" @@ -186,9 +208,13 @@ map<string, string> sol_preset_types::mp = { "count": 3, "build policy": "same-suit" }, - "max rank": 4, - "foundations initial card": true, - "stock size": 3 + "foundations": { + "initial card": true + }, + "stock": { + "size": 3 + }, + "max rank": 4 } )" @@ -201,8 +227,12 @@ map<string, string> sol_preset_types::mp = { "tableau piles": { "count": 6 }, - "reserve size": 16, - "foundations removable": true + "reserve": { + "size": 16 + }, + "foundations": { + "removable": true + } } )" @@ -215,9 +245,13 @@ map<string, string> sol_preset_types::mp = { "tableau piles": { "count": 3 }, - "max rank": 3, - "reserve size": 3, - "foundations removable": true + "reserve": { + "size": 3 + }, + "foundations": { + "removable": true + }, + "max rank": 3 } )" @@ -232,9 +266,13 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "move built group": true }, - "stock size": 34, - "reserve size": 14, - "reserve stacked": true + "stock": { + "size": 34 + }, + "reserve": { + "size": 14, + "stacked": true + } } )" @@ -249,10 +287,14 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "move built group": true }, - "max rank": 3, - "stock size": 1, - "reserve size": 2, - "reserve stacked": true + "stock": { + "size": 1 + }, + "reserve": { + "size": 2, + "stacked": true + }, + "max rank": 3 } )" @@ -267,7 +309,9 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "diagonal deal": true }, - "foundations removable": true + "foundations": { + "removable": true + } } )" @@ -282,8 +326,10 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "diagonal deal": true }, - "max rank": 2, - "foundations removable": true + "foundations": { + "removable": true + }, + "max rank": 2 } )" @@ -298,7 +344,9 @@ map<string, string> sol_preset_types::mp = { "build policy": "same-suit", "move built group": true }, - "foundations initial card": true + "foundations": { + "initial card": true + } } )" @@ -313,8 +361,10 @@ map<string, string> sol_preset_types::mp = { "build policy": "same-suit", "move built group": true }, - "max rank": 6, - "foundations initial card": true + "foundations": { + "initial card": true + }, + "max rank": 6 } )" @@ -330,10 +380,14 @@ map<string, string> sol_preset_types::mp = { "move built group": true, "move built group policy": "same-suit" }, - "foundations complete piles": true, - "two decks": true, - "stock size": 50, - "stock deal type": "tableau piles" + "foundations": { + "only complete pile moves": true + }, + "stock": { + "size": 50, + "deal type": "tableau piles" + }, + "two decks": true } )" @@ -349,11 +403,15 @@ map<string, string> sol_preset_types::mp = { "move built group": true, "move built group policy": "same-suit" }, - "foundations complete piles": true, + "foundations": { + "only complete pile moves": true + }, + "stock": { + "size": 12, + "deal type": "tableau piles" + }, "two decks": true, - "max rank": 3, - "stock size": 12, - "stock deal type": "tableau piles" + "max rank": 3 } )" @@ -371,10 +429,12 @@ map<string, string> sol_preset_types::mp = { "diagonal deal": true, "face up cards": "top" }, - "max rank": 4, - "stock size": 6, - "stock deal count": 3, - "stock redeal": true + "stock": { + "size": 6, + "deal count": 3, + "redeal": true + }, + "max rank": 4 } )" }, @@ -390,9 +450,13 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "diagonal deal": true }, - "reserve size": 6, - "foundations removable": true, - "foundations initial card": true + "reserve": { + "size": 6 + }, + "foundations": { + "removable": true, + "initial card": true + } } )" @@ -411,10 +475,14 @@ map<string, string> sol_preset_types::mp = { "diagonal deal": true, "face up cards": "top" }, - "foundations removable": true, - "stock size": 24, - "stock deal count": 1, - "stock redeal": true + "foundations": { + "removable": true + }, + "stock": { + "size": 24, + "deal count": 1, + "redeal": true + } } )" @@ -433,10 +501,14 @@ map<string, string> sol_preset_types::mp = { "diagonal deal": true, "face up cards": "top" }, - "foundations removable": true, - "stock size": 24, - "stock deal count": 3, - "stock redeal": true + "foundations": { + "removable": true + }, + "stock": { + "size": 24, + "deal count": 3, + "redeal": true + } } )" @@ -451,7 +523,9 @@ map<string, string> sol_preset_types::mp = { "build policy": "any-suit", "spaces policy": "no-build" }, - "foundations removable": true, + "foundations": { + "removable": true + }, "cells": 1 } )" @@ -466,7 +540,9 @@ map<string, string> sol_preset_types::mp = { "count": 13, "build policy": "red-black" }, - "foundations removable": true + "foundations": { + "removable": true + } } )" @@ -480,7 +556,9 @@ map<string, string> sol_preset_types::mp = { "count": 12, "build policy": "same-suit" }, - "foundations initial card": true + "foundations": { + "initial card": true + } } )" @@ -494,8 +572,10 @@ map<string, string> sol_preset_types::mp = { "count": 12, "build policy": "red-black" }, - "foundations removable": true, - "foundations initial card": true + "foundations": { + "initial card": true, + "removable": true + } } )" @@ -509,8 +589,10 @@ map<string, string> sol_preset_types::mp = { "count": 13, "build policy": "red-black" }, - "foundations removable": true, - "foundations initial card": true, + "foundations": { + "initial card": true, + "removable": true + }, "cells": 1 } )" @@ -525,8 +607,10 @@ map<string, string> sol_preset_types::mp = { "count": 13, "build policy": "red-black" }, - "foundations removable": true, - "foundations initial card": true, + "foundations": { + "initial card": true, + "removable": true + }, "cells": 2 } )" @@ -706,7 +790,9 @@ map<string, string> sol_preset_types::mp = { "spaces policy": "kings" }, "cells": 7, - "foundations initial card": true + "foundations": { + "initial card": true + } } )" @@ -737,9 +823,15 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "move built group": true }, - "reserve size": 12, - "foundations initial card": true, - "stock size": 36 + "foundations": { + "initial card": true + }, + "reserve": { + "size": 12 + }, + "stock": { + "size": 36 + } } )" @@ -754,9 +846,13 @@ map<string, string> sol_preset_types::mp = { "build policy": "any-suit", "move built group": true }, - "reserve size": 13, - "reserve stacked": true, - "stock size": 36 + "reserve": { + "size": 13, + "stacked": true + }, + "stock": { + "size": 36 + } } )" @@ -771,7 +867,9 @@ map<string, string> sol_preset_types::mp = { "build policy": "same-suit", "spaces policy": "kings" }, - "foundations removable": true + "foundations": { + "removable": true + } } )" @@ -786,7 +884,9 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "spaces policy": "no-build" }, - "foundations removable": true + "foundations": { + "removable": true + } } )" @@ -801,9 +901,13 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "move built group": true }, - "stock size": 30, - "foundations initial card": true, - "foundations removable": true + "foundations": { + "initial card": true, + "removable": true + }, + "stock": { + "size": 30 + } } )" @@ -819,9 +923,13 @@ map<string, string> sol_preset_types::mp = { "move built group": true, "face up cards": "top" }, - "foundations removable": true, - "stock size": 31, - "stock deal type": "tableau piles" + "foundations": { + "removable": true + }, + "stock": { + "size": 31, + "deal type": "tableau piles" + } } )" @@ -837,8 +945,12 @@ map<string, string> sol_preset_types::mp = { "spaces policy": "kings", "diagonal deal": true }, - "foundations removable": true, - "reserve size": 16 + "foundations": { + "removable": true + }, + "reserve": { + "size": 16 + } } )" @@ -853,8 +965,12 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "diagonal deal": true }, - "foundations removable": true, - "reserve size": 7 + "foundations": { + "removable": true + }, + "reserve": { + "size": 7 + } } )" @@ -871,9 +987,13 @@ map<string, string> sol_preset_types::mp = { "move built group policy": "same-suit", "diagonal deal": true }, - "foundations complete piles": true, - "stock size": 24, - "stock deal type": "tableau piles" + "foundations": { + "only complete pile moves": true + }, + "stock": { + "size": 24, + "deal type": "tableau piles" + } } )" @@ -889,70 +1009,16 @@ map<string, string> sol_preset_types::mp = { "move built group": true, "move built group policy": "same-suit" }, - "foundations complete piles": true, - "stock size": 31, - "stock deal type": "tableau piles" -} -)" - - }, - { - "test-game-black-hole", - - R"( - -{ - "tableau piles": { - "count": 7, - "build policy": "no-build" + "foundations": { + "only complete pile moves": true }, - "max rank": 8, - "hole": true, - "foundations": false + "stock": { + "size": 31, + "deal type": "tableau piles" + } } )" - }, - { - "test-game-any-suit", - R"( - -{ - "tableau piles": { - "count": 4, - "build policy": "any-suit" - }, - "max rank": 5 -} -)" - }, - { - "test-game-red-black", - - R"( - -{ - "tableau piles": { - "count": 6, - "build policy": "red-black" - }, - "max rank": 7 -} -)" - }, - { - "test-game-same-suit", - - R"( - -{ - "tableau piles": { - "count": 10, - "build policy": "same-suit" - }, - "max rank": 9 -} -)" }, { "simple-simon", @@ -965,7 +1031,9 @@ map<string, string> sol_preset_types::mp = { "move built group": true, "move built group policy": "same-suit" }, - "foundations complete piles": true + "foundations": { + "only complete pile moves": true + } } )" diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index 9d13129..67702e2 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -4,12 +4,13 @@ #include "state_printer.h" -using namespace std; +using std::ostream; +using std::vector; typedef sol_rules::stock_deal_type sdt; ostream& state_printer::print(ostream& stream, const game_state& gs) { - if (gs.rules.foundations) { + if (gs.rules.foundations_present) { state_printer::print_header(stream, "Foundations"); state_printer::print_top_of_piles(stream, gs.foundations, gs); } @@ -114,3 +115,36 @@ void state_printer::print_top_of_pile(ostream& stream, void state_printer::print_card(std::ostream& s, const card c) { s << c.to_string(); } + +void state_printer::print_move(std::ostream& s, const move m) { + s << "move("; + switch (m.type) { + case move::mtype::regular: + s << "regular"; + break; + case move::mtype::dominance: + s << "dominance"; + break; + case move::mtype::built_group: + s << "built group"; + break; + case move::mtype::stock_k_plus: + s << "stock k-plus"; + break; + case move::mtype::stock_to_all_tableau: + s << "stock-to-all-tableau"; + break; + case move::mtype::null: + s << "null"; + break; + } + s << ", " + << int(m.from) + << ", " + << int(m.to) + << ", " + << int(m.count) + << ", " + << (m.reveal_move ? "true" : "false") + << ")\n"; +} diff --git a/src/main/input-output/output/state_printer.h b/src/main/input-output/output/state_printer.h index 2b2090b..7d4a702 100644 --- a/src/main/input-output/output/state_printer.h +++ b/src/main/input-output/output/state_printer.h @@ -12,6 +12,7 @@ class state_printer { public: static std::ostream& print(std::ostream&, const game_state&); + static void print_move(std::ostream&, move); private: static void print_header(std::ostream&, const char*); diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index e600835..6b0c980 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -12,6 +12,7 @@ #include "solver.h" #include "../game/move.h" #include "../input-output/output/log_helper.h" +#include "../input-output/output/state_printer.h" using std::atomic; using std::vector; @@ -57,6 +58,7 @@ solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver) if (current_node->mv.type == move::mtype::dominance) { LOG_DEBUG("(dominance move)"); } + state_printer::print_move(clog, current_node->mv); LOG_DEBUG(state); #endif @@ -134,11 +136,19 @@ bool solver::revert_to_last_node_with_children(optional<lru_cache::item_list::it // (as long as the move wasn't a dominance move) if (current_node->mv.type != move::mtype::dominance) { - if (cache.get_states_removed_from_cache() == 0) assert(cache.contains(state)); + if (cache.get_states_removed_from_cache() == 0) { + if (!cache.contains(state)) { + state_printer::print_move(clog, current_node->mv); + LOG_DEBUG("-------"); + LOG_DEBUG(state); + assert(false); + } + } LOG_DEBUG("(undo move)"); } else { LOG_DEBUG("(undo dominance move)"); } + state_printer::print_move(clog, current_node->mv); LOG_DEBUG(state); #endif diff --git a/src/test/unit_tests/k_plus_stock_test.cpp b/src/test/unit_tests/k_plus_stock_test.cpp index 8a26cbe..eb376ef 100644 --- a/src/test/unit_tests/k_plus_stock_test.cpp +++ b/src/test/unit_tests/k_plus_stock_test.cpp @@ -306,7 +306,7 @@ TEST(KPlusStock, NoRedealCellFoundationMoves) { sr.stock_redeal = false; sr.stock_deal_count = 3; sr.cells = 2; - sr.foundations = true; + sr.foundations_present = true; test_helper::k_plus_stock_test( sr, @@ -341,7 +341,7 @@ TEST(KPlusStock, RedealCellFoundationMoves) { sr.stock_redeal = true; sr.stock_deal_count = 3; sr.cells = 2; - sr.foundations = true; + sr.foundations_present = true; test_helper::k_plus_stock_test( sr, diff --git a/src/test/unit_tests/legal_move_gen_test.cpp b/src/test/unit_tests/legal_move_gen_test.cpp index cbdeae0..4625163 100644 --- a/src/test/unit_tests/legal_move_gen_test.cpp +++ b/src/test/unit_tests/legal_move_gen_test.cpp @@ -199,7 +199,7 @@ TEST(LegalMoveGen, MoveBuiltGroupFalse) { TEST(LegalMoveGen, Foundations) { sol_rules sr; - sr.foundations = true; + sr.foundations_present = true; sr.tableau_pile_count = 4; game_state gs(sr, string_il{ @@ -223,7 +223,7 @@ TEST(LegalMoveGen, Foundations) { TEST(LegalMoveGen, FoundationsRemovable) { sol_rules sr; - sr.foundations = true; + sr.foundations_present = true; sr.foundations_removable = true; sr.tableau_pile_count = 1; @@ -264,7 +264,7 @@ TEST(LegalMoveGen, Cells) { TEST(LegalMoveGen, StockDealToTableau) { sol_rules sr; - sr.foundations = true; + sr.foundations_present = true; sr.tableau_pile_count = 3; sr.stock_size = 2; sr.stock_deal_t = sdt::TABLEAU_PILES; diff --git a/src/test/unit_tests/rules_parser_test.cpp b/src/test/unit_tests/rules_parser_test.cpp deleted file mode 100644 index 31a7101..0000000 --- a/src/test/unit_tests/rules_parser_test.cpp +++ /dev/null @@ -1,377 +0,0 @@ -// -// Created by thecharlesblake on 1/11/18. -// - -#include <gtest/gtest.h> - -#include "../../../lib/rapidjson/stringbuffer.h" -#include "../../../lib/rapidjson/document.h" - -#include "../../main/input-output/input/json-parsing/rules_parser.h" -#include "../../main/input-output/input/json-parsing/json_helper.h" - -using namespace std; -using namespace rapidjson; - -void run_rules_parser_test(bool, const char*); - -// invalid int, build pol, bool, other - -TEST(RulesParser, DefaultGameRules) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 8, - "build policy": "any-suit", - "spaces policy": "any", - "diagonal deal": false, - "move built group": false, - "move built group policy": "same-as-build" - }, - "max rank": 13, - "two decks": false, - "hole": false, - "foundations": true, - "foundations initial card": false, - "foundations removable": false, - "foundations complete piles": false, - "cells": 0, - "stock size": 0, - "stock deal type": "waste", - "reserve size": 0, - "reserve stacked": false -} -)"); -} - -TEST(RulesParser, InvalidType) { - run_rules_parser_test(false, R"( -{ - "tableau piles": { - "count": "13" - } -} -)"); -} - -TEST(RulesParser, InvalidNumber) { - run_rules_parser_test(false, R"( -{ - "tableau piles": { - "count": 13 - }, - "max rank": 20 -} -)"); -} - -TEST(RulesParser, InvalidEnum) { - run_rules_parser_test(false, R"( -{ - "tableau piles": { - "count": 13, - "build policy": "red-black-green" - } -} -)"); -} - -TEST(RulesParser, InvalidRuleName) { - run_rules_parser_test(false, R"( -{ - "tableau piles": { - "count number": 13 - } -} -)"); -} - -TEST(RulesParser, SpanishPatience) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 13 - }, - "foundations removable": true -} -)"); -} - -TEST(RulesParser, SimpleSpanishPatience) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 3 - }, - "max rank": 2, - "foundations removable": true -} -)"); -} - -TEST(RulesParser, FreeCell) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "build policy": "red-black" - }, - "cells": 4 -} -)"); -} - -TEST(RulesParser, SimpleFreeCell) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 4, - "build policy": "red-black" - }, - "max rank": 4, - "cells": 1 -} -)"); -} - -TEST(RulesParser, BakersDozen) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 13, - "spaces policy": "no-build" - }, - "foundations removable": true -} -)"); -} - -TEST(RulesParser, SimpleBakersDozen) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 3, - "spaces policy": "no-build" - }, - "max rank": 3, - "foundations removable": true -} -)"); -} - -TEST(RulesParser, BlackHole) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 17, - "build policy": "no-build" - }, - "hole": true, - "foundations": false -} -)"); -} - -TEST(RulesParser, SimpleBlackHole) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 4, - "build policy": "no-build" - }, - "max rank": 5, - "hole": true, - "foundations": false -} -)"); -} - -TEST(RulesParser, FortunesFavor) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 12, - "build policy": "same-suit" - }, - "foundations initial card": true, - "stock size": 36 -} -)"); -} - -TEST(RulesParser, SimpleFortunesFavor) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 3, - "build policy": "same-suit" - }, - "max rank": 4, - "foundations initial card": true, - "stock size": 3 -} -)"); -} - -TEST(RulesParser, FlowerGarden) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 6 - }, - "reserve size": 16, - "foundations removable": true -} -)"); -} - -TEST(RulesParser, SimpleFlowerGarden) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 3 - }, - "max rank": 3, - "reserve size": 3, - "foundations removable": true -} -)"); -} - -TEST(RulesParser, Canfield) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 4, - "build policy": "red-black" - }, - "stock size": 34, - "reserve size": 14, - "reserve stacked": true -} -)"); -} - -TEST(RulesParser, SimpleCanfield) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 3, - "build policy": "red-black" - }, - "max rank": 3, - "stock size": 1, - "reserve size": 2, - "reserve stacked": true -} -)"); -} - -TEST(RulesParser, Somerset) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 10, - "build policy": "red-black", - "diagonal deal": true - }, - "foundations removable": true -} -)"); -} - -TEST(RulesParser, SimpleSomerset) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 4, - "build policy": "red-black", - "diagonal deal": true - }, - "max rank": 2, - "foundations removable": true -} -)"); -} - -TEST(RulesParser, AlphaStar) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 12, - "build policy": "same-suit", - "move built group": true - }, - "foundations initial card": true -} -)"); -} - -TEST(RulesParser, SimpleAlphaStar) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 3, - "build policy": "same-suit", - "move built group": true - }, - "max rank": 5, - "foundations initial card": true -} -)"); -} - -TEST(RulesParser, Spider) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 10, - "build policy": "any-suit", - "move built group": true, - "move built group policy": "same-suit" - }, - "foundations complete piles": true, - "two decks": true, - "stock size": 50, - "stock deal type": "tableau piles" -} -)"); -} - -TEST(RulesParser, SimpleSpider) { - run_rules_parser_test(true, R"( -{ - "tableau piles": { - "count": 4, - "build policy": "any-suit", - "move built group": true, - "move built group policy": "same-suit" - }, - "foundations complete piles": true, - "two decks": true, - "max rank": 3, - "stock size": 12, - "stock deal type": "tableau piles" -} -)"); -} - -void run_rules_parser_test(bool validity, const char* json) { - Document in_doc; - in_doc.Parse(json); - if (in_doc.HasParseError()) { - throw runtime_error("invalid json"); - } - - Document val_doc; - val_doc.Parse(rules_parser::rules_schema_json().c_str()); - SchemaDocument sd(val_doc); - SchemaValidator validator(sd); - bool acc = in_doc.Accept(validator); - if (acc) { - ASSERT_TRUE(validity == acc) << "JSON accepted when it should not have " - "been:\n" << json; - } else { - ASSERT_TRUE(validity == acc) << json_helper::schema_err_str(validator) - << "\n" << json; - } -} |
