aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--centre.cpp32
-rw-r--r--metasol.cpp369
-rw-r--r--metasol.h137
4 files changed, 430 insertions, 110 deletions
diff --git a/Makefile b/Makefile
index 62fd1cc..cccc5c7 100644
--- a/Makefile
+++ b/Makefile
@@ -22,7 +22,7 @@ init-submodules:
git submodule update
$(MAKE) -C thpool
$(MAKE) -C jsmn
- (cd docopt; cmake .; make docopt_s)
+ (cd docopt; cmake .) $(MAKE) -C docopt docopt_s
(cd solvitaire; sh build.sh)
run: $(TRGT)
diff --git a/centre.cpp b/centre.cpp
index 64d4862..e680019 100644
--- a/centre.cpp
+++ b/centre.cpp
@@ -501,7 +501,7 @@ ms_settings get_settings(user_data *d) {
s.reserved_move_count = 10u;
s.max_concurrent_threads = 24u;
- s.max_votes = 1000u;
+ s.max_votes = 10u;
s.user_data = (void *) d;
@@ -511,9 +511,9 @@ ms_settings get_settings(user_data *d) {
user_data get_user_data() {
user_data d;
- d.run_cache_size = 1024u;
+ d.run_cache_size = 1000000u;
d.run_timeout = 20000u;
- d.thoughtful_run_timeout = 20000u;
+ d.thoughtful_run_timeout = 2000000u;
return d;
}
@@ -589,13 +589,16 @@ int assign_rule_prim(jsmntok_t *key, const char *name, PRIM_TYPE type,
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)) {
+ char *key_str = &js[key->start];
+ if (!strncmp(key_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';
+
+ char *val_str = &js[val->start];
+ long val_length = val->end - val->start;
+ strncpy(buf, val_str, val_length);
+ buf[val_length] = '\0';
*rule = func(buf);
return 2;
} else {
@@ -694,8 +697,12 @@ ms_rules json_rules(const char *filename) {
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("foundations only complete pile moves", 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);
@@ -707,6 +714,14 @@ ms_rules json_rules(const char *filename) {
arp("cards in sequence", NUMBER_TYPE, &r.sequence_count);
arp("sequence fixed suit", BOOL_TYPE, &r.sequence_fixed_suit);
+ /*
+ * FIXME
+ * Doing some really sketchy casting here, but it should work:
+ * enums have to be at least a char in size, and these enum
+ * values should never end up being more than a char in value.
+ * In fact, they shouldn't ever touch the eighth bit (which
+ * could cause signed-casting problems).
+ */
# define are(n, f, p) { \
x = assign_rule_enum(tok, n, (char (*)(char *)) f, \
(char *) p, buf); \
@@ -720,7 +735,10 @@ ms_rules json_rules(const char *filename) {
&r.built_group_policy);
are("foundations initialised", str_foundations_init,
&r.foundations_init_cards);
+
+ are("stock deal type", str_stock_deal, &r.stock_deal_method);
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);
diff --git a/metasol.cpp b/metasol.cpp
index 6e3b670..ae38319 100644
--- a/metasol.cpp
+++ b/metasol.cpp
@@ -3,6 +3,7 @@
#include <random>
#include <assert.h>
+#include <err.h>
#include "thpool/thpool.h"
@@ -28,23 +29,39 @@ struct dictcmp {
#define kv_dict(t) std::map<const char *, t, dictcmp>
+#define C(x) #x,
+
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 },
+ { "same-suit", BUILD_SAME_SUIT },
{ "suits", BUILD_SAME_SUIT },
{ "alternating", BUILD_ALTERNATING },
{ "red-black", BUILD_ALTERNATING },
{ "any", BUILD_ANY },
+ { "any-suit", BUILD_ANY },
{ "all", BUILD_ANY }
};
lower(str);
- return dict[str];
+ auto result = dict.find(str);
+
+ if (result == dict.end()) {
+ errx(EXIT_FAILURE, "unknown key '%s'", str);
+ }
+
+ return result->second;
+}
+
+const char *build_policy_str(build_policy_t bp) {
+ const char *const build_policy_arr[] = { build_policy_t_gen };
+ assert(bp < build_policy_t_count);
+ return build_policy_arr[bp];
}
spaces_policy_t str_spaces_policy(char *str) {
@@ -66,7 +83,19 @@ spaces_policy_t str_spaces_policy(char *str) {
};
lower(str);
- return dict[str];
+ auto result = dict.find(str);
+
+ if (result == dict.end()) {
+ errx(EXIT_FAILURE, "unknown key '%s'", str);
+ }
+
+ return result->second;
+}
+
+const char *spaces_policy_str(spaces_policy_t bp) {
+ const char *const spaces_policy_arr[] = { spaces_policy_t_gen };
+ assert(bp < spaces_policy_t_count);
+ return spaces_policy_arr[bp];
}
accordion_policy_t str_accordion_policy(char *str) {
@@ -84,7 +113,19 @@ accordion_policy_t str_accordion_policy(char *str) {
};
lower(str);
- return dict[str];
+ auto result = dict.find(str);
+
+ if (result == dict.end()) {
+ errx(EXIT_FAILURE, "unknown key '%s'", str);
+ }
+
+ return result->second;
+}
+
+const char *accordion_policy_str(accordion_policy_t bp) {
+ const char *const accordion_policy_arr[] = { accordion_policy_t_gen };
+ assert(bp < accordion_policy_t_count);
+ return accordion_policy_arr[bp];
}
stock_deal_t str_stock_deal(char *str) {
@@ -97,7 +138,19 @@ stock_deal_t str_stock_deal(char *str) {
};
lower(str);
- return dict[str];
+ auto result = dict.find(str);
+
+ if (result == dict.end()) {
+ errx(EXIT_FAILURE, "unknown key '%s'", str);
+ }
+
+ return result->second;
+}
+
+const char *stock_deal_str(stock_deal_t bp) {
+ const char *const stock_deal_arr[] = { stock_deal_t_gen };
+ assert(bp < stock_deal_t_count);
+ return stock_deal_arr[bp];
}
face_up_policy_t str_face_up_policy(char *str) {
@@ -109,7 +162,19 @@ face_up_policy_t str_face_up_policy(char *str) {
};
lower(str);
- return dict[str];
+ auto result = dict.find(str);
+
+ if (result == dict.end()) {
+ errx(EXIT_FAILURE, "unknown key '%s'", str);
+ }
+
+ return result->second;
+}
+
+const char *face_up_policy_str(face_up_policy_t bp) {
+ const char *const face_up_policy_arr[] = { face_up_policy_t_gen };
+ assert(bp < face_up_policy_t_count);
+ return face_up_policy_arr[bp];
}
direction_t str_direction(char *str) {
@@ -123,7 +188,19 @@ direction_t str_direction(char *str) {
};
lower(str);
- return dict[str];
+ auto result = dict.find(str);
+
+ if (result == dict.end()) {
+ errx(EXIT_FAILURE, "unknown key '%s'", str);
+ }
+
+ return result->second;
+}
+
+const char *direction_str(direction_t bp) {
+ const char *const direction_arr[] = { direction_t_gen };
+ assert(bp < direction_t_count);
+ return direction_arr[bp];
}
built_group_t str_built_group(char *str) {
@@ -141,7 +218,19 @@ built_group_t str_built_group(char *str) {
};
lower(str);
- return dict[str];
+ auto result = dict.find(str);
+
+ if (result == dict.end()) {
+ errx(EXIT_FAILURE, "unknown key '%s'", str);
+ }
+
+ return result->second;
+}
+
+const char *built_group_str(built_group_t bp) {
+ const char *const built_group_arr[] = { built_group_t_gen };
+ assert(bp < built_group_t_count);
+ return built_group_arr[bp];
}
foundations_init_t str_foundations_init(char *str) {
@@ -156,7 +245,19 @@ foundations_init_t str_foundations_init(char *str) {
};
lower(str);
- return dict[str];
+ auto result = dict.find(str);
+
+ if (result == dict.end()) {
+ errx(EXIT_FAILURE, "unknown key '%s'", str);
+ }
+
+ return result->second;
+}
+
+const char *foundations_init_str(foundations_init_t bp) {
+ const char *const foundations_init_arr[] = { foundations_init_t_gen };
+ assert(bp < foundations_init_t_count);
+ return foundations_init_arr[bp];
}
ms_settings fetch_default_settings() {
@@ -339,17 +440,13 @@ ms_card_pile *get_pile_by_index(ms_game_state *gs, ms_rules *r, uint8_t i) {
}
void *run_thread(void *vargp) {
- struct thread_info *ti = (struct thread_info *) vargp;
+ thread_info *ti = (thread_info *) vargp;
*ti->result = ti->s->run_func(ti->gs, ti->r, ti->move_buf,
ti->s->reserved_move_count, ti->s->user_data);
return NULL;
}
-// TODO: this shouldn't be implemented as an operator - it should be its own
-// function.
-bool operator< (const ms_move &a, const ms_move &b) {
- // FIXME: if we want to group all moves that go through the stock together,
- // it's necessary to make them "not less than" one another.
+bool ms_move_lt(const ms_move &a, const ms_move &b) {
if (a.stock && b.stock) return false;
if (a.from < b.from) return true;
@@ -358,6 +455,12 @@ bool operator< (const ms_move &a, const ms_move &b) {
return false;
}
+struct votecmp {
+ bool operator()(const ms_move &a, const ms_move &b) {
+ return ms_move_lt(a, b);
+ }
+};
+
int intlen(int i) {
if (i == 0) return 1;
return floor(log10(abs(i))) + 1;
@@ -387,19 +490,40 @@ int flip_waste(ms_game_state *gs) {
return 0;
}
-int go_through_stock(ms_game_state *gs, unsigned n, bool *reveal) {
- // going through the stock is treated strangely, so recalculations are
- // always required
+int go_through_stock(ms_game_state *gs, ms_rules *r, bool *reveal) {
+
+ /*
+ * Currently, going through the stock overrides the actual move provided by
+ * the voters, which means that future moves of these voters can't be used
+ * and as such all voters will have to be re-run regeardless of whether or
+ * not cards were actually revealed. `reveal` here is not really tracking if
+ * a card is revealed, but if voters should be reset.
+ */
*reveal = true;
if (gs->stock.size() == 0) {
flip_waste(gs);
}
- unsigned turn_over = std::min(n, (unsigned) gs->stock.size());
+ /*
+ * FIXME: casting used here, but maybe it can be avoided?
+ */
+ unsigned turn_over = std::min(r->stock_deal_count,
+ (unsigned) gs->stock.size());
- for (unsigned i = 0; i < turn_over; ++i) {
- move_atop(&gs->stock, &gs->waste, 1u, reveal);
+ switch (r->stock_deal_method) {
+ case STOCK_TO_WASTE: {
+ for (unsigned i = 0; i < turn_over; ++i) {
+ move_atop(&gs->stock, &gs->waste, 1u, reveal);
+ }
+ break;
+ } case STOCK_TO_TABLEAU: {
+ for (ms_card_pile &p : gs->tableau) {
+ move_atop(&gs->stock, &p, 1u, reveal);
+ }
+ } default: {
+ assert(false);
+ }
}
return 0;
@@ -409,7 +533,7 @@ int make_move(ms_game_state *gs, ms_rules *r, ms_move *m, bool *reveal) {
*reveal = false;
if (m->stock) {
- go_through_stock(gs, r->stock_deal_count, reveal);
+ go_through_stock(gs, r, reveal);
} else {
ms_card_pile *from = get_pile_by_index(gs, r, m->from);
ms_card_pile *to = get_pile_by_index(gs, r, m->to);
@@ -429,6 +553,18 @@ char const *finished_state_str(finished_state f) {
}
}
+const char *bool_str(bool b) {
+ return b ? "true" : "false";
+}
+
+bool equal_moves(ms_move *a, ms_move *b) {
+ bool result =
+ !(ms_move_lt(*a, *b)) &&
+ !(ms_move_lt(*b, *a));
+
+ return result;
+}
+
bool opposite_move(ms_move *a, ms_move *b) {
if (!a || !b) {
return false;
@@ -446,14 +582,21 @@ bool opposite_move(ms_move *a, ms_move *b) {
}
void run_loop(ms_game_state *gs, ms_rules *r, ms_settings *s,
- std::vector<ms_move> *votes, finished_state *vote_results,
- ms_move *previous_move, bool *card_revealed_last_move,
- threadpool *thpool, thread_info *t_infos, ms_game_state *shuffled_states
- ) {
+ bool *card_revealed_last_move, threadpool *thpool, vote *votes,
+ thread_info *t_infos, ms_game_state *shuffled_states) {
+ /*
+ * Create the thread_info structs and add a job to the threadpool for each
+ * one.
+ */
for (unsigned i = 0; i < s->max_votes; ++i) {
- if (vote_results[i] == SOLUTION_FOUND
- && !votes[i].empty()
+
+ /*
+ * If this move sequence still represents the game being played it does
+ * not need to be overwritten.
+ */
+ if (votes[i].result == SOLUTION_FOUND
+ && !votes[i].moves.empty()
&& !*card_revealed_last_move) {
continue;
}
@@ -464,22 +607,46 @@ void run_loop(ms_game_state *gs, ms_rules *r, ms_settings *s,
t_infos[i].gs = &shuffled_states[i];
t_infos[i].r = r;
t_infos[i].s = s;
- t_infos[i].move_buf = &votes[i];
- t_infos[i].result = &vote_results[i];
+ t_infos[i].move_buf = &votes[i].moves;
+ t_infos[i].result = &votes[i].result;
thpool_add_work(*thpool, (void (*)(void *)) &run_thread,
(void *) &t_infos[i]);
}
+ /*
+ * Wait on all of the voters to complete.
+ */
thpool_wait(*thpool);
- std::map<ms_move, int> tallied_votes;
+ /*
+ * Print information about each voter.
+ */
+ debug("state\t\t\tmove\tstock\tmoves left\n");
+ for (unsigned i = 0; i < s->max_votes; ++i) {
+ if (votes[i].moves.empty()) {
+ debug("%s\t+x+->+\t+ \t%lu\n",
+ finished_state_str(votes[i].result),
+ votes[i].moves.size());
+ } else {
+ ms_move m = votes[i].moves.back();
+
+ debug("%s\t \t%dx%d->%d\t%s\t%lu\n",
+ finished_state_str(votes[i].result),
+ m.size, m.from, m.to, bool_str(m.stock),
+ votes[i].moves.size());
+ }
+ }
+
+ /*
+ * Count the number of occurences votes for each move.
+ */
+ std::map<ms_move, int, votecmp> tallied_votes;
for (unsigned i = 0; i < s->max_votes; ++i) {
- if (vote_results[i] == SOLUTION_FOUND) {
- assert(!votes[i].empty());
+ if (votes[i].result == SOLUTION_FOUND) {
+ assert(!votes[i].moves.empty());
- ms_move vote = votes[i].back();
- votes[i].pop_back();
+ ms_move vote = votes[i].moves.back();
auto t = tallied_votes.find(vote);
if (t == tallied_votes.end()) {
@@ -490,7 +657,10 @@ void run_loop(ms_game_state *gs, ms_rules *r, ms_settings *s,
}
}
- ms_move *max_move;
+ /*
+ * Find the move with the most votes.
+ */
+ ms_move max_move;
int max_votes = 0;
for (auto &v : tallied_votes) {
ms_move move = v.first;
@@ -499,17 +669,22 @@ 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) {
- max_move = &move;
+ max_move = move;
max_votes = vote;
}
}
if (max_votes == 0) {
+
+ /*
+ * If no votes were cast, more information about the state of the system
+ * is printed, and no move is made.
+ */
debug("no votes cast!\n");
std::map<finished_state, int> res;
for (unsigned i = 0; i < s->max_votes; ++i) {
- finished_state f = vote_results[i];
+ finished_state f = votes[i].result;
auto c = res.find(f);
res[f] = (c == res.end()) ? 1 : (c->second + 1);
}
@@ -519,12 +694,38 @@ void run_loop(ms_game_state *gs, ms_rules *r, ms_settings *s,
debug("%s: %d\n", finished_state_str(p.first), p.second);
}
} else {
- make_move(gs, r, max_move, card_revealed_last_move);
- *previous_move = *max_move;
+ /*
+ * If at least one vote is cast, the move with the most votes in taken.
+ */
+ make_move(gs, r, &max_move, card_revealed_last_move);
+
+ /*
+ * The voters that made the popular vote have their move list trimmed,
+ * and to ensure that voters that made the popular vote are the only
+ * voters that don't need to recalculate their votes, the other voters
+ * are reset.
+ */
+ for (unsigned i = 0; i < s->max_votes; ++i) {
+ if (votes[i].result == SOLUTION_FOUND) {
+ if (equal_moves(&max_move, &votes[i].moves.back())) {
+ votes[i].moves.pop_back();
+ } else {
+ debug("voter %d is being reset\n", i);
+ votes[i].result = CANCELLED;
+ }
+ }
+ }
}
}
+bool solved(ms_game_state *gs, ms_rules *r, ms_settings *s) {
+ assert(s->solved_func);
+ return s->solved_func(gs, r, s->user_data);
+}
+
bool solvable(ms_game_state *gs, ms_rules *r, ms_settings *s) {
+ assert(s->thoughtful_run_func);
+
finished_state fs = s->thoughtful_run_func(gs, r, s->user_data);
bool result = fs == SOLUTION_FOUND;
@@ -538,24 +739,32 @@ bool solvable(ms_game_state *gs, ms_rules *r, ms_settings *s) {
}
int ms_run(ms_game_state *gs, ms_rules *r, ms_settings *s) {
-#define gimme_mem(t) ((t *) malloc(sizeof(t) * s->max_votes))
- std::vector<ms_move> *votes = gimme_mem(std::vector<ms_move>);
- finished_state *vote_results = gimme_mem(finished_state);
+
+ /*
+ * Here is where the memory required for storing all of the information
+ * about the maximum number of voters is allocated. Currently the largest
+ * memory quantity that could possibly be required (the case where the max
+ * number of votes is needed), although if this number is significantly
+ * larger than the usual quantity required then maybe this should start low
+ * and be stepped up only if necessary.
+ */
+# define gimme_mem(t) ((t *) malloc(sizeof(t) * s->max_votes))
+
+ std::vector<vote> votes(s->max_votes);;
+
thread_info *t_infos = gimme_mem(thread_info);
ms_game_state *states = gimme_mem(ms_game_state);
threadpool thpool = thpool_init(s->max_concurrent_threads);
bool reveal = true;
- ms_move previous_move;
- previous_move.stock = true;
- while (!s->solved_func(gs, r, s->user_data)) {
- if (s->thoughtful_run_func && !solvable(gs, r, s)) {
+
+ while (!solved(gs, r, s)) {
+ if (!solvable(gs, r, s)) {
return 1;
}
- run_loop(gs, r, s, votes, vote_results, &previous_move,
- &reveal, &thpool, t_infos, states);
+ run_loop(gs, r, s, &reveal, &thpool, &votes[0], t_infos, states);
}
debug("successful seed: %lu\n", seed);
return 0;
@@ -608,6 +817,66 @@ ms_rules fetch_default_rules() {
return dr;
}
+void print_rules(ms_rules *r, FILE *fh = stdout) {
+ fprintf(fh, "tableau size: %u\n"
+ "build policy: %s\n"
+ "spaces policy: %s\n"
+ "move built group: %s\n"
+ "built group policy: %s\n"
+ "deck count: %u\n"
+ "max rank: %u\n"
+ "hole: %s\n"
+ "foundations: %s\n"
+ "foundations init cards: %s\n"
+ "foundations base: %u\n"
+ "foundations removable: %s\n"
+ "foundations only accept complete piles: %s\n"
+ "diagonal deal: %s\n"
+ "cell count: %u\n"
+ "cells pre-filled: %s\n"
+ "cards in stock: %u\n"
+ "stock deal method: %s\n"
+ "stock deal count: %u\n"
+ "stock redeal: %s\n"
+ "cards in reserve: %u\n"
+ "reserve stacked: %s\n"
+ "face up policy: %s\n"
+ "sequences: %u\n"
+ "sequence direction: %s\n"
+ "sequence build policy: %s\n"
+ "sequence fixed suit: %s\n"
+ "cards in accordion: %u\n"
+ "(TODO: accordion moves)",
+ r->tableau_size,
+ build_policy_str(r->build_policy),
+ spaces_policy_str(r->spaces_policy),
+ built_group_str(r->move_built_group),
+ build_policy_str(r->built_group_policy),
+ r->deck_count,
+ r->max_rank,
+ bool_str(r->hole_present),
+ bool_str(r->foundations_present),
+ bool_str(r->foundations_init_cards),
+ r->foundations_base,
+ bool_str(r->foundations_removable),
+ bool_str(r->foundations_only_comp_piles),
+ bool_str(r->diagonal_deal),
+ r->cells,
+ bool_str(r->cells_pre_filled),
+ r->stock_size,
+ stock_deal_str(r->stock_deal_method),
+ r->stock_deal_count,
+ bool_str(r->stock_redeal),
+ r->reserve_size,
+ bool_str(r->reserve_stacked),
+ face_up_policy_str(r->face_up_policy),
+ r->sequence_count,
+ direction_str(r->sequence_direction),
+ build_policy_str(r->sequence_build_policy),
+ bool_str(r->sequence_fixed_suit),
+ r->accordion_size);
+}
+
int make_deck(ms_rules *r, ms_card_pile *buf) {
buf->clear();
for (unsigned i = 0; i < r->deck_count; ++i) {
@@ -729,6 +998,7 @@ ms_game_state random_game_state(long user_seed, ms_rules *r) {
for (unsigned i = 0; i < r->tableau_size; ++i) {
for (unsigned j = 0; j <= i; ++j) {
ms_card c = deck.back();
+ c.hidden = r->face_up_policy != ALL_CARDS_FACE_UP;
deck.pop_back();
gs.tableau[i].push_back(c);
}
@@ -740,7 +1010,8 @@ ms_game_state random_game_state(long user_seed, ms_rules *r) {
for (auto &t : gs.tableau) {
ms_card c = deck.back();
deck.pop_back();
- c.hidden = i != deep - 1;
+ c.hidden = i != deep - 1
+ && r->face_up_policy != ALL_CARDS_FACE_UP;
t.push_back(c);
}
}
diff --git a/metasol.h b/metasol.h
index 68cf605..8c9d9dc 100644
--- a/metasol.h
+++ b/metasol.h
@@ -6,61 +6,87 @@
#include <stdint.h>
#include <string.h>
-enum build_policy_t {
- NO_BUILD, BUILD_SAME_SUIT, BUILD_ALTERNATING, BUILD_ANY
+/*
+ * This all looks very ugly, but saves a lot of repetition of enum values.
+ * The suite and C macros are inspired by
+ * http://www.purl.org/stefan_ram/pub/c_preprocessor_applications_en .
+ * Essentially, the suite macros expand to the declarations of an enum and two
+ * functions: one string-to-enum, the other enum-to-string. The C macro can be
+ * used later with a _gen macro to initialise a static string array filled with
+ * the contents of an enum.
+ *
+ * If this is too messy for a header file to an API, the preprocessor output can
+ * be used to get a fairly clean preprocessed version of what's below.
+ */
+#define _suite(x, _t, _gen, str_, _str, _count) \
+ enum x##_t { x##_t##_gen x##_t##_count }; \
+ x##_t str_##x(char *); \
+ const char *x##_str(x##_t)
+#define suite(x) _suite(x, _t, _gen, str_, _str, _count)
+
+#define C(x) x,
+
+#define build_policy_t_gen \
+ C(NO_BUILD)\
+ C(BUILD_SAME_SUIT)\
+ C(BUILD_ALTERNATING)\
+ C(BUILD_ANY)
+suite(build_policy);
+
+#define spaces_policy_t_gen \
+ C(NO_SPACE_FILL)\
+ C(KINGS_FILL_SPACE)\
+ C(ANY_FILL_SPACE)\
+ C(AUTO_RESERVE_THEN_WASTE)\
+ C(AUTO_WASTE_THEN_STOCK)
+suite(spaces_policy);
+
+#define built_group_t_gen \
+ C(CAN_MOVE_BUILT_GROUP)\
+ C(CANNOT_MOVE_BUILT_GROUP)\
+ C(CAN_MOVE_WHOLE_PILE)\
+ C(CAN_MOVE_MAXIMAL_GROUP)
+suite(built_group);
+
+enum {
+ KINGS_FILL_SPACES = KINGS_FILL_SPACE,
+ ANY_FILL_SPACES = ANY_FILL_SPACE
};
-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
+#define accordion_policy_t_gen \
+ C(ACCORDION_SAME_RANK)\
+ C(ACCORDION_SAME_SUIT)\
+ C(ACCORDION_ALTERNATE_COLOUR)\
+ C(ACCORDION_ANY_SUIT)
+suite(accordion_policy);
+
+#define stock_deal_t_gen \
+ C(STOCK_TO_WASTE)\
+ C(STOCK_TO_TABLEAU)
+suite(stock_deal);
+
+#define face_up_policy_t_gen \
+ C(ALL_CARDS_FACE_UP)\
+ C(TOP_CARDS_FACE_UP)
+suite(face_up_policy);
+
+#define direction_t_gen \
+ C(LEFT)\
+ C(RIGHT)\
+ C(BOTH)
+suite(direction);
+
+#define foundations_init_t_gen \
+ C(NO_FOUNDATION_INIT)\
+ C(ONE_FOUNDATION_INIT)\
+ C(ALL_FOUNDATIONS_INIT)
+suite(foundations_init);
+
+enum {
+ NO_FOUNDATIONS_INIT = NO_FOUNDATION_INIT,
};
-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 *);
+#undef C
enum card_suit { HEARTS, SPADES, CLUBS, DIAMONDS };
extern card_suit SUITS[4];
@@ -132,6 +158,11 @@ enum finished_state {
SOLUTION_FOUND, NO_SOLUTION, TIMEOUT, SOLVER_OOM, CANCELLED
};
+typedef struct {
+ finished_state result;
+ std::vector<ms_move> moves;
+} vote;
+
typedef finished_state thoughtful_run_func_t(ms_game_state *,
ms_rules *, void *);
typedef finished_state run_func_t(ms_game_state *, ms_rules *,
@@ -153,13 +184,13 @@ unsigned total_pile_count(ms_rules *r);
ms_card_pile *get_pile_by_index(ms_game_state *gs, uint8_t i);
-struct thread_info {
+typedef struct {
ms_game_state *gs;
ms_rules *r;
ms_settings *s;
std::vector<ms_move> *move_buf;
finished_state *result;
-};
+} thread_info;
ms_rules fetch_default_rules();
ms_settings fetch_default_settings();