aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Harley2019-02-01 15:53:27 +0000
committerTom Harley2019-02-01 15:53:27 +0000
commit912a11b8bc173fada75815fb4f740cd11879205b (patch)
tree27f7a32b2e57221ebc7923a1715d0ba150e12834
parentUpdate submodules (diff)
downloadmetasol-912a11b8bc173fada75815fb4f740cd11879205b.tar.gz
metasol-912a11b8bc173fada75815fb4f740cd11879205b.zip

Add support for reading rules from .json files

Game rules can now be read from .json files passed as argument with -r/--rules on the command-line. The rules descriptions are built on top of sane default rules, so some rules can be omitted.

Without a rules file specified, centre plays canfield.

-rw-r--r--centre.cpp205
-rw-r--r--klondike.json16
-rw-r--r--metasol.cpp145
-rw-r--r--metasol.h22
4 files changed, 359 insertions, 29 deletions
diff --git a/centre.cpp b/centre.cpp
index 39593b3..64d4862 100644
--- a/centre.cpp
+++ b/centre.cpp
@@ -16,9 +16,9 @@ Usage: centre [options]
Options:
-h --help Display this help.
-r FILE --rules FILE Specify a .json file containing game rules.
- -g FILE --game FILE Specify a .json file describing a game layout.
- -s SEED --seed SEED Specify a seed to use when randomly generating a game
- (set to 0 to use an unspecified seed).)";
+ -g FILE --game FILE Specify a .json file describing a game layout.
+ -s SEED --seed SEED Specify a seed to use when randomly generating a game
+ (set to 0 to use an unspecified seed).)";
static const char VERSION_STR[] = "Solver in-development";
@@ -542,6 +542,67 @@ jsmntok_t *next_tok(jsmntok_t *t) {
return t + 1;
}
+enum PRIM_TYPE {
+ NUMBER_TYPE, BOOL_TYPE, NULL_TYPE
+};
+
+PRIM_TYPE prim_type(jsmntok_t *tok, char *js) {
+ switch (js[tok->start]) {
+ case '0': case '1': case '2': case '3': case '4': case '5': case '6':
+ case '7': case '8': case '9': case '-':
+ return NUMBER_TYPE;
+ case 't': case 'f':
+ return BOOL_TYPE;
+ case 'n':
+ return NULL_TYPE;
+ default:
+ assert(false);
+ }
+}
+
+int assign_rule_prim(jsmntok_t *key, const char *name, PRIM_TYPE type,
+ void *rule, char *js) {
+ if (!strncmp(&js[key->start], name, key->end - key->start)) {
+ jsmntok_t *val = next_tok(key);
+ assert(val->type == JSMN_PRIMITIVE);
+ PRIM_TYPE pt = prim_type(val, js);
+ assert(pt == type);
+ unsigned *irule = (unsigned *) rule;
+ bool *brule = (bool *) rule;
+ switch (pt) {
+ case NUMBER_TYPE:
+ *irule = atoi(&js[val->start]);
+ break;
+ case BOOL_TYPE:
+ *brule = (js[val->start] == 't');
+ break;
+ case NULL_TYPE:
+ default:
+ assert(false);
+ }
+ return 2;
+ } else {
+ return 0;
+ }
+}
+
+int assign_rule_enum(jsmntok_t *key, const char *name, char (*func)(char *),
+ char *rule, char *js) {
+ long length = key->end - key->start;
+ char *str = &js[key->start];
+ if (!strncmp(str, name, length)) {
+ jsmntok_t *val = next_tok(key);
+ assert(val->type == JSMN_STRING);
+ char *buf = (char *) malloc(length + 1);
+ strncpy(buf, str, length);
+ buf[length] = '\0';
+ *rule = func(buf);
+ return 2;
+ } else {
+ return 0;
+ }
+}
+
int assign_pile(jsmntok_t *key, const char *pile_name, ms_card_pile *pile,
char *js) {
pile->clear();
@@ -587,7 +648,7 @@ int assign_pile_group(jsmntok_t *key, const char *group_name,
}
}
-ms_game_state json_game_state(const char *filename) {
+ms_rules json_rules(const char *filename) {
FILE *f = fopen(filename, "rb");
long length = filelen(f);
char *buf = (char *) malloc(length);
@@ -610,7 +671,7 @@ ms_game_state json_game_state(const char *filename) {
assert(parse_val != JSMN_ERROR_NOMEM);
assert(parse_val != JSMN_ERROR_PART);
- ms_game_state gs;
+ ms_rules r = fetch_default_rules();
for (int i = 0; i < tok_count;) {
jsmntok_t *tok = &tokens[i];
@@ -621,37 +682,117 @@ ms_game_state json_game_state(const char *filename) {
x = 1;
break;
} case JSMN_STRING: {
- x = assign_pile(tok, "stock", &gs.stock, buf);
- if (x) {
- break;
+# define arp(n, t, p) { \
+ x = assign_rule_prim(tok, n, t, p, buf); \
+ if (x) break; \
}
- x = assign_pile(tok, "waste", &gs.waste, buf);
- if (x) {
- break;
+ arp("tableau size", NUMBER_TYPE, &r.tableau_size);
+ arp("deck count", NUMBER_TYPE, &r.deck_count);
+ arp("max rank", NUMBER_TYPE, &r.max_rank);
+ arp("hole", BOOL_TYPE, &r.hole_present);
+ arp("foundations", BOOL_TYPE, &r.foundations_present);
+ arp("foundations removable", BOOL_TYPE,
+ &r.foundations_removable);
+ arp("foundations accept only complete piles", BOOL_TYPE,
+ &r.foundations_only_comp_piles);
+ arp("diagonal deal", BOOL_TYPE, &r.diagonal_deal);
+ arp("number of cells", NUMBER_TYPE, &r.cells);
+ arp("cells pre-filled", BOOL_TYPE, &r.cells_pre_filled);
+ arp("cards in stock", NUMBER_TYPE, &r.stock_size);
+ arp("stock deal count", NUMBER_TYPE, &r.stock_deal_count);
+ arp("stock redeal allowed", BOOL_TYPE, &r.stock_redeal);
+ arp("cards in reserve", NUMBER_TYPE, &r.reserve_size);
+ arp("reserve stacked", BOOL_TYPE, &r.reserve_stacked);
+ arp("cards in sequence", NUMBER_TYPE, &r.sequence_count);
+ arp("sequence fixed suit", BOOL_TYPE, &r.sequence_fixed_suit);
+
+# define are(n, f, p) { \
+ x = assign_rule_enum(tok, n, (char (*)(char *)) f, \
+ (char *) p, buf); \
+ if (x) break; \
}
- x = assign_pile(tok, "reserve", &gs.reserve, buf);
- if (x) {
- break;
- }
+ are("build policy", str_build_policy, &r.build_policy);
+ are("spaces policy", str_spaces_policy, &r.spaces_policy);
+ are("move built group", str_built_group, &r.move_built_group);
+ are("group build policy", str_build_policy,
+ &r.built_group_policy);
+ are("foundations initialised", str_foundations_init,
+ &r.foundations_init_cards);
+ are("stock deal method", str_stock_deal, &r.stock_deal_method);
+ are("face up cards", str_face_up_policy, &r.face_up_policy);
+ are("face up policy", str_face_up_policy, &r.face_up_policy);
+ are("sequence direction", str_direction, &r.sequence_direction);
+ are("sequence build policy", str_build_policy,
+ &r.sequence_build_policy);
+
+ fprintf(stderr, "%s\n", &buf[tok->start]);
+ assert(false);
+ } default: {
+ assert(false);
+ }
+ }
+ i += x;
+ }
- x = assign_pile_group(tok, "tableau", &gs.tableau, buf);
- if (x) {
- break;
- }
+ free(buf);
+ free(tokens);
+
+ return r;
+}
- x = assign_pile_group(tok, "foundations", &gs.foundations,
- buf);
- if (x) {
- break;
+ms_game_state json_game_state(const char *filename) {
+ FILE *f = fopen(filename, "rb");
+ long length = filelen(f);
+ char *buf = (char *) malloc(length);
+
+ assert(buf);
+
+ file_str(f, buf, length);
+
+ jsmn_parser parser;
+ jsmn_init(&parser);
+ int tok_count = jsmn_parse(&parser, buf, length, NULL, 0);
+ jsmntok_t *tokens = (jsmntok_t *) malloc(tok_count * sizeof(jsmntok_t));
+
+ assert(tokens);
+
+ jsmn_init(&parser);
+ auto parse_val = jsmn_parse(&parser, buf, length, tokens, tok_count);
+
+ assert(parse_val != JSMN_ERROR_INVAL);
+ assert(parse_val != JSMN_ERROR_NOMEM);
+ assert(parse_val != JSMN_ERROR_PART);
+
+ ms_game_state gs;
+
+ for (int i = 0; i < tok_count;) {
+ jsmntok_t *tok = &tokens[i];
+
+ int x = 0;
+ switch (tok->type) {
+ case JSMN_OBJECT: {
+ x = 1;
+ break;
+ } case JSMN_STRING: {
+# define _assign_pile(n, p) { \
+ x = assign_pile(tok, n, p, buf); \
+ if (x) break; \
}
- x = assign_pile_group(tok, "cells", &gs.cells, buf);
- if (x) {
- break;
+# define _assign_pile_group(n, p) { \
+ x = assign_pile_group(tok, n, p, buf); \
+ if (x) break; \
}
+ _assign_pile("stock", &gs.stock);
+ _assign_pile("waste", &gs.waste);
+ _assign_pile("reserve", &gs.reserve);
+ _assign_pile_group("tableau", &gs.tableau);
+ _assign_pile_group("foundations", &gs.foundations);
+ _assign_pile_group("cells", &gs.cells);
+
assert(false);
} default: {
fprintf(stderr, "%d\n", tok->type);
@@ -684,13 +825,21 @@ ms_game_state make_game_state(ms_rules *r,
}
}
+ms_rules make_rules(std::map<std::string, docopt::value> &args) {
+ if (args["--rules"]) {
+ const char *filename = args["--rules"].asString().c_str();
+ return json_rules(filename);
+ } else {
+ return canfield();
+ }
+}
+
void parse_args(int argc, char **argv, ms_game_state *gs, ms_rules *r) {
std::map<std::string, docopt::value> args = docopt::docopt(USAGE,
{ argv + 1, argv + argc }, true, VERSION_STR);
- *r = simple_canfield();
+ *r = make_rules(args);
*gs = make_game_state(r, args);
- /* make_rules(r, args); */
/* make_settings(s, args); */
}
diff --git a/klondike.json b/klondike.json
new file mode 100644
index 0000000..88af397
--- /dev/null
+++ b/klondike.json
@@ -0,0 +1,16 @@
+{
+ "tableau size": 7,
+ "build policy": "red-black",
+ "spaces policy": "kings",
+ "move built group": "yes",
+ "group build policy": "red-black",
+
+ "diagonal deal": true,
+ "face up policy": "top",
+
+ "foundations removable": true,
+
+ "cards in stock": 24,
+ "stock deal count": 3,
+ "stock redeal": true
+}
diff --git a/metasol.cpp b/metasol.cpp
index ebb9b70..5aafcec 100644
--- a/metasol.cpp
+++ b/metasol.cpp
@@ -16,6 +16,149 @@
card_suit SUITS[4] = { HEARTS, SPADES, CLUBS, DIAMONDS };
+void lower(char *str) {
+ for (; *str; ++str) *str = tolower(*str);
+}
+
+struct dictcmp {
+ bool operator()(const char *a, const char *b) {
+ return strcmp(a, b) < 0;
+ }
+};
+
+#define kv_dict(t) std::map<const char *, t, dictcmp>
+
+build_policy_t str_build_policy(char *str) {
+ kv_dict(build_policy_t) dict = {
+ { "no build", NO_BUILD },
+ { "none", NO_BUILD },
+
+ { "same suit", BUILD_SAME_SUIT },
+ { "suits", BUILD_SAME_SUIT },
+
+ { "alternating", BUILD_ALTERNATING },
+ { "red-black", BUILD_ALTERNATING },
+
+ { "any", BUILD_ANY },
+ { "all", BUILD_ANY }
+ };
+
+ lower(str);
+ return dict[str];
+}
+
+spaces_policy_t str_spaces_policy(char *str) {
+ kv_dict(spaces_policy_t) dict = {
+ { "none", NO_SPACE_FILL },
+ { "no fill", NO_SPACE_FILL },
+
+ { "kings only", KINGS_FILL_SPACE },
+ { "kings", KINGS_FILL_SPACE },
+
+ { "any", ANY_FILL_SPACE },
+ { "all", ANY_FILL_SPACE },
+
+ { "reserve first", AUTO_RESERVE_THEN_WASTE },
+ { "reserve then waste", AUTO_RESERVE_THEN_WASTE },
+
+ { "waste first", AUTO_WASTE_THEN_STOCK },
+ { "waste then stock", AUTO_WASTE_THEN_STOCK }
+ };
+
+ lower(str);
+ return dict[str];
+}
+
+accordion_policy_t str_accordion_policy(char *str) {
+ kv_dict(accordion_policy_t) dict = {
+ { "same rank", ACCORDION_SAME_RANK },
+ { "rank", ACCORDION_SAME_RANK },
+
+ { "same suit", ACCORDION_SAME_SUIT },
+ { "suit", ACCORDION_SAME_SUIT },
+
+ { "alternating colour", ACCORDION_ALTERNATE_COLOUR },
+ { "red-black", ACCORDION_ALTERNATE_COLOUR },
+
+ { "any", ACCORDION_ANY_SUIT }
+ };
+
+ lower(str);
+ return dict[str];
+}
+
+stock_deal_t str_stock_deal(char *str) {
+ kv_dict(stock_deal_t) dict = {
+ { "waste", STOCK_TO_WASTE },
+ { "stock to waste", STOCK_TO_WASTE },
+
+ { "tableau", STOCK_TO_TABLEAU },
+ { "stock to tableau", STOCK_TO_TABLEAU }
+ };
+
+ lower(str);
+ return dict[str];
+}
+
+face_up_policy_t str_face_up_policy(char *str) {
+ kv_dict(face_up_policy_t) dict = {
+ { "all", ALL_CARDS_FACE_UP },
+
+ { "top", TOP_CARDS_FACE_UP },
+ { "top only", TOP_CARDS_FACE_UP }
+ };
+
+ lower(str);
+ return dict[str];
+}
+
+direction_t str_direction(char *str) {
+ kv_dict(direction_t) dict = {
+ { "left", LEFT },
+
+ { "right", RIGHT },
+
+ { "both", BOTH },
+ { "lr", BOTH }
+ };
+
+ lower(str);
+ return dict[str];
+}
+
+built_group_t str_built_group(char *str) {
+ kv_dict(built_group_t) dict = {
+ { "yes", CAN_MOVE_BUILT_GROUP },
+
+ { "no", CANNOT_MOVE_BUILT_GROUP },
+ { "none", CANNOT_MOVE_BUILT_GROUP },
+
+ { "whole pile only", CAN_MOVE_WHOLE_PILE },
+ { "whole pile", CAN_MOVE_WHOLE_PILE },
+
+ { "maximal group only", CAN_MOVE_MAXIMAL_GROUP },
+ { "maximal", CAN_MOVE_MAXIMAL_GROUP }
+ };
+
+ lower(str);
+ return dict[str];
+}
+
+foundations_init_t str_foundations_init(char *str) {
+ kv_dict (foundations_init_t) dict = {
+ { "no", NO_FOUNDATION_INIT },
+ { "none", NO_FOUNDATION_INIT },
+
+ { "one", ONE_FOUNDATION_INIT },
+
+ { "all", ALL_FOUNDATIONS_INIT },
+ { "yes", ALL_FOUNDATIONS_INIT }
+ };
+
+ lower(str);
+ return dict[str];
+}
+
ms_settings fetch_default_settings() {
ms_settings s;
s.run_func = NULL;
@@ -355,7 +498,7 @@ void run_loop(ms_game_state *gs, ms_rules *r, ms_settings *s,
debug("%u->%u(%u) has %d votes\n", move.from, move.to, move.size, vote);
- if (vote > max_votes) {// && !opposite_move(&move, previous_move)) {
+ if (vote > max_votes) {
max_move = &move;
max_votes = vote;
}
diff --git a/metasol.h b/metasol.h
index 8cd7799..68cf605 100644
--- a/metasol.h
+++ b/metasol.h
@@ -4,42 +4,64 @@
#include <vector>
#include <stdint.h>
+#include <string.h>
enum build_policy_t {
NO_BUILD, BUILD_SAME_SUIT, BUILD_ALTERNATING, BUILD_ANY
};
+build_policy_t str_build_policy(char *);
+
enum spaces_policy_t {
NO_SPACE_FILL, KINGS_FILL_SPACE, ANY_FILL_SPACE, AUTO_RESERVE_THEN_WASTE,
AUTO_WASTE_THEN_STOCK
};
+#define KINGS_FILL_SPACES KINGS_FILL_SPACE
+#define ANY_FILL_SPACES ANY_FILL_SPACE
+
+spaces_policy_t str_spaces_policy(char *);
+
enum accordion_policy_t {
ACCORDION_SAME_RANK, ACCORDION_SAME_SUIT, ACCORDION_ALTERNATE_COLOUR,
ACCORDION_ANY_SUIT
};
+accordion_policy_t str_accordion_policy(char *);
+
enum stock_deal_t {
STOCK_TO_WASTE, STOCK_TO_TABLEAU
};
+stock_deal_t str_stock_deal(char *);
+
enum face_up_policy_t {
ALL_CARDS_FACE_UP, TOP_CARDS_FACE_UP
};
+face_up_policy_t str_face_up_policy(char *);
+
enum direction_t {
LEFT, RIGHT, BOTH
};
+direction_t str_direction(char *);
+
enum built_group_t {
CAN_MOVE_BUILT_GROUP, CANNOT_MOVE_BUILT_GROUP, CAN_MOVE_WHOLE_PILE,
CAN_MOVE_MAXIMAL_GROUP
};
+built_group_t str_built_group(char *);
+
enum foundations_init_t {
NO_FOUNDATION_INIT, ONE_FOUNDATION_INIT, ALL_FOUNDATIONS_INIT
};
+#define NO_FOUNDATIONS_INIT NO_FOUNDATION_INIT
+
+foundations_init_t str_foundations_init(char *);
+
enum card_suit { HEARTS, SPADES, CLUBS, DIAMONDS };
extern card_suit SUITS[4];