From b044ca495c6214250628fc984863eb45c5d60852 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Fri, 6 Jul 2018 11:14:04 +0100 Subject: Refactored game rules JSON --- .../input/json-parsing/deal_parser.cpp | 2 +- .../input/json-parsing/rules_parser.cpp | 327 +++++++++++------ src/main/input-output/input/sol_preset_types.cpp | 392 ++++++++++++--------- 3 files changed, 451 insertions(+), 270 deletions(-) (limited to 'src/main/input-output/input') 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(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(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(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(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(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(d["stock deal count"].GetInt()); + if (d["stock"].HasMember("redeal")) { + if (d["stock"]["redeal"].IsBool()) { + sr.stock_redeal = static_cast(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(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(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(d["reserve size"].GetInt()); + if (d.HasMember("max rank")) { + if (d["max rank"].IsInt()) { + sr.max_rank = static_cast(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 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 sol_preset_types::mp = { "tableau piles": { "count": 13 }, - "foundations removable": true + "foundations": { + "removable": true + } } )" @@ -68,8 +76,10 @@ map 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 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 sol_preset_types::mp = { "count": 13, "spaces policy": "no-build" }, - "foundations removable": true + "foundations": { + "removable": true + } } )" @@ -125,8 +137,10 @@ map 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 sol_preset_types::mp = { "count": 17, "build policy": "no-build" }, - "hole": true, - "foundations": false + "foundations": { + "present": false + }, + "hole": true } )" @@ -155,9 +171,11 @@ map 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 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 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 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 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 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 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 sol_preset_types::mp = { "build policy": "red-black", "diagonal deal": true }, - "foundations removable": true + "foundations": { + "removable": true + } } )" @@ -282,8 +326,10 @@ map 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 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 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 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 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 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 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 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 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 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 sol_preset_types::mp = { "count": 13, "build policy": "red-black" }, - "foundations removable": true + "foundations": { + "removable": true + } } )" @@ -480,7 +556,9 @@ map sol_preset_types::mp = { "count": 12, "build policy": "same-suit" }, - "foundations initial card": true + "foundations": { + "initial card": true + } } )" @@ -494,8 +572,10 @@ map 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 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 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 sol_preset_types::mp = { "spaces policy": "kings" }, "cells": 7, - "foundations initial card": true + "foundations": { + "initial card": true + } } )" @@ -737,9 +823,15 @@ map 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 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 sol_preset_types::mp = { "build policy": "same-suit", "spaces policy": "kings" }, - "foundations removable": true + "foundations": { + "removable": true + } } )" @@ -786,7 +884,9 @@ map sol_preset_types::mp = { "build policy": "red-black", "spaces policy": "no-build" }, - "foundations removable": true + "foundations": { + "removable": true + } } )" @@ -801,9 +901,13 @@ map 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 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 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 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 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 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 sol_preset_types::mp = { "move built group": true, "move built group policy": "same-suit" }, - "foundations complete piles": true + "foundations": { + "only complete pile moves": true + } } )" -- cgit v1.2.1