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_FOUND (1<<1)
44 tile_t *tiles[MAX_TILES];
46 char guess[MAX_TILES+1];
52 GooCanvasItem *solution_item;
56 #define LETTER_SIZE 60
60 guess_tile_position (int i, int *x, int *y)
62 *x = 20 + i * (LETTER_SIZE + LETTER_PAD);
67 rack_tile_position (int i, int *x, int *y)
69 guess_tile_position (i, x, y);
70 *y += (LETTER_SIZE + LETTER_PAD);
73 typedef enum dict_paint_cursor_show
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;
80 typedef struct _dict_paint_cursor
88 dict_paint_cursor_show_t show;
89 } dict_paint_cursor_t;
92 dict_paint_action (void *closure, char *word, dict_entry_t *entry)
94 dict_paint_cursor_t *cursor = closure;
95 cairo_t *cr = cursor->cr;
97 int found, show_blanks = FALSE;
99 if (strlen (word) < 3)
102 found = *entry & RACK_DICT_ENTRY_FOUND;
104 cairo_set_source_rgb (cr, 0, 0, 0); /* black */
106 switch (cursor->show) {
107 case DICT_PAINT_CURSOR_SHOW_FOUND:
111 case DICT_PAINT_CURSOR_SHOW_UNFOUND_BLANKS:
116 case DICT_PAINT_CURSOR_SHOW_ALL:
118 cairo_set_source_rgb (cr, 1, 0, 0); /* red */
122 cairo_move_to (cr, cursor->x, cursor->y);
124 int i, length = strlen (word);
125 for (i = 0; i < length; i++)
126 cairo_show_text (cr, "_");
128 cairo_show_text (cr, word);
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;
140 #define SOLUTION_FONT_SIZE 12
141 #define SOLUTION_LINE_HEIGHT (1.5 * SOLUTION_FONT_SIZE)
144 dict_paint (cairo_t *cr, void *closure, double width, double height)
146 rack_t *rack = closure;
147 dict_paint_cursor_t cursor;
151 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
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;
160 cursor.y = cursor.line_height;
162 cursor.max_column_width = 0;
163 cursor.max_y = height;
169 cursor.show = DICT_PAINT_CURSOR_SHOW_ALL;
171 cursor.show = DICT_PAINT_CURSOR_SHOW_FOUND;
172 count += dict_for_each_of_length (&rack->solution,
173 dict_paint_action, &cursor,
176 cursor.show = DICT_PAINT_CURSOR_SHOW_UNFOUND_BLANKS;
177 dict_for_each_of_length (&rack->solution,
178 dict_paint_action, &cursor,
182 } while (count < rack->solution_total);
188 tile_paint (cairo_t *cr, void *closure, double width, double height)
190 tile_t *tile = closure;
192 cairo_pattern_t *gradient;
193 cairo_text_extents_t extents;
194 int rad = (int) MIN (width / 2, height / 2);
198 double spot_angle = M_PI / 4.0;
199 double spot_rad = rad / 2.0;
204 gradient = cairo_pattern_create_radial (cx - spot_rad * cos (spot_angle),
205 cy - spot_rad * sin (spot_angle),
207 cx - spot_rad * cos (spot_angle),
208 cy - spot_rad * sin (spot_angle),
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);
213 cairo_set_source (cr, gradient);
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);
226 string[0] = tile->letter;
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;
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);
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);
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);
248 tile_glide_to (tile_t *tile, int x, int y)
250 goo_canvas_item_animate (tile->item, x, y,
253 GOO_CANVAS_ANIMATE_FREEZE);
259 tile_create (GooCanvasItem *parent,
260 char letter, int rack_index)
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,
270 LETTER_SIZE, LETTER_SIZE,
274 tile->guessed = FALSE;
280 on_delete_event (GtkWidget *window,
282 gpointer unused_data)
288 rand_within (int num_values)
290 return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
294 shuffle (int *array, int length)
298 for (i = 0; i < length; i++)
300 r = i + rand_within (length - i);
308 rack_init (rack_t *rack,
312 GooCanvasItem *root = goo_canvas_get_root_item (canvas);
314 for (i = 0; i < MAX_TILES; i++)
315 rack->tiles[i] = NULL;
318 rack->guess_length = 0;
321 bag_init (&rack->bag);
323 dict_init (&rack->dict);
324 dict_add_words_from_file (&rack->dict, "words.txt");
326 dict_init (&rack->solution);
327 rack->solution_total = 0;
329 for (i = 0; i < MAX_TILES; i++)
330 rack->tiles[i] = tile_create (root, 'A', i);
335 rack_new_game (rack_t *rack)
338 char word[MAX_TILES + 1];
339 int length = MAX_TILES;
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
346 bag_shuffle (&rack->bag);
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. */
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] = '?';
357 /* Re-examine ith element */
362 for (i = 0; i + length <= bottom + 1; i++) {
363 memcpy (word, &rack->bag.tiles[i], length);
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,
379 rack->solution_total = dict_count (&rack->solution);
380 goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->solution_item), FALSE);
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);
386 rack->num_tiles = length;
390 rack_shuffle (rack_t *rack)
392 int indices[MAX_TILES];
395 for (i = 0; i < rack->num_tiles; i++)
398 shuffle (indices, rack->num_tiles);
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);
410 rack_return_tile (rack_t *rack, tile_t *tile)
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';
422 rack_return_all (rack_t *rack)
426 for (i = 0; i < rack->num_tiles; i++) {
427 if (rack->tiles[i]->guessed)
428 rack_return_tile (rack, rack->tiles[i]);
430 rack->guess_length = 0;
431 rack->guess[0] = '\0';
435 on_key_press_event (GtkWidget *widget,
441 rack_t *rack = user_data;
443 if (event->state & GDK_CONTROL_MASK &&
444 event->keyval == GDK_c)
447 goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->solution_item), FALSE);
450 if (event->keyval == GDK_Return) {
454 rack_new_game (rack);
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);
467 rack_return_all (rack);
471 if (event->keyval == GDK_space) {
472 rack_return_all (rack);
477 if (event->keyval == GDK_BackSpace) {
478 gboolean found = FALSE;
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)
492 rack_return_tile (rack, rack->tiles[found_index]);
498 /* XXX: event->string is deprecated, but the non-deprecated
499 * input-method stuff (GtkIMContext) is extremely non-obvious to
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)
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';
519 create_window (rack_t *rack)
521 GtkWidget *window, *scrolled_window;
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);
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);
533 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
534 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
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);
542 return scrolled_window;
546 create_canvas (GtkWidget *parent, rack_t *rack)
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);
557 root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
559 rack->solution_item = goo_demo_item_new (root,
561 LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD),
562 400 - 20, 480 - (LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD)),
566 return GOO_CANVAS (canvas);
570 main (int argc, char *argv[])
577 gettimeofday (&tv, NULL);
578 srand (tv.tv_sec ^ tv.tv_usec);
580 gtk_init (&argc, &argv);
582 window = create_window (&rack);
584 canvas = create_canvas (window, &rack);
586 rack_init (&rack, canvas);
588 rack_new_game (&rack);