4efb3868 |
1 | /* |
2 | * sixteen.c: `16-puzzle', a sliding-tiles jigsaw which differs |
3 | * from the 15-puzzle in that you toroidally rotate a row or column |
4 | * at a time. |
5 | */ |
6 | |
7 | #include <stdio.h> |
8 | #include <stdlib.h> |
9 | #include <string.h> |
10 | #include <assert.h> |
b0e26073 |
11 | #include <ctype.h> |
4efb3868 |
12 | #include <math.h> |
13 | |
14 | #include "puzzles.h" |
15 | |
1e3e152d |
16 | #define PREFERRED_TILE_SIZE 48 |
17 | #define TILE_SIZE (ds->tilesize) |
18 | #define BORDER TILE_SIZE |
4efb3868 |
19 | #define HIGHLIGHT_WIDTH (TILE_SIZE / 20) |
20 | #define COORD(x) ( (x) * TILE_SIZE + BORDER ) |
21 | #define FROMCOORD(x) ( ((x) - BORDER + 2*TILE_SIZE) / TILE_SIZE - 2 ) |
22 | |
8c1fd974 |
23 | #define ANIM_TIME 0.13F |
24 | #define FLASH_FRAME 0.13F |
4efb3868 |
25 | |
26 | #define X(state, i) ( (i) % (state)->w ) |
27 | #define Y(state, i) ( (i) / (state)->w ) |
28 | #define C(state, x, y) ( (y) * (state)->w + (x) ) |
29 | |
30 | enum { |
31 | COL_BACKGROUND, |
32 | COL_TEXT, |
33 | COL_HIGHLIGHT, |
34 | COL_LOWLIGHT, |
35 | NCOLOURS |
36 | }; |
37 | |
38 | struct game_params { |
39 | int w, h; |
81875211 |
40 | int movetarget; |
4efb3868 |
41 | }; |
42 | |
43 | struct game_state { |
44 | int w, h, n; |
45 | int *tiles; |
46 | int completed; |
2ac6d24e |
47 | int just_used_solve; /* used to suppress undo animation */ |
48 | int used_solve; /* used to suppress completion flash */ |
81875211 |
49 | int movecount, movetarget; |
c8230524 |
50 | int last_movement_sense; |
4efb3868 |
51 | }; |
52 | |
be8d5aa1 |
53 | static game_params *default_params(void) |
4efb3868 |
54 | { |
55 | game_params *ret = snew(game_params); |
56 | |
57 | ret->w = ret->h = 4; |
81875211 |
58 | ret->movetarget = 0; |
4efb3868 |
59 | |
60 | return ret; |
61 | } |
62 | |
be8d5aa1 |
63 | static int game_fetch_preset(int i, char **name, game_params **params) |
4efb3868 |
64 | { |
65 | game_params *ret; |
66 | int w, h; |
67 | char buf[80]; |
68 | |
69 | switch (i) { |
70 | case 0: w = 3, h = 3; break; |
71 | case 1: w = 4, h = 3; break; |
72 | case 2: w = 4, h = 4; break; |
73 | case 3: w = 5, h = 4; break; |
74 | case 4: w = 5, h = 5; break; |
75 | default: return FALSE; |
76 | } |
77 | |
78 | sprintf(buf, "%dx%d", w, h); |
79 | *name = dupstr(buf); |
80 | *params = ret = snew(game_params); |
81 | ret->w = w; |
82 | ret->h = h; |
81875211 |
83 | ret->movetarget = 0; |
4efb3868 |
84 | return TRUE; |
85 | } |
86 | |
be8d5aa1 |
87 | static void free_params(game_params *params) |
4efb3868 |
88 | { |
89 | sfree(params); |
90 | } |
91 | |
be8d5aa1 |
92 | static game_params *dup_params(game_params *params) |
4efb3868 |
93 | { |
94 | game_params *ret = snew(game_params); |
95 | *ret = *params; /* structure copy */ |
96 | return ret; |
97 | } |
98 | |
1185e3c5 |
99 | static void decode_params(game_params *ret, char const *string) |
b0e26073 |
100 | { |
b0e26073 |
101 | ret->w = ret->h = atoi(string); |
1185e3c5 |
102 | ret->movetarget = 0; |
b0e26073 |
103 | while (*string && isdigit(*string)) string++; |
104 | if (*string == 'x') { |
105 | string++; |
106 | ret->h = atoi(string); |
81875211 |
107 | while (*string && isdigit((unsigned char)*string)) |
108 | string++; |
109 | } |
110 | if (*string == 'm') { |
111 | string++; |
112 | ret->movetarget = atoi(string); |
113 | while (*string && isdigit((unsigned char)*string)) |
114 | string++; |
b0e26073 |
115 | } |
b0e26073 |
116 | } |
117 | |
1185e3c5 |
118 | static char *encode_params(game_params *params, int full) |
b0e26073 |
119 | { |
120 | char data[256]; |
121 | |
122 | sprintf(data, "%dx%d", params->w, params->h); |
1185e3c5 |
123 | /* Shuffle limit is part of the limited parameters, because we have to |
124 | * supply the target move count. */ |
125 | if (params->movetarget) |
126 | sprintf(data + strlen(data), "m%d", params->movetarget); |
b0e26073 |
127 | |
128 | return dupstr(data); |
129 | } |
130 | |
be8d5aa1 |
131 | static config_item *game_configure(game_params *params) |
c8230524 |
132 | { |
133 | config_item *ret; |
134 | char buf[80]; |
135 | |
81875211 |
136 | ret = snewn(4, config_item); |
c8230524 |
137 | |
138 | ret[0].name = "Width"; |
95709966 |
139 | ret[0].type = C_STRING; |
c8230524 |
140 | sprintf(buf, "%d", params->w); |
141 | ret[0].sval = dupstr(buf); |
142 | ret[0].ival = 0; |
143 | |
144 | ret[1].name = "Height"; |
95709966 |
145 | ret[1].type = C_STRING; |
c8230524 |
146 | sprintf(buf, "%d", params->h); |
147 | ret[1].sval = dupstr(buf); |
148 | ret[1].ival = 0; |
149 | |
81875211 |
150 | ret[2].name = "Number of shuffling moves"; |
151 | ret[2].type = C_STRING; |
152 | sprintf(buf, "%d", params->movetarget); |
153 | ret[2].sval = dupstr(buf); |
c8230524 |
154 | ret[2].ival = 0; |
155 | |
81875211 |
156 | ret[3].name = NULL; |
157 | ret[3].type = C_END; |
158 | ret[3].sval = NULL; |
159 | ret[3].ival = 0; |
160 | |
c8230524 |
161 | return ret; |
162 | } |
163 | |
be8d5aa1 |
164 | static game_params *custom_params(config_item *cfg) |
c8230524 |
165 | { |
166 | game_params *ret = snew(game_params); |
167 | |
168 | ret->w = atoi(cfg[0].sval); |
169 | ret->h = atoi(cfg[1].sval); |
81875211 |
170 | ret->movetarget = atoi(cfg[2].sval); |
c8230524 |
171 | |
172 | return ret; |
173 | } |
174 | |
be8d5aa1 |
175 | static char *validate_params(game_params *params) |
c8230524 |
176 | { |
ab53eb64 |
177 | if (params->w < 2 || params->h < 2) |
c8230524 |
178 | return "Width and height must both be at least two"; |
179 | |
180 | return NULL; |
181 | } |
182 | |
be8d5aa1 |
183 | static int perm_parity(int *perm, int n) |
4efb3868 |
184 | { |
185 | int i, j, ret; |
186 | |
187 | ret = 0; |
188 | |
189 | for (i = 0; i < n-1; i++) |
190 | for (j = i+1; j < n; j++) |
191 | if (perm[i] > perm[j]) |
192 | ret = !ret; |
193 | |
194 | return ret; |
195 | } |
196 | |
1185e3c5 |
197 | static char *new_game_desc(game_params *params, random_state *rs, |
c566778e |
198 | char **aux, int interactive) |
4efb3868 |
199 | { |
200 | int stop, n, i, x; |
201 | int x1, x2, p1, p2; |
202 | int *tiles, *used; |
203 | char *ret; |
204 | int retlen; |
205 | |
206 | n = params->w * params->h; |
207 | |
208 | tiles = snewn(n, int); |
4efb3868 |
209 | |
81875211 |
210 | if (params->movetarget) { |
060ba134 |
211 | int prevoffset = -1; |
212 | int max = (params->w > params->h ? params->w : params->h); |
213 | int *prevmoves = snewn(max, int); |
4efb3868 |
214 | |
81875211 |
215 | /* |
216 | * Shuffle the old-fashioned way, by making a series of |
217 | * single moves on the grid. |
218 | */ |
4efb3868 |
219 | |
81875211 |
220 | for (i = 0; i < n; i++) |
221 | tiles[i] = i; |
222 | |
223 | for (i = 0; i < params->movetarget; i++) { |
060ba134 |
224 | int start, offset, len, direction, index; |
81875211 |
225 | int j, tmp; |
226 | |
227 | /* |
228 | * Choose a move to make. We can choose from any row |
229 | * or any column. |
230 | */ |
231 | while (1) { |
232 | j = random_upto(rs, params->w + params->h); |
233 | |
234 | if (j < params->w) { |
235 | /* Column. */ |
060ba134 |
236 | index = j; |
81875211 |
237 | start = j; |
238 | offset = params->w; |
239 | len = params->h; |
240 | } else { |
241 | /* Row. */ |
060ba134 |
242 | index = j - params->w; |
243 | start = index * params->w; |
81875211 |
244 | offset = 1; |
245 | len = params->w; |
246 | } |
4efb3868 |
247 | |
81875211 |
248 | direction = -1 + 2 * random_upto(rs, 2); |
4efb3868 |
249 | |
81875211 |
250 | /* |
060ba134 |
251 | * To at least _try_ to avoid boring cases, check |
252 | * that this move doesn't directly undo a previous |
253 | * one, or repeat it so many times as to turn it |
254 | * into fewer moves in the opposite direction. (For |
255 | * example, in a row of length 4, we're allowed to |
256 | * move it the same way twice, but not three |
257 | * times.) |
258 | * |
259 | * We track this for each individual row/column, |
260 | * and clear all the counters as soon as a |
261 | * perpendicular move is made. This isn't perfect |
262 | * (it _can't_ guaranteeably be perfect - there |
263 | * will always come a move count beyond which a |
264 | * shorter solution will be possible than the one |
265 | * which constructed the position) but it should |
266 | * sort out all the obvious cases. |
81875211 |
267 | */ |
060ba134 |
268 | if (offset == prevoffset) { |
269 | tmp = prevmoves[index] + direction; |
270 | if (abs(2*tmp) > len || abs(tmp) < abs(prevmoves[index])) |
271 | continue; |
272 | } |
4efb3868 |
273 | |
81875211 |
274 | /* If we didn't `continue', we've found an OK move to make. */ |
060ba134 |
275 | if (offset != prevoffset) { |
276 | int i; |
277 | for (i = 0; i < max; i++) |
278 | prevmoves[i] = 0; |
279 | prevoffset = offset; |
280 | } |
281 | prevmoves[index] += direction; |
81875211 |
282 | break; |
283 | } |
4efb3868 |
284 | |
81875211 |
285 | /* |
060ba134 |
286 | * Make the move. |
81875211 |
287 | */ |
288 | if (direction < 0) { |
289 | start += (len-1) * offset; |
290 | offset = -offset; |
291 | } |
292 | tmp = tiles[start]; |
293 | for (j = 0; j+1 < len; j++) |
294 | tiles[start + j*offset] = tiles[start + (j+1)*offset]; |
295 | tiles[start + (len-1) * offset] = tmp; |
296 | } |
297 | |
060ba134 |
298 | sfree(prevmoves); |
299 | |
81875211 |
300 | } else { |
301 | |
302 | used = snewn(n, int); |
303 | |
304 | for (i = 0; i < n; i++) { |
305 | tiles[i] = -1; |
306 | used[i] = FALSE; |
307 | } |
308 | |
309 | /* |
310 | * If both dimensions are odd, there is a parity |
311 | * constraint. |
312 | */ |
313 | if (params->w & params->h & 1) |
314 | stop = 2; |
315 | else |
316 | stop = 0; |
317 | |
318 | /* |
319 | * Place everything except (possibly) the last two tiles. |
320 | */ |
321 | for (x = 0, i = n; i > stop; i--) { |
322 | int k = i > 1 ? random_upto(rs, i) : 0; |
323 | int j; |
324 | |
325 | for (j = 0; j < n; j++) |
326 | if (!used[j] && (k-- == 0)) |
327 | break; |
328 | |
329 | assert(j < n && !used[j]); |
330 | used[j] = TRUE; |
331 | |
332 | while (tiles[x] >= 0) |
333 | x++; |
334 | assert(x < n); |
335 | tiles[x] = j; |
336 | } |
337 | |
338 | if (stop) { |
339 | /* |
340 | * Find the last two locations, and the last two |
341 | * pieces. |
342 | */ |
343 | while (tiles[x] >= 0) |
344 | x++; |
345 | assert(x < n); |
346 | x1 = x; |
347 | x++; |
348 | while (tiles[x] >= 0) |
349 | x++; |
350 | assert(x < n); |
351 | x2 = x; |
352 | |
353 | for (i = 0; i < n; i++) |
354 | if (!used[i]) |
355 | break; |
356 | p1 = i; |
357 | for (i = p1+1; i < n; i++) |
358 | if (!used[i]) |
359 | break; |
360 | p2 = i; |
361 | |
362 | /* |
363 | * Try the last two tiles one way round. If that fails, |
364 | * swap them. |
365 | */ |
366 | tiles[x1] = p1; |
367 | tiles[x2] = p2; |
368 | if (perm_parity(tiles, n) != 0) { |
369 | tiles[x1] = p2; |
370 | tiles[x2] = p1; |
371 | assert(perm_parity(tiles, n) == 0); |
372 | } |
373 | } |
374 | |
375 | sfree(used); |
4efb3868 |
376 | } |
377 | |
378 | /* |
1185e3c5 |
379 | * Now construct the game description, by describing the tile |
380 | * array as a simple sequence of comma-separated integers. |
4efb3868 |
381 | */ |
382 | ret = NULL; |
383 | retlen = 0; |
384 | for (i = 0; i < n; i++) { |
385 | char buf[80]; |
386 | int k; |
387 | |
388 | k = sprintf(buf, "%d,", tiles[i]+1); |
389 | |
390 | ret = sresize(ret, retlen + k + 1, char); |
391 | strcpy(ret + retlen, buf); |
392 | retlen += k; |
393 | } |
394 | ret[retlen-1] = '\0'; /* delete last comma */ |
395 | |
396 | sfree(tiles); |
4efb3868 |
397 | |
398 | return ret; |
399 | } |
400 | |
5928817c |
401 | |
1185e3c5 |
402 | static char *validate_desc(game_params *params, char *desc) |
5928817c |
403 | { |
404 | char *p, *err; |
405 | int i, area; |
406 | int *used; |
407 | |
408 | area = params->w * params->h; |
1185e3c5 |
409 | p = desc; |
5928817c |
410 | err = NULL; |
411 | |
412 | used = snewn(area, int); |
413 | for (i = 0; i < area; i++) |
414 | used[i] = FALSE; |
415 | |
416 | for (i = 0; i < area; i++) { |
417 | char *q = p; |
418 | int n; |
419 | |
420 | if (*p < '0' || *p > '9') { |
421 | err = "Not enough numbers in string"; |
422 | goto leave; |
423 | } |
424 | while (*p >= '0' && *p <= '9') |
425 | p++; |
426 | if (i < area-1 && *p != ',') { |
427 | err = "Expected comma after number"; |
428 | goto leave; |
429 | } |
430 | else if (i == area-1 && *p) { |
431 | err = "Excess junk at end of string"; |
432 | goto leave; |
433 | } |
434 | n = atoi(q); |
435 | if (n < 1 || n > area) { |
436 | err = "Number out of range"; |
437 | goto leave; |
438 | } |
439 | if (used[n-1]) { |
440 | err = "Number used twice"; |
441 | goto leave; |
442 | } |
443 | used[n-1] = TRUE; |
444 | |
445 | if (*p) p++; /* eat comma */ |
446 | } |
447 | |
448 | leave: |
449 | sfree(used); |
450 | return err; |
451 | } |
452 | |
c380832d |
453 | static game_state *new_game(midend_data *me, game_params *params, char *desc) |
4efb3868 |
454 | { |
455 | game_state *state = snew(game_state); |
456 | int i; |
457 | char *p; |
458 | |
459 | state->w = params->w; |
460 | state->h = params->h; |
461 | state->n = params->w * params->h; |
462 | state->tiles = snewn(state->n, int); |
463 | |
1185e3c5 |
464 | p = desc; |
4efb3868 |
465 | i = 0; |
466 | for (i = 0; i < state->n; i++) { |
467 | assert(*p); |
468 | state->tiles[i] = atoi(p); |
469 | while (*p && *p != ',') |
470 | p++; |
471 | if (*p) p++; /* eat comma */ |
472 | } |
473 | assert(!*p); |
474 | |
fd1a1a2b |
475 | state->completed = state->movecount = 0; |
81875211 |
476 | state->movetarget = params->movetarget; |
2ac6d24e |
477 | state->used_solve = state->just_used_solve = FALSE; |
c8230524 |
478 | state->last_movement_sense = 0; |
4efb3868 |
479 | |
480 | return state; |
481 | } |
482 | |
be8d5aa1 |
483 | static game_state *dup_game(game_state *state) |
4efb3868 |
484 | { |
485 | game_state *ret = snew(game_state); |
486 | |
487 | ret->w = state->w; |
488 | ret->h = state->h; |
489 | ret->n = state->n; |
490 | ret->tiles = snewn(state->w * state->h, int); |
491 | memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int)); |
492 | ret->completed = state->completed; |
fd1a1a2b |
493 | ret->movecount = state->movecount; |
81875211 |
494 | ret->movetarget = state->movetarget; |
2ac6d24e |
495 | ret->used_solve = state->used_solve; |
496 | ret->just_used_solve = state->just_used_solve; |
c8230524 |
497 | ret->last_movement_sense = state->last_movement_sense; |
4efb3868 |
498 | |
499 | return ret; |
500 | } |
501 | |
be8d5aa1 |
502 | static void free_game(game_state *state) |
4efb3868 |
503 | { |
ab53eb64 |
504 | sfree(state->tiles); |
4efb3868 |
505 | sfree(state); |
506 | } |
507 | |
df11cd4e |
508 | static char *solve_game(game_state *state, game_state *currstate, |
c566778e |
509 | char *aux, char **error) |
2ac6d24e |
510 | { |
df11cd4e |
511 | return dupstr("S"); |
2ac6d24e |
512 | } |
513 | |
9b4b03d3 |
514 | static char *game_text_format(game_state *state) |
515 | { |
af52394e |
516 | char *ret, *p, buf[80]; |
517 | int x, y, col, maxlen; |
518 | |
519 | /* |
520 | * First work out how many characters we need to display each |
521 | * number. |
522 | */ |
523 | col = sprintf(buf, "%d", state->n); |
524 | |
525 | /* |
526 | * Now we know the exact total size of the grid we're going to |
527 | * produce: it's got h rows, each containing w lots of col, w-1 |
528 | * spaces and a trailing newline. |
529 | */ |
530 | maxlen = state->h * state->w * (col+1); |
531 | |
48a10826 |
532 | ret = snewn(maxlen+1, char); |
af52394e |
533 | p = ret; |
534 | |
535 | for (y = 0; y < state->h; y++) { |
536 | for (x = 0; x < state->w; x++) { |
537 | int v = state->tiles[state->w*y+x]; |
538 | sprintf(buf, "%*d", col, v); |
539 | memcpy(p, buf, col); |
540 | p += col; |
541 | if (x+1 == state->w) |
542 | *p++ = '\n'; |
543 | else |
544 | *p++ = ' '; |
545 | } |
546 | } |
547 | |
548 | assert(p - ret == maxlen); |
549 | *p = '\0'; |
550 | return ret; |
9b4b03d3 |
551 | } |
552 | |
be8d5aa1 |
553 | static game_ui *new_ui(game_state *state) |
74a4e547 |
554 | { |
555 | return NULL; |
556 | } |
557 | |
be8d5aa1 |
558 | static void free_ui(game_ui *ui) |
74a4e547 |
559 | { |
560 | } |
561 | |
844f605f |
562 | static char *encode_ui(game_ui *ui) |
ae8290c6 |
563 | { |
564 | return NULL; |
565 | } |
566 | |
844f605f |
567 | static void decode_ui(game_ui *ui, char *encoding) |
ae8290c6 |
568 | { |
569 | } |
570 | |
07dfb697 |
571 | static void game_changed_state(game_ui *ui, game_state *oldstate, |
572 | game_state *newstate) |
573 | { |
574 | } |
575 | |
1e3e152d |
576 | struct game_drawstate { |
577 | int started; |
578 | int w, h, bgcolour; |
579 | int *tiles; |
580 | int tilesize; |
581 | }; |
582 | |
df11cd4e |
583 | static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, |
584 | int x, int y, int button) |
585 | { |
586 | int cx, cy, dx, dy; |
587 | char buf[80]; |
4efb3868 |
588 | |
f0ee053c |
589 | button &= ~MOD_MASK; |
e91825f8 |
590 | if (button != LEFT_BUTTON && button != RIGHT_BUTTON) |
4efb3868 |
591 | return NULL; |
592 | |
593 | cx = FROMCOORD(x); |
594 | cy = FROMCOORD(y); |
df11cd4e |
595 | if (cx == -1 && cy >= 0 && cy < state->h) |
596 | dx = -1, dy = 0; |
597 | else if (cx == state->w && cy >= 0 && cy < state->h) |
598 | dx = +1, dy = 0; |
599 | else if (cy == -1 && cx >= 0 && cx < state->w) |
600 | dy = -1, dx = 0; |
601 | else if (cy == state->h && cx >= 0 && cx < state->w) |
602 | dy = +1, dx = 0; |
4efb3868 |
603 | else |
604 | return NULL; /* invalid click location */ |
605 | |
e91825f8 |
606 | /* reverse direction if right hand button is pressed */ |
df11cd4e |
607 | if (button == RIGHT_BUTTON) { |
608 | dx = -dx; |
609 | dy = -dy; |
e91825f8 |
610 | } |
611 | |
df11cd4e |
612 | if (dx) |
613 | sprintf(buf, "R%d,%d", cy, dx); |
614 | else |
615 | sprintf(buf, "C%d,%d", cx, dy); |
616 | return dupstr(buf); |
617 | } |
618 | |
619 | static game_state *execute_move(game_state *from, char *move) |
620 | { |
621 | int cx, cy, dx, dy; |
622 | int tx, ty, n; |
623 | game_state *ret; |
624 | |
625 | if (!strcmp(move, "S")) { |
626 | int i; |
627 | |
628 | ret = dup_game(from); |
629 | |
630 | /* |
631 | * Simply replace the grid with a solved one. For this game, |
632 | * this isn't a useful operation for actually telling the user |
633 | * what they should have done, but it is useful for |
634 | * conveniently being able to get hold of a clean state from |
635 | * which to practise manoeuvres. |
636 | */ |
637 | for (i = 0; i < ret->n; i++) |
638 | ret->tiles[i] = i+1; |
639 | ret->used_solve = ret->just_used_solve = TRUE; |
640 | ret->completed = ret->movecount = 1; |
641 | |
642 | return ret; |
643 | } |
644 | |
645 | if (move[0] == 'R' && sscanf(move+1, "%d,%d", &cy, &dx) == 2 && |
646 | cy >= 0 && cy < from->h) { |
647 | cx = dy = 0; |
648 | n = from->w; |
649 | } else if (move[0] == 'C' && sscanf(move+1, "%d,%d", &cx, &dy) == 2 && |
650 | cx >= 0 && cx < from->w) { |
651 | cy = dx = 0; |
652 | n = from->h; |
653 | } else |
654 | return NULL; |
655 | |
4efb3868 |
656 | ret = dup_game(from); |
2ac6d24e |
657 | ret->just_used_solve = FALSE; /* zero this in a hurry */ |
4efb3868 |
658 | |
659 | do { |
df11cd4e |
660 | tx = (cx - dx + from->w) % from->w; |
661 | ty = (cy - dy + from->h) % from->h; |
4efb3868 |
662 | ret->tiles[C(ret, cx, cy)] = from->tiles[C(from, tx, ty)]; |
df11cd4e |
663 | cx = tx; |
664 | cy = ty; |
4efb3868 |
665 | } while (--n > 0); |
666 | |
fd1a1a2b |
667 | ret->movecount++; |
668 | |
df11cd4e |
669 | ret->last_movement_sense = dx+dy; |
c8230524 |
670 | |
4efb3868 |
671 | /* |
672 | * See if the game has been completed. |
673 | */ |
674 | if (!ret->completed) { |
fd1a1a2b |
675 | ret->completed = ret->movecount; |
4efb3868 |
676 | for (n = 0; n < ret->n; n++) |
677 | if (ret->tiles[n] != n+1) |
678 | ret->completed = FALSE; |
679 | } |
680 | |
681 | return ret; |
682 | } |
683 | |
684 | /* ---------------------------------------------------------------------- |
685 | * Drawing routines. |
686 | */ |
687 | |
1e3e152d |
688 | static void game_size(game_params *params, game_drawstate *ds, |
689 | int *x, int *y, int expand) |
4efb3868 |
690 | { |
1e3e152d |
691 | int tsx, tsy, ts; |
692 | /* |
693 | * Each window dimension equals the tile size times two more |
694 | * than the grid dimension (the border is the same size as the |
695 | * tiles). |
696 | */ |
697 | tsx = *x / (params->w + 2); |
698 | tsy = *y / (params->h + 2); |
699 | ts = min(tsx, tsy); |
700 | |
701 | if (expand) |
702 | ds->tilesize = ts; |
703 | else |
704 | ds->tilesize = min(ts, PREFERRED_TILE_SIZE); |
705 | |
4efb3868 |
706 | *x = TILE_SIZE * params->w + 2 * BORDER; |
707 | *y = TILE_SIZE * params->h + 2 * BORDER; |
708 | } |
709 | |
be8d5aa1 |
710 | static float *game_colours(frontend *fe, game_state *state, int *ncolours) |
4efb3868 |
711 | { |
712 | float *ret = snewn(3 * NCOLOURS, float); |
713 | int i; |
714 | float max; |
715 | |
716 | frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); |
717 | |
718 | /* |
719 | * Drop the background colour so that the highlight is |
720 | * noticeably brighter than it while still being under 1. |
721 | */ |
722 | max = ret[COL_BACKGROUND*3]; |
723 | for (i = 1; i < 3; i++) |
724 | if (ret[COL_BACKGROUND*3+i] > max) |
725 | max = ret[COL_BACKGROUND*3+i]; |
726 | if (max * 1.2F > 1.0F) { |
727 | for (i = 0; i < 3; i++) |
728 | ret[COL_BACKGROUND*3+i] /= (max * 1.2F); |
729 | } |
730 | |
731 | for (i = 0; i < 3; i++) { |
732 | ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F; |
733 | ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F; |
734 | ret[COL_TEXT * 3 + i] = 0.0; |
735 | } |
736 | |
737 | *ncolours = NCOLOURS; |
738 | return ret; |
739 | } |
740 | |
be8d5aa1 |
741 | static game_drawstate *game_new_drawstate(game_state *state) |
4efb3868 |
742 | { |
743 | struct game_drawstate *ds = snew(struct game_drawstate); |
744 | int i; |
745 | |
746 | ds->started = FALSE; |
747 | ds->w = state->w; |
748 | ds->h = state->h; |
749 | ds->bgcolour = COL_BACKGROUND; |
750 | ds->tiles = snewn(ds->w*ds->h, int); |
1e3e152d |
751 | ds->tilesize = 0; /* haven't decided yet */ |
4efb3868 |
752 | for (i = 0; i < ds->w*ds->h; i++) |
753 | ds->tiles[i] = -1; |
754 | |
755 | return ds; |
756 | } |
757 | |
be8d5aa1 |
758 | static void game_free_drawstate(game_drawstate *ds) |
4efb3868 |
759 | { |
760 | sfree(ds->tiles); |
761 | sfree(ds); |
762 | } |
763 | |
1e3e152d |
764 | static void draw_tile(frontend *fe, game_drawstate *ds, |
765 | game_state *state, int x, int y, |
4efb3868 |
766 | int tile, int flash_colour) |
767 | { |
768 | if (tile == 0) { |
769 | draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE, |
770 | flash_colour); |
771 | } else { |
772 | int coords[6]; |
773 | char str[40]; |
774 | |
775 | coords[0] = x + TILE_SIZE - 1; |
776 | coords[1] = y + TILE_SIZE - 1; |
777 | coords[2] = x + TILE_SIZE - 1; |
778 | coords[3] = y; |
779 | coords[4] = x; |
780 | coords[5] = y + TILE_SIZE - 1; |
28b5987d |
781 | draw_polygon(fe, coords, 3, COL_LOWLIGHT, COL_LOWLIGHT); |
4efb3868 |
782 | |
783 | coords[0] = x; |
784 | coords[1] = y; |
28b5987d |
785 | draw_polygon(fe, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT); |
4efb3868 |
786 | |
787 | draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH, |
788 | TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH, |
789 | flash_colour); |
790 | |
791 | sprintf(str, "%d", tile); |
792 | draw_text(fe, x + TILE_SIZE/2, y + TILE_SIZE/2, |
793 | FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE, |
794 | COL_TEXT, str); |
795 | } |
796 | draw_update(fe, x, y, TILE_SIZE, TILE_SIZE); |
797 | } |
798 | |
1e3e152d |
799 | static void draw_arrow(frontend *fe, game_drawstate *ds, |
800 | int x, int y, int xdx, int xdy) |
4efb3868 |
801 | { |
802 | int coords[14]; |
803 | int ydy = -xdx, ydx = xdy; |
804 | |
805 | #define POINT(n, xx, yy) ( \ |
806 | coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \ |
807 | coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy) |
808 | |
809 | POINT(0, TILE_SIZE / 2, 3 * TILE_SIZE / 4); /* top of arrow */ |
810 | POINT(1, 3 * TILE_SIZE / 4, TILE_SIZE / 2); /* right corner */ |
811 | POINT(2, 5 * TILE_SIZE / 8, TILE_SIZE / 2); /* right concave */ |
812 | POINT(3, 5 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom right */ |
813 | POINT(4, 3 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom left */ |
814 | POINT(5, 3 * TILE_SIZE / 8, TILE_SIZE / 2); /* left concave */ |
815 | POINT(6, TILE_SIZE / 4, TILE_SIZE / 2); /* left corner */ |
816 | |
28b5987d |
817 | draw_polygon(fe, coords, 7, COL_LOWLIGHT, COL_TEXT); |
4efb3868 |
818 | } |
819 | |
be8d5aa1 |
820 | static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, |
c822de4a |
821 | game_state *state, int dir, game_ui *ui, |
74a4e547 |
822 | float animtime, float flashtime) |
4efb3868 |
823 | { |
b443c381 |
824 | int i, bgcolour; |
4efb3868 |
825 | |
826 | if (flashtime > 0) { |
827 | int frame = (int)(flashtime / FLASH_FRAME); |
828 | bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT); |
829 | } else |
830 | bgcolour = COL_BACKGROUND; |
831 | |
832 | if (!ds->started) { |
19f24306 |
833 | int coords[10]; |
4efb3868 |
834 | |
835 | draw_rect(fe, 0, 0, |
836 | TILE_SIZE * state->w + 2 * BORDER, |
837 | TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND); |
838 | draw_update(fe, 0, 0, |
839 | TILE_SIZE * state->w + 2 * BORDER, |
840 | TILE_SIZE * state->h + 2 * BORDER); |
841 | |
842 | /* |
843 | * Recessed area containing the whole puzzle. |
844 | */ |
845 | coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1; |
846 | coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1; |
847 | coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1; |
848 | coords[3] = COORD(0) - HIGHLIGHT_WIDTH; |
19f24306 |
849 | coords[4] = coords[2] - TILE_SIZE; |
850 | coords[5] = coords[3] + TILE_SIZE; |
851 | coords[8] = COORD(0) - HIGHLIGHT_WIDTH; |
852 | coords[9] = COORD(state->h) + HIGHLIGHT_WIDTH - 1; |
853 | coords[6] = coords[8] + TILE_SIZE; |
854 | coords[7] = coords[9] - TILE_SIZE; |
28b5987d |
855 | draw_polygon(fe, coords, 5, COL_HIGHLIGHT, COL_HIGHLIGHT); |
4efb3868 |
856 | |
857 | coords[1] = COORD(0) - HIGHLIGHT_WIDTH; |
858 | coords[0] = COORD(0) - HIGHLIGHT_WIDTH; |
28b5987d |
859 | draw_polygon(fe, coords, 5, COL_LOWLIGHT, COL_LOWLIGHT); |
4efb3868 |
860 | |
861 | /* |
862 | * Arrows for making moves. |
863 | */ |
864 | for (i = 0; i < state->w; i++) { |
1e3e152d |
865 | draw_arrow(fe, ds, COORD(i), COORD(0), +1, 0); |
866 | draw_arrow(fe, ds, COORD(i+1), COORD(state->h), -1, 0); |
4efb3868 |
867 | } |
868 | for (i = 0; i < state->h; i++) { |
1e3e152d |
869 | draw_arrow(fe, ds, COORD(state->w), COORD(i), 0, +1); |
870 | draw_arrow(fe, ds, COORD(0), COORD(i+1), 0, -1); |
4efb3868 |
871 | } |
872 | |
873 | ds->started = TRUE; |
874 | } |
875 | |
876 | /* |
b443c381 |
877 | * Now draw each tile. |
4efb3868 |
878 | */ |
879 | |
880 | clip(fe, COORD(0), COORD(0), TILE_SIZE*state->w, TILE_SIZE*state->h); |
881 | |
b443c381 |
882 | for (i = 0; i < state->n; i++) { |
883 | int t, t0; |
884 | /* |
885 | * Figure out what should be displayed at this |
886 | * location. It's either a simple tile, or it's a |
887 | * transition between two tiles (in which case we say |
888 | * -1 because it must always be drawn). |
889 | */ |
890 | |
891 | if (oldstate && oldstate->tiles[i] != state->tiles[i]) |
892 | t = -1; |
893 | else |
894 | t = state->tiles[i]; |
895 | |
896 | t0 = t; |
897 | |
898 | if (ds->bgcolour != bgcolour || /* always redraw when flashing */ |
899 | ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) { |
900 | int x, y, x2, y2; |
901 | |
902 | /* |
903 | * Figure out what to _actually_ draw, and where to |
904 | * draw it. |
905 | */ |
906 | if (t == -1) { |
907 | int x0, y0, x1, y1, dx, dy; |
908 | int j; |
909 | float c; |
910 | int sense; |
911 | |
5b5c6b12 |
912 | if (dir < 0) { |
913 | assert(oldstate); |
b443c381 |
914 | sense = -oldstate->last_movement_sense; |
5b5c6b12 |
915 | } else { |
b443c381 |
916 | sense = state->last_movement_sense; |
5b5c6b12 |
917 | } |
b443c381 |
918 | |
919 | t = state->tiles[i]; |
920 | |
921 | /* |
922 | * FIXME: must be prepared to draw a double |
923 | * tile in some situations. |
924 | */ |
925 | |
926 | /* |
927 | * Find the coordinates of this tile in the old and |
928 | * new states. |
929 | */ |
930 | x1 = COORD(X(state, i)); |
931 | y1 = COORD(Y(state, i)); |
932 | for (j = 0; j < oldstate->n; j++) |
933 | if (oldstate->tiles[j] == state->tiles[i]) |
934 | break; |
935 | assert(j < oldstate->n); |
936 | x0 = COORD(X(state, j)); |
937 | y0 = COORD(Y(state, j)); |
938 | |
939 | dx = (x1 - x0); |
940 | if (dx != 0 && |
941 | dx != TILE_SIZE * sense) { |
942 | dx = (dx < 0 ? dx + TILE_SIZE * state->w : |
943 | dx - TILE_SIZE * state->w); |
944 | assert(abs(dx) == TILE_SIZE); |
945 | } |
946 | dy = (y1 - y0); |
947 | if (dy != 0 && |
948 | dy != TILE_SIZE * sense) { |
949 | dy = (dy < 0 ? dy + TILE_SIZE * state->h : |
950 | dy - TILE_SIZE * state->h); |
951 | assert(abs(dy) == TILE_SIZE); |
952 | } |
953 | |
954 | c = (animtime / ANIM_TIME); |
955 | if (c < 0.0F) c = 0.0F; |
956 | if (c > 1.0F) c = 1.0F; |
957 | |
958 | x = x0 + (int)(c * dx); |
959 | y = y0 + (int)(c * dy); |
960 | x2 = x1 - dx + (int)(c * dx); |
961 | y2 = y1 - dy + (int)(c * dy); |
962 | } else { |
963 | x = COORD(X(state, i)); |
964 | y = COORD(Y(state, i)); |
965 | x2 = y2 = -1; |
966 | } |
967 | |
1e3e152d |
968 | draw_tile(fe, ds, state, x, y, t, bgcolour); |
b443c381 |
969 | if (x2 != -1 || y2 != -1) |
1e3e152d |
970 | draw_tile(fe, ds, state, x2, y2, t, bgcolour); |
b443c381 |
971 | } |
972 | ds->tiles[i] = t0; |
4efb3868 |
973 | } |
974 | |
975 | unclip(fe); |
976 | |
977 | ds->bgcolour = bgcolour; |
fd1a1a2b |
978 | |
979 | /* |
980 | * Update the status bar. |
981 | */ |
982 | { |
983 | char statusbuf[256]; |
984 | |
d108c342 |
985 | /* |
986 | * Don't show the new status until we're also showing the |
987 | * new _state_ - after the game animation is complete. |
988 | */ |
989 | if (oldstate) |
990 | state = oldstate; |
991 | |
2ac6d24e |
992 | if (state->used_solve) |
993 | sprintf(statusbuf, "Moves since auto-solve: %d", |
994 | state->movecount - state->completed); |
81875211 |
995 | else { |
2ac6d24e |
996 | sprintf(statusbuf, "%sMoves: %d", |
997 | (state->completed ? "COMPLETED! " : ""), |
998 | (state->completed ? state->completed : state->movecount)); |
81875211 |
999 | if (state->movetarget) |
1000 | sprintf(statusbuf+strlen(statusbuf), " (target %d)", |
1001 | state->movetarget); |
1002 | } |
fd1a1a2b |
1003 | |
1004 | status_bar(fe, statusbuf); |
1005 | } |
4efb3868 |
1006 | } |
1007 | |
be8d5aa1 |
1008 | static float game_anim_length(game_state *oldstate, |
e3f21163 |
1009 | game_state *newstate, int dir, game_ui *ui) |
4efb3868 |
1010 | { |
2ac6d24e |
1011 | if ((dir > 0 && newstate->just_used_solve) || |
1012 | (dir < 0 && oldstate->just_used_solve)) |
1013 | return 0.0F; |
1014 | else |
1015 | return ANIM_TIME; |
4efb3868 |
1016 | } |
1017 | |
be8d5aa1 |
1018 | static float game_flash_length(game_state *oldstate, |
e3f21163 |
1019 | game_state *newstate, int dir, game_ui *ui) |
4efb3868 |
1020 | { |
2ac6d24e |
1021 | if (!oldstate->completed && newstate->completed && |
1022 | !oldstate->used_solve && !newstate->used_solve) |
4efb3868 |
1023 | return 2 * FLASH_FRAME; |
1024 | else |
1025 | return 0.0F; |
1026 | } |
fd1a1a2b |
1027 | |
be8d5aa1 |
1028 | static int game_wants_statusbar(void) |
fd1a1a2b |
1029 | { |
1030 | return TRUE; |
1031 | } |
be8d5aa1 |
1032 | |
48dcdd62 |
1033 | static int game_timing_state(game_state *state) |
1034 | { |
1035 | return TRUE; |
1036 | } |
1037 | |
19ef4855 |
1038 | #ifdef COMBINED |
1039 | #define thegame sixteen |
1040 | #endif |
1041 | |
be8d5aa1 |
1042 | const struct game thegame = { |
1d228b10 |
1043 | "Sixteen", "games.sixteen", |
be8d5aa1 |
1044 | default_params, |
1045 | game_fetch_preset, |
1046 | decode_params, |
1047 | encode_params, |
1048 | free_params, |
1049 | dup_params, |
1d228b10 |
1050 | TRUE, game_configure, custom_params, |
be8d5aa1 |
1051 | validate_params, |
1185e3c5 |
1052 | new_game_desc, |
1185e3c5 |
1053 | validate_desc, |
be8d5aa1 |
1054 | new_game, |
1055 | dup_game, |
1056 | free_game, |
2ac6d24e |
1057 | TRUE, solve_game, |
af52394e |
1058 | TRUE, game_text_format, |
be8d5aa1 |
1059 | new_ui, |
1060 | free_ui, |
ae8290c6 |
1061 | encode_ui, |
1062 | decode_ui, |
07dfb697 |
1063 | game_changed_state, |
df11cd4e |
1064 | interpret_move, |
1065 | execute_move, |
be8d5aa1 |
1066 | game_size, |
1067 | game_colours, |
1068 | game_new_drawstate, |
1069 | game_free_drawstate, |
1070 | game_redraw, |
1071 | game_anim_length, |
1072 | game_flash_length, |
1073 | game_wants_statusbar, |
48dcdd62 |
1074 | FALSE, game_timing_state, |
93b1da3d |
1075 | 0, /* mouse_priorities */ |
be8d5aa1 |
1076 | }; |