aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--almost-finished.json12
-rw-r--r--centre.cpp103
-rw-r--r--debug.h26
-rw-r--r--metasol.cpp121
4 files changed, 173 insertions, 89 deletions
diff --git a/almost-finished.json b/almost-finished.json
deleted file mode 100644
index 6516f6f..0000000
--- a/almost-finished.json
+++ /dev/null
@@ -1,12 +0,0 @@
-{
- "foundations": [
- [ "AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC" ],
- [ "AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD" ],
- [ "AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH" ],
- [ "AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS" ]
- ],
-
- "tableau": [
- [ "KD" ], [], [], [], [], [], []
- ]
-}
diff --git a/centre.cpp b/centre.cpp
index e680019..7afd1f0 100644
--- a/centre.cpp
+++ b/centre.cpp
@@ -2,11 +2,14 @@
#include <map>
#include <algorithm>
+#include <err.h>
+
#include "solvitaire/headers/api.h"
#include "docopt/docopt.h"
#include "jsmn/jsmn.h"
#include "metasol.h"
+#include "debug.h"
static const char USAGE[] =
R"(centre - a metasol instance.
@@ -363,6 +366,35 @@ finished_state convert_result(solver::result::type r) {
}
}
+unsigned calculate_stock_moves(int n, unsigned *stock, unsigned *waste,
+ unsigned deal) {
+ unsigned stock_waste = *stock + *waste;
+
+ unsigned moves = 0;
+ while (n != 0) {
+ if (*stock == 0) {
+ *stock = stock_waste;
+ *waste = 0;
+ }
+
+ if (n < 0) {
+ n += stock_waste;
+ continue;
+ }
+
+ unsigned m = std::min(deal, *stock);
+ *stock -= m;
+ *waste += m;
+ n -= m;
+ ++moves;
+ }
+
+ assert(*waste > 0);
+ *waste -= 1u;
+
+ return moves;
+}
+
/**
* The function that is called by every voter, to get its vote.
*/
@@ -379,19 +411,62 @@ finished_state run_single(ms_game_state *gs, ms_rules *r,
if (result == SOLUTION_FOUND) {
unsigned n = std::min(move_count, (unsigned) ml.size());
moves->clear();
- moves->resize(n);
- int skip_first = is_dud_move(&ml[0]) ? 1 : 0;
- std::transform(ml.begin() + skip_first,
- ml.begin() + skip_first + move_count,
- moves->begin(),
- [gs, r](move m) -> ms_move {
- ms_move out;
- convert_move(gs, r, &m, &out);
- return out;
- });
- std::reverse(moves->begin(), moves->end());
+
+ unsigned stock = gs->stock.size();
+ unsigned waste = gs->waste.size();
+
+ /*
+ * FIXME: This section counts the number of converted moves rather than
+ * the number of moves produced during converting. This shouldn't be a
+ * problem (for now) but should be changed at some point.
+ */
+ for (unsigned i = 0; i < n; ++i) {
+ move *m = &ml[i];
+ if (is_dud_move(m)) {
+ continue;
+ }
+
+ switch (m->type) {
+ case move::mtype::regular:
+ case move::mtype::built_group: {
+ ms_move rm;
+ rm.from = m->from;
+ rm.to = m->to;
+ rm.size = m->count;
+ rm.stock = false;
+
+ moves->push_back(rm);
+ break;
+ } case move::mtype::stock_k_plus: {
+ int cards_moved = m->count;
+ unsigned stock_moves = calculate_stock_moves(cards_moved,
+ &stock, &waste, r->stock_deal_count);
+
+ for (unsigned j = 0; j < stock_moves; ++j) {
+ ms_move sm;
+ sm.stock = true;
+
+ moves->push_back(sm);
+ }
+
+ ms_move kpm;
+ kpm.from = get_waste_index(r);
+ kpm.to = m->to;
+ kpm.size = 1u;
+ kpm.stock = false;
+
+ moves->push_back(kpm);
+ break;
+ } default: {
+ errx(EXIT_FAILURE, "unimplmented move type %s",
+ mtype_str(m->type));
+ }
+ }
+ }
}
+ std::reverse(moves->begin(), moves->end());
+
return result;
}
@@ -499,7 +574,7 @@ ms_settings get_settings(user_data *d) {
s.thoughtful_run_func = &thoughtful_run;
s.solved_func = &solved;
- s.reserved_move_count = 10u;
+ s.reserved_move_count = 30u;
s.max_concurrent_threads = 24u;
s.max_votes = 10u;
@@ -745,7 +820,7 @@ ms_rules json_rules(const char *filename) {
are("sequence build policy", str_build_policy,
&r.sequence_build_policy);
- fprintf(stderr, "%s\n", &buf[tok->start]);
+ debug("%s\n", &buf[tok->start]);
assert(false);
} default: {
assert(false);
@@ -813,7 +888,7 @@ ms_game_state json_game_state(const char *filename) {
assert(false);
} default: {
- fprintf(stderr, "%d\n", tok->type);
+ debug("%d\n", tok->type);
assert(false);
}
}
diff --git a/debug.h b/debug.h
new file mode 100644
index 0000000..96447b2
--- /dev/null
+++ b/debug.h
@@ -0,0 +1,26 @@
+#ifndef DEBUG_H
+#define DEBUG_H
+
+#include <stdio.h>
+
+#ifndef NDEBUG
+# define debug(...) fprintf(stderr, __VA_ARGS__)
+#else
+# define debug(...)
+#endif
+
+static inline char *move_str_buf(ms_move *m) {
+ static const int buf_size = 30;
+ static char buf[buf_size];
+ static char *buf_p = &buf[0];
+
+ if (m->stock) {
+ snprintf(buf_p, buf_size, "stock");
+ } else {
+ snprintf(buf_p, buf_size, "%2ux %2u->%2u", m->size, m->from, m->to);
+ }
+
+ return buf;
+}
+
+#endif /* DEBUG_H */
diff --git a/metasol.cpp b/metasol.cpp
index ae38319..ec88338 100644
--- a/metasol.cpp
+++ b/metasol.cpp
@@ -8,12 +8,7 @@
#include "thpool/thpool.h"
#include "metasol.h"
-
-#ifndef NDEBUG
-# define debug(...) fprintf(stderr, __VA_ARGS__)
-#else
-# define debug(...)
-#endif
+#include "debug.h"
card_suit SUITS[4] = { HEARTS, SPADES, CLUBS, DIAMONDS };
@@ -466,19 +461,18 @@ int intlen(int i) {
return floor(log10(abs(i))) + 1;
}
-int move_atop(ms_card_pile *f, ms_card_pile *t, unsigned count, bool *reveal) {
+bool move_atop(ms_card_pile *f, ms_card_pile *t, unsigned count) {
ms_card_pile temp(f->end() - count, f->end());
f->erase(f->end() - count, f->end());
t->insert(t->end(), temp.begin(), temp.end());
if (!f->empty()) {
- if (reveal) {
- *reveal = *reveal || f->back().hidden;
- }
+ bool result = f->back().hidden;
f->back().hidden = false;
+ return result;
+ } else {
+ return false;
}
-
- return 0;
}
int flip_waste(ms_game_state *gs) {
@@ -490,17 +484,7 @@ int flip_waste(ms_game_state *gs) {
return 0;
}
-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;
-
+bool go_through_stock(ms_game_state *gs, ms_rules *r) {
if (gs->stock.size() == 0) {
flip_waste(gs);
}
@@ -511,35 +495,34 @@ int go_through_stock(ms_game_state *gs, ms_rules *r, bool *reveal) {
unsigned turn_over = std::min(r->stock_deal_count,
(unsigned) gs->stock.size());
+ bool result = false;
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);
+ bool reveal = move_atop(&gs->stock, &gs->waste, 1u);
+ result = result || reveal;
}
- break;
+ return result;
} case STOCK_TO_TABLEAU: {
for (ms_card_pile &p : gs->tableau) {
- move_atop(&gs->stock, &p, 1u, reveal);
+ bool reveal = move_atop(&gs->stock, &p, 1u);
+ result = result || reveal;
}
+ return result;
} default: {
assert(false);
}
}
-
- return 0;
}
-int make_move(ms_game_state *gs, ms_rules *r, ms_move *m, bool *reveal) {
- *reveal = false;
-
+bool make_move(ms_game_state *gs, ms_rules *r, ms_move *m) {
if (m->stock) {
- go_through_stock(gs, r, reveal);
+ return go_through_stock(gs, r);
} 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);
- move_atop(from, to, m->size, reveal);
+ return move_atop(from, to, m->size);
}
- return 0;
}
char const *finished_state_str(finished_state f) {
@@ -596,8 +579,7 @@ void run_loop(ms_game_state *gs, ms_rules *r, ms_settings *s,
* not need to be overwritten.
*/
if (votes[i].result == SOLUTION_FOUND
- && !votes[i].moves.empty()
- && !*card_revealed_last_move) {
+ && !votes[i].moves.empty()) {
continue;
}
@@ -622,27 +604,26 @@ void run_loop(ms_game_state *gs, ms_rules *r, ms_settings *s,
/*
* Print information about each voter.
*/
- debug("state\t\t\tmove\tstock\tmoves left\n");
+ debug("state\t\t\tmove\tmoves left\n");
+ for (unsigned i = 0; i < s->max_votes; ++i) {
+ }
+
+ /*
+ * Count the number of occurences of votes for each move.
+ */
+ std::map<ms_move, int, votecmp> tallied_votes;
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());
+ debug("%-15s\n", finished_state_str(votes[i].result));
} else {
ms_move m = votes[i].moves.back();
- debug("%s\t \t%dx%d->%d\t%s\t%lu\n",
+ debug("%-15s %-10s %3lu\n",
finished_state_str(votes[i].result),
- m.size, m.from, m.to, bool_str(m.stock),
+ move_str_buf(&m),
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 (votes[i].result == SOLUTION_FOUND) {
assert(!votes[i].moves.empty());
@@ -666,7 +647,7 @@ void run_loop(ms_game_state *gs, ms_rules *r, ms_settings *s,
ms_move move = v.first;
int vote = v.second;
- debug("%u->%u(%u) has %d votes\n", move.from, move.to, move.size, vote);
+ debug("%s has %d votes\n", move_str_buf(&move), vote);
if (vote > max_votes) {
max_move = move;
@@ -694,24 +675,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 {
+
/*
* 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);
+ *card_revealed_last_move = make_move(gs, r, &max_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;
+ if (*card_revealed_last_move) {
+
+ debug("card revealed; resetting all voters\n");
+
+ /*
+ * Revealing a card means that all the voters have to be reset.
+ */
+ for (unsigned i = 0; i < s->max_votes; ++i) {
+ votes[i].result = CANCELLED;
+ }
+ } else {
+
+ /*
+ * 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;
+ }
}
}
}