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"
43 tile_t *tiles[MAX_TILES];
47 static rack_t the_rack;
49 static char the_guess[MAX_TILES];
50 static int the_guess_index = 0;
52 #define LETTER_SIZE 60
56 rack_tile_position (int i, int *x, int *y)
58 *x = 20 + i * (LETTER_SIZE + LETTER_PAD);
63 guess_tile_position (int i, int *x, int *y)
65 rack_tile_position (i, x, y);
66 *y -= (LETTER_SIZE + LETTER_PAD);
70 tile_create (GooCanvasItem *parent,
71 char letter, int rack_index)
75 tile = g_malloc (sizeof (tile_t));
76 tile->letter = tolower (letter);
77 tile->rack_index = rack_index;
78 rack_tile_position (rack_index, &tile->x, &tile->y);
79 tile->item = goo_demo_item_new (parent,
84 tile->guessed = FALSE;
90 tile_glide_to (tile_t *tile, int x, int y)
92 goo_demo_item_glide_to (tile->item, x, y);
98 on_delete_event (GtkWidget *window,
100 gpointer unused_data)
106 rand_within (int num_values)
108 return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
112 shuffle (int *array, int length)
116 for (i = 0; i < length; i++)
118 r = i + rand_within (length - i);
126 rack_init (rack_t *rack, GooCanvasItem *parent, char *word)
130 for (i = 0; i < MIN (MAX_TILES, strlen (word)); i++)
131 rack->tiles[i] = tile_create (parent, word[i], i);
133 while (i < MAX_TILES)
134 rack->tiles[i] = NULL;
138 rack_shuffle (rack_t *rack)
140 int indices[MAX_TILES];
143 for (i = 0; i < rack->num_tiles; i++)
146 shuffle (indices, rack->num_tiles);
148 for (i = 0; i < rack->num_tiles; i++) {
149 rack->tiles[i]->rack_index = indices[i];
150 rack_tile_position (indices[i], &x, &y);
151 tile_glide_to (rack->tiles[i], x, y);
158 on_key_press_event (GtkWidget *widget,
164 rack_t *rack = user_data;
166 if (event->keyval == GDK_space) {
167 for (i = 0; i < rack->num_tiles; i++) {
168 if (rack->tiles[i]->guessed) {
169 rack_tile_position (rack->tiles[i]->rack_index, &x, &y);
170 tile_glide_to (rack->tiles[i], x, y);
171 rack->tiles[i]->guessed = FALSE;
179 if (event->keyval == GDK_BackSpace) {
180 gboolean found = FALSE;
183 for (i = 0; i < rack->num_tiles; i++) {
184 /* XXX: evil stuff here... please refactor a lot */
185 if (the_guess[the_guess_index-1] == rack->tiles[i]->letter &&
186 rack->tiles[i]->guessed &&
187 rack->tiles[i]->x > x)
194 rack_tile_position (rack->tiles[found_index]->rack_index, &x, &y);
195 tile_glide_to (rack->tiles[found_index], x, y);
196 rack->tiles[found_index]->guessed = FALSE;
203 /* XXX: event->string is deprecated, but the non-deprecated
204 * input-method stuff (GtkIMContext) is extremely non-obvious to
206 guess_letter = tolower (event->string[0]);
207 for (i = 0; i < rack->num_tiles; i++) {
208 if (guess_letter == rack->tiles[i]->letter &&
209 ! rack->tiles[i]->guessed)
211 guess_tile_position (the_guess_index, &x, &y);
212 tile_glide_to (rack->tiles[i], x, y);
213 rack->tiles[i]->guessed = TRUE;
214 the_guess[the_guess_index++] = guess_letter;
225 GtkWidget *window, *scrolled_window;
227 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
228 gtk_window_set_default_size (GTK_WINDOW (window), 500, 500);
229 gtk_widget_show (window);
230 g_signal_connect (window, "delete_event",
231 (GtkSignalFunc) on_delete_event, NULL);
233 gtk_widget_add_events (window, GDK_KEY_PRESS_MASK);
234 g_signal_connect (window, "key_press_event",
235 (GtkSignalFunc) on_key_press_event, &the_rack);
237 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
238 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
240 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
241 GTK_POLICY_AUTOMATIC,
242 GTK_POLICY_AUTOMATIC);
243 gtk_widget_show (scrolled_window);
244 gtk_container_add (GTK_CONTAINER (window), scrolled_window);
246 return scrolled_window;
250 create_canvas (GtkWidget *parent, char *word)
255 canvas = goo_canvas_new ();
256 gtk_widget_set_size_request (canvas, 400, 400);
257 goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 400, 400);
258 gtk_widget_show (canvas);
259 gtk_container_add (GTK_CONTAINER (parent), canvas);
261 root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
263 rack_init (&the_rack, root, word);
267 main (int argc, char *argv[])
275 gettimeofday (&tv, NULL);
276 srand (tv.tv_sec ^ tv.tv_usec);
281 memcpy (rack, bag.tiles, 7);
284 for (i = 0; i < 7; i++)
285 rack[i] = toupper (rack[i]);
287 gtk_init (&argc, &argv);
288 window = create_window ();
290 create_canvas (window, rack);