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