]> git.cworth.org Git - wordgame/blob - rack-fancy.c
7f0542d0dc3fc0ba3523154a35fd4d1938b579f8
[wordgame] / rack-fancy.c
1 /*
2  * Copyright © 2006 Carl Worth
3  *
4  * This program is free software; you can redistribute it and\/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
17  */
18 #include <stdlib.h>
19 #include <string.h>
20 #include <goocanvas.h>
21 #include <sys/time.h>
22 #include <time.h>
23 #include <ctype.h>
24 #include <math.h>
25 #include <gdk/gdkkeysyms.h>
26
27 #include "word-game.h"
28 #include "demo-item.h"
29
30 #define RACK_DICT_ENTRY_FOUND (1<<1)
31 #define MAX_TILES 7
32
33 typedef struct _tile
34 {
35     char letter;
36     int rack_index;
37     int x, y;
38     GooCanvasItem *item;
39     gboolean guessed;
40 } tile_t;
41
42 typedef struct _rack
43 {
44     tile_t *tiles[MAX_TILES];
45     int num_tiles;
46     char guess[MAX_TILES+1];
47     int guess_length;
48     bag_t bag;
49     dict_t dict;
50     dict_t solution;
51     int solution_total;
52     GooCanvasItem *solution_item;
53     gboolean done;
54 } rack_t;
55
56 #define LETTER_SIZE 60
57 #define LETTER_PAD 5
58
59 static void
60 guess_tile_position (int i, int *x, int *y)
61 {
62     *x = 20 + i * (LETTER_SIZE + LETTER_PAD);
63     *y = LETTER_PAD;
64 }
65
66 static void
67 rack_tile_position (int i, int *x, int *y)
68 {
69     guess_tile_position (i, x, y);
70     *y += (LETTER_SIZE + LETTER_PAD);
71 }
72
73 typedef enum dict_paint_cursor_show
74 {
75     DICT_PAINT_CURSOR_SHOW_FOUND,
76     DICT_PAINT_CURSOR_SHOW_UNFOUND_BLANKS,
77     DICT_PAINT_CURSOR_SHOW_ALL
78 } dict_paint_cursor_show_t;
79
80 typedef struct _dict_paint_cursor
81 {
82     cairo_t *cr;
83     int line_height;
84     int x;
85     int y;
86     int max_column_width;
87     int max_y;
88     dict_paint_cursor_show_t show;
89 } dict_paint_cursor_t;
90
91 static void
92 dict_paint_action (void *closure, char *word, dict_entry_t *entry)
93 {
94     dict_paint_cursor_t *cursor = closure;
95     cairo_t *cr = cursor->cr;
96     double new_x, new_y;
97     int found, show_blanks = FALSE;
98
99     if (strlen (word) < 3)
100         return;
101
102     found = *entry & RACK_DICT_ENTRY_FOUND;
103
104     cairo_set_source_rgb (cr, 0, 0, 0); /* black */
105
106     switch (cursor->show) {
107     case DICT_PAINT_CURSOR_SHOW_FOUND:
108         if (! found)
109             return;
110         break;
111     case DICT_PAINT_CURSOR_SHOW_UNFOUND_BLANKS:
112         if (found)
113             return;
114         show_blanks = TRUE;
115         break;
116     case DICT_PAINT_CURSOR_SHOW_ALL:
117         if (! found)
118             cairo_set_source_rgb (cr, 1, 0, 0); /* red */
119         break;
120     }
121
122     cairo_move_to (cr, cursor->x, cursor->y);
123     if (show_blanks) {
124         int i, length = strlen (word);
125         for (i = 0; i < length; i++)
126             cairo_show_text (cr, "_");
127     } else {
128         cairo_show_text (cr, word);
129     }
130     cairo_get_current_point (cr, &new_x, &new_y);
131     if (new_x > cursor->max_column_width)
132         cursor->max_column_width = new_x;
133     cursor->y += cursor->line_height;
134     if (cursor->y > cursor->max_y) {
135         cursor->x = cursor->max_column_width + cursor->line_height / 2;
136         cursor->y = cursor->line_height;
137     }
138 }
139
140 #define SOLUTION_FONT_SIZE      12
141 #define SOLUTION_LINE_HEIGHT    (1.5 * SOLUTION_FONT_SIZE)
142
143 static void
144 dict_paint (cairo_t *cr, void *closure, double width, double height)
145 {
146     rack_t *rack = closure;
147     dict_paint_cursor_t cursor;
148     int length, count;
149
150     cairo_save (cr);
151     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
152
153     cursor.cr = cr;
154
155     cairo_select_font_face (cr, "mono", 0, 0);
156     cairo_set_font_size (cr, SOLUTION_FONT_SIZE);
157     cursor.line_height = SOLUTION_LINE_HEIGHT;
158     
159     cursor.x = 0;
160     cursor.y = cursor.line_height;
161
162     cursor.max_column_width = 0;
163     cursor.max_y = height;
164
165     length = 1;
166     count = 0;
167     do {
168         if (rack->done)
169             cursor.show = DICT_PAINT_CURSOR_SHOW_ALL;
170         else
171             cursor.show = DICT_PAINT_CURSOR_SHOW_FOUND;
172         count += dict_for_each_of_length (&rack->solution,
173                                           dict_paint_action, &cursor,
174                                           length, length);
175         if (! rack->done) {
176             cursor.show = DICT_PAINT_CURSOR_SHOW_UNFOUND_BLANKS;
177             dict_for_each_of_length (&rack->solution,
178                                      dict_paint_action, &cursor,
179                                      length, length);
180         }
181         length++;
182     } while (count < rack->solution_total);
183
184     cairo_restore (cr);
185 }
186
187 static void
188 tile_paint (cairo_t *cr, void *closure, double width, double height)
189 {
190     tile_t *tile = closure;
191
192     cairo_pattern_t *gradient;
193     cairo_text_extents_t extents;
194     int rad = (int) MIN (width / 2, height / 2);
195     int cx = width / 2;
196     int cy = cx;
197     int tx, ty;
198     double spot_angle = M_PI / 4.0;
199     double spot_rad = rad / 2.0;
200     char string[2];
201
202     cairo_save (cr);
203
204     gradient = cairo_pattern_create_radial (cx - spot_rad * cos (spot_angle),
205                                             cy - spot_rad * sin (spot_angle),
206                                             0.0,
207                                             cx - spot_rad * cos (spot_angle),
208                                             cy - spot_rad * sin (spot_angle),
209                                             rad + spot_rad);
210     cairo_pattern_add_color_stop_rgb (gradient, 0.0, 1.0, 1.0, 1.0);
211     cairo_pattern_add_color_stop_rgb (gradient, 1.0, 0.33, 0.33, 0.33);
212
213     cairo_set_source (cr, gradient);
214
215     cairo_arc (cr,
216                cx, cy,
217                rad, 0, 2 * M_PI);
218
219     cairo_fill (cr);
220
221     cairo_select_font_face (cr, "mono",
222                             CAIRO_FONT_SLANT_NORMAL,
223                             CAIRO_FONT_WEIGHT_BOLD);
224     cairo_set_font_size (cr, 1.8 * rad);
225
226     string[0] = tile->letter;
227     string[1] = '\0';
228     cairo_text_extents (cr, string, &extents);
229     tx = cx - extents.width / 2 - extents.x_bearing;
230     ty = cy - extents.height / 2 - extents.y_bearing;
231
232     cairo_set_source_rgb (cr, 0.7, 0.7, 0.7);
233     cairo_move_to (cr, tx + 1, ty + 1);
234     cairo_show_text (cr, string);
235                 
236     cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
237     cairo_move_to (cr, tx - 1, ty - 1);
238     cairo_show_text (cr, string);
239
240     cairo_set_source_rgb (cr, 0.2, 0.3, 0.8);
241     cairo_move_to (cr, tx, ty);
242     cairo_show_text (cr, string);
243
244     cairo_restore (cr);
245 }
246
247 static void
248 tile_glide_to (tile_t *tile, int x, int y)
249 {
250     goo_canvas_item_animate (tile->item, x, y,
251                              1.0, 0,
252                              500, 40,
253                              GOO_CANVAS_ANIMATE_FREEZE);
254     tile->x = x;
255     tile->y = y;
256 }
257
258 static tile_t *
259 tile_create (GooCanvasItem *parent,
260              char letter, int rack_index)
261 {
262     tile_t *tile;
263
264     tile = g_malloc (sizeof (tile_t));
265     tile->letter = tolower (letter);
266     tile->rack_index = rack_index;
267     rack_tile_position (rack_index, &tile->x, &tile->y);
268     tile->item = goo_demo_item_new (parent,
269                                     tile->x, tile->y,
270                                     LETTER_SIZE, LETTER_SIZE,
271                                     tile_paint,
272                                     tile, NULL);
273
274     tile->guessed = FALSE;
275
276     return tile;
277 }
278
279 static gboolean
280 on_delete_event (GtkWidget *window,
281                  GdkEvent  *event,
282                  gpointer   unused_data)
283 {
284     exit (0);
285 }
286
287 static int
288 rand_within (int num_values)
289 {
290     return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
291 }
292
293 static void
294 shuffle (int *array, int length)
295 {
296     int i, r, tmp;
297
298     for (i = 0; i < length; i++)
299     {
300         r = i + rand_within (length - i);
301         tmp = array[i];
302         array[i] = array[r];
303         array[r] = tmp;
304     }
305 }
306
307 static void
308 rack_init (rack_t       *rack,
309            GooCanvas    *canvas)
310 {
311     int i;
312     GooCanvasItem *root = goo_canvas_get_root_item (canvas);
313
314     for (i = 0; i < MAX_TILES; i++)
315         rack->tiles[i] = NULL;
316     rack->num_tiles = 0;
317
318     rack->guess_length = 0;
319     rack->done = FALSE;
320
321     bag_init (&rack->bag);
322
323     dict_init (&rack->dict);
324     dict_add_words_from_file (&rack->dict, "words.txt");
325
326     dict_init (&rack->solution);
327     rack->solution_total = 0;
328
329     for (i = 0; i < MAX_TILES; i++)
330         rack->tiles[i] = tile_create (root, 'A', i);
331     rack->num_tiles = 0;
332 }
333
334 static void
335 rack_new_game (rack_t *rack)
336 {
337     int i, bottom;
338     char word[MAX_TILES + 1];
339     int length = MAX_TILES;
340     int count;
341
342     /* We'll shuffle as many times as necessary until we can find a
343      * sequence of <length> letters with at least one full-length
344      * word. */
345     while (1) {
346         bag_shuffle (&rack->bag);
347
348         /* In this game, we're not interested in blank tiles, so first
349          * find any blanks and sort them to the bottom of the bag. */
350         i = 0;
351         bottom = BAG_SIZE - 1;
352         for (i = 0; i < bottom; i++) {
353             if (rack->bag.tiles[i] == '?') {
354                 rack->bag.tiles[i] = rack->bag.tiles[bottom];
355                 rack->bag.tiles[bottom] = '?';
356                 bottom--;
357                 /* Re-examine ith element */
358                 i--;
359             }
360         }
361
362         for (i = 0; i + length <= bottom + 1; i++) {
363             memcpy (word, &rack->bag.tiles[i], length);
364             word[length] = '\0';
365             printf ("Candidate word %s\n", word);
366             dict_fini (&rack->solution);
367             dict_init (&rack->solution);
368             subanagram_expand (word, &rack->dict, &rack->solution);
369             count = dict_for_each_of_length (&rack->solution,
370                                              NULL, NULL,
371                                              length, length);
372             if (count)
373                 goto DONE;
374             i++;
375         }
376     }
377
378   DONE:
379     rack->solution_total = dict_count (&rack->solution);
380     goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->solution_item), FALSE);
381
382     for (i = 0; i < length; i++) {
383         rack->tiles[i]->letter = toupper (word[i]);
384         goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->tiles[i]->item), FALSE);
385     }
386     rack->num_tiles = length;
387 }
388
389 static gboolean
390 rack_shuffle (rack_t *rack)
391 {
392     int indices[MAX_TILES];
393     int i, x, y;
394
395     for (i = 0; i < rack->num_tiles; i++)
396         indices[i] = i;
397
398     shuffle (indices, rack->num_tiles);
399
400     for (i = 0; i < rack->num_tiles; i++) {
401         rack->tiles[i]->rack_index = indices[i];
402         rack_tile_position (indices[i], &x, &y);
403         tile_glide_to (rack->tiles[i], x, y);
404     }
405
406     return TRUE;
407 }
408
409 static void
410 rack_return_tile (rack_t *rack, tile_t *tile)
411 {
412     int x, y;
413
414     rack_tile_position (tile->rack_index, &x, &y);
415     tile_glide_to (tile, x, y);
416     tile->guessed = FALSE;
417     rack->guess_length--;
418     rack->guess[rack->guess_length] = '\0';
419 }
420
421 static void
422 rack_return_all (rack_t *rack)
423 {
424     int i;
425
426     for (i = 0; i < rack->num_tiles; i++) {
427         if (rack->tiles[i]->guessed)
428             rack_return_tile (rack, rack->tiles[i]);
429     }
430     rack->guess_length = 0;
431     rack->guess[0] = '\0';
432 }
433
434 static gboolean
435 on_key_press_event (GtkWidget   *widget,
436                     GdkEventKey *event,
437                     gpointer     user_data)
438 {
439     int i, x, y;
440     char guess_letter;
441     rack_t *rack = user_data;
442
443     if (event->state & GDK_CONTROL_MASK &&
444         event->keyval == GDK_c)
445     {
446         rack->done = TRUE;
447         goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->solution_item), FALSE);
448     }
449
450     if (event->keyval == GDK_Return) {
451         dict_entry_t *entry;
452         if (rack->done) {
453             rack->done = FALSE;
454             rack_new_game (rack);
455             return TRUE;
456         }
457         if (rack->guess_length >= 3) {
458             entry = dict_lookup (&rack->solution, rack->guess);
459             if (DICT_ENTRY_IS_WORD (entry)) {
460                 *entry = *entry | RACK_DICT_ENTRY_FOUND;
461                 goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->solution_item), FALSE);
462             } else {
463                 printf ("\a");
464                 fflush (stdout);
465             }
466         }
467         rack_return_all (rack);
468         return TRUE;
469     }
470
471     if (event->keyval == GDK_space) {
472         rack_return_all (rack);
473         rack_shuffle (rack);
474         return TRUE;
475     }
476
477     if (event->keyval == GDK_BackSpace) {
478         gboolean found = FALSE;
479         int found_index;
480         x = 0;
481         for (i = 0; i < rack->num_tiles; i++) {
482             /* XXX: evil stuff here... please refactor a lot */
483             if (rack->guess[rack->guess_length-1] == rack->tiles[i]->letter &&
484                 rack->tiles[i]->guessed &&
485                 rack->tiles[i]->x > x)
486             {
487                 found = TRUE;
488                 found_index = i;
489             }
490         }
491         if (found) {
492             rack_return_tile (rack, rack->tiles[found_index]);
493             return TRUE;
494         }
495         return FALSE;
496     }
497
498     /* XXX: event->string is deprecated, but the non-deprecated
499      * input-method stuff (GtkIMContext) is extremely non-obvious to
500      * use. */
501     guess_letter = toupper (event->string[0]);
502     for (i = 0; i < rack->num_tiles; i++) {
503         if (guess_letter == rack->tiles[i]->letter && 
504             ! rack->tiles[i]->guessed)
505         {
506             guess_tile_position (rack->guess_length, &x, &y);
507             tile_glide_to (rack->tiles[i], x, y);
508             rack->tiles[i]->guessed = TRUE;
509             rack->guess[rack->guess_length++] = guess_letter;
510             rack->guess[rack->guess_length] = '\0';
511             return TRUE;
512         }
513     }
514
515     return FALSE;
516 }
517
518 static GtkWidget *
519 create_window (rack_t *rack)
520 {
521     GtkWidget *window, *scrolled_window;
522
523     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
524     gtk_window_set_default_size (GTK_WINDOW (window), 500, 500);
525     gtk_widget_show (window);
526     g_signal_connect (window, "delete_event",
527                       (GtkSignalFunc) on_delete_event, NULL);
528
529     gtk_widget_add_events (window, GDK_KEY_PRESS_MASK);
530     g_signal_connect (window, "key_press_event",
531                       (GtkSignalFunc) on_key_press_event, rack);
532
533     scrolled_window = gtk_scrolled_window_new (NULL, NULL);
534     gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
535                                          GTK_SHADOW_IN);
536     gtk_scrolled_window_set_policy  (GTK_SCROLLED_WINDOW (scrolled_window),
537                                      GTK_POLICY_AUTOMATIC,
538                                      GTK_POLICY_AUTOMATIC);
539     gtk_widget_show (scrolled_window);
540     gtk_container_add (GTK_CONTAINER (window), scrolled_window);
541
542     return scrolled_window;
543 }
544
545 static GooCanvas *
546 create_canvas (GtkWidget *parent, rack_t *rack)
547 {
548     GtkWidget *canvas;
549     GooCanvasItem *root;
550
551     canvas = goo_canvas_new ();
552     gtk_widget_set_size_request (canvas, 400, 480);
553     goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 400, 480);
554     gtk_widget_show (canvas);
555     gtk_container_add (GTK_CONTAINER (parent), canvas);
556
557     root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
558
559     rack->solution_item = goo_demo_item_new (root,
560                                              20,
561                                              LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD),
562                                              400 - 20, 480 - (LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD)),
563                                              dict_paint, rack,
564                                              NULL);
565
566     return GOO_CANVAS (canvas);
567 }
568
569 int
570 main (int argc, char *argv[])
571 {
572     struct timeval tv;
573     rack_t rack;
574     GtkWidget *window;
575     GooCanvas *canvas;
576
577     gettimeofday (&tv, NULL);
578     srand (tv.tv_sec ^ tv.tv_usec);
579
580     gtk_init (&argc, &argv);
581
582     window = create_window (&rack);
583
584     canvas = create_canvas (window, &rack);
585
586     rack_init (&rack, canvas);
587
588     rack_new_game (&rack);
589
590     gtk_main ();
591
592     return 0;
593 }