chiark / gitweb /
GTK and Windows appear to handle timers very differently:
[sgt-puzzles.git] / sixteen.c
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>
11 #include <math.h>
12
13 #include "puzzles.h"
14
15 const char *const game_name = "Sixteen";
16 const int game_can_configure = TRUE;
17
18 #define TILE_SIZE 48
19 #define BORDER    TILE_SIZE            /* big border to fill with arrows */
20 #define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
21 #define COORD(x)  ( (x) * TILE_SIZE + BORDER )
22 #define FROMCOORD(x)  ( ((x) - BORDER + 2*TILE_SIZE) / TILE_SIZE - 2 )
23
24 #define ANIM_TIME 0.13F
25 #define FLASH_FRAME 0.13F
26
27 #define X(state, i) ( (i) % (state)->w )
28 #define Y(state, i) ( (i) / (state)->w )
29 #define C(state, x, y) ( (y) * (state)->w + (x) )
30
31 enum {
32     COL_BACKGROUND,
33     COL_TEXT,
34     COL_HIGHLIGHT,
35     COL_LOWLIGHT,
36     NCOLOURS
37 };
38
39 struct game_params {
40     int w, h;
41 };
42
43 struct game_state {
44     int w, h, n;
45     int *tiles;
46     int completed;
47     int movecount;
48     int last_movement_sense;
49 };
50
51 game_params *default_params(void)
52 {
53     game_params *ret = snew(game_params);
54
55     ret->w = ret->h = 4;
56
57     return ret;
58 }
59
60 int game_fetch_preset(int i, char **name, game_params **params)
61 {
62     game_params *ret;
63     int w, h;
64     char buf[80];
65
66     switch (i) {
67       case 0: w = 3, h = 3; break;
68       case 1: w = 4, h = 3; break;
69       case 2: w = 4, h = 4; break;
70       case 3: w = 5, h = 4; break;
71       case 4: w = 5, h = 5; break;
72       default: return FALSE;
73     }
74
75     sprintf(buf, "%dx%d", w, h);
76     *name = dupstr(buf);
77     *params = ret = snew(game_params);
78     ret->w = w;
79     ret->h = h;
80     return TRUE;
81 }
82
83 void free_params(game_params *params)
84 {
85     sfree(params);
86 }
87
88 game_params *dup_params(game_params *params)
89 {
90     game_params *ret = snew(game_params);
91     *ret = *params;                    /* structure copy */
92     return ret;
93 }
94
95 config_item *game_configure(game_params *params)
96 {
97     config_item *ret;
98     char buf[80];
99
100     ret = snewn(3, config_item);
101
102     ret[0].name = "Width";
103     ret[0].type = C_STRING;
104     sprintf(buf, "%d", params->w);
105     ret[0].sval = dupstr(buf);
106     ret[0].ival = 0;
107
108     ret[1].name = "Height";
109     ret[1].type = C_STRING;
110     sprintf(buf, "%d", params->h);
111     ret[1].sval = dupstr(buf);
112     ret[1].ival = 0;
113
114     ret[2].name = NULL;
115     ret[2].type = C_END;
116     ret[2].sval = NULL;
117     ret[2].ival = 0;
118
119     return ret;
120 }
121
122 game_params *custom_params(config_item *cfg)
123 {
124     game_params *ret = snew(game_params);
125
126     ret->w = atoi(cfg[0].sval);
127     ret->h = atoi(cfg[1].sval);
128
129     return ret;
130 }
131
132 char *validate_params(game_params *params)
133 {
134     if (params->w < 2 && params->h < 2)
135         return "Width and height must both be at least two";
136
137     return NULL;
138 }
139
140 int perm_parity(int *perm, int n)
141 {
142     int i, j, ret;
143
144     ret = 0;
145
146     for (i = 0; i < n-1; i++)
147         for (j = i+1; j < n; j++)
148             if (perm[i] > perm[j])
149                 ret = !ret;
150
151     return ret;
152 }
153
154 char *new_game_seed(game_params *params, random_state *rs)
155 {
156     int stop, n, i, x;
157     int x1, x2, p1, p2;
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
172     /*
173      * If both dimensions are odd, there is a parity constraint.
174      */
175     if (params->w & params->h & 1)
176         stop = 2;
177     else
178         stop = 0;
179
180     /*
181      * Place everything except (possibly) the last two tiles.
182      */
183     for (x = 0, i = n; i > stop; i--) {
184         int k = i > 1 ? random_upto(rs, i) : 0;
185         int j;
186
187         for (j = 0; j < n; j++)
188             if (!used[j] && (k-- == 0))
189                 break;
190
191         assert(j < n && !used[j]);
192         used[j] = TRUE;
193
194         while (tiles[x] >= 0)
195             x++;
196         assert(x < n);
197         tiles[x] = j;
198     }
199
200     if (stop) {
201         /*
202          * Find the last two locations, and the last two pieces.
203          */
204         while (tiles[x] >= 0)
205             x++;
206         assert(x < n);
207         x1 = x;
208         x++;
209         while (tiles[x] >= 0)
210             x++;
211         assert(x < n);
212         x2 = x;
213
214         for (i = 0; i < n; i++)
215             if (!used[i])
216                 break;
217         p1 = i;
218         for (i = p1+1; i < n; i++)
219             if (!used[i])
220                 break;
221         p2 = i;
222
223         /*
224          * Try the last two tiles one way round. If that fails, swap
225          * them.
226          */
227         tiles[x1] = p1;
228         tiles[x2] = p2;
229         if (perm_parity(tiles, n) != 0) {
230             tiles[x1] = p2;
231             tiles[x2] = p1;
232             assert(perm_parity(tiles, n) == 0);
233         }
234     }
235
236     /*
237      * Now construct the game seed, by describing the tile array as
238      * a simple sequence of comma-separated integers.
239      */
240     ret = NULL;
241     retlen = 0;
242     for (i = 0; i < n; i++) {
243         char buf[80];
244         int k;
245
246         k = sprintf(buf, "%d,", tiles[i]+1);
247
248         ret = sresize(ret, retlen + k + 1, char);
249         strcpy(ret + retlen, buf);
250         retlen += k;
251     }
252     ret[retlen-1] = '\0';              /* delete last comma */
253
254     sfree(tiles);
255     sfree(used);
256
257     return ret;
258 }
259
260
261 char *validate_seed(game_params *params, char *seed)
262 {
263     char *p, *err;
264     int i, area;
265     int *used;
266
267     area = params->w * params->h;
268     p = seed;
269     err = NULL;
270
271     used = snewn(area, int);
272     for (i = 0; i < area; i++)
273         used[i] = FALSE;
274
275     for (i = 0; i < area; i++) {
276         char *q = p;
277         int n;
278
279         if (*p < '0' || *p > '9') {
280             err = "Not enough numbers in string";
281             goto leave;
282         }
283         while (*p >= '0' && *p <= '9')
284             p++;
285         if (i < area-1 && *p != ',') {
286             err = "Expected comma after number";
287             goto leave;
288         }
289         else if (i == area-1 && *p) {
290             err = "Excess junk at end of string";
291             goto leave;
292         }
293         n = atoi(q);
294         if (n < 1 || n > area) {
295             err = "Number out of range";
296             goto leave;
297         }
298         if (used[n-1]) {
299             err = "Number used twice";
300             goto leave;
301         }
302         used[n-1] = TRUE;
303
304         if (*p) p++;                   /* eat comma */
305     }
306
307     leave:
308     sfree(used);
309     return err;
310 }
311
312 game_state *new_game(game_params *params, char *seed)
313 {
314     game_state *state = snew(game_state);
315     int i;
316     char *p;
317
318     state->w = params->w;
319     state->h = params->h;
320     state->n = params->w * params->h;
321     state->tiles = snewn(state->n, int);
322
323     p = seed;
324     i = 0;
325     for (i = 0; i < state->n; i++) {
326         assert(*p);
327         state->tiles[i] = atoi(p);
328         while (*p && *p != ',')
329             p++;
330         if (*p) p++;                   /* eat comma */
331     }
332     assert(!*p);
333
334     state->completed = state->movecount = 0;
335     state->last_movement_sense = 0;
336
337     return state;
338 }
339
340 game_state *dup_game(game_state *state)
341 {
342     game_state *ret = snew(game_state);
343
344     ret->w = state->w;
345     ret->h = state->h;
346     ret->n = state->n;
347     ret->tiles = snewn(state->w * state->h, int);
348     memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
349     ret->completed = state->completed;
350     ret->movecount = state->movecount;
351     ret->last_movement_sense = state->last_movement_sense;
352
353     return ret;
354 }
355
356 void free_game(game_state *state)
357 {
358     sfree(state);
359 }
360
361 game_state *make_move(game_state *from, int x, int y, int button)
362 {
363     int cx, cy;
364     int dx, dy, tx, ty, n;
365     game_state *ret;
366
367     if (button != LEFT_BUTTON)
368         return NULL;
369
370     cx = FROMCOORD(x);
371     cy = FROMCOORD(y);
372     if (cx == -1 && cy >= 0 && cy < from->h)
373         n = from->w, dx = +1, dy = 0;
374     else if (cx == from->w && cy >= 0 && cy < from->h)
375         n = from->w, dx = -1, dy = 0;
376     else if (cy == -1 && cx >= 0 && cx < from->w)
377         n = from->h, dy = +1, dx = 0;
378     else if (cy == from->h && cx >= 0 && cx < from->w)
379         n = from->h, dy = -1, dx = 0;
380     else
381         return NULL;                   /* invalid click location */
382
383     ret = dup_game(from);
384
385     do {
386         cx += dx;
387         cy += dy;
388         tx = (cx + dx + from->w) % from->w;
389         ty = (cy + dy + from->h) % from->h;
390         ret->tiles[C(ret, cx, cy)] = from->tiles[C(from, tx, ty)];
391     } while (--n > 0);
392
393     ret->movecount++;
394
395     ret->last_movement_sense = -(dx+dy);
396
397     /*
398      * See if the game has been completed.
399      */
400     if (!ret->completed) {
401         ret->completed = ret->movecount;
402         for (n = 0; n < ret->n; n++)
403             if (ret->tiles[n] != n+1)
404                 ret->completed = FALSE;
405     }
406
407     return ret;
408 }
409
410 /* ----------------------------------------------------------------------
411  * Drawing routines.
412  */
413
414 struct game_drawstate {
415     int started;
416     int w, h, bgcolour;
417     int *tiles;
418 };
419
420 void game_size(game_params *params, int *x, int *y)
421 {
422     *x = TILE_SIZE * params->w + 2 * BORDER;
423     *y = TILE_SIZE * params->h + 2 * BORDER;
424 }
425
426 float *game_colours(frontend *fe, game_state *state, int *ncolours)
427 {
428     float *ret = snewn(3 * NCOLOURS, float);
429     int i;
430     float max;
431
432     frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
433
434     /*
435      * Drop the background colour so that the highlight is
436      * noticeably brighter than it while still being under 1.
437      */
438     max = ret[COL_BACKGROUND*3];
439     for (i = 1; i < 3; i++)
440         if (ret[COL_BACKGROUND*3+i] > max)
441             max = ret[COL_BACKGROUND*3+i];
442     if (max * 1.2F > 1.0F) {
443         for (i = 0; i < 3; i++)
444             ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
445     }
446
447     for (i = 0; i < 3; i++) {
448         ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
449         ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
450         ret[COL_TEXT * 3 + i] = 0.0;
451     }
452
453     *ncolours = NCOLOURS;
454     return ret;
455 }
456
457 game_drawstate *game_new_drawstate(game_state *state)
458 {
459     struct game_drawstate *ds = snew(struct game_drawstate);
460     int i;
461
462     ds->started = FALSE;
463     ds->w = state->w;
464     ds->h = state->h;
465     ds->bgcolour = COL_BACKGROUND;
466     ds->tiles = snewn(ds->w*ds->h, int);
467     for (i = 0; i < ds->w*ds->h; i++)
468         ds->tiles[i] = -1;
469
470     return ds;
471 }
472
473 void game_free_drawstate(game_drawstate *ds)
474 {
475     sfree(ds->tiles);
476     sfree(ds);
477 }
478
479 static void draw_tile(frontend *fe, game_state *state, int x, int y,
480                       int tile, int flash_colour)
481 {
482     if (tile == 0) {
483         draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE,
484                   flash_colour);
485     } else {
486         int coords[6];
487         char str[40];
488
489         coords[0] = x + TILE_SIZE - 1;
490         coords[1] = y + TILE_SIZE - 1;
491         coords[2] = x + TILE_SIZE - 1;
492         coords[3] = y;
493         coords[4] = x;
494         coords[5] = y + TILE_SIZE - 1;
495         draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
496         draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
497
498         coords[0] = x;
499         coords[1] = y;
500         draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
501         draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
502
503         draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
504                   TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
505                   flash_colour);
506
507         sprintf(str, "%d", tile);
508         draw_text(fe, x + TILE_SIZE/2, y + TILE_SIZE/2,
509                   FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
510                   COL_TEXT, str);
511     }
512     draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
513 }
514
515 static void draw_arrow(frontend *fe, int x, int y, int xdx, int xdy)
516 {
517     int coords[14];
518     int ydy = -xdx, ydx = xdy;
519
520 #define POINT(n, xx, yy) ( \
521     coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
522     coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
523
524     POINT(0, TILE_SIZE / 2, 3 * TILE_SIZE / 4);   /* top of arrow */
525     POINT(1, 3 * TILE_SIZE / 4, TILE_SIZE / 2);   /* right corner */
526     POINT(2, 5 * TILE_SIZE / 8, TILE_SIZE / 2);   /* right concave */
527     POINT(3, 5 * TILE_SIZE / 8, TILE_SIZE / 4);   /* bottom right */
528     POINT(4, 3 * TILE_SIZE / 8, TILE_SIZE / 4);   /* bottom left */
529     POINT(5, 3 * TILE_SIZE / 8, TILE_SIZE / 2);   /* left concave */
530     POINT(6,     TILE_SIZE / 4, TILE_SIZE / 2);   /* left corner */
531
532     draw_polygon(fe, coords, 7, TRUE, COL_LOWLIGHT);
533     draw_polygon(fe, coords, 7, FALSE, COL_TEXT);
534 }
535
536 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
537                  game_state *state, float animtime, float flashtime)
538 {
539     int i, pass, bgcolour;
540
541     if (flashtime > 0) {
542         int frame = (int)(flashtime / FLASH_FRAME);
543         bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
544     } else
545         bgcolour = COL_BACKGROUND;
546
547     if (!ds->started) {
548         int coords[6];
549
550         draw_rect(fe, 0, 0,
551                   TILE_SIZE * state->w + 2 * BORDER,
552                   TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
553         draw_update(fe, 0, 0,
554                     TILE_SIZE * state->w + 2 * BORDER,
555                     TILE_SIZE * state->h + 2 * BORDER);
556
557         /*
558          * Recessed area containing the whole puzzle.
559          */
560         coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
561         coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
562         coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
563         coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
564         coords[4] = COORD(0) - HIGHLIGHT_WIDTH;
565         coords[5] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
566         draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
567         draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
568
569         coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
570         coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
571         draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
572         draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
573
574         /*
575          * Arrows for making moves.
576          */
577         for (i = 0; i < state->w; i++) {
578             draw_arrow(fe, COORD(i), COORD(0), +1, 0);
579             draw_arrow(fe, COORD(i+1), COORD(state->h), -1, 0);
580         }
581         for (i = 0; i < state->h; i++) {
582             draw_arrow(fe, COORD(state->w), COORD(i), 0, +1);
583             draw_arrow(fe, COORD(0), COORD(i+1), 0, -1);
584         }
585
586         ds->started = TRUE;
587     }
588
589     /*
590      * Now draw each tile. We do this in two passes to make
591      * animation easy.
592      */
593
594     clip(fe, COORD(0), COORD(0), TILE_SIZE*state->w, TILE_SIZE*state->h);
595
596     for (pass = 0; pass < 2; pass++) {
597         for (i = 0; i < state->n; i++) {
598             int t, t0;
599             /*
600              * Figure out what should be displayed at this
601              * location. It's either a simple tile, or it's a
602              * transition between two tiles (in which case we say
603              * -1 because it must always be drawn).
604              */
605
606             if (oldstate && oldstate->tiles[i] != state->tiles[i])
607                 t = -1;
608             else
609                 t = state->tiles[i];
610
611             t0 = t;
612
613             if (ds->bgcolour != bgcolour ||   /* always redraw when flashing */
614                 ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) {
615                 int x, y, x2, y2;
616
617                 /*
618                  * Figure out what to _actually_ draw, and where to
619                  * draw it.
620                  */
621                 if (t == -1) {
622                     int x0, y0, x1, y1, dx, dy;
623                     int j;
624
625                     /*
626                      * On the first pass, just blank the tile.
627                      */
628                     if (pass == 0) {
629                         x = COORD(X(state, i));
630                         y = COORD(Y(state, i));
631                         x2 = y2 = -1;
632                         t = 0;
633                     } else {
634                         float c;
635
636                         t = state->tiles[i];
637
638                         /*
639                          * FIXME: must be prepared to draw a double
640                          * tile in some situations.
641                          */
642
643                         /*
644                          * Find the coordinates of this tile in the old and
645                          * new states.
646                          */
647                         x1 = COORD(X(state, i));
648                         y1 = COORD(Y(state, i));
649                         for (j = 0; j < oldstate->n; j++)
650                             if (oldstate->tiles[j] == state->tiles[i])
651                                 break;
652                         assert(j < oldstate->n);
653                         x0 = COORD(X(state, j));
654                         y0 = COORD(Y(state, j));
655
656                         dx = (x1 - x0);
657                         if (dx != 0 &&
658                             dx != TILE_SIZE * state->last_movement_sense) {
659                             dx = (dx < 0 ? dx + TILE_SIZE * state->w :
660                                   dx - TILE_SIZE * state->w);
661                             assert(abs(dx) == TILE_SIZE);
662                         }
663                         dy = (y1 - y0);
664                         if (dy != 0 &&
665                             dy != TILE_SIZE * state->last_movement_sense) {
666                             dy = (dy < 0 ? dy + TILE_SIZE * state->h :
667                                   dy - TILE_SIZE * state->h);
668                             assert(abs(dy) == TILE_SIZE);
669                         }
670
671                         c = (animtime / ANIM_TIME);
672                         if (c < 0.0F) c = 0.0F;
673                         if (c > 1.0F) c = 1.0F;
674
675                         x = x0 + (int)(c * dx);
676                         y = y0 + (int)(c * dy);
677                         x2 = x1 - dx + (int)(c * dx);
678                         y2 = y1 - dy + (int)(c * dy);
679                     }
680
681                 } else {
682                     if (pass == 0)
683                         continue;
684                     x = COORD(X(state, i));
685                     y = COORD(Y(state, i));
686                     x2 = y2 = -1;
687                 }
688
689                 draw_tile(fe, state, x, y, t, bgcolour);
690                 if (x2 != -1 || y2 != -1)
691                     draw_tile(fe, state, x2, y2, t, bgcolour);
692             }
693             ds->tiles[i] = t0;
694         }
695     }
696
697     unclip(fe);
698
699     ds->bgcolour = bgcolour;
700
701     /*
702      * Update the status bar.
703      */
704     {
705         char statusbuf[256];
706
707         /*
708          * Don't show the new status until we're also showing the
709          * new _state_ - after the game animation is complete.
710          */
711         if (oldstate)
712             state = oldstate;
713
714         sprintf(statusbuf, "%sMoves: %d",
715                 (state->completed ? "COMPLETED! " : ""),
716                 (state->completed ? state->completed : state->movecount));
717
718         status_bar(fe, statusbuf);
719     }
720 }
721
722 float game_anim_length(game_state *oldstate, game_state *newstate)
723 {
724     return ANIM_TIME;
725 }
726
727 float game_flash_length(game_state *oldstate, game_state *newstate)
728 {
729     if (!oldstate->completed && newstate->completed)
730         return 2 * FLASH_FRAME;
731     else
732         return 0.0F;
733 }
734
735 int game_wants_statusbar(void)
736 {
737     return TRUE;
738 }