2 * Copyright © 2006 Carl Worth
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)
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.
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."
20 #include <goocanvas.h>
25 #include <gdk/gdkkeysyms.h>
27 #include "word-game.h"
28 #include "demo-item.h"
30 #define RACK_DICT_ENTRY_OBSCURE (1<<1)
31 #define RACK_DICT_ENTRY_FOUND (1<<2)
45 tile_t *tiles[MAX_TILES];
47 char guess[MAX_TILES+1];
54 GooCanvasItem *solution_item;
58 #define LETTER_SIZE 60
62 guess_tile_position (int i, int *x, int *y)
64 *x = 20 + i * (LETTER_SIZE + LETTER_PAD);
69 rack_tile_position (int i, int *x, int *y)
71 guess_tile_position (i, x, y);
72 *y += (LETTER_SIZE + LETTER_PAD);
75 typedef enum dict_paint_cursor_show
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;
82 typedef struct _dict_paint_cursor
90 dict_paint_cursor_show_t show;
91 } dict_paint_cursor_t;
94 dict_paint_action (void *closure, char *word, dict_entry_t *entry)
96 dict_paint_cursor_t *cursor = closure;
97 cairo_t *cr = cursor->cr;
99 int found, show_blanks = FALSE;
101 if (strlen (word) < 3)
104 found = *entry & RACK_DICT_ENTRY_FOUND;
106 cairo_set_source_rgb (cr, 0, 0, 0); /* black */
108 switch (cursor->show) {
109 case DICT_PAINT_CURSOR_SHOW_FOUND:
113 case DICT_PAINT_CURSOR_SHOW_UNFOUND_BLANKS:
118 case DICT_PAINT_CURSOR_SHOW_ALL:
120 cairo_set_source_rgb (cr, 1, 0, 0); /* red */
124 /* Don't bother displaying unfound obscure words. */
125 if (*entry & RACK_DICT_ENTRY_OBSCURE) {
128 cairo_set_source_rgb (cr, 0, 1, 0); /* green */
131 cairo_move_to (cr, cursor->x, cursor->y);
133 int i, length = strlen (word);
134 for (i = 0; i < length; i++)
135 cairo_show_text (cr, "_");
137 cairo_show_text (cr, word);
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;
149 #define SOLUTION_FONT_SIZE 12
150 #define SOLUTION_LINE_HEIGHT (1.5 * SOLUTION_FONT_SIZE)
153 dict_paint (cairo_t *cr, void *closure, double width, double height)
155 rack_t *rack = closure;
156 dict_paint_cursor_t cursor;
160 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
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;
169 cursor.y = cursor.line_height;
171 cursor.max_column_width = 0;
172 cursor.max_y = height;
178 cursor.show = DICT_PAINT_CURSOR_SHOW_ALL;
180 cursor.show = DICT_PAINT_CURSOR_SHOW_FOUND;
181 count += dict_for_each_of_length (&rack->solution,
182 dict_paint_action, &cursor,
185 cursor.show = DICT_PAINT_CURSOR_SHOW_UNFOUND_BLANKS;
186 dict_for_each_of_length (&rack->solution,
187 dict_paint_action, &cursor,
191 } while (count < rack->solution_total);
197 tile_paint (cairo_t *cr, void *closure, double width, double height)
199 tile_t *tile = closure;
201 cairo_pattern_t *gradient;
202 cairo_text_extents_t extents;
203 int rad = (int) MIN (width / 2, height / 2);
207 double spot_angle = M_PI / 4.0;
208 double spot_rad = rad / 2.0;
213 gradient = cairo_pattern_create_radial (cx - spot_rad * cos (spot_angle),
214 cy - spot_rad * sin (spot_angle),
216 cx - spot_rad * cos (spot_angle),
217 cy - spot_rad * sin (spot_angle),
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);
222 cairo_set_source (cr, gradient);
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);
235 string[0] = tile->letter;
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;
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);
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);
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);
257 tile_glide_to (tile_t *tile, int x, int y)
259 goo_canvas_item_animate (tile->item, x, y,
262 GOO_CANVAS_ANIMATE_FREEZE);
268 tile_create (GooCanvasItem *parent,
269 char letter, int rack_index)
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,
279 LETTER_SIZE, LETTER_SIZE,
283 tile->guessed = FALSE;
289 on_delete_event (GtkWidget *window,
291 gpointer unused_data)
297 rand_within (int num_values)
299 return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
303 shuffle (int *array, int length)
307 for (i = 0; i < length; i++)
309 r = i + rand_within (length - i);
317 rack_init (rack_t *rack,
321 GooCanvasItem *root = goo_canvas_get_root_item (canvas);
323 for (i = 0; i < MAX_TILES; i++)
324 rack->tiles[i] = NULL;
327 rack->guess_length = 0;
330 bag_init (&rack->bag);
332 dict_init (&rack->dict);
333 dict_add_words_from_file (&rack->dict, "words.txt");
335 dict_init (&rack->obscure);
336 dict_add_words_from_file (&rack->obscure, "obscure.txt");
338 dict_init (&rack->solution);
339 rack->solution_total = 0;
341 for (i = 0; i < MAX_TILES; i++)
342 rack->tiles[i] = tile_create (root, 'A', i);
347 _flag_obscure_word (void *closure, char *word, dict_entry_t *entry)
349 dict_t *obscure = closure;
350 dict_entry_t *obscure_entry;
352 obscure_entry = dict_lookup (obscure, word);
353 if (DICT_ENTRY_IS_WORD (obscure_entry))
354 *entry |= RACK_DICT_ENTRY_OBSCURE;
358 rack_new_game (rack_t *rack)
361 char word[MAX_TILES + 1];
362 int length = MAX_TILES;
365 /* We'll shuffle as many times as necessary until we can find a
366 * sequence of <length> letters with at least one full-length
369 bag_shuffle (&rack->bag);
371 /* In this game, we're not interested in blank tiles, so first
372 * find any blanks and sort them to the bottom of the bag. */
374 bottom = BAG_SIZE - 1;
375 for (i = 0; i < bottom; i++) {
376 if (rack->bag.tiles[i] == '?') {
377 rack->bag.tiles[i] = rack->bag.tiles[bottom];
378 rack->bag.tiles[bottom] = '?';
380 /* Re-examine ith element */
385 for (i = 0; i + length <= bottom + 1; i++) {
386 memcpy (word, &rack->bag.tiles[i], length);
388 dict_fini (&rack->solution);
389 dict_init (&rack->solution);
390 subanagram_expand (word, &rack->dict, &rack->solution);
391 count = dict_for_each_of_length (&rack->solution,
401 dict_for_each (&rack->solution,
402 _flag_obscure_word, &rack->obscure);
403 rack->solution_total = dict_count (&rack->solution);
404 goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->solution_item), FALSE);
406 for (i = 0; i < length; i++) {
407 rack->tiles[i]->letter = toupper (word[i]);
408 goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->tiles[i]->item), FALSE);
410 rack->num_tiles = length;
414 rack_shuffle (rack_t *rack)
416 int indices[MAX_TILES];
419 for (i = 0; i < rack->num_tiles; i++)
422 shuffle (indices, rack->num_tiles);
424 for (i = 0; i < rack->num_tiles; i++) {
425 rack->tiles[i]->rack_index = indices[i];
426 rack_tile_position (indices[i], &x, &y);
427 tile_glide_to (rack->tiles[i], x, y);
434 rack_return_tile (rack_t *rack, tile_t *tile)
438 rack_tile_position (tile->rack_index, &x, &y);
439 tile_glide_to (tile, x, y);
440 tile->guessed = FALSE;
441 rack->guess_length--;
442 rack->guess[rack->guess_length] = '\0';
446 rack_return_all (rack_t *rack)
450 for (i = 0; i < rack->num_tiles; i++) {
451 if (rack->tiles[i]->guessed)
452 rack_return_tile (rack, rack->tiles[i]);
454 rack->guess_length = 0;
455 rack->guess[0] = '\0';
459 on_key_press_event (GtkWidget *widget,
465 rack_t *rack = user_data;
467 if (event->state & GDK_CONTROL_MASK &&
468 event->keyval == GDK_c)
471 goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->solution_item), FALSE);
474 if (event->keyval == GDK_Return) {
478 rack_new_game (rack);
481 if (rack->guess_length >= 3) {
482 entry = dict_lookup (&rack->solution, rack->guess);
483 if (DICT_ENTRY_IS_WORD (entry)) {
484 *entry = *entry | RACK_DICT_ENTRY_FOUND;
485 goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->solution_item), FALSE);
491 rack_return_all (rack);
495 if (event->keyval == GDK_space) {
496 rack_return_all (rack);
501 if (event->keyval == GDK_BackSpace) {
502 gboolean found = FALSE;
505 for (i = 0; i < rack->num_tiles; i++) {
506 /* XXX: evil stuff here... please refactor a lot */
507 if (rack->guess[rack->guess_length-1] == rack->tiles[i]->letter &&
508 rack->tiles[i]->guessed &&
509 rack->tiles[i]->x > x)
516 rack_return_tile (rack, rack->tiles[found_index]);
522 /* XXX: event->string is deprecated, but the non-deprecated
523 * input-method stuff (GtkIMContext) is extremely non-obvious to
525 guess_letter = toupper (event->string[0]);
526 for (i = 0; i < rack->num_tiles; i++) {
527 if (guess_letter == rack->tiles[i]->letter &&
528 ! rack->tiles[i]->guessed)
530 guess_tile_position (rack->guess_length, &x, &y);
531 tile_glide_to (rack->tiles[i], x, y);
532 rack->tiles[i]->guessed = TRUE;
533 rack->guess[rack->guess_length++] = guess_letter;
534 rack->guess[rack->guess_length] = '\0';
543 create_window (rack_t *rack)
545 GtkWidget *window, *scrolled_window;
547 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
548 gtk_window_set_default_size (GTK_WINDOW (window), 430, 430);
549 gtk_widget_show (window);
550 g_signal_connect (window, "delete_event",
551 (GtkSignalFunc) on_delete_event, NULL);
553 gtk_widget_add_events (window, GDK_KEY_PRESS_MASK);
554 g_signal_connect (window, "key_press_event",
555 (GtkSignalFunc) on_key_press_event, rack);
557 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
558 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
560 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
561 GTK_POLICY_AUTOMATIC,
562 GTK_POLICY_AUTOMATIC);
563 gtk_widget_show (scrolled_window);
564 gtk_container_add (GTK_CONTAINER (window), scrolled_window);
566 return scrolled_window;
570 create_canvas (GtkWidget *parent, rack_t *rack)
575 canvas = goo_canvas_new ();
576 gtk_widget_set_size_request (canvas, 400, 400);
577 goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 400, 400);
578 gtk_widget_show (canvas);
579 gtk_container_add (GTK_CONTAINER (parent), canvas);
581 root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
583 rack->solution_item = goo_demo_item_new (root,
585 LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD),
586 400 - 20, 400 - (LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD)),
590 return GOO_CANVAS (canvas);
594 main (int argc, char *argv[])
601 gettimeofday (&tv, NULL);
602 srand (tv.tv_sec ^ tv.tv_usec);
604 gtk_init (&argc, &argv);
606 window = create_window (&rack);
608 canvas = create_canvas (window, &rack);
610 rack_init (&rack, canvas);
612 rack_new_game (&rack);