4efb3868 |
1 | /* |
2 | * fifteen.c: standard 15-puzzle. |
3 | */ |
4 | |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
7 | #include <string.h> |
8 | #include <assert.h> |
b0e26073 |
9 | #include <ctype.h> |
4efb3868 |
10 | #include <math.h> |
11 | |
12 | #include "puzzles.h" |
13 | |
1e3e152d |
14 | #define PREFERRED_TILE_SIZE 48 |
15 | #define TILE_SIZE (ds->tilesize) |
4efb3868 |
16 | #define BORDER (TILE_SIZE / 2) |
17 | #define HIGHLIGHT_WIDTH (TILE_SIZE / 20) |
18 | #define COORD(x) ( (x) * TILE_SIZE + BORDER ) |
19 | #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 ) |
20 | |
8c1fd974 |
21 | #define ANIM_TIME 0.13F |
22 | #define FLASH_FRAME 0.13F |
4efb3868 |
23 | |
24 | #define X(state, i) ( (i) % (state)->w ) |
25 | #define Y(state, i) ( (i) / (state)->w ) |
26 | #define C(state, x, y) ( (y) * (state)->w + (x) ) |
27 | |
28 | enum { |
29 | COL_BACKGROUND, |
30 | COL_TEXT, |
31 | COL_HIGHLIGHT, |
32 | COL_LOWLIGHT, |
33 | NCOLOURS |
34 | }; |
35 | |
36 | struct game_params { |
37 | int w, h; |
38 | }; |
39 | |
40 | struct game_state { |
41 | int w, h, n; |
42 | int *tiles; |
43 | int gap_pos; |
44 | int completed; |
2ac6d24e |
45 | int used_solve; /* used to suppress completion flash */ |
fd1a1a2b |
46 | int movecount; |
4efb3868 |
47 | }; |
48 | |
be8d5aa1 |
49 | static game_params *default_params(void) |
4efb3868 |
50 | { |
51 | game_params *ret = snew(game_params); |
52 | |
53 | ret->w = ret->h = 4; |
54 | |
55 | return ret; |
56 | } |
57 | |
be8d5aa1 |
58 | static int game_fetch_preset(int i, char **name, game_params **params) |
4efb3868 |
59 | { |
60 | return FALSE; |
61 | } |
62 | |
be8d5aa1 |
63 | static void free_params(game_params *params) |
4efb3868 |
64 | { |
65 | sfree(params); |
66 | } |
67 | |
be8d5aa1 |
68 | static game_params *dup_params(game_params *params) |
4efb3868 |
69 | { |
70 | game_params *ret = snew(game_params); |
71 | *ret = *params; /* structure copy */ |
72 | return ret; |
73 | } |
74 | |
1185e3c5 |
75 | static void decode_params(game_params *ret, char const *string) |
b0e26073 |
76 | { |
b0e26073 |
77 | ret->w = ret->h = atoi(string); |
89167dad |
78 | while (*string && isdigit((unsigned char)*string)) string++; |
b0e26073 |
79 | if (*string == 'x') { |
80 | string++; |
81 | ret->h = atoi(string); |
82 | } |
b0e26073 |
83 | } |
84 | |
1185e3c5 |
85 | static char *encode_params(game_params *params, int full) |
b0e26073 |
86 | { |
87 | char data[256]; |
88 | |
89 | sprintf(data, "%dx%d", params->w, params->h); |
90 | |
91 | return dupstr(data); |
92 | } |
93 | |
be8d5aa1 |
94 | static config_item *game_configure(game_params *params) |
c8230524 |
95 | { |
96 | config_item *ret; |
97 | char buf[80]; |
98 | |
99 | ret = snewn(3, config_item); |
100 | |
101 | ret[0].name = "Width"; |
95709966 |
102 | ret[0].type = C_STRING; |
c8230524 |
103 | sprintf(buf, "%d", params->w); |
104 | ret[0].sval = dupstr(buf); |
105 | ret[0].ival = 0; |
106 | |
107 | ret[1].name = "Height"; |
95709966 |
108 | ret[1].type = C_STRING; |
c8230524 |
109 | sprintf(buf, "%d", params->h); |
110 | ret[1].sval = dupstr(buf); |
111 | ret[1].ival = 0; |
112 | |
113 | ret[2].name = NULL; |
95709966 |
114 | ret[2].type = C_END; |
c8230524 |
115 | ret[2].sval = NULL; |
116 | ret[2].ival = 0; |
117 | |
118 | return ret; |
119 | } |
120 | |
be8d5aa1 |
121 | static game_params *custom_params(config_item *cfg) |
c8230524 |
122 | { |
123 | game_params *ret = snew(game_params); |
124 | |
125 | ret->w = atoi(cfg[0].sval); |
126 | ret->h = atoi(cfg[1].sval); |
127 | |
128 | return ret; |
129 | } |
130 | |
3ff276f2 |
131 | static char *validate_params(game_params *params, int full) |
c8230524 |
132 | { |
ab53eb64 |
133 | if (params->w < 2 || params->h < 2) |
c8230524 |
134 | return "Width and height must both be at least two"; |
135 | |
136 | return NULL; |
137 | } |
138 | |
be8d5aa1 |
139 | static int perm_parity(int *perm, int n) |
4efb3868 |
140 | { |
141 | int i, j, ret; |
142 | |
143 | ret = 0; |
144 | |
145 | for (i = 0; i < n-1; i++) |
146 | for (j = i+1; j < n; j++) |
147 | if (perm[i] > perm[j]) |
148 | ret = !ret; |
149 | |
150 | return ret; |
151 | } |
152 | |
1185e3c5 |
153 | static char *new_game_desc(game_params *params, random_state *rs, |
c566778e |
154 | char **aux, int interactive) |
4efb3868 |
155 | { |
156 | int gap, n, i, x; |
157 | int x1, x2, p1, p2, parity; |
158 | int *tiles, *used; |
159 | char *ret; |
160 | int retlen; |
161 | |
162 | n = params->w * params->h; |
163 | |
164 | tiles = snewn(n, int); |
165 | used = snewn(n, int); |
166 | |
167 | for (i = 0; i < n; i++) { |
168 | tiles[i] = -1; |
169 | used[i] = FALSE; |
170 | } |
171 | |
48d70ca9 |
172 | gap = random_upto(rs, n); |
4efb3868 |
173 | tiles[gap] = 0; |
174 | used[0] = TRUE; |
175 | |
176 | /* |
177 | * Place everything else except the last two tiles. |
178 | */ |
179 | for (x = 0, i = n-1; i > 2; i--) { |
48d70ca9 |
180 | int k = random_upto(rs, i); |
4efb3868 |
181 | int j; |
182 | |
183 | for (j = 0; j < n; j++) |
184 | if (!used[j] && (k-- == 0)) |
185 | break; |
186 | |
187 | assert(j < n && !used[j]); |
188 | used[j] = TRUE; |
189 | |
190 | while (tiles[x] >= 0) |
191 | x++; |
192 | assert(x < n); |
193 | tiles[x] = j; |
194 | } |
195 | |
196 | /* |
197 | * Find the last two locations, and the last two pieces. |
198 | */ |
199 | while (tiles[x] >= 0) |
200 | x++; |
201 | assert(x < n); |
202 | x1 = x; |
203 | x++; |
204 | while (tiles[x] >= 0) |
205 | x++; |
206 | assert(x < n); |
207 | x2 = x; |
208 | |
209 | for (i = 0; i < n; i++) |
210 | if (!used[i]) |
211 | break; |
212 | p1 = i; |
213 | for (i = p1+1; i < n; i++) |
214 | if (!used[i]) |
215 | break; |
216 | p2 = i; |
217 | |
218 | /* |
219 | * Determine the required parity of the overall permutation. |
220 | * This is the XOR of: |
221 | * |
d3a026ed |
222 | * - The chessboard parity ((x^y)&1) of the gap square. The |
223 | * bottom right counts as even. |
4efb3868 |
224 | * |
225 | * - The parity of n. (The target permutation is 1,...,n-1,0 |
226 | * rather than 0,...,n-1; this is a cyclic permutation of |
227 | * the starting point and hence is odd iff n is even.) |
228 | */ |
d3a026ed |
229 | parity = ((X(params, gap) - (params->w-1)) ^ |
230 | (Y(params, gap) - (params->h-1)) ^ |
231 | (n+1)) & 1; |
4efb3868 |
232 | |
233 | /* |
234 | * Try the last two tiles one way round. If that fails, swap |
235 | * them. |
236 | */ |
237 | tiles[x1] = p1; |
238 | tiles[x2] = p2; |
239 | if (perm_parity(tiles, n) != parity) { |
240 | tiles[x1] = p2; |
241 | tiles[x2] = p1; |
242 | assert(perm_parity(tiles, n) == parity); |
243 | } |
244 | |
245 | /* |
1185e3c5 |
246 | * Now construct the game description, by describing the tile |
247 | * array as a simple sequence of comma-separated integers. |
4efb3868 |
248 | */ |
249 | ret = NULL; |
250 | retlen = 0; |
251 | for (i = 0; i < n; i++) { |
252 | char buf[80]; |
253 | int k; |
254 | |
255 | k = sprintf(buf, "%d,", tiles[i]); |
256 | |
257 | ret = sresize(ret, retlen + k + 1, char); |
258 | strcpy(ret + retlen, buf); |
259 | retlen += k; |
260 | } |
261 | ret[retlen-1] = '\0'; /* delete last comma */ |
262 | |
263 | sfree(tiles); |
264 | sfree(used); |
265 | |
266 | return ret; |
267 | } |
268 | |
1185e3c5 |
269 | static char *validate_desc(game_params *params, char *desc) |
5928817c |
270 | { |
271 | char *p, *err; |
272 | int i, area; |
273 | int *used; |
274 | |
275 | area = params->w * params->h; |
1185e3c5 |
276 | p = desc; |
5928817c |
277 | err = NULL; |
278 | |
279 | used = snewn(area, int); |
280 | for (i = 0; i < area; i++) |
281 | used[i] = FALSE; |
282 | |
283 | for (i = 0; i < area; i++) { |
284 | char *q = p; |
285 | int n; |
286 | |
287 | if (*p < '0' || *p > '9') { |
288 | err = "Not enough numbers in string"; |
289 | goto leave; |
290 | } |
291 | while (*p >= '0' && *p <= '9') |
292 | p++; |
293 | if (i < area-1 && *p != ',') { |
294 | err = "Expected comma after number"; |
295 | goto leave; |
296 | } |
297 | else if (i == area-1 && *p) { |
298 | err = "Excess junk at end of string"; |
299 | goto leave; |
300 | } |
301 | n = atoi(q); |
302 | if (n < 0 || n >= area) { |
303 | err = "Number out of range"; |
304 | goto leave; |
305 | } |
306 | if (used[n]) { |
307 | err = "Number used twice"; |
308 | goto leave; |
309 | } |
310 | used[n] = TRUE; |
311 | |
312 | if (*p) p++; /* eat comma */ |
313 | } |
314 | |
315 | leave: |
316 | sfree(used); |
317 | return err; |
318 | } |
319 | |
dafd6cf6 |
320 | static game_state *new_game(midend *me, game_params *params, char *desc) |
4efb3868 |
321 | { |
322 | game_state *state = snew(game_state); |
323 | int i; |
324 | char *p; |
325 | |
326 | state->w = params->w; |
327 | state->h = params->h; |
328 | state->n = params->w * params->h; |
329 | state->tiles = snewn(state->n, int); |
330 | |
331 | state->gap_pos = 0; |
1185e3c5 |
332 | p = desc; |
4efb3868 |
333 | i = 0; |
334 | for (i = 0; i < state->n; i++) { |
335 | assert(*p); |
336 | state->tiles[i] = atoi(p); |
337 | if (state->tiles[i] == 0) |
338 | state->gap_pos = i; |
339 | while (*p && *p != ',') |
340 | p++; |
341 | if (*p) p++; /* eat comma */ |
342 | } |
343 | assert(!*p); |
344 | assert(state->tiles[state->gap_pos] == 0); |
345 | |
fd1a1a2b |
346 | state->completed = state->movecount = 0; |
a440f184 |
347 | state->used_solve = FALSE; |
4efb3868 |
348 | |
349 | return state; |
350 | } |
351 | |
be8d5aa1 |
352 | static game_state *dup_game(game_state *state) |
4efb3868 |
353 | { |
354 | game_state *ret = snew(game_state); |
355 | |
356 | ret->w = state->w; |
357 | ret->h = state->h; |
358 | ret->n = state->n; |
359 | ret->tiles = snewn(state->w * state->h, int); |
360 | memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int)); |
361 | ret->gap_pos = state->gap_pos; |
362 | ret->completed = state->completed; |
fd1a1a2b |
363 | ret->movecount = state->movecount; |
2ac6d24e |
364 | ret->used_solve = state->used_solve; |
4efb3868 |
365 | |
366 | return ret; |
367 | } |
368 | |
be8d5aa1 |
369 | static void free_game(game_state *state) |
4efb3868 |
370 | { |
ab53eb64 |
371 | sfree(state->tiles); |
4efb3868 |
372 | sfree(state); |
373 | } |
374 | |
df11cd4e |
375 | static char *solve_game(game_state *state, game_state *currstate, |
c566778e |
376 | char *aux, char **error) |
2ac6d24e |
377 | { |
df11cd4e |
378 | return dupstr("S"); |
2ac6d24e |
379 | } |
380 | |
9b4b03d3 |
381 | static char *game_text_format(game_state *state) |
382 | { |
af52394e |
383 | char *ret, *p, buf[80]; |
384 | int x, y, col, maxlen; |
385 | |
386 | /* |
387 | * First work out how many characters we need to display each |
388 | * number. |
389 | */ |
390 | col = sprintf(buf, "%d", state->n-1); |
391 | |
392 | /* |
393 | * Now we know the exact total size of the grid we're going to |
394 | * produce: it's got h rows, each containing w lots of col, w-1 |
395 | * spaces and a trailing newline. |
396 | */ |
397 | maxlen = state->h * state->w * (col+1); |
398 | |
48a10826 |
399 | ret = snewn(maxlen+1, char); |
af52394e |
400 | p = ret; |
401 | |
402 | for (y = 0; y < state->h; y++) { |
403 | for (x = 0; x < state->w; x++) { |
404 | int v = state->tiles[state->w*y+x]; |
405 | if (v == 0) |
406 | sprintf(buf, "%*s", col, ""); |
407 | else |
408 | sprintf(buf, "%*d", col, v); |
409 | memcpy(p, buf, col); |
410 | p += col; |
411 | if (x+1 == state->w) |
412 | *p++ = '\n'; |
413 | else |
414 | *p++ = ' '; |
415 | } |
416 | } |
417 | |
418 | assert(p - ret == maxlen); |
419 | *p = '\0'; |
420 | return ret; |
9b4b03d3 |
421 | } |
422 | |
be8d5aa1 |
423 | static game_ui *new_ui(game_state *state) |
74a4e547 |
424 | { |
425 | return NULL; |
426 | } |
427 | |
be8d5aa1 |
428 | static void free_ui(game_ui *ui) |
74a4e547 |
429 | { |
430 | } |
431 | |
844f605f |
432 | static char *encode_ui(game_ui *ui) |
ae8290c6 |
433 | { |
434 | return NULL; |
435 | } |
436 | |
844f605f |
437 | static void decode_ui(game_ui *ui, char *encoding) |
ae8290c6 |
438 | { |
439 | } |
440 | |
07dfb697 |
441 | static void game_changed_state(game_ui *ui, game_state *oldstate, |
442 | game_state *newstate) |
443 | { |
444 | } |
445 | |
1e3e152d |
446 | struct game_drawstate { |
447 | int started; |
448 | int w, h, bgcolour; |
449 | int *tiles; |
450 | int tilesize; |
451 | }; |
452 | |
df11cd4e |
453 | static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, |
454 | int x, int y, int button) |
455 | { |
456 | int gx, gy, dx, dy; |
457 | char buf[80]; |
4efb3868 |
458 | |
f0ee053c |
459 | button &= ~MOD_MASK; |
460 | |
df11cd4e |
461 | gx = X(state, state->gap_pos); |
462 | gy = Y(state, state->gap_pos); |
4efb3868 |
463 | |
464 | if (button == CURSOR_RIGHT && gx > 0) |
465 | dx = gx - 1, dy = gy; |
df11cd4e |
466 | else if (button == CURSOR_LEFT && gx < state->w-1) |
4efb3868 |
467 | dx = gx + 1, dy = gy; |
468 | else if (button == CURSOR_DOWN && gy > 0) |
469 | dy = gy - 1, dx = gx; |
df11cd4e |
470 | else if (button == CURSOR_UP && gy < state->h-1) |
4efb3868 |
471 | dy = gy + 1, dx = gx; |
472 | else if (button == LEFT_BUTTON) { |
473 | dx = FROMCOORD(x); |
474 | dy = FROMCOORD(y); |
df11cd4e |
475 | if (dx < 0 || dx >= state->w || dy < 0 || dy >= state->h) |
4efb3868 |
476 | return NULL; /* out of bounds */ |
477 | /* |
478 | * Any click location should be equal to the gap location |
479 | * in _precisely_ one coordinate. |
480 | */ |
481 | if ((dx == gx && dy == gy) || (dx != gx && dy != gy)) |
482 | return NULL; |
483 | } else |
484 | return NULL; /* no move */ |
485 | |
df11cd4e |
486 | sprintf(buf, "M%d,%d", dx, dy); |
487 | return dupstr(buf); |
488 | } |
489 | |
490 | static game_state *execute_move(game_state *from, char *move) |
491 | { |
492 | int gx, gy, dx, dy, ux, uy, up, p; |
493 | game_state *ret; |
494 | |
495 | if (!strcmp(move, "S")) { |
496 | int i; |
497 | |
498 | ret = dup_game(from); |
499 | |
500 | /* |
501 | * Simply replace the grid with a solved one. For this game, |
502 | * this isn't a useful operation for actually telling the user |
503 | * what they should have done, but it is useful for |
504 | * conveniently being able to get hold of a clean state from |
505 | * which to practise manoeuvres. |
506 | */ |
507 | for (i = 0; i < ret->n; i++) |
508 | ret->tiles[i] = (i+1) % ret->n; |
509 | ret->gap_pos = ret->n-1; |
a440f184 |
510 | ret->used_solve = TRUE; |
df11cd4e |
511 | ret->completed = ret->movecount = 1; |
512 | |
513 | return ret; |
514 | } |
515 | |
516 | gx = X(from, from->gap_pos); |
517 | gy = Y(from, from->gap_pos); |
518 | |
519 | if (move[0] != 'M' || |
520 | sscanf(move+1, "%d,%d", &dx, &dy) != 2 || |
521 | (dx == gx && dy == gy) || (dx != gx && dy != gy) || |
522 | dx < 0 || dx >= from->w || dy < 0 || dy >= from->h) |
523 | return NULL; |
524 | |
4efb3868 |
525 | /* |
526 | * Find the unit displacement from the original gap |
527 | * position towards this one. |
528 | */ |
529 | ux = (dx < gx ? -1 : dx > gx ? +1 : 0); |
530 | uy = (dy < gy ? -1 : dy > gy ? +1 : 0); |
531 | up = C(from, ux, uy); |
532 | |
533 | ret = dup_game(from); |
534 | |
535 | ret->gap_pos = C(from, dx, dy); |
536 | assert(ret->gap_pos >= 0 && ret->gap_pos < ret->n); |
537 | |
538 | ret->tiles[ret->gap_pos] = 0; |
539 | |
540 | for (p = from->gap_pos; p != ret->gap_pos; p += up) { |
541 | assert(p >= 0 && p < from->n); |
542 | ret->tiles[p] = from->tiles[p + up]; |
fd1a1a2b |
543 | ret->movecount++; |
4efb3868 |
544 | } |
545 | |
546 | /* |
547 | * See if the game has been completed. |
548 | */ |
549 | if (!ret->completed) { |
fd1a1a2b |
550 | ret->completed = ret->movecount; |
4efb3868 |
551 | for (p = 0; p < ret->n; p++) |
552 | if (ret->tiles[p] != (p < ret->n-1 ? p+1 : 0)) |
fd1a1a2b |
553 | ret->completed = 0; |
4efb3868 |
554 | } |
555 | |
556 | return ret; |
557 | } |
558 | |
559 | /* ---------------------------------------------------------------------- |
560 | * Drawing routines. |
561 | */ |
562 | |
1f3ee4ee |
563 | static void game_compute_size(game_params *params, int tilesize, |
564 | int *x, int *y) |
4efb3868 |
565 | { |
1f3ee4ee |
566 | /* Ick: fake up `ds->tilesize' for macro expansion purposes */ |
567 | struct { int tilesize; } ads, *ds = &ads; |
568 | ads.tilesize = tilesize; |
1e3e152d |
569 | |
4efb3868 |
570 | *x = TILE_SIZE * params->w + 2 * BORDER; |
571 | *y = TILE_SIZE * params->h + 2 * BORDER; |
572 | } |
573 | |
dafd6cf6 |
574 | static void game_set_size(drawing *dr, game_drawstate *ds, |
575 | game_params *params, int tilesize) |
1f3ee4ee |
576 | { |
577 | ds->tilesize = tilesize; |
578 | } |
579 | |
8266f3fc |
580 | static float *game_colours(frontend *fe, int *ncolours) |
4efb3868 |
581 | { |
582 | float *ret = snewn(3 * NCOLOURS, float); |
583 | int i; |
4efb3868 |
584 | |
937a9eff |
585 | game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT); |
4efb3868 |
586 | |
937a9eff |
587 | for (i = 0; i < 3; i++) |
4efb3868 |
588 | ret[COL_TEXT * 3 + i] = 0.0; |
4efb3868 |
589 | |
590 | *ncolours = NCOLOURS; |
591 | return ret; |
592 | } |
593 | |
dafd6cf6 |
594 | static game_drawstate *game_new_drawstate(drawing *dr, game_state *state) |
4efb3868 |
595 | { |
596 | struct game_drawstate *ds = snew(struct game_drawstate); |
597 | int i; |
598 | |
599 | ds->started = FALSE; |
600 | ds->w = state->w; |
601 | ds->h = state->h; |
602 | ds->bgcolour = COL_BACKGROUND; |
603 | ds->tiles = snewn(ds->w*ds->h, int); |
1e3e152d |
604 | ds->tilesize = 0; /* haven't decided yet */ |
4efb3868 |
605 | for (i = 0; i < ds->w*ds->h; i++) |
606 | ds->tiles[i] = -1; |
607 | |
608 | return ds; |
609 | } |
610 | |
dafd6cf6 |
611 | static void game_free_drawstate(drawing *dr, game_drawstate *ds) |
4efb3868 |
612 | { |
613 | sfree(ds->tiles); |
614 | sfree(ds); |
615 | } |
616 | |
dafd6cf6 |
617 | static void draw_tile(drawing *dr, game_drawstate *ds, game_state *state, |
1e3e152d |
618 | int x, int y, int tile, int flash_colour) |
4efb3868 |
619 | { |
620 | if (tile == 0) { |
dafd6cf6 |
621 | draw_rect(dr, x, y, TILE_SIZE, TILE_SIZE, |
4efb3868 |
622 | flash_colour); |
623 | } else { |
624 | int coords[6]; |
625 | char str[40]; |
626 | |
627 | coords[0] = x + TILE_SIZE - 1; |
628 | coords[1] = y + TILE_SIZE - 1; |
629 | coords[2] = x + TILE_SIZE - 1; |
630 | coords[3] = y; |
631 | coords[4] = x; |
632 | coords[5] = y + TILE_SIZE - 1; |
dafd6cf6 |
633 | draw_polygon(dr, coords, 3, COL_LOWLIGHT, COL_LOWLIGHT); |
4efb3868 |
634 | |
635 | coords[0] = x; |
636 | coords[1] = y; |
dafd6cf6 |
637 | draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT); |
4efb3868 |
638 | |
dafd6cf6 |
639 | draw_rect(dr, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH, |
4efb3868 |
640 | TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH, |
641 | flash_colour); |
642 | |
643 | sprintf(str, "%d", tile); |
dafd6cf6 |
644 | draw_text(dr, x + TILE_SIZE/2, y + TILE_SIZE/2, |
4efb3868 |
645 | FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE, |
646 | COL_TEXT, str); |
647 | } |
dafd6cf6 |
648 | draw_update(dr, x, y, TILE_SIZE, TILE_SIZE); |
4efb3868 |
649 | } |
650 | |
dafd6cf6 |
651 | static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, |
c822de4a |
652 | game_state *state, int dir, game_ui *ui, |
74a4e547 |
653 | float animtime, float flashtime) |
4efb3868 |
654 | { |
655 | int i, pass, bgcolour; |
656 | |
657 | if (flashtime > 0) { |
658 | int frame = (int)(flashtime / FLASH_FRAME); |
659 | bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT); |
660 | } else |
661 | bgcolour = COL_BACKGROUND; |
662 | |
663 | if (!ds->started) { |
19f24306 |
664 | int coords[10]; |
4efb3868 |
665 | |
dafd6cf6 |
666 | draw_rect(dr, 0, 0, |
4efb3868 |
667 | TILE_SIZE * state->w + 2 * BORDER, |
668 | TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND); |
dafd6cf6 |
669 | draw_update(dr, 0, 0, |
4efb3868 |
670 | TILE_SIZE * state->w + 2 * BORDER, |
671 | TILE_SIZE * state->h + 2 * BORDER); |
672 | |
673 | /* |
674 | * Recessed area containing the whole puzzle. |
675 | */ |
676 | coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1; |
677 | coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1; |
678 | coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1; |
679 | coords[3] = COORD(0) - HIGHLIGHT_WIDTH; |
19f24306 |
680 | coords[4] = coords[2] - TILE_SIZE; |
681 | coords[5] = coords[3] + TILE_SIZE; |
682 | coords[8] = COORD(0) - HIGHLIGHT_WIDTH; |
683 | coords[9] = COORD(state->h) + HIGHLIGHT_WIDTH - 1; |
684 | coords[6] = coords[8] + TILE_SIZE; |
685 | coords[7] = coords[9] - TILE_SIZE; |
dafd6cf6 |
686 | draw_polygon(dr, coords, 5, COL_HIGHLIGHT, COL_HIGHLIGHT); |
4efb3868 |
687 | |
688 | coords[1] = COORD(0) - HIGHLIGHT_WIDTH; |
689 | coords[0] = COORD(0) - HIGHLIGHT_WIDTH; |
dafd6cf6 |
690 | draw_polygon(dr, coords, 5, COL_LOWLIGHT, COL_LOWLIGHT); |
4efb3868 |
691 | |
692 | ds->started = TRUE; |
693 | } |
694 | |
695 | /* |
696 | * Now draw each tile. We do this in two passes to make |
697 | * animation easy. |
698 | */ |
699 | for (pass = 0; pass < 2; pass++) { |
700 | for (i = 0; i < state->n; i++) { |
701 | int t, t0; |
702 | /* |
703 | * Figure out what should be displayed at this |
704 | * location. It's either a simple tile, or it's a |
705 | * transition between two tiles (in which case we say |
706 | * -1 because it must always be drawn). |
707 | */ |
708 | |
709 | if (oldstate && oldstate->tiles[i] != state->tiles[i]) |
710 | t = -1; |
711 | else |
712 | t = state->tiles[i]; |
713 | |
714 | t0 = t; |
715 | |
716 | if (ds->bgcolour != bgcolour || /* always redraw when flashing */ |
717 | ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) { |
718 | int x, y; |
719 | |
720 | /* |
721 | * Figure out what to _actually_ draw, and where to |
722 | * draw it. |
723 | */ |
724 | if (t == -1) { |
725 | int x0, y0, x1, y1; |
726 | int j; |
727 | |
728 | /* |
729 | * On the first pass, just blank the tile. |
730 | */ |
731 | if (pass == 0) { |
732 | x = COORD(X(state, i)); |
733 | y = COORD(Y(state, i)); |
734 | t = 0; |
735 | } else { |
736 | float c; |
737 | |
738 | t = state->tiles[i]; |
739 | |
740 | /* |
741 | * Don't bother moving the gap; just don't |
742 | * draw it. |
743 | */ |
744 | if (t == 0) |
745 | continue; |
746 | |
747 | /* |
748 | * Find the coordinates of this tile in the old and |
749 | * new states. |
750 | */ |
751 | x1 = COORD(X(state, i)); |
752 | y1 = COORD(Y(state, i)); |
753 | for (j = 0; j < oldstate->n; j++) |
754 | if (oldstate->tiles[j] == state->tiles[i]) |
755 | break; |
756 | assert(j < oldstate->n); |
757 | x0 = COORD(X(state, j)); |
758 | y0 = COORD(Y(state, j)); |
759 | |
760 | c = (animtime / ANIM_TIME); |
761 | if (c < 0.0F) c = 0.0F; |
762 | if (c > 1.0F) c = 1.0F; |
763 | |
764 | x = x0 + (int)(c * (x1 - x0)); |
765 | y = y0 + (int)(c * (y1 - y0)); |
766 | } |
767 | |
768 | } else { |
769 | if (pass == 0) |
770 | continue; |
771 | x = COORD(X(state, i)); |
772 | y = COORD(Y(state, i)); |
773 | } |
774 | |
dafd6cf6 |
775 | draw_tile(dr, ds, state, x, y, t, bgcolour); |
4efb3868 |
776 | } |
777 | ds->tiles[i] = t0; |
778 | } |
779 | } |
780 | ds->bgcolour = bgcolour; |
fd1a1a2b |
781 | |
782 | /* |
783 | * Update the status bar. |
784 | */ |
785 | { |
786 | char statusbuf[256]; |
787 | |
d108c342 |
788 | /* |
789 | * Don't show the new status until we're also showing the |
790 | * new _state_ - after the game animation is complete. |
791 | */ |
792 | if (oldstate) |
793 | state = oldstate; |
794 | |
2ac6d24e |
795 | if (state->used_solve) |
796 | sprintf(statusbuf, "Moves since auto-solve: %d", |
797 | state->movecount - state->completed); |
798 | else |
799 | sprintf(statusbuf, "%sMoves: %d", |
800 | (state->completed ? "COMPLETED! " : ""), |
801 | (state->completed ? state->completed : state->movecount)); |
fd1a1a2b |
802 | |
dafd6cf6 |
803 | status_bar(dr, statusbuf); |
fd1a1a2b |
804 | } |
4efb3868 |
805 | } |
806 | |
be8d5aa1 |
807 | static float game_anim_length(game_state *oldstate, |
e3f21163 |
808 | game_state *newstate, int dir, game_ui *ui) |
4efb3868 |
809 | { |
a440f184 |
810 | return ANIM_TIME; |
4efb3868 |
811 | } |
812 | |
be8d5aa1 |
813 | static float game_flash_length(game_state *oldstate, |
e3f21163 |
814 | game_state *newstate, int dir, game_ui *ui) |
4efb3868 |
815 | { |
2ac6d24e |
816 | if (!oldstate->completed && newstate->completed && |
817 | !oldstate->used_solve && !newstate->used_solve) |
4efb3868 |
818 | return 2 * FLASH_FRAME; |
819 | else |
820 | return 0.0F; |
821 | } |
fd1a1a2b |
822 | |
4d08de49 |
823 | static int game_timing_state(game_state *state, game_ui *ui) |
48dcdd62 |
824 | { |
825 | return TRUE; |
826 | } |
827 | |
dafd6cf6 |
828 | static void game_print_size(game_params *params, float *x, float *y) |
829 | { |
830 | } |
831 | |
832 | static void game_print(drawing *dr, game_state *state, int tilesize) |
833 | { |
834 | } |
835 | |
be8d5aa1 |
836 | #ifdef COMBINED |
837 | #define thegame fifteen |
838 | #endif |
839 | |
840 | const struct game thegame = { |
750037d7 |
841 | "Fifteen", "games.fifteen", "fifteen", |
be8d5aa1 |
842 | default_params, |
843 | game_fetch_preset, |
844 | decode_params, |
845 | encode_params, |
846 | free_params, |
847 | dup_params, |
1d228b10 |
848 | TRUE, game_configure, custom_params, |
be8d5aa1 |
849 | validate_params, |
1185e3c5 |
850 | new_game_desc, |
1185e3c5 |
851 | validate_desc, |
be8d5aa1 |
852 | new_game, |
853 | dup_game, |
854 | free_game, |
2ac6d24e |
855 | TRUE, solve_game, |
af52394e |
856 | TRUE, game_text_format, |
be8d5aa1 |
857 | new_ui, |
858 | free_ui, |
ae8290c6 |
859 | encode_ui, |
860 | decode_ui, |
07dfb697 |
861 | game_changed_state, |
df11cd4e |
862 | interpret_move, |
863 | execute_move, |
1f3ee4ee |
864 | PREFERRED_TILE_SIZE, game_compute_size, game_set_size, |
be8d5aa1 |
865 | game_colours, |
866 | game_new_drawstate, |
867 | game_free_drawstate, |
868 | game_redraw, |
869 | game_anim_length, |
870 | game_flash_length, |
dafd6cf6 |
871 | FALSE, FALSE, game_print_size, game_print, |
ac9f41c4 |
872 | TRUE, /* wants_statusbar */ |
48dcdd62 |
873 | FALSE, game_timing_state, |
2705d374 |
874 | 0, /* flags */ |
be8d5aa1 |
875 | }; |