pegs.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. /*
  2. * pegs.c: the classic Peg Solitaire game.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <ctype.h>
  9. #include <limits.h>
  10. #ifdef NO_TGMATH_H
  11. # include <math.h>
  12. #else
  13. # include <tgmath.h>
  14. #endif
  15. #include "puzzles.h"
  16. #include "tree234.h"
  17. #define GRID_HOLE 0
  18. #define GRID_PEG 1
  19. #define GRID_OBST 2
  20. #define GRID_CURSOR 10
  21. #define GRID_JUMPING 20
  22. enum {
  23. COL_BACKGROUND,
  24. COL_HIGHLIGHT,
  25. COL_LOWLIGHT,
  26. COL_PEG,
  27. COL_CURSOR,
  28. NCOLOURS
  29. };
  30. /*
  31. * Grid shapes. I do some macro ickery here to ensure that my enum
  32. * and the various forms of my name list always match up.
  33. */
  34. #define TYPELIST(A) \
  35. A(CROSS,Cross,cross) \
  36. A(OCTAGON,Octagon,octagon) \
  37. A(RANDOM,Random,random)
  38. #define ENUM(upper,title,lower) TYPE_ ## upper,
  39. #define TITLE(upper,title,lower) #title,
  40. #define LOWER(upper,title,lower) #lower,
  41. #define CONFIG(upper,title,lower) ":" #title
  42. enum { TYPELIST(ENUM) TYPECOUNT };
  43. static char const *const pegs_titletypes[] = { TYPELIST(TITLE) };
  44. static char const *const pegs_lowertypes[] = { TYPELIST(LOWER) };
  45. #define TYPECONFIG TYPELIST(CONFIG)
  46. #define FLASH_FRAME 0.13F
  47. struct game_params {
  48. int w, h;
  49. int type;
  50. };
  51. struct game_state {
  52. int w, h;
  53. bool completed;
  54. unsigned char *grid;
  55. };
  56. static game_params *default_params(void)
  57. {
  58. game_params *ret = snew(game_params);
  59. ret->w = ret->h = 7;
  60. ret->type = TYPE_CROSS;
  61. return ret;
  62. }
  63. static const struct game_params pegs_presets[] = {
  64. {5, 7, TYPE_CROSS},
  65. {7, 7, TYPE_CROSS},
  66. {5, 9, TYPE_CROSS},
  67. {7, 9, TYPE_CROSS},
  68. {9, 9, TYPE_CROSS},
  69. {7, 7, TYPE_OCTAGON},
  70. {5, 5, TYPE_RANDOM},
  71. {7, 7, TYPE_RANDOM},
  72. {9, 9, TYPE_RANDOM},
  73. };
  74. static bool game_fetch_preset(int i, char **name, game_params **params)
  75. {
  76. game_params *ret;
  77. char str[80];
  78. if (i < 0 || i >= lenof(pegs_presets))
  79. return false;
  80. ret = snew(game_params);
  81. *ret = pegs_presets[i];
  82. strcpy(str, pegs_titletypes[ret->type]);
  83. if (ret->type == TYPE_CROSS || ret->type == TYPE_RANDOM)
  84. sprintf(str + strlen(str), " %dx%d", ret->w, ret->h);
  85. *name = dupstr(str);
  86. *params = ret;
  87. return true;
  88. }
  89. static void free_params(game_params *params)
  90. {
  91. sfree(params);
  92. }
  93. static game_params *dup_params(const game_params *params)
  94. {
  95. game_params *ret = snew(game_params);
  96. *ret = *params; /* structure copy */
  97. return ret;
  98. }
  99. static void decode_params(game_params *params, char const *string)
  100. {
  101. char const *p = string;
  102. int i;
  103. params->w = atoi(p);
  104. while (*p && isdigit((unsigned char)*p)) p++;
  105. if (*p == 'x') {
  106. p++;
  107. params->h = atoi(p);
  108. while (*p && isdigit((unsigned char)*p)) p++;
  109. } else {
  110. params->h = params->w;
  111. }
  112. for (i = 0; i < lenof(pegs_lowertypes); i++)
  113. if (!strcmp(p, pegs_lowertypes[i]))
  114. params->type = i;
  115. }
  116. static char *encode_params(const game_params *params, bool full)
  117. {
  118. char str[80];
  119. sprintf(str, "%dx%d", params->w, params->h);
  120. if (full) {
  121. assert(params->type >= 0 && params->type < lenof(pegs_lowertypes));
  122. strcat(str, pegs_lowertypes[params->type]);
  123. }
  124. return dupstr(str);
  125. }
  126. static config_item *game_configure(const game_params *params)
  127. {
  128. config_item *ret = snewn(4, config_item);
  129. char buf[80];
  130. ret[0].name = "Width";
  131. ret[0].type = C_STRING;
  132. sprintf(buf, "%d", params->w);
  133. ret[0].u.string.sval = dupstr(buf);
  134. ret[1].name = "Height";
  135. ret[1].type = C_STRING;
  136. sprintf(buf, "%d", params->h);
  137. ret[1].u.string.sval = dupstr(buf);
  138. ret[2].name = "Board type";
  139. ret[2].type = C_CHOICES;
  140. ret[2].u.choices.choicenames = TYPECONFIG;
  141. ret[2].u.choices.selected = params->type;
  142. ret[3].name = NULL;
  143. ret[3].type = C_END;
  144. return ret;
  145. }
  146. static game_params *custom_params(const config_item *cfg)
  147. {
  148. game_params *ret = snew(game_params);
  149. ret->w = atoi(cfg[0].u.string.sval);
  150. ret->h = atoi(cfg[1].u.string.sval);
  151. ret->type = cfg[2].u.choices.selected;
  152. return ret;
  153. }
  154. static const char *validate_params(const game_params *params, bool full)
  155. {
  156. if (full && (params->w <= 3 || params->h <= 3))
  157. return "Width and height must both be greater than three";
  158. if (params->w < 1 || params->h < 1)
  159. return "Width and height must both be at least one";
  160. if (params->w > INT_MAX / params->h)
  161. return "Width times height must not be unreasonably large";
  162. /*
  163. * At http://www.gibell.net/pegsolitaire/GenCross/GenCrossBoards0.html
  164. * George I. Bell asserts that various generalised cross-shaped
  165. * boards are soluble starting (and finishing) with the centre
  166. * hole. We permit the symmetric ones. Bell's notation for each
  167. * soluble board is listed.
  168. */
  169. if (full && params->type == TYPE_CROSS) {
  170. if (!((params->w == 9 && params->h == 5) || /* (3,1,3,1) */
  171. (params->w == 5 && params->h == 9) || /* (1,3,1,3) */
  172. (params->w == 9 && params->h == 9) || /* (3,3,3,3) */
  173. (params->w == 7 && params->h == 5) || /* (2,1,2,1) */
  174. (params->w == 5 && params->h == 7) || /* (1,2,1,2) */
  175. (params->w == 9 && params->h == 7) || /* (3,2,3,2) */
  176. (params->w == 7 && params->h == 9) || /* (2,3,2,3) */
  177. (params->w == 7 && params->h == 7))) /* (2,2,2,2) */
  178. return "This board type is only supported at "
  179. "5x7, 5x9, 7x7, 7x9, and 9x9";
  180. }
  181. /*
  182. * It might be possible to implement generalisations of
  183. * Octagon, but only if I can find a proof that they're all
  184. * soluble. For the moment, therefore, I'm going to disallow
  185. * it at any size other than the standard one.
  186. */
  187. if (full && params->type == TYPE_OCTAGON) {
  188. if (params->w != 7 || params->h != 7)
  189. return "This board type is only supported at 7x7";
  190. }
  191. return NULL;
  192. }
  193. /* ----------------------------------------------------------------------
  194. * Beginning of code to generate random Peg Solitaire boards.
  195. *
  196. * This procedure is done with no aesthetic judgment, no effort at
  197. * symmetry, no difficulty grading and generally no finesse
  198. * whatsoever. We simply begin with an empty board containing a
  199. * single peg, and repeatedly make random reverse moves until it's
  200. * plausibly full. This typically yields a scrappy haphazard mess
  201. * with several holes, an uneven shape, and no redeeming features
  202. * except guaranteed solubility.
  203. *
  204. * My only concessions to sophistication are (a) to repeat the
  205. * generation process until I at least get a grid that touches
  206. * every edge of the specified board size, and (b) to try when
  207. * selecting moves to reuse existing space rather than expanding
  208. * into new space (so that non-rectangular board shape becomes a
  209. * factor during play).
  210. */
  211. struct move {
  212. /*
  213. * x,y are the start point of the move during generation (hence
  214. * its endpoint during normal play).
  215. *
  216. * dx,dy are the direction of the move during generation.
  217. * Absolute value 1. Hence, for example, x=3,y=5,dx=1,dy=0
  218. * means that the move during generation starts at (3,5) and
  219. * ends at (5,5), and vice versa during normal play.
  220. */
  221. int x, y, dx, dy;
  222. /*
  223. * cost is 0, 1 or 2, depending on how many GRID_OBSTs we must
  224. * turn into GRID_HOLEs to play this move.
  225. */
  226. int cost;
  227. };
  228. static int movecmp(void *av, void *bv)
  229. {
  230. struct move *a = (struct move *)av;
  231. struct move *b = (struct move *)bv;
  232. if (a->y < b->y)
  233. return -1;
  234. else if (a->y > b->y)
  235. return +1;
  236. if (a->x < b->x)
  237. return -1;
  238. else if (a->x > b->x)
  239. return +1;
  240. if (a->dy < b->dy)
  241. return -1;
  242. else if (a->dy > b->dy)
  243. return +1;
  244. if (a->dx < b->dx)
  245. return -1;
  246. else if (a->dx > b->dx)
  247. return +1;
  248. return 0;
  249. }
  250. static int movecmpcost(void *av, void *bv)
  251. {
  252. struct move *a = (struct move *)av;
  253. struct move *b = (struct move *)bv;
  254. if (a->cost < b->cost)
  255. return -1;
  256. else if (a->cost > b->cost)
  257. return +1;
  258. return movecmp(av, bv);
  259. }
  260. struct movetrees {
  261. tree234 *bymove, *bycost;
  262. };
  263. static void update_moves(unsigned char *grid, int w, int h, int x, int y,
  264. struct movetrees *trees)
  265. {
  266. struct move move;
  267. int dir, pos;
  268. /*
  269. * There are twelve moves that can include (x,y): three in each
  270. * of four directions. Check each one to see if it's possible.
  271. */
  272. for (dir = 0; dir < 4; dir++) {
  273. int dx, dy;
  274. if (dir & 1)
  275. dx = 0, dy = dir - 2;
  276. else
  277. dy = 0, dx = dir - 1;
  278. assert(abs(dx) + abs(dy) == 1);
  279. for (pos = 0; pos < 3; pos++) {
  280. int v1, v2, v3;
  281. move.dx = dx;
  282. move.dy = dy;
  283. move.x = x - pos*dx;
  284. move.y = y - pos*dy;
  285. if (move.x < 0 || move.x >= w || move.y < 0 || move.y >= h)
  286. continue; /* completely invalid move */
  287. if (move.x+2*move.dx < 0 || move.x+2*move.dx >= w ||
  288. move.y+2*move.dy < 0 || move.y+2*move.dy >= h)
  289. continue; /* completely invalid move */
  290. v1 = grid[move.y * w + move.x];
  291. v2 = grid[(move.y+move.dy) * w + (move.x+move.dx)];
  292. v3 = grid[(move.y+2*move.dy)*w + (move.x+2*move.dx)];
  293. if (v1 == GRID_PEG && v2 != GRID_PEG && v3 != GRID_PEG) {
  294. struct move *m;
  295. move.cost = (v2 == GRID_OBST) + (v3 == GRID_OBST);
  296. /*
  297. * This move is possible. See if it's already in
  298. * the tree.
  299. */
  300. m = find234(trees->bymove, &move, NULL);
  301. if (m && m->cost != move.cost) {
  302. /*
  303. * It's in the tree but listed with the wrong
  304. * cost. Remove the old version.
  305. */
  306. #ifdef GENERATION_DIAGNOSTICS
  307. printf("correcting %d%+d,%d%+d at cost %d\n",
  308. m->x, m->dx, m->y, m->dy, m->cost);
  309. #endif
  310. del234(trees->bymove, m);
  311. del234(trees->bycost, m);
  312. sfree(m);
  313. m = NULL;
  314. }
  315. if (!m) {
  316. struct move *m, *m2;
  317. m = snew(struct move);
  318. *m = move;
  319. m2 = add234(trees->bymove, m);
  320. m2 = add234(trees->bycost, m);
  321. assert(m2 == m);
  322. #ifdef GENERATION_DIAGNOSTICS
  323. printf("adding %d%+d,%d%+d at cost %d\n",
  324. move.x, move.dx, move.y, move.dy, move.cost);
  325. #endif
  326. } else {
  327. #ifdef GENERATION_DIAGNOSTICS
  328. printf("not adding %d%+d,%d%+d at cost %d\n",
  329. move.x, move.dx, move.y, move.dy, move.cost);
  330. #endif
  331. }
  332. } else {
  333. /*
  334. * This move is impossible. If it is already in the
  335. * tree, delete it.
  336. *
  337. * (We make use here of the fact that del234
  338. * doesn't have to be passed a pointer to the
  339. * _actual_ element it's deleting: it merely needs
  340. * one that compares equal to it, and it will
  341. * return the one it deletes.)
  342. */
  343. struct move *m = del234(trees->bymove, &move);
  344. #ifdef GENERATION_DIAGNOSTICS
  345. printf("%sdeleting %d%+d,%d%+d\n", m ? "" : "not ",
  346. move.x, move.dx, move.y, move.dy);
  347. #endif
  348. if (m) {
  349. del234(trees->bycost, m);
  350. sfree(m);
  351. }
  352. }
  353. }
  354. }
  355. }
  356. static void pegs_genmoves(unsigned char *grid, int w, int h, random_state *rs)
  357. {
  358. struct movetrees atrees, *trees = &atrees;
  359. struct move *m;
  360. int x, y, i, nmoves;
  361. trees->bymove = newtree234(movecmp);
  362. trees->bycost = newtree234(movecmpcost);
  363. for (y = 0; y < h; y++)
  364. for (x = 0; x < w; x++)
  365. if (grid[y*w+x] == GRID_PEG)
  366. update_moves(grid, w, h, x, y, trees);
  367. nmoves = 0;
  368. while (1) {
  369. int limit, maxcost, index;
  370. struct move mtmp, move, *m;
  371. /*
  372. * See how many moves we can make at zero cost. Make one,
  373. * if possible. Failing that, make a one-cost move, and
  374. * then a two-cost one.
  375. *
  376. * After filling at least half the input grid, we no longer
  377. * accept cost-2 moves: if that's our only option, we give
  378. * up and finish.
  379. */
  380. mtmp.y = h+1;
  381. maxcost = (nmoves < w*h/2 ? 2 : 1);
  382. m = NULL; /* placate optimiser */
  383. for (mtmp.cost = 0; mtmp.cost <= maxcost; mtmp.cost++) {
  384. limit = -1;
  385. m = findrelpos234(trees->bycost, &mtmp, NULL, REL234_LT, &limit);
  386. #ifdef GENERATION_DIAGNOSTICS
  387. printf("%d moves available with cost %d\n", limit+1, mtmp.cost);
  388. #endif
  389. if (m)
  390. break;
  391. }
  392. if (!m)
  393. break;
  394. index = random_upto(rs, limit+1);
  395. move = *(struct move *)index234(trees->bycost, index);
  396. #ifdef GENERATION_DIAGNOSTICS
  397. printf("selecting move %d%+d,%d%+d at cost %d\n",
  398. move.x, move.dx, move.y, move.dy, move.cost);
  399. #endif
  400. grid[move.y * w + move.x] = GRID_HOLE;
  401. grid[(move.y+move.dy) * w + (move.x+move.dx)] = GRID_PEG;
  402. grid[(move.y+2*move.dy)*w + (move.x+2*move.dx)] = GRID_PEG;
  403. for (i = 0; i <= 2; i++) {
  404. int tx = move.x + i*move.dx;
  405. int ty = move.y + i*move.dy;
  406. update_moves(grid, w, h, tx, ty, trees);
  407. }
  408. nmoves++;
  409. }
  410. while ((m = delpos234(trees->bymove, 0)) != NULL) {
  411. del234(trees->bycost, m);
  412. sfree(m);
  413. }
  414. freetree234(trees->bymove);
  415. freetree234(trees->bycost);
  416. }
  417. static void pegs_generate(unsigned char *grid, int w, int h, random_state *rs)
  418. {
  419. while (1) {
  420. int x, y, extremes;
  421. memset(grid, GRID_OBST, w*h);
  422. grid[(h/2) * w + (w/2)] = GRID_PEG;
  423. #ifdef GENERATION_DIAGNOSTICS
  424. printf("beginning move selection\n");
  425. #endif
  426. pegs_genmoves(grid, w, h, rs);
  427. #ifdef GENERATION_DIAGNOSTICS
  428. printf("finished move selection\n");
  429. #endif
  430. extremes = 0;
  431. for (y = 0; y < h; y++) {
  432. if (grid[y*w+0] != GRID_OBST)
  433. extremes |= 1;
  434. if (grid[y*w+w-1] != GRID_OBST)
  435. extremes |= 2;
  436. }
  437. for (x = 0; x < w; x++) {
  438. if (grid[0*w+x] != GRID_OBST)
  439. extremes |= 4;
  440. if (grid[(h-1)*w+x] != GRID_OBST)
  441. extremes |= 8;
  442. }
  443. if (extremes == 15)
  444. break;
  445. #ifdef GENERATION_DIAGNOSTICS
  446. printf("insufficient extent; trying again\n");
  447. #endif
  448. }
  449. #ifdef GENERATION_DIAGNOSTICS
  450. fflush(stdout);
  451. #endif
  452. }
  453. /* ----------------------------------------------------------------------
  454. * End of board generation code. Now for the client code which uses
  455. * it as part of the puzzle.
  456. */
  457. static char *new_game_desc(const game_params *params, random_state *rs,
  458. char **aux, bool interactive)
  459. {
  460. int w = params->w, h = params->h;
  461. unsigned char *grid;
  462. char *ret;
  463. int i;
  464. grid = snewn(w*h, unsigned char);
  465. if (params->type == TYPE_RANDOM) {
  466. pegs_generate(grid, w, h, rs);
  467. } else {
  468. int x, y, cx, cy, v;
  469. for (y = 0; y < h; y++)
  470. for (x = 0; x < w; x++) {
  471. v = GRID_OBST; /* placate optimiser */
  472. switch (params->type) {
  473. case TYPE_CROSS:
  474. cx = abs(x - w/2);
  475. cy = abs(y - h/2);
  476. if (cx == 0 && cy == 0)
  477. v = GRID_HOLE;
  478. else if (cx > 1 && cy > 1)
  479. v = GRID_OBST;
  480. else
  481. v = GRID_PEG;
  482. break;
  483. case TYPE_OCTAGON:
  484. cx = abs(x - w/2);
  485. cy = abs(y - h/2);
  486. if (cx + cy > 1 + max(w,h)/2)
  487. v = GRID_OBST;
  488. else
  489. v = GRID_PEG;
  490. break;
  491. }
  492. grid[y*w+x] = v;
  493. }
  494. if (params->type == TYPE_OCTAGON) {
  495. /*
  496. * The octagonal (European) solitaire layout is
  497. * actually _insoluble_ with the starting hole at the
  498. * centre. Here's a proof:
  499. *
  500. * Colour the squares of the board diagonally in
  501. * stripes of three different colours, which I'll call
  502. * A, B and C. So the board looks like this:
  503. *
  504. * A B C
  505. * A B C A B
  506. * A B C A B C A
  507. * B C A B C A B
  508. * C A B C A B C
  509. * B C A B C
  510. * A B C
  511. *
  512. * Suppose we keep running track of the number of pegs
  513. * occuping each colour of square. This colouring has
  514. * the property that any valid move whatsoever changes
  515. * all three of those counts by one (two of them go
  516. * down and one goes up), which means that the _parity_
  517. * of every count flips on every move.
  518. *
  519. * If the centre square starts off unoccupied, then
  520. * there are twelve pegs on each colour and all three
  521. * counts start off even; therefore, after 35 moves all
  522. * three counts would have to be odd, which isn't
  523. * possible if there's only one peg left. []
  524. *
  525. * This proof works just as well if the starting hole
  526. * is _any_ of the thirteen positions labelled B. Also,
  527. * we can stripe the board in the opposite direction
  528. * and rule out any square labelled B in that colouring
  529. * as well. This leaves:
  530. *
  531. * Y n Y
  532. * n n Y n n
  533. * Y n n Y n n Y
  534. * n Y Y n Y Y n
  535. * Y n n Y n n Y
  536. * n n Y n n
  537. * Y n Y
  538. *
  539. * where the ns are squares we've proved insoluble, and
  540. * the Ys are the ones remaining.
  541. *
  542. * That doesn't prove all those starting positions to
  543. * be soluble, of course; they're merely the ones we
  544. * _haven't_ proved to be impossible. Nevertheless, it
  545. * turns out that they are all soluble, so when the
  546. * user requests an Octagon board the simplest thing is
  547. * to pick one of these at random.
  548. *
  549. * Rather than picking equiprobably from those twelve
  550. * positions, we'll pick equiprobably from the three
  551. * equivalence classes
  552. */
  553. switch (random_upto(rs, 3)) {
  554. case 0:
  555. /* Remove a random corner piece. */
  556. {
  557. int dx, dy;
  558. dx = random_upto(rs, 2) * 2 - 1; /* +1 or -1 */
  559. dy = random_upto(rs, 2) * 2 - 1; /* +1 or -1 */
  560. if (random_upto(rs, 2))
  561. dy *= 3;
  562. else
  563. dx *= 3;
  564. grid[(3+dy)*w+(3+dx)] = GRID_HOLE;
  565. }
  566. break;
  567. case 1:
  568. /* Remove a random piece two from the centre. */
  569. {
  570. int dx, dy;
  571. dx = 2 * (random_upto(rs, 2) * 2 - 1);
  572. if (random_upto(rs, 2))
  573. dy = 0;
  574. else
  575. dy = dx, dx = 0;
  576. grid[(3+dy)*w+(3+dx)] = GRID_HOLE;
  577. }
  578. break;
  579. default /* case 2 */:
  580. /* Remove a random piece one from the centre. */
  581. {
  582. int dx, dy;
  583. dx = random_upto(rs, 2) * 2 - 1;
  584. if (random_upto(rs, 2))
  585. dy = 0;
  586. else
  587. dy = dx, dx = 0;
  588. grid[(3+dy)*w+(3+dx)] = GRID_HOLE;
  589. }
  590. break;
  591. }
  592. }
  593. }
  594. /*
  595. * Encode a game description which is simply a long list of P
  596. * for peg, H for hole or O for obstacle.
  597. */
  598. ret = snewn(w*h+1, char);
  599. for (i = 0; i < w*h; i++)
  600. ret[i] = (grid[i] == GRID_PEG ? 'P' :
  601. grid[i] == GRID_HOLE ? 'H' : 'O');
  602. ret[w*h] = '\0';
  603. sfree(grid);
  604. return ret;
  605. }
  606. static const char *validate_desc(const game_params *params, const char *desc)
  607. {
  608. int len, i, npeg = 0, nhole = 0;
  609. len = params->w * params->h;
  610. if (len != strlen(desc))
  611. return "Game description is wrong length";
  612. if (len != strspn(desc, "PHO"))
  613. return "Invalid character in game description";
  614. for (i = 0; i < len; i++) {
  615. npeg += desc[i] == 'P';
  616. nhole += desc[i] == 'H';
  617. }
  618. /* The minimal soluble game has two pegs and a hole: "3x1:PPH". */
  619. if (npeg < 2)
  620. return "Too few pegs in game description";
  621. if (nhole < 1)
  622. return "Too few holes in game description";
  623. return NULL;
  624. }
  625. static game_state *new_game(midend *me, const game_params *params,
  626. const char *desc)
  627. {
  628. int w = params->w, h = params->h;
  629. game_state *state = snew(game_state);
  630. int i;
  631. state->w = w;
  632. state->h = h;
  633. state->completed = false;
  634. state->grid = snewn(w*h, unsigned char);
  635. for (i = 0; i < w*h; i++)
  636. state->grid[i] = (desc[i] == 'P' ? GRID_PEG :
  637. desc[i] == 'H' ? GRID_HOLE : GRID_OBST);
  638. return state;
  639. }
  640. static game_state *dup_game(const game_state *state)
  641. {
  642. int w = state->w, h = state->h;
  643. game_state *ret = snew(game_state);
  644. ret->w = state->w;
  645. ret->h = state->h;
  646. ret->completed = state->completed;
  647. ret->grid = snewn(w*h, unsigned char);
  648. memcpy(ret->grid, state->grid, w*h);
  649. return ret;
  650. }
  651. static void free_game(game_state *state)
  652. {
  653. sfree(state->grid);
  654. sfree(state);
  655. }
  656. static bool game_can_format_as_text_now(const game_params *params)
  657. {
  658. return true;
  659. }
  660. static char *game_text_format(const game_state *state)
  661. {
  662. int w = state->w, h = state->h;
  663. int x, y;
  664. char *ret;
  665. ret = snewn((w+1)*h + 1, char);
  666. for (y = 0; y < h; y++) {
  667. for (x = 0; x < w; x++)
  668. ret[y*(w+1)+x] = (state->grid[y*w+x] == GRID_HOLE ? '-' :
  669. state->grid[y*w+x] == GRID_PEG ? '*' : ' ');
  670. ret[y*(w+1)+w] = '\n';
  671. }
  672. ret[h*(w+1)] = '\0';
  673. return ret;
  674. }
  675. struct game_ui {
  676. bool dragging; /* is a drag in progress? */
  677. int sx, sy; /* grid coords of drag start cell */
  678. int dx, dy; /* pixel coords of current drag posn */
  679. int cur_x, cur_y;
  680. bool cur_visible, cur_jumping;
  681. };
  682. static game_ui *new_ui(const game_state *state)
  683. {
  684. game_ui *ui = snew(game_ui);
  685. int x, y, v;
  686. ui->sx = ui->sy = ui->dx = ui->dy = 0;
  687. ui->dragging = false;
  688. ui->cur_visible = getenv_bool("PUZZLES_SHOW_CURSOR", false);
  689. ui->cur_jumping = false;
  690. /* make sure we start the cursor somewhere on the grid. */
  691. for (x = 0; x < state->w; x++) {
  692. for (y = 0; y < state->h; y++) {
  693. v = state->grid[y*state->w+x];
  694. if (v == GRID_PEG || v == GRID_HOLE) {
  695. ui->cur_x = x; ui->cur_y = y;
  696. goto found;
  697. }
  698. }
  699. }
  700. assert(!"new_ui found nowhere for cursor");
  701. found:
  702. return ui;
  703. }
  704. static void free_ui(game_ui *ui)
  705. {
  706. sfree(ui);
  707. }
  708. static void game_changed_state(game_ui *ui, const game_state *oldstate,
  709. const game_state *newstate)
  710. {
  711. /*
  712. * Cancel a drag, in case the source square has become
  713. * unoccupied.
  714. */
  715. ui->dragging = false;
  716. /*
  717. * Also, cancel a keyboard-driven jump if one is half way to being
  718. * input.
  719. */
  720. ui->cur_jumping = false;
  721. }
  722. static const char *current_key_label(const game_ui *ui,
  723. const game_state *state, int button)
  724. {
  725. int w = state->w;
  726. if (IS_CURSOR_SELECT(button)) {
  727. if (!ui->cur_visible) return "";
  728. if (ui->cur_jumping) return "Cancel";
  729. if (state->grid[ui->cur_y*w+ui->cur_x] == GRID_PEG) return "Select";
  730. }
  731. return "";
  732. }
  733. #define PREFERRED_TILE_SIZE 33
  734. #define TILESIZE (ds->tilesize)
  735. #define BORDER (TILESIZE / 2)
  736. #define HIGHLIGHT_WIDTH (TILESIZE / 16)
  737. #define COORD(x) ( BORDER + (x) * TILESIZE )
  738. #define FROMCOORD(x) ( ((x) + TILESIZE - BORDER) / TILESIZE - 1 )
  739. struct game_drawstate {
  740. int tilesize;
  741. blitter *drag_background;
  742. bool dragging;
  743. int dragx, dragy;
  744. int w, h;
  745. unsigned char *grid;
  746. bool started;
  747. int bgcolour;
  748. };
  749. static char *interpret_move(const game_state *state, game_ui *ui,
  750. const game_drawstate *ds,
  751. int x, int y, int button)
  752. {
  753. int w = state->w, h = state->h;
  754. char buf[80];
  755. if (button == LEFT_BUTTON) {
  756. int tx, ty;
  757. /*
  758. * Left button down: we attempt to start a drag.
  759. */
  760. /*
  761. * There certainly shouldn't be a current drag in progress,
  762. * unless the midend failed to send us button events in
  763. * order; it has a responsibility to always get that right,
  764. * so we can legitimately punish it by failing an
  765. * assertion.
  766. */
  767. assert(!ui->dragging);
  768. tx = FROMCOORD(x);
  769. ty = FROMCOORD(y);
  770. if (tx >= 0 && tx < w && ty >= 0 && ty < h) {
  771. switch (state->grid[ty*w+tx]) {
  772. case GRID_PEG:
  773. ui->dragging = true;
  774. ui->sx = tx;
  775. ui->sy = ty;
  776. ui->dx = x;
  777. ui->dy = y;
  778. ui->cur_visible = false;
  779. ui->cur_jumping = false;
  780. return MOVE_UI_UPDATE;
  781. case GRID_HOLE:
  782. return MOVE_NO_EFFECT;
  783. case GRID_OBST:
  784. default:
  785. return MOVE_UNUSED;
  786. }
  787. }
  788. } else if (button == LEFT_DRAG && ui->dragging) {
  789. /*
  790. * Mouse moved; just move the peg being dragged.
  791. */
  792. ui->dx = x;
  793. ui->dy = y;
  794. return MOVE_UI_UPDATE;
  795. } else if (button == LEFT_RELEASE && ui->dragging) {
  796. int tx, ty, dx, dy;
  797. /*
  798. * Button released. Identify the target square of the drag,
  799. * see if it represents a valid move, and if so make it.
  800. */
  801. ui->dragging = false; /* cancel the drag no matter what */
  802. tx = FROMCOORD(x);
  803. ty = FROMCOORD(y);
  804. if (tx < 0 || tx >= w || ty < 0 || ty >= h)
  805. return MOVE_UI_UPDATE; /* target out of range */
  806. dx = tx - ui->sx;
  807. dy = ty - ui->sy;
  808. if (max(abs(dx),abs(dy)) != 2 || min(abs(dx),abs(dy)) != 0)
  809. return MOVE_UI_UPDATE; /* move length was wrong */
  810. dx /= 2;
  811. dy /= 2;
  812. if (state->grid[ty*w+tx] != GRID_HOLE ||
  813. state->grid[(ty-dy)*w+(tx-dx)] != GRID_PEG ||
  814. state->grid[ui->sy*w+ui->sx] != GRID_PEG)
  815. return MOVE_UI_UPDATE; /* grid contents were invalid */
  816. /*
  817. * We have a valid move. Encode it simply as source and
  818. * destination coordinate pairs.
  819. */
  820. sprintf(buf, "%d,%d-%d,%d", ui->sx, ui->sy, tx, ty);
  821. return dupstr(buf);
  822. } else if (IS_CURSOR_MOVE(button)) {
  823. if (!ui->cur_jumping) {
  824. /* Not jumping; move cursor as usual, making sure we don't
  825. * leave the gameboard (which may be an irregular shape) */
  826. int cx = ui->cur_x, cy = ui->cur_y;
  827. move_cursor(button, &cx, &cy, w, h, false);
  828. ui->cur_visible = true;
  829. if (state->grid[cy*w+cx] == GRID_HOLE ||
  830. state->grid[cy*w+cx] == GRID_PEG) {
  831. ui->cur_x = cx;
  832. ui->cur_y = cy;
  833. }
  834. return MOVE_UI_UPDATE;
  835. } else {
  836. int dx, dy, mx, my, jx, jy;
  837. /* We're jumping; if the requested direction has a hole, and
  838. * there's a peg in the way, */
  839. assert(state->grid[ui->cur_y*w+ui->cur_x] == GRID_PEG);
  840. dx = (button == CURSOR_RIGHT) ? 1 : (button == CURSOR_LEFT) ? -1 : 0;
  841. dy = (button == CURSOR_DOWN) ? 1 : (button == CURSOR_UP) ? -1 : 0;
  842. mx = ui->cur_x+dx; my = ui->cur_y+dy;
  843. jx = mx+dx; jy = my+dy;
  844. ui->cur_jumping = false; /* reset, whatever. */
  845. if (jx >= 0 && jy >= 0 && jx < w && jy < h &&
  846. state->grid[my*w+mx] == GRID_PEG &&
  847. state->grid[jy*w+jx] == GRID_HOLE) {
  848. /* Move cursor to the jumped-to location (this felt more
  849. * natural while playtesting) */
  850. sprintf(buf, "%d,%d-%d,%d", ui->cur_x, ui->cur_y, jx, jy);
  851. ui->cur_x = jx; ui->cur_y = jy;
  852. return dupstr(buf);
  853. }
  854. return MOVE_UI_UPDATE;
  855. }
  856. } else if (IS_CURSOR_SELECT(button)) {
  857. if (!ui->cur_visible) {
  858. ui->cur_visible = true;
  859. return MOVE_UI_UPDATE;
  860. }
  861. if (ui->cur_jumping) {
  862. ui->cur_jumping = false;
  863. return MOVE_UI_UPDATE;
  864. }
  865. if (state->grid[ui->cur_y*w+ui->cur_x] == GRID_PEG) {
  866. /* cursor is on peg: next arrow-move will jump. */
  867. ui->cur_jumping = true;
  868. return MOVE_UI_UPDATE;
  869. }
  870. return MOVE_NO_EFFECT;
  871. }
  872. return MOVE_UNUSED;
  873. }
  874. static game_state *execute_move(const game_state *state, const char *move)
  875. {
  876. int w = state->w, h = state->h;
  877. int sx, sy, tx, ty;
  878. game_state *ret;
  879. if (sscanf(move, "%d,%d-%d,%d", &sx, &sy, &tx, &ty) == 4) {
  880. int mx, my, dx, dy;
  881. if (sx < 0 || sx >= w || sy < 0 || sy >= h)
  882. return NULL; /* source out of range */
  883. if (tx < 0 || tx >= w || ty < 0 || ty >= h)
  884. return NULL; /* target out of range */
  885. dx = tx - sx;
  886. dy = ty - sy;
  887. if (max(abs(dx),abs(dy)) != 2 || min(abs(dx),abs(dy)) != 0)
  888. return NULL; /* move length was wrong */
  889. mx = sx + dx/2;
  890. my = sy + dy/2;
  891. if (state->grid[sy*w+sx] != GRID_PEG ||
  892. state->grid[my*w+mx] != GRID_PEG ||
  893. state->grid[ty*w+tx] != GRID_HOLE)
  894. return NULL; /* grid contents were invalid */
  895. ret = dup_game(state);
  896. ret->grid[sy*w+sx] = GRID_HOLE;
  897. ret->grid[my*w+mx] = GRID_HOLE;
  898. ret->grid[ty*w+tx] = GRID_PEG;
  899. /*
  900. * Opinion varies on whether getting to a single peg counts as
  901. * completing the game, or whether that peg has to be at a
  902. * specific location (central in the classic cross game, for
  903. * instance). For now we take the former, rather lax position.
  904. */
  905. if (!ret->completed) {
  906. int count = 0, i;
  907. for (i = 0; i < w*h; i++)
  908. if (ret->grid[i] == GRID_PEG)
  909. count++;
  910. if (count == 1)
  911. ret->completed = true;
  912. }
  913. return ret;
  914. }
  915. return NULL;
  916. }
  917. /* ----------------------------------------------------------------------
  918. * Drawing routines.
  919. */
  920. static void game_compute_size(const game_params *params, int tilesize,
  921. const game_ui *ui, int *x, int *y)
  922. {
  923. /* Ick: fake up `ds->tilesize' for macro expansion purposes */
  924. struct { int tilesize; } ads, *ds = &ads;
  925. ads.tilesize = tilesize;
  926. *x = TILESIZE * params->w + 2 * BORDER;
  927. *y = TILESIZE * params->h + 2 * BORDER;
  928. }
  929. static void game_set_size(drawing *dr, game_drawstate *ds,
  930. const game_params *params, int tilesize)
  931. {
  932. ds->tilesize = tilesize;
  933. assert(TILESIZE > 0);
  934. assert(!ds->drag_background); /* set_size is never called twice */
  935. ds->drag_background = blitter_new(dr, TILESIZE, TILESIZE);
  936. }
  937. static float *game_colours(frontend *fe, int *ncolours)
  938. {
  939. float *ret = snewn(3 * NCOLOURS, float);
  940. game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
  941. ret[COL_PEG * 3 + 0] = 0.0F;
  942. ret[COL_PEG * 3 + 1] = 0.0F;
  943. ret[COL_PEG * 3 + 2] = 1.0F;
  944. ret[COL_CURSOR * 3 + 0] = 0.5F;
  945. ret[COL_CURSOR * 3 + 1] = 0.5F;
  946. ret[COL_CURSOR * 3 + 2] = 1.0F;
  947. *ncolours = NCOLOURS;
  948. return ret;
  949. }
  950. static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
  951. {
  952. int w = state->w, h = state->h;
  953. struct game_drawstate *ds = snew(struct game_drawstate);
  954. ds->tilesize = 0; /* not decided yet */
  955. /* We can't allocate the blitter rectangle for the drag background
  956. * until we know what size to make it. */
  957. ds->drag_background = NULL;
  958. ds->dragging = false;
  959. ds->w = w;
  960. ds->h = h;
  961. ds->grid = snewn(w*h, unsigned char);
  962. memset(ds->grid, 255, w*h);
  963. ds->started = false;
  964. ds->bgcolour = -1;
  965. return ds;
  966. }
  967. static void game_free_drawstate(drawing *dr, game_drawstate *ds)
  968. {
  969. if (ds->drag_background)
  970. blitter_free(dr, ds->drag_background);
  971. sfree(ds->grid);
  972. sfree(ds);
  973. }
  974. static void draw_tile(drawing *dr, game_drawstate *ds,
  975. int x, int y, int v, int bgcolour)
  976. {
  977. bool cursor = false, jumping = false;
  978. int bg;
  979. if (bgcolour >= 0) {
  980. draw_rect(dr, x, y, TILESIZE, TILESIZE, bgcolour);
  981. }
  982. if (v >= GRID_JUMPING) {
  983. jumping = true; v -= GRID_JUMPING;
  984. }
  985. if (v >= GRID_CURSOR) {
  986. cursor = true; v -= GRID_CURSOR;
  987. }
  988. if (v == GRID_HOLE) {
  989. bg = cursor ? COL_HIGHLIGHT : COL_LOWLIGHT;
  990. assert(!jumping); /* can't jump from a hole! */
  991. draw_circle(dr, x+TILESIZE/2, y+TILESIZE/2, TILESIZE/4,
  992. bg, bg);
  993. } else if (v == GRID_PEG) {
  994. bg = (cursor || jumping) ? COL_CURSOR : COL_PEG;
  995. draw_circle(dr, x+TILESIZE/2, y+TILESIZE/2, TILESIZE/3,
  996. bg, bg);
  997. bg = (!cursor || jumping) ? COL_PEG : COL_CURSOR;
  998. draw_circle(dr, x+TILESIZE/2, y+TILESIZE/2, TILESIZE/4,
  999. bg, bg);
  1000. }
  1001. draw_update(dr, x, y, TILESIZE, TILESIZE);
  1002. }
  1003. static void game_redraw(drawing *dr, game_drawstate *ds,
  1004. const game_state *oldstate, const game_state *state,
  1005. int dir, const game_ui *ui,
  1006. float animtime, float flashtime)
  1007. {
  1008. int w = state->w, h = state->h;
  1009. int x, y;
  1010. int bgcolour;
  1011. if (flashtime > 0) {
  1012. int frame = (int)(flashtime / FLASH_FRAME);
  1013. bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
  1014. } else
  1015. bgcolour = COL_BACKGROUND;
  1016. /*
  1017. * Erase the sprite currently being dragged, if any.
  1018. */
  1019. if (ds->dragging) {
  1020. assert(ds->drag_background);
  1021. blitter_load(dr, ds->drag_background, ds->dragx, ds->dragy);
  1022. draw_update(dr, ds->dragx, ds->dragy, TILESIZE, TILESIZE);
  1023. ds->dragging = false;
  1024. }
  1025. if (!ds->started) {
  1026. /*
  1027. * Draw relief marks around all the squares that aren't
  1028. * GRID_OBST.
  1029. */
  1030. for (y = 0; y < h; y++)
  1031. for (x = 0; x < w; x++)
  1032. if (state->grid[y*w+x] != GRID_OBST) {
  1033. /*
  1034. * First pass: draw the full relief square.
  1035. */
  1036. int coords[6];
  1037. coords[0] = COORD(x+1) + HIGHLIGHT_WIDTH - 1;
  1038. coords[1] = COORD(y) - HIGHLIGHT_WIDTH;
  1039. coords[2] = COORD(x) - HIGHLIGHT_WIDTH;
  1040. coords[3] = COORD(y+1) + HIGHLIGHT_WIDTH - 1;
  1041. coords[4] = COORD(x) - HIGHLIGHT_WIDTH;
  1042. coords[5] = COORD(y) - HIGHLIGHT_WIDTH;
  1043. draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT);
  1044. coords[4] = COORD(x+1) + HIGHLIGHT_WIDTH - 1;
  1045. coords[5] = COORD(y+1) + HIGHLIGHT_WIDTH - 1;
  1046. draw_polygon(dr, coords, 3, COL_LOWLIGHT, COL_LOWLIGHT);
  1047. }
  1048. for (y = 0; y < h; y++)
  1049. for (x = 0; x < w; x++)
  1050. if (state->grid[y*w+x] != GRID_OBST) {
  1051. /*
  1052. * Second pass: draw everything but the two
  1053. * diagonal corners.
  1054. */
  1055. draw_rect(dr, COORD(x) - HIGHLIGHT_WIDTH,
  1056. COORD(y) - HIGHLIGHT_WIDTH,
  1057. TILESIZE + HIGHLIGHT_WIDTH,
  1058. TILESIZE + HIGHLIGHT_WIDTH, COL_HIGHLIGHT);
  1059. draw_rect(dr, COORD(x),
  1060. COORD(y),
  1061. TILESIZE + HIGHLIGHT_WIDTH,
  1062. TILESIZE + HIGHLIGHT_WIDTH, COL_LOWLIGHT);
  1063. }
  1064. for (y = 0; y < h; y++)
  1065. for (x = 0; x < w; x++)
  1066. if (state->grid[y*w+x] != GRID_OBST) {
  1067. /*
  1068. * Third pass: draw a trapezium on each edge.
  1069. */
  1070. int coords[8];
  1071. int dx, dy, s, sn, c;
  1072. for (dx = 0; dx < 2; dx++) {
  1073. dy = 1 - dx;
  1074. for (s = 0; s < 2; s++) {
  1075. sn = 2*s - 1;
  1076. c = s ? COL_LOWLIGHT : COL_HIGHLIGHT;
  1077. coords[0] = COORD(x) + (s*dx)*(TILESIZE-1);
  1078. coords[1] = COORD(y) + (s*dy)*(TILESIZE-1);
  1079. coords[2] = COORD(x) + (s*dx+dy)*(TILESIZE-1);
  1080. coords[3] = COORD(y) + (s*dy+dx)*(TILESIZE-1);
  1081. coords[4] = coords[2] - HIGHLIGHT_WIDTH * (dy-sn*dx);
  1082. coords[5] = coords[3] - HIGHLIGHT_WIDTH * (dx-sn*dy);
  1083. coords[6] = coords[0] + HIGHLIGHT_WIDTH * (dy+sn*dx);
  1084. coords[7] = coords[1] + HIGHLIGHT_WIDTH * (dx+sn*dy);
  1085. draw_polygon(dr, coords, 4, c, c);
  1086. }
  1087. }
  1088. }
  1089. for (y = 0; y < h; y++)
  1090. for (x = 0; x < w; x++)
  1091. if (state->grid[y*w+x] != GRID_OBST) {
  1092. /*
  1093. * Second pass: draw everything but the two
  1094. * diagonal corners.
  1095. */
  1096. draw_rect(dr, COORD(x),
  1097. COORD(y),
  1098. TILESIZE,
  1099. TILESIZE, COL_BACKGROUND);
  1100. }
  1101. ds->started = true;
  1102. draw_update(dr, 0, 0,
  1103. TILESIZE * state->w + 2 * BORDER,
  1104. TILESIZE * state->h + 2 * BORDER);
  1105. }
  1106. /*
  1107. * Loop over the grid redrawing anything that looks as if it
  1108. * needs it.
  1109. */
  1110. for (y = 0; y < h; y++)
  1111. for (x = 0; x < w; x++) {
  1112. int v;
  1113. v = state->grid[y*w+x];
  1114. /*
  1115. * Blank the source of a drag so it looks as if the
  1116. * user picked the peg up physically.
  1117. */
  1118. if (ui->dragging && ui->sx == x && ui->sy == y && v == GRID_PEG)
  1119. v = GRID_HOLE;
  1120. if (ui->cur_visible && ui->cur_x == x && ui->cur_y == y)
  1121. v += ui->cur_jumping ? GRID_JUMPING : GRID_CURSOR;
  1122. if (v != GRID_OBST &&
  1123. (bgcolour != ds->bgcolour || /* always redraw when flashing */
  1124. v != ds->grid[y*w+x])) {
  1125. draw_tile(dr, ds, COORD(x), COORD(y), v, bgcolour);
  1126. ds->grid[y*w+x] = v;
  1127. }
  1128. }
  1129. /*
  1130. * Draw the dragging sprite if any.
  1131. */
  1132. if (ui->dragging) {
  1133. ds->dragging = true;
  1134. ds->dragx = ui->dx - TILESIZE/2;
  1135. ds->dragy = ui->dy - TILESIZE/2;
  1136. blitter_save(dr, ds->drag_background, ds->dragx, ds->dragy);
  1137. draw_tile(dr, ds, ds->dragx, ds->dragy, GRID_PEG, -1);
  1138. }
  1139. ds->bgcolour = bgcolour;
  1140. }
  1141. static float game_anim_length(const game_state *oldstate,
  1142. const game_state *newstate, int dir, game_ui *ui)
  1143. {
  1144. return 0.0F;
  1145. }
  1146. static float game_flash_length(const game_state *oldstate,
  1147. const game_state *newstate, int dir, game_ui *ui)
  1148. {
  1149. if (!oldstate->completed && newstate->completed)
  1150. return 2 * FLASH_FRAME;
  1151. else
  1152. return 0.0F;
  1153. }
  1154. static void game_get_cursor_location(const game_ui *ui,
  1155. const game_drawstate *ds,
  1156. const game_state *state,
  1157. const game_params *params,
  1158. int *x, int *y, int *w, int *h)
  1159. {
  1160. if(ui->cur_visible) {
  1161. *x = COORD(ui->cur_x);
  1162. *y = COORD(ui->cur_y);
  1163. *w = *h = TILESIZE;
  1164. }
  1165. }
  1166. static int game_status(const game_state *state)
  1167. {
  1168. /*
  1169. * Dead-end situations are assumed to be rescuable by Undo, so we
  1170. * don't bother to identify them and return -1.
  1171. */
  1172. return state->completed ? +1 : 0;
  1173. }
  1174. #ifdef COMBINED
  1175. #define thegame pegs
  1176. #endif
  1177. const struct game thegame = {
  1178. "Pegs", "games.pegs", "pegs",
  1179. default_params,
  1180. game_fetch_preset, NULL,
  1181. decode_params,
  1182. encode_params,
  1183. free_params,
  1184. dup_params,
  1185. true, game_configure, custom_params,
  1186. validate_params,
  1187. new_game_desc,
  1188. validate_desc,
  1189. new_game,
  1190. dup_game,
  1191. free_game,
  1192. false, NULL, /* solve */
  1193. true, game_can_format_as_text_now, game_text_format,
  1194. NULL, NULL, /* get_prefs, set_prefs */
  1195. new_ui,
  1196. free_ui,
  1197. NULL, /* encode_ui */
  1198. NULL, /* decode_ui */
  1199. NULL, /* game_request_keys */
  1200. game_changed_state,
  1201. current_key_label,
  1202. interpret_move,
  1203. execute_move,
  1204. PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
  1205. game_colours,
  1206. game_new_drawstate,
  1207. game_free_drawstate,
  1208. game_redraw,
  1209. game_anim_length,
  1210. game_flash_length,
  1211. game_get_cursor_location,
  1212. game_status,
  1213. false, false, NULL, NULL, /* print_size, print */
  1214. false, /* wants_statusbar */
  1215. false, NULL, /* timing_state */
  1216. 0, /* flags */
  1217. };
  1218. /* vim: set shiftwidth=4 tabstop=8: */