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