#include "word-game.h"
#include "demo-item.h"
+#define RACK_DICT_ENTRY_FOUND (1<<1)
#define MAX_TILES 7
typedef struct _tile
char guess[MAX_TILES+1];
int guess_length;
dict_t *solution;
+ GooCanvasItem *solution_item;
} rack_t;
-static rack_t the_rack;
-
#define LETTER_SIZE 60
#define LETTER_PAD 5
} dict_paint_cursor_t;
static void
-dict_paint_action (void *closure, char *word)
+dict_paint_action (void *closure, char *word, dict_entry_t *entry)
{
dict_paint_cursor_t *cursor = closure;
cairo_t *cr = cursor->cr;
double new_x, new_y;
+ if (strlen (word) < 3)
+ return;
+
cairo_move_to (cr, cursor->x, cursor->y);
- cairo_show_text (cr, word);
+ if (*entry & RACK_DICT_ENTRY_FOUND) {
+ cairo_show_text (cr, word);
+ } else {
+ int i, length = strlen (word);
+ for (i = 0; i < length; i++)
+ cairo_show_text (cr, "_");
+ }
cairo_get_current_point (cr, &new_x, &new_y);
if (new_x > cursor->max_column_width)
cursor->max_column_width = new_x;
}
}
+#define SOLUTION_FONT_SIZE 12
+#define SOLUTION_LINE_HEIGHT (1.5 * SOLUTION_FONT_SIZE)
+
static void
dict_paint (cairo_t *cr, void *closure, double width, double height)
{
cursor.cr = cr;
- cairo_set_font_size (cr, 12);
- cursor.line_height = 14;
+ cairo_select_font_face (cr, "mono", 0, 0);
+ cairo_set_font_size (cr, SOLUTION_FONT_SIZE);
+ cursor.line_height = SOLUTION_LINE_HEIGHT;
cursor.x = 0;
cursor.y = 0;
}
static void
-rack_init (rack_t *rack, GooCanvasItem *parent, char *word, dict_t *solution)
+rack_init (rack_t *rack,
+ dict_t *solution)
{
int i;
- for (i = 0; i < MIN (MAX_TILES, strlen (word)); i++)
- rack->tiles[i] = tile_create (parent, word[i], i);
- rack->num_tiles = i;
- while (i < MAX_TILES)
+ for (i = 0; i < MAX_TILES; i++)
rack->tiles[i] = NULL;
+ rack->num_tiles = 0;
+
rack->guess_length = 0;
rack->solution = solution;
+ rack->solution_item = NULL;
+}
+
+static void
+rack_create_tiles (rack_t *rack,
+ GooCanvas *canvas,
+ char *word)
+{
+ int i;
+
+ GooCanvasItem *root = goo_canvas_get_root_item (canvas);
+
+ for (i = 0; i < MIN (MAX_TILES, strlen (word)); i++)
+ rack->tiles[i] = tile_create (root, word[i], i);
+ rack->num_tiles = i;
}
static gboolean
if (event->keyval == GDK_Return) {
dict_entry_t *entry;
- entry = dict_lookup (rack->solution, rack->guess);
- if (DICT_ENTRY_IS_WORD (entry))
- printf ("%s is good\n", rack->guess);
- else
- printf ("%s is not a word\n", rack->guess);
+ if (rack->guess_length >= 3) {
+ entry = dict_lookup (rack->solution, rack->guess);
+ if (DICT_ENTRY_IS_WORD (entry)) {
+ *entry = *entry | RACK_DICT_ENTRY_FOUND;
+ goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->solution_item), FALSE);
+ }
+ }
rack_return_all (rack);
return TRUE;
}
}
static GtkWidget *
-create_window (void)
+create_window (rack_t *rack)
{
GtkWidget *window, *scrolled_window;
gtk_widget_add_events (window, GDK_KEY_PRESS_MASK);
g_signal_connect (window, "key_press_event",
- (GtkSignalFunc) on_key_press_event, &the_rack);
+ (GtkSignalFunc) on_key_press_event, rack);
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
return scrolled_window;
}
-static void
-create_canvas (GtkWidget *parent, char *word, dict_t *solution)
+static GooCanvas *
+create_canvas (GtkWidget *parent, rack_t *rack)
{
GtkWidget *canvas;
GooCanvasItem *root;
root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
- rack_init (&the_rack, root, word, solution);
+ rack->solution_item = goo_demo_item_new (root,
+ LETTER_PAD + SOLUTION_LINE_HEIGHT,
+ LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD),
+ 400, 400 - (2 * (LETTER_SIZE + 2 * LETTER_PAD)),
+ dict_paint, rack->solution,
+ NULL);
- goo_demo_item_new (root,
- LETTER_PAD,
- LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD),
- 400, 400 - (2 * (LETTER_SIZE + 2 * LETTER_PAD)),
- dict_paint,
- solution, NULL);
+ return GOO_CANVAS (canvas);
}
int
dict_t dict, solution;
struct timeval tv;
bag_t bag;
- char rack[8];
+ rack_t rack;
+ char word[8];
int i;
+ char *draw;
GtkWidget *window;
+ GooCanvas *canvas;
gettimeofday (&tv, NULL);
srand (tv.tv_sec ^ tv.tv_usec);
bag_init (&bag);
bag_shuffle (&bag);
- memcpy (rack, bag.tiles, 7);
- rack[7] = '\0';
-
- for (i = 0; i < 7; i++)
- rack[i] = toupper (rack[i]);
+ /* Keep drawing until we get 7 non-blank tiles */
+ i = 0;
+ draw = bag.tiles;
+ while (i < 7) {
+ if (*draw != '?')
+ word[i++] = toupper (*draw);
+ draw++;
+ }
dict_init (&dict);
dict_add_words_from_file (&dict, "words.txt");
dict_init (&solution);
- subanagram_expand (rack, &dict, &solution);
+ subanagram_expand (word, &dict, &solution);
gtk_init (&argc, &argv);
- window = create_window ();
- create_canvas (window, rack, &solution);
+ rack_init (&rack, &solution);
+
+ window = create_window (&rack);
+
+ canvas = create_canvas (window, &rack);
+
+ rack_create_tiles (&rack, canvas, word);
gtk_main ();