flip.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. /*
  2. * flip.c: Puzzle involving lighting up all the squares on a grid,
  3. * where each click toggles an overlapping set of lights.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <assert.h>
  9. #include <ctype.h>
  10. #include <limits.h>
  11. #ifdef NO_TGMATH_H
  12. # include <math.h>
  13. #else
  14. # include <tgmath.h>
  15. #endif
  16. #include "puzzles.h"
  17. #include "tree234.h"
  18. enum {
  19. COL_BACKGROUND,
  20. COL_WRONG,
  21. COL_RIGHT,
  22. COL_GRID,
  23. COL_DIAG,
  24. COL_HINT,
  25. COL_CURSOR,
  26. NCOLOURS
  27. };
  28. #define PREFERRED_TILE_SIZE 48
  29. #define TILE_SIZE (ds->tilesize)
  30. #define BORDER (TILE_SIZE / 2)
  31. #define COORD(x) ( (x) * TILE_SIZE + BORDER )
  32. #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
  33. #define ANIM_TIME 0.25F
  34. #define FLASH_FRAME 0.07F
  35. /*
  36. * Possible ways to decide which lights are toggled by each click.
  37. * Essentially, each of these describes a means of inventing a
  38. * matrix over GF(2).
  39. */
  40. enum {
  41. CROSSES, RANDOM
  42. };
  43. struct game_params {
  44. int w, h;
  45. int matrix_type;
  46. };
  47. /*
  48. * This structure is shared between all the game_states describing
  49. * a particular game, so it's reference-counted.
  50. */
  51. struct matrix {
  52. int refcount;
  53. unsigned char *matrix; /* array of (w*h) by (w*h) */
  54. };
  55. struct game_state {
  56. int w, h;
  57. int moves;
  58. bool completed, cheated, hints_active;
  59. unsigned char *grid; /* array of w*h */
  60. struct matrix *matrix;
  61. };
  62. static game_params *default_params(void)
  63. {
  64. game_params *ret = snew(game_params);
  65. ret->w = ret->h = 5;
  66. ret->matrix_type = CROSSES;
  67. return ret;
  68. }
  69. static const struct game_params flip_presets[] = {
  70. {3, 3, CROSSES},
  71. {4, 4, CROSSES},
  72. {5, 5, CROSSES},
  73. {3, 3, RANDOM},
  74. {4, 4, RANDOM},
  75. {5, 5, RANDOM},
  76. };
  77. static bool game_fetch_preset(int i, char **name, game_params **params)
  78. {
  79. game_params *ret;
  80. char str[80];
  81. if (i < 0 || i >= lenof(flip_presets))
  82. return false;
  83. ret = snew(game_params);
  84. *ret = flip_presets[i];
  85. sprintf(str, "%dx%d %s", ret->w, ret->h,
  86. ret->matrix_type == CROSSES ? "Crosses" : "Random");
  87. *name = dupstr(str);
  88. *params = ret;
  89. return true;
  90. }
  91. static void free_params(game_params *params)
  92. {
  93. sfree(params);
  94. }
  95. static game_params *dup_params(const game_params *params)
  96. {
  97. game_params *ret = snew(game_params);
  98. *ret = *params; /* structure copy */
  99. return ret;
  100. }
  101. static void decode_params(game_params *ret, char const *string)
  102. {
  103. ret->w = ret->h = atoi(string);
  104. while (*string && isdigit((unsigned char)*string)) string++;
  105. if (*string == 'x') {
  106. string++;
  107. ret->h = atoi(string);
  108. while (*string && isdigit((unsigned char)*string)) string++;
  109. }
  110. if (*string == 'r') {
  111. string++;
  112. ret->matrix_type = RANDOM;
  113. } else if (*string == 'c') {
  114. string++;
  115. ret->matrix_type = CROSSES;
  116. }
  117. }
  118. static char *encode_params(const game_params *params, bool full)
  119. {
  120. char data[256];
  121. sprintf(data, "%dx%d%s", params->w, params->h,
  122. !full ? "" : params->matrix_type == CROSSES ? "c" : "r");
  123. return dupstr(data);
  124. }
  125. static config_item *game_configure(const game_params *params)
  126. {
  127. config_item *ret = snewn(4, config_item);
  128. char buf[80];
  129. ret[0].name = "Width";
  130. ret[0].type = C_STRING;
  131. sprintf(buf, "%d", params->w);
  132. ret[0].u.string.sval = dupstr(buf);
  133. ret[1].name = "Height";
  134. ret[1].type = C_STRING;
  135. sprintf(buf, "%d", params->h);
  136. ret[1].u.string.sval = dupstr(buf);
  137. ret[2].name = "Shape type";
  138. ret[2].type = C_CHOICES;
  139. ret[2].u.choices.choicenames = ":Crosses:Random";
  140. ret[2].u.choices.selected = params->matrix_type;
  141. ret[3].name = NULL;
  142. ret[3].type = C_END;
  143. return ret;
  144. }
  145. static game_params *custom_params(const config_item *cfg)
  146. {
  147. game_params *ret = snew(game_params);
  148. ret->w = atoi(cfg[0].u.string.sval);
  149. ret->h = atoi(cfg[1].u.string.sval);
  150. ret->matrix_type = cfg[2].u.choices.selected;
  151. return ret;
  152. }
  153. static const char *validate_params(const game_params *params, bool full)
  154. {
  155. int wh;
  156. if (params->w <= 0 || params->h <= 0)
  157. return "Width and height must both be greater than zero";
  158. if (params->w > (INT_MAX - 3) / params->h)
  159. return "Width times height must not be unreasonably large";
  160. wh = params->w * params->h;
  161. if (wh > (INT_MAX - 3) / wh)
  162. return "Width times height is too large";
  163. return NULL;
  164. }
  165. static char *encode_bitmap(unsigned char *bmp, int len)
  166. {
  167. int slen = (len + 3) / 4;
  168. char *ret;
  169. int i;
  170. ret = snewn(slen + 1, char);
  171. for (i = 0; i < slen; i++) {
  172. int j, v;
  173. v = 0;
  174. for (j = 0; j < 4; j++)
  175. if (i*4+j < len && bmp[i*4+j])
  176. v |= 8 >> j;
  177. ret[i] = "0123456789abcdef"[v];
  178. }
  179. ret[slen] = '\0';
  180. return ret;
  181. }
  182. static void decode_bitmap(unsigned char *bmp, int len, const char *hex)
  183. {
  184. int slen = (len + 3) / 4;
  185. int i;
  186. for (i = 0; i < slen; i++) {
  187. int j, v, c = hex[i];
  188. if (c >= '0' && c <= '9')
  189. v = c - '0';
  190. else if (c >= 'A' && c <= 'F')
  191. v = c - 'A' + 10;
  192. else if (c >= 'a' && c <= 'f')
  193. v = c - 'a' + 10;
  194. else
  195. v = 0; /* shouldn't happen */
  196. for (j = 0; j < 4; j++) {
  197. if (i*4+j < len) {
  198. if (v & (8 >> j))
  199. bmp[i*4+j] = 1;
  200. else
  201. bmp[i*4+j] = 0;
  202. }
  203. }
  204. }
  205. }
  206. /*
  207. * Structure used during random matrix generation, and a compare
  208. * function to permit storage in a tree234.
  209. */
  210. struct sq {
  211. int cx, cy; /* coords of click square */
  212. int x, y; /* coords of output square */
  213. /*
  214. * Number of click squares which currently affect this output
  215. * square.
  216. */
  217. int coverage;
  218. /*
  219. * Number of output squares currently affected by this click
  220. * square.
  221. */
  222. int ominosize;
  223. };
  224. #define SORT(field) do { \
  225. if (a->field < b->field) \
  226. return -1; \
  227. else if (a->field > b->field) \
  228. return +1; \
  229. } while (0)
  230. /*
  231. * Compare function for choosing the next square to add. We must
  232. * sort by coverage, then by omino size, then everything else.
  233. */
  234. static int sqcmp_pick(void *av, void *bv)
  235. {
  236. struct sq *a = (struct sq *)av;
  237. struct sq *b = (struct sq *)bv;
  238. SORT(coverage);
  239. SORT(ominosize);
  240. SORT(cy);
  241. SORT(cx);
  242. SORT(y);
  243. SORT(x);
  244. return 0;
  245. }
  246. /*
  247. * Compare function for adjusting the coverage figures after a
  248. * change. We sort first by coverage and output square, then by
  249. * everything else.
  250. */
  251. static int sqcmp_cov(void *av, void *bv)
  252. {
  253. struct sq *a = (struct sq *)av;
  254. struct sq *b = (struct sq *)bv;
  255. SORT(coverage);
  256. SORT(y);
  257. SORT(x);
  258. SORT(ominosize);
  259. SORT(cy);
  260. SORT(cx);
  261. return 0;
  262. }
  263. /*
  264. * Compare function for adjusting the omino sizes after a change.
  265. * We sort first by omino size and input square, then by everything
  266. * else.
  267. */
  268. static int sqcmp_osize(void *av, void *bv)
  269. {
  270. struct sq *a = (struct sq *)av;
  271. struct sq *b = (struct sq *)bv;
  272. SORT(ominosize);
  273. SORT(cy);
  274. SORT(cx);
  275. SORT(coverage);
  276. SORT(y);
  277. SORT(x);
  278. return 0;
  279. }
  280. static void addsq(tree234 *t, int w, int h, int cx, int cy,
  281. int x, int y, unsigned char *matrix)
  282. {
  283. int wh = w * h;
  284. struct sq *sq;
  285. int i;
  286. if (x < 0 || x >= w || y < 0 || y >= h)
  287. return;
  288. if (abs(x-cx) > 1 || abs(y-cy) > 1)
  289. return;
  290. if (matrix[(cy*w+cx) * wh + y*w+x])
  291. return;
  292. sq = snew(struct sq);
  293. sq->cx = cx;
  294. sq->cy = cy;
  295. sq->x = x;
  296. sq->y = y;
  297. sq->coverage = sq->ominosize = 0;
  298. for (i = 0; i < wh; i++) {
  299. if (matrix[i * wh + y*w+x])
  300. sq->coverage++;
  301. if (matrix[(cy*w+cx) * wh + i])
  302. sq->ominosize++;
  303. }
  304. if (add234(t, sq) != sq)
  305. sfree(sq); /* already there */
  306. }
  307. static void addneighbours(tree234 *t, int w, int h, int cx, int cy,
  308. int x, int y, unsigned char *matrix)
  309. {
  310. addsq(t, w, h, cx, cy, x-1, y, matrix);
  311. addsq(t, w, h, cx, cy, x+1, y, matrix);
  312. addsq(t, w, h, cx, cy, x, y-1, matrix);
  313. addsq(t, w, h, cx, cy, x, y+1, matrix);
  314. }
  315. static char *new_game_desc(const game_params *params, random_state *rs,
  316. char **aux, bool interactive)
  317. {
  318. int w = params->w, h = params->h, wh = w * h;
  319. int i, j;
  320. unsigned char *matrix, *grid;
  321. char *mbmp, *gbmp, *ret;
  322. matrix = snewn(wh * wh, unsigned char);
  323. grid = snewn(wh, unsigned char);
  324. /*
  325. * First set up the matrix.
  326. */
  327. switch (params->matrix_type) {
  328. case CROSSES:
  329. for (i = 0; i < wh; i++) {
  330. int ix = i % w, iy = i / w;
  331. for (j = 0; j < wh; j++) {
  332. int jx = j % w, jy = j / w;
  333. if (abs(jx - ix) + abs(jy - iy) <= 1)
  334. matrix[i*wh+j] = 1;
  335. else
  336. matrix[i*wh+j] = 0;
  337. }
  338. }
  339. break;
  340. case RANDOM:
  341. while (1) {
  342. tree234 *pick, *cov, *osize;
  343. int limit;
  344. pick = newtree234(sqcmp_pick);
  345. cov = newtree234(sqcmp_cov);
  346. osize = newtree234(sqcmp_osize);
  347. memset(matrix, 0, wh * wh);
  348. for (i = 0; i < wh; i++) {
  349. matrix[i*wh+i] = 1;
  350. }
  351. for (i = 0; i < wh; i++) {
  352. int ix = i % w, iy = i / w;
  353. addneighbours(pick, w, h, ix, iy, ix, iy, matrix);
  354. addneighbours(cov, w, h, ix, iy, ix, iy, matrix);
  355. addneighbours(osize, w, h, ix, iy, ix, iy, matrix);
  356. }
  357. /*
  358. * Repeatedly choose a square to add to the matrix,
  359. * until we have enough. I'll arbitrarily choose our
  360. * limit to be the same as the total number of set bits
  361. * in the crosses matrix.
  362. */
  363. limit = 4*wh - 2*(w+h); /* centre squares already present */
  364. while (limit-- > 0) {
  365. struct sq *sq, *sq2, sqlocal;
  366. int k;
  367. /*
  368. * Find the lowest element in the pick tree.
  369. */
  370. sq = index234(pick, 0);
  371. /*
  372. * Find the highest element with the same coverage
  373. * and omino size, by setting all other elements to
  374. * lots.
  375. */
  376. sqlocal = *sq;
  377. sqlocal.cx = sqlocal.cy = sqlocal.x = sqlocal.y = wh;
  378. sq = findrelpos234(pick, &sqlocal, NULL, REL234_LT, &k);
  379. assert(sq != 0);
  380. /*
  381. * Pick at random from all elements up to k of the
  382. * pick tree.
  383. */
  384. k = random_upto(rs, k+1);
  385. sq = delpos234(pick, k);
  386. del234(cov, sq);
  387. del234(osize, sq);
  388. /*
  389. * Add this square to the matrix.
  390. */
  391. matrix[(sq->cy * w + sq->cx) * wh + (sq->y * w + sq->x)] = 1;
  392. /*
  393. * Correct the matrix coverage field of any sq
  394. * which points at this output square.
  395. */
  396. sqlocal = *sq;
  397. sqlocal.cx = sqlocal.cy = sqlocal.ominosize = -1;
  398. while ((sq2 = findrel234(cov, &sqlocal, NULL,
  399. REL234_GT)) != NULL &&
  400. sq2->coverage == sq->coverage &&
  401. sq2->x == sq->x && sq2->y == sq->y) {
  402. del234(pick, sq2);
  403. del234(cov, sq2);
  404. del234(osize, sq2);
  405. sq2->coverage++;
  406. add234(pick, sq2);
  407. add234(cov, sq2);
  408. add234(osize, sq2);
  409. }
  410. /*
  411. * Correct the omino size field of any sq which
  412. * points at this input square.
  413. */
  414. sqlocal = *sq;
  415. sqlocal.x = sqlocal.y = sqlocal.coverage = -1;
  416. while ((sq2 = findrel234(osize, &sqlocal, NULL,
  417. REL234_GT)) != NULL &&
  418. sq2->ominosize == sq->ominosize &&
  419. sq2->cx == sq->cx && sq2->cy == sq->cy) {
  420. del234(pick, sq2);
  421. del234(cov, sq2);
  422. del234(osize, sq2);
  423. sq2->ominosize++;
  424. add234(pick, sq2);
  425. add234(cov, sq2);
  426. add234(osize, sq2);
  427. }
  428. /*
  429. * The sq we actually picked out of the tree is
  430. * finished with; but its neighbours now need to
  431. * appear.
  432. */
  433. addneighbours(pick, w,h, sq->cx,sq->cy, sq->x,sq->y, matrix);
  434. addneighbours(cov, w,h, sq->cx,sq->cy, sq->x,sq->y, matrix);
  435. addneighbours(osize, w,h, sq->cx,sq->cy, sq->x,sq->y, matrix);
  436. sfree(sq);
  437. }
  438. /*
  439. * Free all remaining sq structures.
  440. */
  441. {
  442. struct sq *sq;
  443. while ((sq = delpos234(pick, 0)) != NULL)
  444. sfree(sq);
  445. }
  446. freetree234(pick);
  447. freetree234(cov);
  448. freetree234(osize);
  449. /*
  450. * Finally, check to see if any two matrix rows are
  451. * exactly identical. If so, this is not an acceptable
  452. * matrix, and we give up and go round again.
  453. *
  454. * I haven't been immediately able to think of a
  455. * plausible means of algorithmically avoiding this
  456. * situation (by, say, making a small perturbation to
  457. * an offending matrix), so for the moment I'm just
  458. * going to deal with it by throwing the whole thing
  459. * away. I suspect this will lead to scalability
  460. * problems (since most of the things happening in
  461. * these matrices are local, the chance of _some_
  462. * neighbourhood having two identical regions will
  463. * increase with the grid area), but so far this puzzle
  464. * seems to be really hard at large sizes so I'm not
  465. * massively worried yet. Anyone needs this done
  466. * better, they're welcome to submit a patch.
  467. */
  468. for (i = 0; i < wh; i++) {
  469. for (j = 0; j < wh; j++)
  470. if (i != j &&
  471. !memcmp(matrix + i * wh, matrix + j * wh, wh))
  472. break;
  473. if (j < wh)
  474. break;
  475. }
  476. if (i == wh)
  477. break; /* no matches found */
  478. }
  479. break;
  480. }
  481. /*
  482. * Now invent a random initial set of lights.
  483. *
  484. * At first glance it looks as if it might be quite difficult
  485. * to choose equiprobably from all soluble light sets. After
  486. * all, soluble light sets are those in the image space of the
  487. * transformation matrix; so first we'd have to identify that
  488. * space and its dimension, then pick a random coordinate for
  489. * each basis vector and recombine. Lot of fiddly matrix
  490. * algebra there.
  491. *
  492. * However, vector spaces are nicely orthogonal and relieve us
  493. * of all that difficulty. For every point in the image space,
  494. * there are precisely as many points in the input space that
  495. * map to it as there are elements in the kernel of the
  496. * transformation matrix (because adding any kernel element to
  497. * the input does not change the output, and because any two
  498. * inputs mapping to the same output must differ by an element
  499. * of the kernel because that's what the kernel _is_); and
  500. * these cosets are all disjoint (obviously, since no input
  501. * point can map to more than one output point) and cover the
  502. * whole space (equally obviously, because no input point can
  503. * map to fewer than one output point!).
  504. *
  505. * So the input space contains the same number of points for
  506. * each point in the output space; thus, we can simply choose
  507. * equiprobably from elements of the _input_ space, and filter
  508. * the result through the transformation matrix in the obvious
  509. * way, and we thereby guarantee to choose equiprobably from
  510. * all the output points. Phew!
  511. */
  512. while (1) {
  513. memset(grid, 0, wh);
  514. for (i = 0; i < wh; i++) {
  515. int v = random_upto(rs, 2);
  516. if (v) {
  517. for (j = 0; j < wh; j++)
  518. grid[j] ^= matrix[i*wh+j];
  519. }
  520. }
  521. /*
  522. * Ensure we don't have the starting state already!
  523. */
  524. for (i = 0; i < wh; i++)
  525. if (grid[i])
  526. break;
  527. if (i < wh)
  528. break;
  529. }
  530. /*
  531. * Now encode the matrix and the starting grid as a game
  532. * description. We'll do this by concatenating two great big
  533. * hex bitmaps.
  534. */
  535. mbmp = encode_bitmap(matrix, wh*wh);
  536. gbmp = encode_bitmap(grid, wh);
  537. ret = snewn(strlen(mbmp) + strlen(gbmp) + 2, char);
  538. sprintf(ret, "%s,%s", mbmp, gbmp);
  539. sfree(mbmp);
  540. sfree(gbmp);
  541. sfree(matrix);
  542. sfree(grid);
  543. return ret;
  544. }
  545. static const char *validate_desc(const game_params *params, const char *desc)
  546. {
  547. int w = params->w, h = params->h, wh = w * h;
  548. int mlen = (wh*wh+3)/4, glen = (wh+3)/4;
  549. if (strspn(desc, "0123456789abcdefABCDEF") != mlen)
  550. return "Matrix description is wrong length";
  551. if (desc[mlen] != ',')
  552. return "Expected comma after matrix description";
  553. if (strspn(desc+mlen+1, "0123456789abcdefABCDEF") != glen)
  554. return "Grid description is wrong length";
  555. if (desc[mlen+1+glen])
  556. return "Unexpected data after grid description";
  557. return NULL;
  558. }
  559. static game_state *new_game(midend *me, const game_params *params,
  560. const char *desc)
  561. {
  562. int w = params->w, h = params->h, wh = w * h;
  563. int mlen = (wh*wh+3)/4;
  564. game_state *state = snew(game_state);
  565. state->w = w;
  566. state->h = h;
  567. state->completed = false;
  568. state->cheated = false;
  569. state->hints_active = false;
  570. state->moves = 0;
  571. state->matrix = snew(struct matrix);
  572. state->matrix->refcount = 1;
  573. state->matrix->matrix = snewn(wh*wh, unsigned char);
  574. decode_bitmap(state->matrix->matrix, wh*wh, desc);
  575. state->grid = snewn(wh, unsigned char);
  576. decode_bitmap(state->grid, wh, desc + mlen + 1);
  577. return state;
  578. }
  579. static game_state *dup_game(const game_state *state)
  580. {
  581. game_state *ret = snew(game_state);
  582. ret->w = state->w;
  583. ret->h = state->h;
  584. ret->completed = state->completed;
  585. ret->cheated = state->cheated;
  586. ret->hints_active = state->hints_active;
  587. ret->moves = state->moves;
  588. ret->matrix = state->matrix;
  589. state->matrix->refcount++;
  590. ret->grid = snewn(ret->w * ret->h, unsigned char);
  591. memcpy(ret->grid, state->grid, ret->w * ret->h);
  592. return ret;
  593. }
  594. static void free_game(game_state *state)
  595. {
  596. sfree(state->grid);
  597. if (--state->matrix->refcount <= 0) {
  598. sfree(state->matrix->matrix);
  599. sfree(state->matrix);
  600. }
  601. sfree(state);
  602. }
  603. static void rowxor(unsigned char *row1, unsigned char *row2, int len)
  604. {
  605. int i;
  606. for (i = 0; i < len; i++)
  607. row1[i] ^= row2[i];
  608. }
  609. static char *solve_game(const game_state *state, const game_state *currstate,
  610. const char *aux, const char **error)
  611. {
  612. int w = state->w, h = state->h, wh = w * h;
  613. unsigned char *equations, *solution, *shortest;
  614. int *und, nund;
  615. int rowsdone, colsdone;
  616. int i, j, k, len, bestlen;
  617. char *ret;
  618. /*
  619. * Set up a list of simultaneous equations. Each one is of
  620. * length (wh+1) and has wh coefficients followed by a value.
  621. */
  622. equations = snewn((wh + 1) * wh, unsigned char);
  623. for (i = 0; i < wh; i++) {
  624. for (j = 0; j < wh; j++)
  625. equations[i * (wh+1) + j] = currstate->matrix->matrix[j*wh+i];
  626. equations[i * (wh+1) + wh] = currstate->grid[i] & 1;
  627. }
  628. /*
  629. * Perform Gaussian elimination over GF(2).
  630. */
  631. rowsdone = colsdone = 0;
  632. nund = 0;
  633. und = snewn(wh, int);
  634. do {
  635. /*
  636. * Find the leftmost column which has a 1 in it somewhere
  637. * outside the first `rowsdone' rows.
  638. */
  639. j = -1;
  640. for (i = colsdone; i < wh; i++) {
  641. for (j = rowsdone; j < wh; j++)
  642. if (equations[j * (wh+1) + i])
  643. break;
  644. if (j < wh)
  645. break; /* found one */
  646. /*
  647. * This is a column which will not have an equation
  648. * controlling it. Mark it as undetermined.
  649. */
  650. und[nund++] = i;
  651. }
  652. /*
  653. * If there wasn't one, then we've finished: all remaining
  654. * equations are of the form 0 = constant. Check to see if
  655. * any of them wants 0 to be equal to 1; this is the
  656. * condition which indicates an insoluble problem
  657. * (therefore _hopefully_ one typed in by a user!).
  658. */
  659. if (i == wh) {
  660. for (j = rowsdone; j < wh; j++)
  661. if (equations[j * (wh+1) + wh]) {
  662. *error = "No solution exists for this position";
  663. sfree(equations);
  664. sfree(und);
  665. return NULL;
  666. }
  667. break;
  668. }
  669. /*
  670. * We've found a 1. It's in column i, and the topmost 1 in
  671. * that column is in row j. Do a row-XOR to move it up to
  672. * the topmost row if it isn't already there.
  673. */
  674. assert(j != -1);
  675. if (j > rowsdone)
  676. rowxor(equations + rowsdone*(wh+1), equations + j*(wh+1), wh+1);
  677. /*
  678. * Do row-XORs to eliminate that 1 from all rows below the
  679. * topmost row.
  680. */
  681. for (j = rowsdone + 1; j < wh; j++)
  682. if (equations[j*(wh+1) + i])
  683. rowxor(equations + j*(wh+1),
  684. equations + rowsdone*(wh+1), wh+1);
  685. /*
  686. * Mark this row and column as done.
  687. */
  688. rowsdone++;
  689. colsdone = i+1;
  690. /*
  691. * If we've done all the rows, terminate.
  692. */
  693. } while (rowsdone < wh);
  694. /*
  695. * If we reach here, we have the ability to produce a solution.
  696. * So we go through _all_ possible solutions (each
  697. * corresponding to a set of arbitrary choices of those
  698. * components not directly determined by an equation), and pick
  699. * one requiring the smallest number of flips.
  700. */
  701. solution = snewn(wh, unsigned char);
  702. shortest = snewn(wh, unsigned char);
  703. memset(solution, 0, wh);
  704. bestlen = wh + 1;
  705. while (1) {
  706. /*
  707. * Find a solution based on the current values of the
  708. * undetermined variables.
  709. */
  710. for (j = rowsdone; j-- ;) {
  711. int v;
  712. /*
  713. * Find the leftmost set bit in this equation.
  714. */
  715. for (i = 0; i < wh; i++)
  716. if (equations[j * (wh+1) + i])
  717. break;
  718. assert(i < wh); /* there must have been one! */
  719. /*
  720. * Compute this variable using the rest.
  721. */
  722. v = equations[j * (wh+1) + wh];
  723. for (k = i+1; k < wh; k++)
  724. if (equations[j * (wh+1) + k])
  725. v ^= solution[k];
  726. solution[i] = v;
  727. }
  728. /*
  729. * Compare this solution to the current best one, and
  730. * replace the best one if this one is shorter.
  731. */
  732. len = 0;
  733. for (i = 0; i < wh; i++)
  734. if (solution[i])
  735. len++;
  736. if (len < bestlen) {
  737. bestlen = len;
  738. memcpy(shortest, solution, wh);
  739. }
  740. /*
  741. * Now increment the binary number given by the
  742. * undetermined variables: turn all 1s into 0s until we see
  743. * a 0, at which point we turn it into a 1.
  744. */
  745. for (i = 0; i < nund; i++) {
  746. solution[und[i]] = !solution[und[i]];
  747. if (solution[und[i]])
  748. break;
  749. }
  750. /*
  751. * If we didn't find a 0 at any point, we have wrapped
  752. * round and are back at the start, i.e. we have enumerated
  753. * all solutions.
  754. */
  755. if (i == nund)
  756. break;
  757. }
  758. /*
  759. * We have a solution. Produce a move string encoding the
  760. * solution.
  761. */
  762. ret = snewn(wh + 2, char);
  763. ret[0] = 'S';
  764. for (i = 0; i < wh; i++)
  765. ret[i+1] = shortest[i] ? '1' : '0';
  766. ret[wh+1] = '\0';
  767. sfree(shortest);
  768. sfree(solution);
  769. sfree(equations);
  770. sfree(und);
  771. return ret;
  772. }
  773. static bool game_can_format_as_text_now(const game_params *params)
  774. {
  775. return true;
  776. }
  777. #define RIGHT 1
  778. #define DOWN gw
  779. static char *game_text_format(const game_state *state)
  780. {
  781. int w = state->w, h = state->h, wh = w*h, r, c, dx, dy;
  782. int cw = 4, ch = 4, gw = w * cw + 2, gh = h * ch + 1, len = gw * gh;
  783. char *board = snewn(len + 1, char);
  784. memset(board, ' ', len - 1);
  785. for (r = 0; r < h; ++r) {
  786. for (c = 0; c < w; ++c) {
  787. int cell = r*ch*gw + c*cw, center = cell+(ch/2)*DOWN + cw/2*RIGHT;
  788. char flip = (state->grid[r*w + c] & 1) ? '#' : '.';
  789. for (dy = -1 + (r == 0); dy <= 1 - (r == h - 1); ++dy)
  790. for (dx = -1 + (c == 0); dx <= 1 - (c == w - 1); ++dx)
  791. if (state->matrix->matrix[(r*w+c)*wh + ((r+dy)*w + c+dx)])
  792. board[center + dy*DOWN + dx*RIGHT] = flip;
  793. board[cell] = '+';
  794. for (dx = 1; dx < cw; ++dx) board[cell+dx*RIGHT] = '-';
  795. for (dy = 1; dy < ch; ++dy) board[cell+dy*DOWN] = '|';
  796. }
  797. board[r*ch*gw + gw - 2] = '+';
  798. board[r*ch*gw + gw - 1] = '\n';
  799. for (dy = 1; dy < ch; ++dy) {
  800. board[r*ch*gw + gw - 2 + dy*DOWN] = '|';
  801. board[r*ch*gw + gw - 1 + dy*DOWN] = '\n';
  802. }
  803. }
  804. memset(board + len - gw, '-', gw - 2);
  805. for (c = 0; c <= w; ++c) board[len - gw + cw*c] = '+';
  806. board[len - 1] = '\n';
  807. board[len] = '\0';
  808. return board;
  809. }
  810. #undef RIGHT
  811. #undef DOWN
  812. struct game_ui {
  813. int cx, cy;
  814. bool cdraw;
  815. };
  816. static game_ui *new_ui(const game_state *state)
  817. {
  818. game_ui *ui = snew(game_ui);
  819. ui->cx = ui->cy = 0;
  820. ui->cdraw = getenv_bool("PUZZLES_SHOW_CURSOR", false);
  821. return ui;
  822. }
  823. static void free_ui(game_ui *ui)
  824. {
  825. sfree(ui);
  826. }
  827. static void game_changed_state(game_ui *ui, const game_state *oldstate,
  828. const game_state *newstate)
  829. {
  830. }
  831. static const char *current_key_label(const game_ui *ui,
  832. const game_state *state, int button)
  833. {
  834. if (IS_CURSOR_SELECT(button)) return "Flip";
  835. return "";
  836. }
  837. struct game_drawstate {
  838. int w, h;
  839. bool started;
  840. unsigned char *tiles;
  841. int tilesize;
  842. };
  843. static char *interpret_move(const game_state *state, game_ui *ui,
  844. const game_drawstate *ds,
  845. int x, int y, int button)
  846. {
  847. int w = state->w, h = state->h, wh = w * h;
  848. char buf[80], *nullret = MOVE_UNUSED;
  849. if (button == LEFT_BUTTON || IS_CURSOR_SELECT(button)) {
  850. int tx, ty;
  851. if (button == LEFT_BUTTON) {
  852. tx = FROMCOORD(x), ty = FROMCOORD(y);
  853. ui->cdraw = false;
  854. } else {
  855. tx = ui->cx; ty = ui->cy;
  856. ui->cdraw = true;
  857. }
  858. nullret = MOVE_UI_UPDATE;
  859. if (tx >= 0 && tx < w && ty >= 0 && ty < h) {
  860. /*
  861. * It's just possible that a manually entered game ID
  862. * will have at least one square do nothing whatsoever.
  863. * If so, we avoid encoding a move at all.
  864. */
  865. int i = ty*w+tx, j;
  866. bool makemove = false;
  867. for (j = 0; j < wh; j++) {
  868. if (state->matrix->matrix[i*wh+j])
  869. makemove = true;
  870. }
  871. if (makemove) {
  872. sprintf(buf, "M%d,%d", tx, ty);
  873. return dupstr(buf);
  874. } else {
  875. return MOVE_NO_EFFECT;
  876. }
  877. }
  878. }
  879. else if (IS_CURSOR_MOVE(button)) {
  880. int dx = 0, dy = 0;
  881. switch (button) {
  882. case CURSOR_UP: dy = -1; break;
  883. case CURSOR_DOWN: dy = 1; break;
  884. case CURSOR_RIGHT: dx = 1; break;
  885. case CURSOR_LEFT: dx = -1; break;
  886. default: assert(!"shouldn't get here");
  887. }
  888. ui->cx += dx; ui->cy += dy;
  889. ui->cx = min(max(ui->cx, 0), state->w - 1);
  890. ui->cy = min(max(ui->cy, 0), state->h - 1);
  891. ui->cdraw = true;
  892. nullret = MOVE_UI_UPDATE;
  893. }
  894. return nullret;
  895. }
  896. static game_state *execute_move(const game_state *from, const char *move)
  897. {
  898. int w = from->w, h = from->h, wh = w * h;
  899. game_state *ret;
  900. int x, y;
  901. if (move[0] == 'S' && strlen(move) == wh+1) {
  902. int i;
  903. ret = dup_game(from);
  904. ret->hints_active = true;
  905. ret->cheated = true;
  906. for (i = 0; i < wh; i++) {
  907. ret->grid[i] &= ~2;
  908. if (move[i+1] != '0')
  909. ret->grid[i] |= 2;
  910. }
  911. return ret;
  912. } else if (move[0] == 'M' &&
  913. sscanf(move+1, "%d,%d", &x, &y) == 2 &&
  914. x >= 0 && x < w && y >= 0 && y < h) {
  915. int i, j;
  916. bool done;
  917. ret = dup_game(from);
  918. if (!ret->completed)
  919. ret->moves++;
  920. i = y * w + x;
  921. done = true;
  922. for (j = 0; j < wh; j++) {
  923. ret->grid[j] ^= ret->matrix->matrix[i*wh+j];
  924. if (ret->grid[j] & 1)
  925. done = false;
  926. }
  927. ret->grid[i] ^= 2; /* toggle hint */
  928. if (done) {
  929. ret->completed = true;
  930. ret->hints_active = false;
  931. }
  932. return ret;
  933. } else
  934. return NULL; /* can't parse move string */
  935. }
  936. /* ----------------------------------------------------------------------
  937. * Drawing routines.
  938. */
  939. static void game_compute_size(const game_params *params, int tilesize,
  940. const game_ui *ui, int *x, int *y)
  941. {
  942. /* Ick: fake up `ds->tilesize' for macro expansion purposes */
  943. struct { int tilesize; } ads, *ds = &ads;
  944. ads.tilesize = tilesize;
  945. *x = TILE_SIZE * params->w + 2 * BORDER;
  946. *y = TILE_SIZE * params->h + 2 * BORDER;
  947. }
  948. static void game_set_size(drawing *dr, game_drawstate *ds,
  949. const game_params *params, int tilesize)
  950. {
  951. ds->tilesize = tilesize;
  952. }
  953. static float *game_colours(frontend *fe, int *ncolours)
  954. {
  955. float *ret = snewn(3 * NCOLOURS, float);
  956. frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
  957. ret[COL_WRONG * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] / 3;
  958. ret[COL_WRONG * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] / 3;
  959. ret[COL_WRONG * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] / 3;
  960. ret[COL_RIGHT * 3 + 0] = 1.0F;
  961. ret[COL_RIGHT * 3 + 1] = 1.0F;
  962. ret[COL_RIGHT * 3 + 2] = 1.0F;
  963. ret[COL_GRID * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] / 1.5F;
  964. ret[COL_GRID * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] / 1.5F;
  965. ret[COL_GRID * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] / 1.5F;
  966. ret[COL_DIAG * 3 + 0] = ret[COL_GRID * 3 + 0];
  967. ret[COL_DIAG * 3 + 1] = ret[COL_GRID * 3 + 1];
  968. ret[COL_DIAG * 3 + 2] = ret[COL_GRID * 3 + 2];
  969. ret[COL_HINT * 3 + 0] = 1.0F;
  970. ret[COL_HINT * 3 + 1] = 0.0F;
  971. ret[COL_HINT * 3 + 2] = 0.0F;
  972. ret[COL_CURSOR * 3 + 0] = 0.8F;
  973. ret[COL_CURSOR * 3 + 1] = 0.0F;
  974. ret[COL_CURSOR * 3 + 2] = 0.0F;
  975. *ncolours = NCOLOURS;
  976. return ret;
  977. }
  978. static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
  979. {
  980. struct game_drawstate *ds = snew(struct game_drawstate);
  981. int i;
  982. ds->started = false;
  983. ds->w = state->w;
  984. ds->h = state->h;
  985. ds->tiles = snewn(ds->w*ds->h, unsigned char);
  986. ds->tilesize = 0; /* haven't decided yet */
  987. for (i = 0; i < ds->w*ds->h; i++)
  988. ds->tiles[i] = -1;
  989. return ds;
  990. }
  991. static void game_free_drawstate(drawing *dr, game_drawstate *ds)
  992. {
  993. sfree(ds->tiles);
  994. sfree(ds);
  995. }
  996. static void draw_tile(drawing *dr, game_drawstate *ds, const game_state *state,
  997. int x, int y, int tile, bool anim, float animtime)
  998. {
  999. int w = ds->w, h = ds->h, wh = w * h;
  1000. int bx = x * TILE_SIZE + BORDER, by = y * TILE_SIZE + BORDER;
  1001. int i, j, dcol = (tile & 4) ? COL_CURSOR : COL_DIAG;
  1002. clip(dr, bx+1, by+1, TILE_SIZE-1, TILE_SIZE-1);
  1003. draw_rect(dr, bx+1, by+1, TILE_SIZE-1, TILE_SIZE-1,
  1004. anim ? COL_BACKGROUND : tile & 1 ? COL_WRONG : COL_RIGHT);
  1005. if (anim) {
  1006. /*
  1007. * Draw a polygon indicating that the square is diagonally
  1008. * flipping over.
  1009. */
  1010. int coords[8], colour;
  1011. coords[0] = bx + TILE_SIZE;
  1012. coords[1] = by;
  1013. coords[2] = bx + (int)((float)TILE_SIZE * animtime);
  1014. coords[3] = by + (int)((float)TILE_SIZE * animtime);
  1015. coords[4] = bx;
  1016. coords[5] = by + TILE_SIZE;
  1017. coords[6] = bx + TILE_SIZE - (int)((float)TILE_SIZE * animtime);
  1018. coords[7] = by + TILE_SIZE - (int)((float)TILE_SIZE * animtime);
  1019. colour = (tile & 1 ? COL_WRONG : COL_RIGHT);
  1020. if (animtime < 0.5F)
  1021. colour = COL_WRONG + COL_RIGHT - colour;
  1022. draw_polygon(dr, coords, 4, colour, COL_GRID);
  1023. }
  1024. /*
  1025. * Draw a little diagram in the tile which indicates which
  1026. * surrounding tiles flip when this one is clicked.
  1027. */
  1028. for (i = 0; i < h; i++)
  1029. for (j = 0; j < w; j++)
  1030. if (state->matrix->matrix[(y*w+x)*wh + i*w+j]) {
  1031. int ox = j - x, oy = i - y;
  1032. int td = TILE_SIZE / 16 ? TILE_SIZE / 16 : 1;
  1033. int cx = (bx + TILE_SIZE/2) + (2 * ox - 1) * td;
  1034. int cy = (by + TILE_SIZE/2) + (2 * oy - 1) * td;
  1035. if (ox == 0 && oy == 0)
  1036. draw_rect(dr, cx, cy, 2*td+1, 2*td+1, dcol);
  1037. else {
  1038. draw_line(dr, cx, cy, cx+2*td, cy, dcol);
  1039. draw_line(dr, cx, cy+2*td, cx+2*td, cy+2*td, dcol);
  1040. draw_line(dr, cx, cy, cx, cy+2*td, dcol);
  1041. draw_line(dr, cx+2*td, cy, cx+2*td, cy+2*td, dcol);
  1042. }
  1043. }
  1044. /*
  1045. * Draw a hint rectangle if required.
  1046. */
  1047. if (tile & 2) {
  1048. int x1 = bx + TILE_SIZE / 20, x2 = bx + TILE_SIZE - TILE_SIZE / 20;
  1049. int y1 = by + TILE_SIZE / 20, y2 = by + TILE_SIZE - TILE_SIZE / 20;
  1050. int i = 3;
  1051. while (i--) {
  1052. draw_line(dr, x1, y1, x2, y1, COL_HINT);
  1053. draw_line(dr, x1, y2, x2, y2, COL_HINT);
  1054. draw_line(dr, x1, y1, x1, y2, COL_HINT);
  1055. draw_line(dr, x2, y1, x2, y2, COL_HINT);
  1056. x1++, y1++, x2--, y2--;
  1057. }
  1058. }
  1059. unclip(dr);
  1060. draw_update(dr, bx+1, by+1, TILE_SIZE-1, TILE_SIZE-1);
  1061. }
  1062. static void game_redraw(drawing *dr, game_drawstate *ds,
  1063. const game_state *oldstate, const game_state *state,
  1064. int dir, const game_ui *ui,
  1065. float animtime, float flashtime)
  1066. {
  1067. int w = ds->w, h = ds->h, wh = w * h;
  1068. int i, flashframe;
  1069. if (!ds->started) {
  1070. /*
  1071. * Draw the grid lines.
  1072. */
  1073. for (i = 0; i <= w; i++)
  1074. draw_line(dr, i * TILE_SIZE + BORDER, BORDER,
  1075. i * TILE_SIZE + BORDER, h * TILE_SIZE + BORDER,
  1076. COL_GRID);
  1077. for (i = 0; i <= h; i++)
  1078. draw_line(dr, BORDER, i * TILE_SIZE + BORDER,
  1079. w * TILE_SIZE + BORDER, i * TILE_SIZE + BORDER,
  1080. COL_GRID);
  1081. draw_update(dr, 0, 0, TILE_SIZE * w + 2 * BORDER,
  1082. TILE_SIZE * h + 2 * BORDER);
  1083. ds->started = true;
  1084. }
  1085. if (flashtime)
  1086. flashframe = (int)(flashtime / FLASH_FRAME);
  1087. else
  1088. flashframe = -1;
  1089. animtime /= ANIM_TIME; /* scale it so it goes from 0 to 1 */
  1090. for (i = 0; i < wh; i++) {
  1091. int x = i % w, y = i / w;
  1092. int fx, fy, fd;
  1093. int v = state->grid[i];
  1094. int vv;
  1095. if (flashframe >= 0) {
  1096. fx = (w+1)/2 - min(x+1, w-x);
  1097. fy = (h+1)/2 - min(y+1, h-y);
  1098. fd = max(fx, fy);
  1099. if (fd == flashframe)
  1100. v |= 1;
  1101. else if (fd == flashframe - 1)
  1102. v &= ~1;
  1103. }
  1104. if (!state->hints_active)
  1105. v &= ~2;
  1106. if (ui->cdraw && ui->cx == x && ui->cy == y)
  1107. v |= 4;
  1108. if (oldstate && ((state->grid[i] ^ oldstate->grid[i]) &~ 2))
  1109. vv = 255; /* means `animated' */
  1110. else
  1111. vv = v;
  1112. if (ds->tiles[i] == 255 || vv == 255 || ds->tiles[i] != vv) {
  1113. draw_tile(dr, ds, state, x, y, v, vv == 255, animtime);
  1114. ds->tiles[i] = vv;
  1115. }
  1116. }
  1117. {
  1118. char buf[256];
  1119. sprintf(buf, "%sMoves: %d",
  1120. (state->completed ?
  1121. (state->cheated ? "Auto-solved. " : "COMPLETED! ") :
  1122. (state->cheated ? "Auto-solver used. " : "")),
  1123. state->moves);
  1124. status_bar(dr, buf);
  1125. }
  1126. }
  1127. static float game_anim_length(const game_state *oldstate,
  1128. const game_state *newstate, int dir, game_ui *ui)
  1129. {
  1130. return ANIM_TIME;
  1131. }
  1132. static float game_flash_length(const game_state *oldstate,
  1133. const game_state *newstate, int dir, game_ui *ui)
  1134. {
  1135. if (!oldstate->completed && newstate->completed)
  1136. return FLASH_FRAME * (max((newstate->w+1)/2, (newstate->h+1)/2)+1);
  1137. return 0.0F;
  1138. }
  1139. static void game_get_cursor_location(const game_ui *ui,
  1140. const game_drawstate *ds,
  1141. const game_state *state,
  1142. const game_params *params,
  1143. int *x, int *y, int *w, int *h)
  1144. {
  1145. if(ui->cdraw)
  1146. {
  1147. *x = COORD(ui->cx);
  1148. *y = COORD(ui->cy);
  1149. *w = *h = TILE_SIZE;
  1150. }
  1151. }
  1152. static int game_status(const game_state *state)
  1153. {
  1154. return state->completed ? +1 : 0;
  1155. }
  1156. #ifdef COMBINED
  1157. #define thegame flip
  1158. #endif
  1159. const struct game thegame = {
  1160. "Flip", "games.flip", "flip",
  1161. default_params,
  1162. game_fetch_preset, NULL,
  1163. decode_params,
  1164. encode_params,
  1165. free_params,
  1166. dup_params,
  1167. true, game_configure, custom_params,
  1168. validate_params,
  1169. new_game_desc,
  1170. validate_desc,
  1171. new_game,
  1172. dup_game,
  1173. free_game,
  1174. true, solve_game,
  1175. true, game_can_format_as_text_now, game_text_format,
  1176. NULL, NULL, /* get_prefs, set_prefs */
  1177. new_ui,
  1178. free_ui,
  1179. NULL, /* encode_ui */
  1180. NULL, /* decode_ui */
  1181. NULL, /* game_request_keys */
  1182. game_changed_state,
  1183. current_key_label,
  1184. interpret_move,
  1185. execute_move,
  1186. PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
  1187. game_colours,
  1188. game_new_drawstate,
  1189. game_free_drawstate,
  1190. game_redraw,
  1191. game_anim_length,
  1192. game_flash_length,
  1193. game_get_cursor_location,
  1194. game_status,
  1195. false, false, NULL, NULL, /* print_size, print */
  1196. true, /* wants_statusbar */
  1197. false, NULL, /* timing_state */
  1198. 0, /* flags */
  1199. };