guess.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  1. /*
  2. * guess.c: Mastermind clone.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <ctype.h>
  9. #ifdef NO_TGMATH_H
  10. # include <math.h>
  11. #else
  12. # include <tgmath.h>
  13. #endif
  14. #include "puzzles.h"
  15. #define FLASH_FRAME 0.5F
  16. enum {
  17. COL_BACKGROUND,
  18. COL_FRAME, COL_CURSOR, COL_FLASH, COL_HOLD,
  19. COL_EMPTY, /* must be COL_1 - 1 */
  20. COL_1, COL_2, COL_3, COL_4, COL_5, COL_6, COL_7, COL_8, COL_9, COL_10,
  21. COL_CORRECTPLACE, COL_CORRECTCOLOUR,
  22. NCOLOURS
  23. };
  24. struct game_params {
  25. int ncolours, npegs, nguesses;
  26. bool allow_blank, allow_multiple;
  27. };
  28. #define FEEDBACK_CORRECTPLACE 1
  29. #define FEEDBACK_CORRECTCOLOUR 2
  30. typedef struct pegrow {
  31. int npegs;
  32. int *pegs; /* 0 is 'empty' */
  33. int *feedback; /* may well be unused */
  34. } *pegrow;
  35. struct game_state {
  36. game_params params;
  37. pegrow *guesses; /* length params->nguesses */
  38. bool *holds;
  39. pegrow solution;
  40. int next_go; /* from 0 to nguesses-1;
  41. if next_go == nguesses then they've lost. */
  42. int solved; /* +1 = win, -1 = lose, 0 = still playing */
  43. };
  44. static game_params *default_params(void)
  45. {
  46. game_params *ret = snew(game_params);
  47. /* AFAIK this is the canonical Mastermind ruleset. */
  48. ret->ncolours = 6;
  49. ret->npegs = 4;
  50. ret->nguesses = 10;
  51. ret->allow_blank = false;
  52. ret->allow_multiple = true;
  53. return ret;
  54. }
  55. static void free_params(game_params *params)
  56. {
  57. sfree(params);
  58. }
  59. static game_params *dup_params(const game_params *params)
  60. {
  61. game_params *ret = snew(game_params);
  62. *ret = *params; /* structure copy */
  63. return ret;
  64. }
  65. static const struct {
  66. const char *name;
  67. game_params params;
  68. } guess_presets[] = {
  69. {"Standard", {6, 4, 10, false, true}},
  70. {"Super", {8, 5, 12, false, true}},
  71. };
  72. static bool game_fetch_preset(int i, char **name, game_params **params)
  73. {
  74. if (i < 0 || i >= lenof(guess_presets))
  75. return false;
  76. *name = dupstr(guess_presets[i].name);
  77. *params = dup_params(&guess_presets[i].params);
  78. return true;
  79. }
  80. static void decode_params(game_params *params, char const *string)
  81. {
  82. char const *p = string;
  83. game_params *defs = default_params();
  84. *params = *defs; free_params(defs);
  85. while (*p) {
  86. switch (*p++) {
  87. case 'c':
  88. params->ncolours = atoi(p);
  89. while (*p && isdigit((unsigned char)*p)) p++;
  90. break;
  91. case 'p':
  92. params->npegs = atoi(p);
  93. while (*p && isdigit((unsigned char)*p)) p++;
  94. break;
  95. case 'g':
  96. params->nguesses = atoi(p);
  97. while (*p && isdigit((unsigned char)*p)) p++;
  98. break;
  99. case 'b':
  100. params->allow_blank = true;
  101. break;
  102. case 'B':
  103. params->allow_blank = false;
  104. break;
  105. case 'm':
  106. params->allow_multiple = true;
  107. break;
  108. case 'M':
  109. params->allow_multiple = false;
  110. break;
  111. default:
  112. ;
  113. }
  114. }
  115. }
  116. static char *encode_params(const game_params *params, bool full)
  117. {
  118. char data[256];
  119. sprintf(data, "c%dp%dg%d%s%s",
  120. params->ncolours, params->npegs, params->nguesses,
  121. params->allow_blank ? "b" : "B", params->allow_multiple ? "m" : "M");
  122. return dupstr(data);
  123. }
  124. static config_item *game_configure(const game_params *params)
  125. {
  126. config_item *ret;
  127. char buf[80];
  128. ret = snewn(6, config_item);
  129. ret[0].name = "Colours";
  130. ret[0].type = C_STRING;
  131. sprintf(buf, "%d", params->ncolours);
  132. ret[0].u.string.sval = dupstr(buf);
  133. ret[1].name = "Pegs per guess";
  134. ret[1].type = C_STRING;
  135. sprintf(buf, "%d", params->npegs);
  136. ret[1].u.string.sval = dupstr(buf);
  137. ret[2].name = "Guesses";
  138. ret[2].type = C_STRING;
  139. sprintf(buf, "%d", params->nguesses);
  140. ret[2].u.string.sval = dupstr(buf);
  141. ret[3].name = "Allow blanks";
  142. ret[3].type = C_BOOLEAN;
  143. ret[3].u.boolean.bval = params->allow_blank;
  144. ret[4].name = "Allow duplicates";
  145. ret[4].type = C_BOOLEAN;
  146. ret[4].u.boolean.bval = params->allow_multiple;
  147. ret[5].name = NULL;
  148. ret[5].type = C_END;
  149. return ret;
  150. }
  151. static game_params *custom_params(const config_item *cfg)
  152. {
  153. game_params *ret = snew(game_params);
  154. ret->ncolours = atoi(cfg[0].u.string.sval);
  155. ret->npegs = atoi(cfg[1].u.string.sval);
  156. ret->nguesses = atoi(cfg[2].u.string.sval);
  157. ret->allow_blank = cfg[3].u.boolean.bval;
  158. ret->allow_multiple = cfg[4].u.boolean.bval;
  159. return ret;
  160. }
  161. static const char *validate_params(const game_params *params, bool full)
  162. {
  163. if (params->ncolours < 2 || params->npegs < 2)
  164. return "Trivial solutions are uninteresting";
  165. /* NB as well as the no. of colours we define, max(ncolours) must
  166. * also fit in an unsigned char; see new_game_desc. */
  167. if (params->ncolours > 10)
  168. return "Too many colours";
  169. if (params->nguesses < 1)
  170. return "Must have at least one guess";
  171. if (!params->allow_multiple && params->ncolours < params->npegs)
  172. return "Disallowing multiple colours requires at least as many colours as pegs";
  173. return NULL;
  174. }
  175. static pegrow new_pegrow(int npegs)
  176. {
  177. pegrow pegs = snew(struct pegrow);
  178. pegs->npegs = npegs;
  179. pegs->pegs = snewn(pegs->npegs, int);
  180. memset(pegs->pegs, 0, pegs->npegs * sizeof(int));
  181. pegs->feedback = snewn(pegs->npegs, int);
  182. memset(pegs->feedback, 0, pegs->npegs * sizeof(int));
  183. return pegs;
  184. }
  185. static pegrow dup_pegrow(pegrow pegs)
  186. {
  187. pegrow newpegs = new_pegrow(pegs->npegs);
  188. memcpy(newpegs->pegs, pegs->pegs, newpegs->npegs * sizeof(int));
  189. memcpy(newpegs->feedback, pegs->feedback, newpegs->npegs * sizeof(int));
  190. return newpegs;
  191. }
  192. static void invalidate_pegrow(pegrow pegs)
  193. {
  194. memset(pegs->pegs, -1, pegs->npegs * sizeof(int));
  195. memset(pegs->feedback, -1, pegs->npegs * sizeof(int));
  196. }
  197. static void free_pegrow(pegrow pegs)
  198. {
  199. sfree(pegs->pegs);
  200. sfree(pegs->feedback);
  201. sfree(pegs);
  202. }
  203. static char *new_game_desc(const game_params *params, random_state *rs,
  204. char **aux, bool interactive)
  205. {
  206. unsigned char *bmp = snewn(params->npegs, unsigned char);
  207. char *ret;
  208. int i, c;
  209. pegrow colcount = new_pegrow(params->ncolours);
  210. for (i = 0; i < params->npegs; i++) {
  211. newcol:
  212. c = random_upto(rs, params->ncolours);
  213. if (!params->allow_multiple && colcount->pegs[c]) goto newcol;
  214. colcount->pegs[c]++;
  215. bmp[i] = (unsigned char)(c+1);
  216. }
  217. obfuscate_bitmap(bmp, params->npegs*8, false);
  218. ret = bin2hex(bmp, params->npegs);
  219. sfree(bmp);
  220. free_pegrow(colcount);
  221. return ret;
  222. }
  223. static const char *validate_desc(const game_params *params, const char *desc)
  224. {
  225. unsigned char *bmp;
  226. int i;
  227. /* desc is just an (obfuscated) bitmap of the solution; check that
  228. * it's the correct length and (when unobfuscated) contains only
  229. * sensible colours. */
  230. if (strlen(desc) != params->npegs * 2)
  231. return "Game description is wrong length";
  232. bmp = hex2bin(desc, params->npegs);
  233. obfuscate_bitmap(bmp, params->npegs*8, true);
  234. for (i = 0; i < params->npegs; i++) {
  235. if (bmp[i] < 1 || bmp[i] > params->ncolours) {
  236. sfree(bmp);
  237. return "Game description is corrupted";
  238. }
  239. }
  240. sfree(bmp);
  241. return NULL;
  242. }
  243. static game_state *new_game(midend *me, const game_params *params,
  244. const char *desc)
  245. {
  246. game_state *state = snew(game_state);
  247. unsigned char *bmp;
  248. int i;
  249. state->params = *params;
  250. state->guesses = snewn(params->nguesses, pegrow);
  251. for (i = 0; i < params->nguesses; i++)
  252. state->guesses[i] = new_pegrow(params->npegs);
  253. state->holds = snewn(params->npegs, bool);
  254. state->solution = new_pegrow(params->npegs);
  255. bmp = hex2bin(desc, params->npegs);
  256. obfuscate_bitmap(bmp, params->npegs*8, true);
  257. for (i = 0; i < params->npegs; i++)
  258. state->solution->pegs[i] = (int)bmp[i];
  259. sfree(bmp);
  260. memset(state->holds, 0, sizeof(bool) * params->npegs);
  261. state->next_go = state->solved = 0;
  262. return state;
  263. }
  264. static game_state *dup_game(const game_state *state)
  265. {
  266. game_state *ret = snew(game_state);
  267. int i;
  268. *ret = *state;
  269. ret->guesses = snewn(state->params.nguesses, pegrow);
  270. for (i = 0; i < state->params.nguesses; i++)
  271. ret->guesses[i] = dup_pegrow(state->guesses[i]);
  272. ret->holds = snewn(state->params.npegs, bool);
  273. memcpy(ret->holds, state->holds, sizeof(bool) * state->params.npegs);
  274. ret->solution = dup_pegrow(state->solution);
  275. return ret;
  276. }
  277. static void free_game(game_state *state)
  278. {
  279. int i;
  280. free_pegrow(state->solution);
  281. for (i = 0; i < state->params.nguesses; i++)
  282. free_pegrow(state->guesses[i]);
  283. sfree(state->holds);
  284. sfree(state->guesses);
  285. sfree(state);
  286. }
  287. static char *solve_game(const game_state *state, const game_state *currstate,
  288. const char *aux, const char **error)
  289. {
  290. return dupstr("S");
  291. }
  292. static bool is_markable(const game_params *params, pegrow pegs)
  293. {
  294. int i, nset = 0, nrequired;
  295. bool ret = false;
  296. pegrow colcount = new_pegrow(params->ncolours);
  297. nrequired = params->allow_blank ? 1 : params->npegs;
  298. for (i = 0; i < params->npegs; i++) {
  299. int c = pegs->pegs[i];
  300. if (c > 0) {
  301. assert(c <= params->ncolours);
  302. colcount->pegs[c-1]++;
  303. nset++;
  304. }
  305. }
  306. if (nset < nrequired) goto done;
  307. if (!params->allow_multiple) {
  308. for (i = 0; i < params->ncolours; i++) {
  309. if (colcount->pegs[i] > 1) goto done;
  310. }
  311. }
  312. ret = true;
  313. done:
  314. free_pegrow(colcount);
  315. return ret;
  316. }
  317. struct game_ui {
  318. game_params params;
  319. pegrow curr_pegs; /* half-finished current move */
  320. bool *holds;
  321. int colour_cur; /* position of up-down colour picker cursor */
  322. int peg_cur; /* position of left-right peg picker cursor */
  323. bool display_cur, markable;
  324. int drag_col, drag_x, drag_y; /* x and y are *center* of peg! */
  325. int drag_opeg; /* peg index, if dragged from a peg (from current guess), otherwise -1 */
  326. bool show_labels; /* label the colours with numbers */
  327. pegrow hint;
  328. };
  329. static game_ui *new_ui(const game_state *state)
  330. {
  331. game_ui *ui = snew(game_ui);
  332. memset(ui, 0, sizeof(game_ui));
  333. if (state != NULL) {
  334. ui->params = state->params; /* structure copy */
  335. ui->curr_pegs = new_pegrow(state->params.npegs);
  336. ui->holds = snewn(state->params.npegs, bool);
  337. memset(ui->holds, 0, sizeof(bool)*state->params.npegs);
  338. }
  339. ui->display_cur = getenv_bool("PUZZLES_SHOW_CURSOR", false);
  340. ui->drag_opeg = -1;
  341. return ui;
  342. }
  343. static config_item *get_prefs(game_ui *ui)
  344. {
  345. config_item *ret;
  346. ret = snewn(2, config_item);
  347. ret[0].name = "Label colours with numbers";
  348. ret[0].kw = "show-labels";
  349. ret[0].type = C_BOOLEAN;
  350. ret[0].u.boolean.bval = ui->show_labels;
  351. ret[1].name = NULL;
  352. ret[1].type = C_END;
  353. return ret;
  354. }
  355. static void set_prefs(game_ui *ui, const config_item *cfg)
  356. {
  357. ui->show_labels = cfg[0].u.boolean.bval;
  358. }
  359. static void free_ui(game_ui *ui)
  360. {
  361. if (ui->hint)
  362. free_pegrow(ui->hint);
  363. if (ui->curr_pegs)
  364. free_pegrow(ui->curr_pegs);
  365. sfree(ui->holds);
  366. sfree(ui);
  367. }
  368. static char *encode_ui(const game_ui *ui)
  369. {
  370. char *ret, *p;
  371. const char *sep;
  372. int i;
  373. /*
  374. * For this game it's worth storing the contents of the current
  375. * guess, and the current set of holds.
  376. */
  377. ret = snewn(40 * ui->curr_pegs->npegs, char);
  378. p = ret;
  379. sep = "";
  380. for (i = 0; i < ui->curr_pegs->npegs; i++) {
  381. p += sprintf(p, "%s%d%s", sep, ui->curr_pegs->pegs[i],
  382. ui->holds[i] ? "_" : "");
  383. sep = ",";
  384. }
  385. *p++ = '\0';
  386. assert(p - ret < 40 * ui->curr_pegs->npegs);
  387. return sresize(ret, p - ret, char);
  388. }
  389. static void decode_ui(game_ui *ui, const char *encoding,
  390. const game_state *state)
  391. {
  392. int i;
  393. const char *p = encoding;
  394. for (i = 0; i < ui->curr_pegs->npegs; i++) {
  395. ui->curr_pegs->pegs[i] = atoi(p);
  396. if (ui->curr_pegs->pegs[i] < 0 ||
  397. ui->curr_pegs->pegs[i] > ui->params.ncolours)
  398. ui->curr_pegs->pegs[i] = 0; /* Remove invalid pegs. */
  399. while (*p && isdigit((unsigned char)*p)) p++;
  400. if (*p == '_') {
  401. /* NB: old versions didn't store holds */
  402. ui->holds[i] = true;
  403. p++;
  404. } else
  405. ui->holds[i] = false;
  406. if (*p == ',') p++;
  407. }
  408. ui->markable = is_markable(&ui->params, ui->curr_pegs);
  409. }
  410. static void game_changed_state(game_ui *ui, const game_state *oldstate,
  411. const game_state *newstate)
  412. {
  413. int i;
  414. if (newstate->next_go < oldstate->next_go) {
  415. sfree(ui->hint);
  416. ui->hint = NULL;
  417. }
  418. /* Implement holds, clear other pegs.
  419. * This does something that is arguably the Right Thing even
  420. * for undo. */
  421. for (i = 0; i < newstate->solution->npegs; i++) {
  422. if (newstate->solved)
  423. ui->holds[i] = false;
  424. else
  425. ui->holds[i] = newstate->holds[i];
  426. if (newstate->solved || (newstate->next_go == 0) || !ui->holds[i]) {
  427. ui->curr_pegs->pegs[i] = 0;
  428. } else
  429. ui->curr_pegs->pegs[i] =
  430. newstate->guesses[newstate->next_go-1]->pegs[i];
  431. }
  432. ui->markable = is_markable(&newstate->params, ui->curr_pegs);
  433. /* Clean up cursor position */
  434. if (!ui->markable && ui->peg_cur == newstate->solution->npegs)
  435. ui->peg_cur = 0;
  436. }
  437. static const char *current_key_label(const game_ui *ui,
  438. const game_state *state, int button)
  439. {
  440. if (state->solved) return "";
  441. if (button == CURSOR_SELECT) {
  442. if (ui->peg_cur == state->params.npegs) return "Submit";
  443. return "Place";
  444. }
  445. if (button == CURSOR_SELECT2 && ui->peg_cur != state->params.npegs)
  446. return "Hold";
  447. return "";
  448. }
  449. #define PEGSZ (ds->pegsz)
  450. #define PEGOFF (ds->pegsz + ds->gapsz)
  451. #define HINTSZ (ds->hintsz)
  452. #define HINTOFF (ds->hintsz + ds->gapsz)
  453. #define GAP (ds->gapsz)
  454. #define CGAP (ds->gapsz / 2)
  455. #define PEGRAD (ds->pegrad)
  456. #define HINTRAD (ds->hintrad)
  457. #define COL_OX (ds->colx)
  458. #define COL_OY (ds->coly)
  459. #define COL_X(c) (COL_OX)
  460. #define COL_Y(c) (COL_OY + (c)*PEGOFF)
  461. #define COL_W PEGOFF
  462. #define COL_H (ds->colours->npegs*PEGOFF)
  463. #define GUESS_OX (ds->guessx)
  464. #define GUESS_OY (ds->guessy)
  465. #define GUESS_X(g,p) (GUESS_OX + (p)*PEGOFF)
  466. #define GUESS_Y(g,p) (GUESS_OY + (g)*PEGOFF)
  467. #define GUESS_W (ds->solution->npegs*PEGOFF)
  468. #define GUESS_H (ds->nguesses*PEGOFF)
  469. #define HINT_OX (GUESS_OX + GUESS_W + ds->gapsz)
  470. #define HINT_OY (GUESS_OY + (PEGSZ - HINTOFF - HINTSZ) / 2)
  471. #define HINT_X(g) HINT_OX
  472. #define HINT_Y(g) (HINT_OY + (g)*PEGOFF)
  473. #define HINT_W ((ds->hintw*HINTOFF) - GAP)
  474. #define HINT_H GUESS_H
  475. #define SOLN_OX GUESS_OX
  476. #define SOLN_OY (GUESS_OY + GUESS_H + ds->gapsz + 2)
  477. #define SOLN_W GUESS_W
  478. #define SOLN_H PEGOFF
  479. struct game_drawstate {
  480. int nguesses;
  481. pegrow *guesses; /* same size as state->guesses */
  482. pegrow solution; /* only displayed if state->solved */
  483. pegrow colours; /* length ncolours, not npegs */
  484. int pegsz, hintsz, gapsz; /* peg size (diameter), etc. */
  485. int pegrad, hintrad; /* radius of peg, hint */
  486. int border;
  487. int colx, coly; /* origin of colours vertical bar */
  488. int guessx, guessy; /* origin of guesses */
  489. int solnx, solny; /* origin of solution */
  490. int hintw; /* no. of hint tiles we're wide per row */
  491. int w, h;
  492. bool started;
  493. int solved;
  494. int next_go;
  495. blitter *blit_peg;
  496. int drag_col, blit_ox, blit_oy;
  497. };
  498. static void set_peg(const game_params *params, game_ui *ui, int peg, int col)
  499. {
  500. ui->curr_pegs->pegs[peg] = col;
  501. ui->markable = is_markable(params, ui->curr_pegs);
  502. }
  503. static int mark_pegs(pegrow guess, const pegrow solution, int ncols)
  504. {
  505. int nc_place = 0, nc_colour = 0, i, j;
  506. assert(guess && solution && (guess->npegs == solution->npegs));
  507. for (i = 0; i < guess->npegs; i++) {
  508. if (guess->pegs[i] == solution->pegs[i]) nc_place++;
  509. }
  510. /* slight bit of cleverness: we have the following formula, from
  511. * http://mathworld.wolfram.com/Mastermind.html that gives:
  512. *
  513. * nc_colour = sum(colours, min(#solution, #guess)) - nc_place
  514. *
  515. * I think this is due to Knuth.
  516. */
  517. for (i = 1; i <= ncols; i++) {
  518. int n_guess = 0, n_solution = 0;
  519. for (j = 0; j < guess->npegs; j++) {
  520. if (guess->pegs[j] == i) n_guess++;
  521. if (solution->pegs[j] == i) n_solution++;
  522. }
  523. nc_colour += min(n_guess, n_solution);
  524. }
  525. nc_colour -= nc_place;
  526. debug(("mark_pegs, %d pegs, %d right place, %d right colour",
  527. guess->npegs, nc_place, nc_colour));
  528. assert((nc_colour + nc_place) <= guess->npegs);
  529. memset(guess->feedback, 0, guess->npegs*sizeof(int));
  530. for (i = 0, j = 0; i < nc_place; i++)
  531. guess->feedback[j++] = FEEDBACK_CORRECTPLACE;
  532. for (i = 0; i < nc_colour; i++)
  533. guess->feedback[j++] = FEEDBACK_CORRECTCOLOUR;
  534. return nc_place;
  535. }
  536. static char *encode_move(const game_state *from, game_ui *ui)
  537. {
  538. char *buf, *p;
  539. const char *sep;
  540. int len, i;
  541. len = ui->curr_pegs->npegs * 20 + 2;
  542. buf = snewn(len, char);
  543. p = buf;
  544. *p++ = 'G';
  545. sep = "";
  546. for (i = 0; i < ui->curr_pegs->npegs; i++) {
  547. p += sprintf(p, "%s%d%s", sep, ui->curr_pegs->pegs[i],
  548. ui->holds[i] ? "_" : "");
  549. sep = ",";
  550. }
  551. *p++ = '\0';
  552. assert(p - buf <= len);
  553. buf = sresize(buf, len, char);
  554. return buf;
  555. }
  556. static void compute_hint(const game_state *state, game_ui *ui)
  557. {
  558. /* Suggest the lexicographically first row consistent with all
  559. * previous feedback. This is not only a useful hint, but also
  560. * a reasonable strategy if applied consistently. If the user
  561. * uses hints in every turn, they may be able to intuit this
  562. * strategy, or one similar to it. I (Jonas Kölker) came up
  563. * with something close to it without seeing it in action. */
  564. /* Some performance characteristics: I want to ask for each n,
  565. * how many solutions are guessed in exactly n guesses if you
  566. * use the hint in each turn.
  567. *
  568. * With 4 pegs and 6 colours you get the following histogram:
  569. *
  570. * 1 guesses: 1 solution
  571. * 2 guesses: 4 solutions
  572. * 3 guesses: 25 solutions
  573. * 4 guesses: 108 solutions
  574. * 5 guesses: 305 solutions
  575. * 6 guesses: 602 solutions
  576. * 7 guesses: 196 solutions
  577. * 8 guesses: 49 solutions
  578. * 9 guesses: 6 solutions
  579. * (note: the tenth guess is never necessary.)
  580. *
  581. * With 5 pegs and 8 colours you get the following histogram:
  582. *
  583. * 1 guesses: 1 solution
  584. * 2 guesses: 5 solutions
  585. * 3 guesses: 43 solutions
  586. * 4 guesses: 278 solutions
  587. * 5 guesses: 1240 solutions
  588. * 6 guesses: 3515 solutions
  589. * 7 guesses: 7564 solutions
  590. * 8 guesses: 14086 solutions
  591. * 9 guesses: 4614 solutions
  592. * 10 guesses: 1239 solutions
  593. * 11 guesses: 175 solutions
  594. * 12 guesses: 7 solutions
  595. * 13 guesses: 1 solution
  596. *
  597. * The solution which takes too many guesses is {8, 8, 5, 6, 7}.
  598. * The game ID is c8p5g12Bm:4991e5e41a. */
  599. int mincolour = 1, maxcolour = 0, i, j;
  600. /* For large values of npegs and ncolours, the lexicographically
  601. * next guess make take a while to find. Finding upper and
  602. * lower limits on which colours we have to consider will speed
  603. * this up, as will caching our progress from one invocation to
  604. * the next. The latter strategy works, since if we have ruled
  605. * out a candidate we will never reverse this judgment in the
  606. * light of new information. Removing information, i.e. undo,
  607. * will require us to backtrack somehow. We backtrack by fully
  608. * forgetting our progress (and recomputing it if required). */
  609. for (i = 0; i < state->next_go; ++i)
  610. for (j = 0; j < state->params.npegs; ++j)
  611. if (state->guesses[i]->pegs[j] > maxcolour)
  612. maxcolour = state->guesses[i]->pegs[j];
  613. if (state->params.allow_multiple)
  614. maxcolour = min(maxcolour + 1, state->params.ncolours);
  615. else
  616. maxcolour = min(maxcolour + state->params.npegs,
  617. state->params.ncolours);
  618. increase_mincolour:
  619. for (i = 0; i < state->next_go; ++i) {
  620. if (state->guesses[i]->feedback[0])
  621. goto next_iteration;
  622. for (j = 0; j < state->params.npegs; ++j)
  623. if (state->guesses[i]->pegs[j] != mincolour)
  624. goto next_iteration;
  625. ++mincolour;
  626. goto increase_mincolour;
  627. next_iteration:
  628. ;
  629. }
  630. if (!ui->hint) {
  631. ui->hint = new_pegrow(state->params.npegs);
  632. for (i = 0; i < state->params.npegs; ++i)
  633. ui->hint->pegs[i] = 1;
  634. }
  635. while (ui->hint->pegs[0] <= state->params.ncolours) {
  636. if (!is_markable(&state->params, ui->hint)) goto increment_pegrow;
  637. for (i = 0; i < state->next_go; ++i) {
  638. mark_pegs(ui->hint, state->guesses[i], maxcolour);
  639. for (j = 0; j < state->params.npegs; ++j)
  640. if (ui->hint->feedback[j] != state->guesses[i]->feedback[j])
  641. goto increment_pegrow;
  642. }
  643. /* a valid guess was found; install it and return */
  644. for (i = 0; i < state->params.npegs; ++i)
  645. ui->curr_pegs->pegs[i] = ui->hint->pegs[i];
  646. ui->markable = true;
  647. ui->peg_cur = state->params.npegs;
  648. ui->display_cur = true;
  649. return;
  650. increment_pegrow:
  651. for (i = ui->hint->npegs;
  652. ++ui->hint->pegs[--i], i && ui->hint->pegs[i] > maxcolour;
  653. ui->hint->pegs[i] = mincolour);
  654. }
  655. /* No solution is compatible with the given hints. Impossible! */
  656. /* (hack new_game_desc to create invalid solutions to get here) */
  657. /* For some values of npegs and ncolours, the hinting function takes a
  658. * long time to complete. To visually indicate completion with failure,
  659. * should it ever happen, update the ui in some trivial way. This gives
  660. * the user a sense of broken(ish)ness and futility. */
  661. if (!ui->display_cur) {
  662. ui->display_cur = true;
  663. } else if (state->params.npegs == 1) {
  664. ui->display_cur = false;
  665. } else {
  666. ui->peg_cur = (ui->peg_cur + 1) % state->params.npegs;
  667. }
  668. }
  669. static char *interpret_move(const game_state *from, game_ui *ui,
  670. const game_drawstate *ds,
  671. int x, int y, int button)
  672. {
  673. int over_col = 0; /* one-indexed */
  674. int over_guess = -1; /* zero-indexed */
  675. int over_past_guess_y = -1; /* zero-indexed */
  676. int over_past_guess_x = -1; /* zero-indexed */
  677. bool over_hint = false;
  678. char *ret = NULL;
  679. int guess_ox = GUESS_X(from->next_go, 0);
  680. int guess_oy = GUESS_Y(from->next_go, 0);
  681. /*
  682. * Enable or disable labels on colours.
  683. */
  684. if (button == 'l' || button == 'L') {
  685. ui->show_labels = !ui->show_labels;
  686. return MOVE_UI_UPDATE;
  687. }
  688. if (from->solved) return NULL;
  689. if (x >= COL_OX && x < (COL_OX + COL_W) &&
  690. y >= COL_OY && y < (COL_OY + COL_H)) {
  691. over_col = ((y - COL_OY) / PEGOFF) + 1;
  692. assert(over_col >= 1 && over_col <= ds->colours->npegs);
  693. } else if (x >= guess_ox &&
  694. y >= guess_oy && y < (guess_oy + GUESS_H)) {
  695. if (x < (guess_ox + GUESS_W)) {
  696. over_guess = (x - guess_ox) / PEGOFF;
  697. assert(over_guess >= 0 && over_guess < ds->solution->npegs);
  698. } else {
  699. over_hint = true;
  700. }
  701. } else if (x >= guess_ox && x < (guess_ox + GUESS_W) &&
  702. y >= GUESS_OY && y < guess_oy) {
  703. over_past_guess_y = (y - GUESS_OY) / PEGOFF;
  704. over_past_guess_x = (x - guess_ox) / PEGOFF;
  705. assert(over_past_guess_y >= 0 && over_past_guess_y < from->next_go);
  706. assert(over_past_guess_x >= 0 && over_past_guess_x < ds->solution->npegs);
  707. }
  708. debug(("make_move: over_col %d, over_guess %d, over_hint %d,"
  709. " over_past_guess (%d,%d)", over_col, over_guess, over_hint,
  710. over_past_guess_x, over_past_guess_y));
  711. assert(ds->blit_peg);
  712. /* mouse input */
  713. if (button == LEFT_BUTTON) {
  714. if (over_col > 0) {
  715. ui->drag_col = over_col;
  716. ui->drag_opeg = -1;
  717. debug(("Start dragging from colours"));
  718. } else if (over_guess > -1) {
  719. int col = ui->curr_pegs->pegs[over_guess];
  720. if (col) {
  721. ui->drag_col = col;
  722. ui->drag_opeg = over_guess;
  723. debug(("Start dragging from a guess"));
  724. }
  725. } else if (over_past_guess_y > -1) {
  726. int col =
  727. from->guesses[over_past_guess_y]->pegs[over_past_guess_x];
  728. if (col) {
  729. ui->drag_col = col;
  730. ui->drag_opeg = -1;
  731. debug(("Start dragging from a past guess"));
  732. }
  733. }
  734. if (ui->drag_col) {
  735. ui->drag_x = x;
  736. ui->drag_y = y;
  737. debug(("Start dragging, col = %d, (%d,%d)",
  738. ui->drag_col, ui->drag_x, ui->drag_y));
  739. ret = MOVE_UI_UPDATE;
  740. }
  741. } else if (button == LEFT_DRAG && ui->drag_col) {
  742. ui->drag_x = x;
  743. ui->drag_y = y;
  744. debug(("Keep dragging, (%d,%d)", ui->drag_x, ui->drag_y));
  745. ret = MOVE_UI_UPDATE;
  746. } else if (button == LEFT_RELEASE && ui->drag_col) {
  747. if (over_guess > -1) {
  748. debug(("Dropping colour %d onto guess peg %d",
  749. ui->drag_col, over_guess));
  750. set_peg(&from->params, ui, over_guess, ui->drag_col);
  751. } else {
  752. if (ui->drag_opeg > -1) {
  753. debug(("Removing colour %d from peg %d",
  754. ui->drag_col, ui->drag_opeg));
  755. set_peg(&from->params, ui, ui->drag_opeg, 0);
  756. }
  757. }
  758. ui->drag_col = 0;
  759. ui->drag_opeg = -1;
  760. ui->display_cur = false;
  761. debug(("Stop dragging."));
  762. ret = MOVE_UI_UPDATE;
  763. } else if (button == RIGHT_BUTTON) {
  764. if (over_guess > -1) {
  765. /* we use ths feedback in the game_ui to signify
  766. * 'carry this peg to the next guess as well'. */
  767. ui->holds[over_guess] ^= 1;
  768. ret = MOVE_UI_UPDATE;
  769. }
  770. } else if (button == LEFT_RELEASE && over_hint && ui->markable) {
  771. /* NB this won't trigger if on the end of a drag; that's on
  772. * purpose, in case you drop by mistake... */
  773. ret = encode_move(from, ui);
  774. }
  775. /* keyboard input */
  776. if (button == CURSOR_UP || button == CURSOR_DOWN) {
  777. ui->display_cur = true;
  778. if (button == CURSOR_DOWN && (ui->colour_cur+1) < from->params.ncolours)
  779. ui->colour_cur++;
  780. if (button == CURSOR_UP && ui->colour_cur > 0)
  781. ui->colour_cur--;
  782. ret = MOVE_UI_UPDATE;
  783. } else if (button == 'h' || button == 'H' || button == '?') {
  784. compute_hint(from, ui);
  785. ret = MOVE_UI_UPDATE;
  786. } else if (button == CURSOR_LEFT || button == CURSOR_RIGHT) {
  787. int maxcur = from->params.npegs;
  788. if (ui->markable) maxcur++;
  789. ui->display_cur = true;
  790. if (button == CURSOR_RIGHT && (ui->peg_cur+1) < maxcur)
  791. ui->peg_cur++;
  792. if (button == CURSOR_LEFT && ui->peg_cur > 0)
  793. ui->peg_cur--;
  794. ret = MOVE_UI_UPDATE;
  795. } else if (button == CURSOR_SELECT) {
  796. ui->display_cur = true;
  797. if (ui->peg_cur == from->params.npegs) {
  798. ret = encode_move(from, ui);
  799. } else {
  800. set_peg(&from->params, ui, ui->peg_cur, ui->colour_cur+1);
  801. ret = MOVE_UI_UPDATE;
  802. }
  803. } else if (((button >= '1' && button <= '0' + from->params.ncolours) ||
  804. (button == '0' && from->params.ncolours == 10)) &&
  805. ui->peg_cur < from->params.npegs) {
  806. ui->display_cur = true;
  807. /* Number keys insert a peg and advance the cursor. */
  808. set_peg(&from->params, ui, ui->peg_cur,
  809. button == '0' ? 10 : button - '0');
  810. if (ui->peg_cur + 1 < from->params.npegs + ui->markable)
  811. ui->peg_cur++;
  812. ret = MOVE_UI_UPDATE;
  813. } else if (button == 'D' || button == 'd' || button == '\b') {
  814. ui->display_cur = true;
  815. set_peg(&from->params, ui, ui->peg_cur, 0);
  816. ret = MOVE_UI_UPDATE;
  817. } else if (button == CURSOR_SELECT2) {
  818. if (ui->peg_cur == from->params.npegs)
  819. return NULL;
  820. ui->display_cur = true;
  821. ui->holds[ui->peg_cur] ^= 1;
  822. ret = MOVE_UI_UPDATE;
  823. }
  824. return ret;
  825. }
  826. static game_state *execute_move(const game_state *from, const char *move)
  827. {
  828. int i, nc_place;
  829. game_state *ret;
  830. const char *p;
  831. if (!strcmp(move, "S")) {
  832. ret = dup_game(from);
  833. ret->solved = -1;
  834. return ret;
  835. } else if (move[0] == 'G') {
  836. /* No guesses are allowed once the game is solved. */
  837. if (from->solved) return NULL;
  838. p = move+1;
  839. ret = dup_game(from);
  840. for (i = 0; i < from->solution->npegs; i++) {
  841. int val = atoi(p);
  842. int min_colour = from->params.allow_blank? 0 : 1;
  843. if (val < min_colour || val > from->params.ncolours) {
  844. free_game(ret);
  845. return NULL;
  846. }
  847. ret->guesses[from->next_go]->pegs[i] = atoi(p);
  848. while (*p && isdigit((unsigned char)*p)) p++;
  849. if (*p == '_') {
  850. ret->holds[i] = true;
  851. p++;
  852. } else
  853. ret->holds[i] = false;
  854. if (*p == ',') p++;
  855. }
  856. nc_place = mark_pegs(ret->guesses[from->next_go], ret->solution, ret->params.ncolours);
  857. if (nc_place == ret->solution->npegs) {
  858. ret->solved = +1; /* win! */
  859. } else {
  860. ret->next_go = from->next_go + 1;
  861. if (ret->next_go >= ret->params.nguesses)
  862. ret->solved = -1; /* lose, meaning we show the pegs. */
  863. }
  864. return ret;
  865. } else
  866. return NULL;
  867. }
  868. /* ----------------------------------------------------------------------
  869. * Drawing routines.
  870. */
  871. #define PEG_PREFER_SZ 32
  872. /* next three are multipliers for pegsz. It will look much nicer if
  873. * (2*PEG_HINT) + PEG_GAP = 1.0 as the hints are formatted like that. */
  874. #define PEG_GAP 0.10
  875. #define PEG_HINT 0.35
  876. #define BORDER 0.5
  877. static void game_compute_size(const game_params *params, int tilesize,
  878. const game_ui *ui, int *x, int *y)
  879. {
  880. double hmul, vmul_c, vmul_g, vmul;
  881. int hintw = (params->npegs+1)/2;
  882. hmul = BORDER * 2.0 + /* border */
  883. 1.0 * 2.0 + /* vertical colour bar */
  884. 1.0 * params->npegs + /* guess pegs */
  885. PEG_GAP * params->npegs + /* guess gaps */
  886. PEG_HINT * hintw + /* hint pegs */
  887. PEG_GAP * (hintw - 1); /* hint gaps */
  888. vmul_c = BORDER * 2.0 + /* border */
  889. 1.0 * params->ncolours + /* colour pegs */
  890. PEG_GAP * (params->ncolours - 1); /* colour gaps */
  891. vmul_g = BORDER * 2.0 + /* border */
  892. 1.0 * (params->nguesses + 1) + /* guesses plus solution */
  893. PEG_GAP * (params->nguesses + 1); /* gaps plus gap above soln */
  894. vmul = max(vmul_c, vmul_g);
  895. *x = (int)ceil((double)tilesize * hmul);
  896. *y = (int)ceil((double)tilesize * vmul);
  897. }
  898. static void game_set_size(drawing *dr, game_drawstate *ds,
  899. const game_params *params, int tilesize)
  900. {
  901. int colh, guessh;
  902. ds->pegsz = tilesize;
  903. ds->hintsz = (int)((double)ds->pegsz * PEG_HINT);
  904. ds->gapsz = (int)((double)ds->pegsz * PEG_GAP);
  905. ds->border = (int)((double)ds->pegsz * BORDER);
  906. ds->pegrad = (ds->pegsz -1)/2; /* radius of peg to fit in pegsz (which is 2r+1) */
  907. ds->hintrad = (ds->hintsz-1)/2;
  908. colh = ((ds->pegsz + ds->gapsz) * params->ncolours) - ds->gapsz;
  909. guessh = ((ds->pegsz + ds->gapsz) * params->nguesses); /* guesses */
  910. guessh += ds->gapsz + ds->pegsz; /* solution */
  911. /* We know we don't need anything from the game_ui we haven't got */
  912. game_compute_size(params, tilesize, NULL, &ds->w, &ds->h);
  913. ds->colx = ds->border;
  914. ds->coly = (ds->h - colh) / 2;
  915. ds->guessx = ds->solnx = ds->border + ds->pegsz * 2; /* border + colours */
  916. ds->guessy = (ds->h - guessh) / 2;
  917. ds->solny = ds->guessy + ((ds->pegsz + ds->gapsz) * params->nguesses) + ds->gapsz;
  918. assert(ds->pegsz > 0);
  919. assert(!ds->blit_peg); /* set_size is never called twice */
  920. ds->blit_peg = blitter_new(dr, ds->pegsz+2, ds->pegsz+2);
  921. }
  922. static float *game_colours(frontend *fe, int *ncolours)
  923. {
  924. float *ret = snewn(3 * NCOLOURS, float), max;
  925. int i;
  926. frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
  927. /* red */
  928. ret[COL_1 * 3 + 0] = 1.0F;
  929. ret[COL_1 * 3 + 1] = 0.0F;
  930. ret[COL_1 * 3 + 2] = 0.0F;
  931. /* yellow */
  932. ret[COL_2 * 3 + 0] = 1.0F;
  933. ret[COL_2 * 3 + 1] = 1.0F;
  934. ret[COL_2 * 3 + 2] = 0.0F;
  935. /* green */
  936. ret[COL_3 * 3 + 0] = 0.0F;
  937. ret[COL_3 * 3 + 1] = 1.0F;
  938. ret[COL_3 * 3 + 2] = 0.0F;
  939. /* blue */
  940. ret[COL_4 * 3 + 0] = 0.2F;
  941. ret[COL_4 * 3 + 1] = 0.3F;
  942. ret[COL_4 * 3 + 2] = 1.0F;
  943. /* orange */
  944. ret[COL_5 * 3 + 0] = 1.0F;
  945. ret[COL_5 * 3 + 1] = 0.5F;
  946. ret[COL_5 * 3 + 2] = 0.0F;
  947. /* purple */
  948. ret[COL_6 * 3 + 0] = 0.5F;
  949. ret[COL_6 * 3 + 1] = 0.0F;
  950. ret[COL_6 * 3 + 2] = 0.7F;
  951. /* brown */
  952. ret[COL_7 * 3 + 0] = 0.5F;
  953. ret[COL_7 * 3 + 1] = 0.3F;
  954. ret[COL_7 * 3 + 2] = 0.3F;
  955. /* light blue */
  956. ret[COL_8 * 3 + 0] = 0.4F;
  957. ret[COL_8 * 3 + 1] = 0.8F;
  958. ret[COL_8 * 3 + 2] = 1.0F;
  959. /* light green */
  960. ret[COL_9 * 3 + 0] = 0.7F;
  961. ret[COL_9 * 3 + 1] = 1.0F;
  962. ret[COL_9 * 3 + 2] = 0.7F;
  963. /* pink */
  964. ret[COL_10 * 3 + 0] = 1.0F;
  965. ret[COL_10 * 3 + 1] = 0.6F;
  966. ret[COL_10 * 3 + 2] = 1.0F;
  967. ret[COL_FRAME * 3 + 0] = 0.0F;
  968. ret[COL_FRAME * 3 + 1] = 0.0F;
  969. ret[COL_FRAME * 3 + 2] = 0.0F;
  970. ret[COL_CURSOR * 3 + 0] = 0.0F;
  971. ret[COL_CURSOR * 3 + 1] = 0.0F;
  972. ret[COL_CURSOR * 3 + 2] = 0.0F;
  973. ret[COL_FLASH * 3 + 0] = 0.5F;
  974. ret[COL_FLASH * 3 + 1] = 1.0F;
  975. ret[COL_FLASH * 3 + 2] = 1.0F;
  976. ret[COL_HOLD * 3 + 0] = 1.0F;
  977. ret[COL_HOLD * 3 + 1] = 0.5F;
  978. ret[COL_HOLD * 3 + 2] = 0.5F;
  979. ret[COL_CORRECTPLACE*3 + 0] = 0.0F;
  980. ret[COL_CORRECTPLACE*3 + 1] = 0.0F;
  981. ret[COL_CORRECTPLACE*3 + 2] = 0.0F;
  982. ret[COL_CORRECTCOLOUR*3 + 0] = 1.0F;
  983. ret[COL_CORRECTCOLOUR*3 + 1] = 1.0F;
  984. ret[COL_CORRECTCOLOUR*3 + 2] = 1.0F;
  985. /* We want to make sure we can distinguish COL_CORRECTCOLOUR
  986. * (which we hard-code as white) from COL_BACKGROUND (which
  987. * could default to white on some platforms).
  988. * Code borrowed from fifteen.c. */
  989. max = ret[COL_BACKGROUND*3];
  990. for (i = 1; i < 3; i++)
  991. if (ret[COL_BACKGROUND*3+i] > max)
  992. max = ret[COL_BACKGROUND*3+i];
  993. if (max * 1.2F > 1.0F) {
  994. for (i = 0; i < 3; i++)
  995. ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
  996. }
  997. /* We also want to be able to tell the difference between BACKGROUND
  998. * and EMPTY, for similar distinguishing-hint reasons. */
  999. ret[COL_EMPTY * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0F / 3.0F;
  1000. ret[COL_EMPTY * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0F / 3.0F;
  1001. ret[COL_EMPTY * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0F / 3.0F;
  1002. *ncolours = NCOLOURS;
  1003. return ret;
  1004. }
  1005. static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
  1006. {
  1007. struct game_drawstate *ds = snew(struct game_drawstate);
  1008. int i;
  1009. memset(ds, 0, sizeof(struct game_drawstate));
  1010. ds->guesses = snewn(state->params.nguesses, pegrow);
  1011. ds->nguesses = state->params.nguesses;
  1012. for (i = 0; i < state->params.nguesses; i++) {
  1013. ds->guesses[i] = new_pegrow(state->params.npegs);
  1014. invalidate_pegrow(ds->guesses[i]);
  1015. }
  1016. ds->solution = new_pegrow(state->params.npegs);
  1017. invalidate_pegrow(ds->solution);
  1018. ds->colours = new_pegrow(state->params.ncolours);
  1019. invalidate_pegrow(ds->colours);
  1020. ds->hintw = (state->params.npegs+1)/2; /* must round up */
  1021. ds->blit_peg = NULL;
  1022. return ds;
  1023. }
  1024. static void game_free_drawstate(drawing *dr, game_drawstate *ds)
  1025. {
  1026. int i;
  1027. if (ds->blit_peg) blitter_free(dr, ds->blit_peg);
  1028. free_pegrow(ds->colours);
  1029. free_pegrow(ds->solution);
  1030. for (i = 0; i < ds->nguesses; i++)
  1031. free_pegrow(ds->guesses[i]);
  1032. sfree(ds->guesses);
  1033. sfree(ds);
  1034. }
  1035. static void draw_peg(drawing *dr, game_drawstate *ds, int cx, int cy,
  1036. bool moving, bool labelled, int col)
  1037. {
  1038. /*
  1039. * Some platforms antialias circles, which means we shouldn't
  1040. * overwrite a circle of one colour with a circle of another
  1041. * colour without erasing the background first. However, if the
  1042. * peg is the one being dragged, we don't erase the background
  1043. * because we _want_ it to alpha-blend nicely into whatever's
  1044. * behind it.
  1045. */
  1046. if (!moving)
  1047. draw_rect(dr, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2,
  1048. COL_BACKGROUND);
  1049. if (PEGRAD > 0) {
  1050. draw_circle(dr, cx+PEGRAD, cy+PEGRAD, PEGRAD,
  1051. COL_EMPTY + col, (col ? COL_FRAME : COL_EMPTY));
  1052. } else
  1053. draw_rect(dr, cx, cy, PEGSZ, PEGSZ, COL_EMPTY + col);
  1054. if (labelled && col) {
  1055. char buf[2];
  1056. buf[0] = '0' + (col % 10);
  1057. buf[1] = '\0';
  1058. draw_text(dr, cx+PEGRAD, cy+PEGRAD, FONT_VARIABLE, PEGRAD,
  1059. ALIGN_HCENTRE|ALIGN_VCENTRE, COL_FRAME, buf);
  1060. }
  1061. draw_update(dr, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2);
  1062. }
  1063. static void draw_cursor(drawing *dr, game_drawstate *ds, int x, int y)
  1064. {
  1065. draw_circle(dr, x+PEGRAD, y+PEGRAD, PEGRAD+CGAP, -1, COL_CURSOR);
  1066. draw_update(dr, x-CGAP, y-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2);
  1067. }
  1068. static void guess_redraw(drawing *dr, game_drawstate *ds, int guess,
  1069. pegrow src, bool *holds, int cur_col, bool force,
  1070. bool labelled)
  1071. {
  1072. pegrow dest;
  1073. int rowx, rowy, i, scol;
  1074. if (guess == -1) {
  1075. dest = ds->solution;
  1076. rowx = SOLN_OX;
  1077. rowy = SOLN_OY;
  1078. } else {
  1079. dest = ds->guesses[guess];
  1080. rowx = GUESS_X(guess,0);
  1081. rowy = GUESS_Y(guess,0);
  1082. }
  1083. if (src) assert(src->npegs == dest->npegs);
  1084. for (i = 0; i < dest->npegs; i++) {
  1085. scol = src ? src->pegs[i] : 0;
  1086. if (i == cur_col)
  1087. scol |= 0x1000;
  1088. if (holds && holds[i])
  1089. scol |= 0x2000;
  1090. if (labelled)
  1091. scol |= 0x4000;
  1092. if ((dest->pegs[i] != scol) || force) {
  1093. draw_peg(dr, ds, rowx + PEGOFF * i, rowy, false, labelled,
  1094. scol &~ 0x7000);
  1095. /*
  1096. * Hold marker.
  1097. */
  1098. draw_rect(dr, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2,
  1099. PEGSZ, 2, (scol & 0x2000 ? COL_HOLD : COL_BACKGROUND));
  1100. draw_update(dr, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2,
  1101. PEGSZ, 2);
  1102. if (scol & 0x1000)
  1103. draw_cursor(dr, ds, rowx + PEGOFF * i, rowy);
  1104. }
  1105. dest->pegs[i] = scol;
  1106. }
  1107. }
  1108. static void hint_redraw(drawing *dr, game_drawstate *ds, int guess,
  1109. pegrow src, bool force, bool cursor, bool markable)
  1110. {
  1111. pegrow dest = ds->guesses[guess];
  1112. int rowx, rowy, i, scol, col, hintlen;
  1113. bool need_redraw;
  1114. int emptycol = (markable ? COL_FLASH : COL_EMPTY);
  1115. if (src) assert(src->npegs == dest->npegs);
  1116. hintlen = (dest->npegs + 1)/2;
  1117. /*
  1118. * Because of the possible presence of the cursor around this
  1119. * entire section, we redraw all or none of it but never part.
  1120. */
  1121. need_redraw = false;
  1122. for (i = 0; i < dest->npegs; i++) {
  1123. scol = src ? src->feedback[i] : 0;
  1124. if (i == 0 && cursor)
  1125. scol |= 0x1000;
  1126. if (i == 0 && markable)
  1127. scol |= 0x2000;
  1128. if ((scol != dest->feedback[i]) || force) {
  1129. need_redraw = true;
  1130. }
  1131. dest->feedback[i] = scol;
  1132. }
  1133. if (need_redraw) {
  1134. int hinth = HINTSZ + GAP + HINTSZ;
  1135. int hx,hy,hw,hh;
  1136. hx = HINT_X(guess)-GAP; hy = HINT_Y(guess)-GAP;
  1137. hw = HINT_W+GAP*2; hh = hinth+GAP*2;
  1138. /* erase a large background rectangle */
  1139. draw_rect(dr, hx, hy, hw, hh, COL_BACKGROUND);
  1140. for (i = 0; i < dest->npegs; i++) {
  1141. scol = src ? src->feedback[i] : 0;
  1142. col = ((scol == FEEDBACK_CORRECTPLACE) ? COL_CORRECTPLACE :
  1143. (scol == FEEDBACK_CORRECTCOLOUR) ? COL_CORRECTCOLOUR :
  1144. emptycol);
  1145. rowx = HINT_X(guess);
  1146. rowy = HINT_Y(guess);
  1147. if (i < hintlen) {
  1148. rowx += HINTOFF * i;
  1149. } else {
  1150. rowx += HINTOFF * (i - hintlen);
  1151. rowy += HINTOFF;
  1152. }
  1153. if (HINTRAD > 0) {
  1154. draw_circle(dr, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, col,
  1155. (col == emptycol ? emptycol : COL_FRAME));
  1156. } else {
  1157. draw_rect(dr, rowx, rowy, HINTSZ, HINTSZ, col);
  1158. }
  1159. }
  1160. if (cursor) {
  1161. int x1,y1,x2,y2;
  1162. x1 = hx + CGAP; y1 = hy + CGAP;
  1163. x2 = hx + hw - CGAP; y2 = hy + hh - CGAP;
  1164. draw_line(dr, x1, y1, x2, y1, COL_CURSOR);
  1165. draw_line(dr, x2, y1, x2, y2, COL_CURSOR);
  1166. draw_line(dr, x2, y2, x1, y2, COL_CURSOR);
  1167. draw_line(dr, x1, y2, x1, y1, COL_CURSOR);
  1168. }
  1169. draw_update(dr, hx, hy, hw, hh);
  1170. }
  1171. }
  1172. static void currmove_redraw(drawing *dr, game_drawstate *ds, int guess, int col)
  1173. {
  1174. int ox = GUESS_X(guess, 0), oy = GUESS_Y(guess, 0), off = PEGSZ/4;
  1175. draw_rect(dr, ox-off-1, oy, 2, PEGSZ, col);
  1176. draw_update(dr, ox-off-1, oy, 2, PEGSZ);
  1177. }
  1178. static void game_redraw(drawing *dr, game_drawstate *ds,
  1179. const game_state *oldstate, const game_state *state,
  1180. int dir, const game_ui *ui,
  1181. float animtime, float flashtime)
  1182. {
  1183. int i;
  1184. bool new_move;
  1185. new_move = (state->next_go != ds->next_go) || !ds->started;
  1186. if (!ds->started) {
  1187. draw_rect(dr, SOLN_OX, SOLN_OY - ds->gapsz - 1, SOLN_W, 2, COL_FRAME);
  1188. draw_update(dr, 0, 0, ds->w, ds->h);
  1189. }
  1190. if (ds->drag_col != 0) {
  1191. debug(("Loading from blitter."));
  1192. blitter_load(dr, ds->blit_peg, ds->blit_ox, ds->blit_oy);
  1193. draw_update(dr, ds->blit_ox, ds->blit_oy, PEGSZ, PEGSZ);
  1194. }
  1195. /* draw the colours */
  1196. for (i = 0; i < state->params.ncolours; i++) {
  1197. int val = i+1;
  1198. if (ui->display_cur && ui->colour_cur == i)
  1199. val |= 0x1000;
  1200. if (ui->show_labels)
  1201. val |= 0x2000;
  1202. if (ds->colours->pegs[i] != val) {
  1203. draw_peg(dr, ds, COL_X(i), COL_Y(i), false, ui->show_labels, i+1);
  1204. if (val & 0x1000)
  1205. draw_cursor(dr, ds, COL_X(i), COL_Y(i));
  1206. ds->colours->pegs[i] = val;
  1207. }
  1208. }
  1209. /* draw the guesses (so far) and the hints
  1210. * (in reverse order to avoid trampling holds, and postponing the
  1211. * next_go'th to not overrender the top of the circular cursor) */
  1212. for (i = state->params.nguesses - 1; i >= 0; i--) {
  1213. if (i < state->next_go || state->solved) {
  1214. /* this info is stored in the game_state already */
  1215. guess_redraw(dr, ds, i, state->guesses[i], NULL, -1, false,
  1216. ui->show_labels);
  1217. hint_redraw(dr, ds, i, state->guesses[i],
  1218. i == (state->next_go-1), false, false);
  1219. } else if (i > state->next_go) {
  1220. /* we've not got here yet; it's blank. */
  1221. guess_redraw(dr, ds, i, NULL, NULL, -1, false, ui->show_labels);
  1222. hint_redraw(dr, ds, i, NULL, false, false, false);
  1223. }
  1224. }
  1225. if (!state->solved) {
  1226. /* this is the one we're on; the (incomplete) guess is stored in
  1227. * the game_ui. */
  1228. guess_redraw(dr, ds, state->next_go, ui->curr_pegs,
  1229. ui->holds, ui->display_cur ? ui->peg_cur : -1, false,
  1230. ui->show_labels);
  1231. hint_redraw(dr, ds, state->next_go, NULL, true,
  1232. ui->display_cur && ui->peg_cur == state->params.npegs,
  1233. ui->markable);
  1234. }
  1235. /* draw the 'current move' and 'able to mark' sign. */
  1236. if (new_move)
  1237. currmove_redraw(dr, ds, ds->next_go, COL_BACKGROUND);
  1238. if (!state->solved)
  1239. currmove_redraw(dr, ds, state->next_go, COL_HOLD);
  1240. /* draw the solution (or the big rectangle) */
  1241. if ((!state->solved ^ !ds->solved) || !ds->started) {
  1242. draw_rect(dr, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H,
  1243. state->solved ? COL_BACKGROUND : COL_EMPTY);
  1244. draw_update(dr, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H);
  1245. }
  1246. if (state->solved)
  1247. guess_redraw(dr, ds, -1, state->solution, NULL, -1, !ds->solved,
  1248. ui->show_labels);
  1249. ds->solved = state->solved;
  1250. ds->next_go = state->next_go;
  1251. /* if ui->drag_col != 0, save the screen to the blitter,
  1252. * draw the peg where we saved, and set ds->drag_* == ui->drag_*. */
  1253. if (ui->drag_col != 0) {
  1254. int ox = ui->drag_x - (PEGSZ/2);
  1255. int oy = ui->drag_y - (PEGSZ/2);
  1256. ds->blit_ox = ox - 1; ds->blit_oy = oy - 1;
  1257. debug(("Saving to blitter at (%d,%d)", ds->blit_ox, ds->blit_oy));
  1258. blitter_save(dr, ds->blit_peg, ds->blit_ox, ds->blit_oy);
  1259. draw_peg(dr, ds, ox, oy, true, ui->show_labels, ui->drag_col);
  1260. }
  1261. ds->drag_col = ui->drag_col;
  1262. ds->started = true;
  1263. }
  1264. static float game_anim_length(const game_state *oldstate,
  1265. const game_state *newstate, int dir, game_ui *ui)
  1266. {
  1267. return 0.0F;
  1268. }
  1269. static float game_flash_length(const game_state *oldstate,
  1270. const game_state *newstate, int dir, game_ui *ui)
  1271. {
  1272. return 0.0F;
  1273. }
  1274. static void game_get_cursor_location(const game_ui *ui,
  1275. const game_drawstate *ds,
  1276. const game_state *state,
  1277. const game_params *params,
  1278. int *x, int *y, int *w, int *h)
  1279. {
  1280. if(ui->display_cur && !state->solved) {
  1281. *x = GUESS_X(state->next_go, ui->peg_cur) - CGAP;
  1282. *y = GUESS_Y(state->next_go, ui->peg_cur) - CGAP;
  1283. *w = *h = 2 * (PEGRAD + CGAP) + 1;
  1284. }
  1285. }
  1286. static int game_status(const game_state *state)
  1287. {
  1288. /*
  1289. * We return nonzero whenever the solution has been revealed, even
  1290. * (on spoiler grounds) if it wasn't guessed correctly. The
  1291. * correct return value from this function is already in
  1292. * state->solved.
  1293. */
  1294. return state->solved;
  1295. }
  1296. #ifdef COMBINED
  1297. #define thegame guess
  1298. #endif
  1299. const struct game thegame = {
  1300. "Guess", "games.guess", "guess",
  1301. default_params,
  1302. game_fetch_preset, NULL,
  1303. decode_params,
  1304. encode_params,
  1305. free_params,
  1306. dup_params,
  1307. true, game_configure, custom_params,
  1308. validate_params,
  1309. new_game_desc,
  1310. validate_desc,
  1311. new_game,
  1312. dup_game,
  1313. free_game,
  1314. true, solve_game,
  1315. false, NULL, NULL, /* can_format_as_text_now, text_format */
  1316. get_prefs, set_prefs,
  1317. new_ui,
  1318. free_ui,
  1319. encode_ui,
  1320. decode_ui,
  1321. NULL, /* game_request_keys */
  1322. game_changed_state,
  1323. current_key_label,
  1324. interpret_move,
  1325. execute_move,
  1326. PEG_PREFER_SZ, game_compute_size, game_set_size,
  1327. game_colours,
  1328. game_new_drawstate,
  1329. game_free_drawstate,
  1330. game_redraw,
  1331. game_anim_length,
  1332. game_flash_length,
  1333. game_get_cursor_location,
  1334. game_status,
  1335. false, false, NULL, NULL, /* print_size, print */
  1336. false, /* wants_statusbar */
  1337. false, NULL, /* timing_state */
  1338. 0, /* flags */
  1339. };
  1340. /* vim: set shiftwidth=4 tabstop=8: */