int num_tiles;
char guess[MAX_TILES+1];
int guess_length;
- dict_t *solution;
+ bag_t bag;
+ dict_t dict;
+ dict_t solution;
+ int solution_total;
GooCanvasItem *solution_item;
+ gboolean done;
} rack_t;
#define LETTER_SIZE 60
*y += (LETTER_SIZE + LETTER_PAD);
}
+typedef enum dict_paint_cursor_show
+{
+ DICT_PAINT_CURSOR_SHOW_FOUND,
+ DICT_PAINT_CURSOR_SHOW_UNFOUND_BLANKS,
+ DICT_PAINT_CURSOR_SHOW_ALL
+} dict_paint_cursor_show_t;
+
typedef struct _dict_paint_cursor
{
cairo_t *cr;
int y;
int max_column_width;
int max_y;
+ dict_paint_cursor_show_t show;
} dict_paint_cursor_t;
static void
dict_paint_cursor_t *cursor = closure;
cairo_t *cr = cursor->cr;
double new_x, new_y;
+ int found, show_blanks = FALSE;
if (strlen (word) < 3)
return;
+ found = *entry & RACK_DICT_ENTRY_FOUND;
+
+ cairo_set_source_rgb (cr, 0, 0, 0); /* black */
+
+ switch (cursor->show) {
+ case DICT_PAINT_CURSOR_SHOW_FOUND:
+ if (! found)
+ return;
+ break;
+ case DICT_PAINT_CURSOR_SHOW_UNFOUND_BLANKS:
+ if (found)
+ return;
+ show_blanks = TRUE;
+ break;
+ case DICT_PAINT_CURSOR_SHOW_ALL:
+ if (! found)
+ cairo_set_source_rgb (cr, 1, 0, 0); /* red */
+ break;
+ }
+
cairo_move_to (cr, cursor->x, cursor->y);
- if (*entry & RACK_DICT_ENTRY_FOUND) {
- cairo_show_text (cr, word);
- } else {
+ if (show_blanks) {
int i, length = strlen (word);
for (i = 0; i < length; i++)
cairo_show_text (cr, "_");
+ } else {
+ cairo_show_text (cr, word);
}
cairo_get_current_point (cr, &new_x, &new_y);
if (new_x > cursor->max_column_width)
cursor->y += cursor->line_height;
if (cursor->y > cursor->max_y) {
cursor->x = cursor->max_column_width + cursor->line_height / 2;
- cursor->y = 0;
+ cursor->y = cursor->line_height;
}
}
static void
dict_paint (cairo_t *cr, void *closure, double width, double height)
{
- dict_t *dict = closure;
+ rack_t *rack = closure;
dict_paint_cursor_t cursor;
+ int length, count;
cairo_save (cr);
cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
cursor.line_height = SOLUTION_LINE_HEIGHT;
cursor.x = 0;
- cursor.y = 0;
+ cursor.y = cursor.line_height;
cursor.max_column_width = 0;
cursor.max_y = height;
- dict_for_each_by_length (dict,
- dict_paint_action,
- &cursor);
+ length = 1;
+ count = 0;
+ do {
+ if (rack->done)
+ cursor.show = DICT_PAINT_CURSOR_SHOW_ALL;
+ else
+ cursor.show = DICT_PAINT_CURSOR_SHOW_FOUND;
+ count += dict_for_each_of_length (&rack->solution,
+ dict_paint_action, &cursor,
+ length, length);
+ if (! rack->done) {
+ cursor.show = DICT_PAINT_CURSOR_SHOW_UNFOUND_BLANKS;
+ dict_for_each_of_length (&rack->solution,
+ dict_paint_action, &cursor,
+ length, length);
+ }
+ length++;
+ } while (count < rack->solution_total);
cairo_restore (cr);
}
static void
rack_init (rack_t *rack,
- dict_t *solution)
+ GooCanvas *canvas)
{
int i;
+ GooCanvasItem *root = goo_canvas_get_root_item (canvas);
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;
+ rack->done = FALSE;
+
+ bag_init (&rack->bag);
+
+ dict_init (&rack->dict);
+ dict_add_words_from_file (&rack->dict, "words.txt");
+
+ dict_init (&rack->solution);
+ rack->solution_total = 0;
+
+ for (i = 0; i < MAX_TILES; i++)
+ rack->tiles[i] = tile_create (root, 'A', i);
+ rack->num_tiles = 0;
}
static void
-rack_create_tiles (rack_t *rack,
- GooCanvas *canvas,
- char *word)
+rack_new_game (rack_t *rack)
{
int i;
+ char *draw;
+ char word[8];
- GooCanvasItem *root = goo_canvas_get_root_item (canvas);
+ /* Clean up any remnants from the last game */
+ dict_fini (&rack->solution);
- for (i = 0; i < MIN (MAX_TILES, strlen (word)); i++)
- rack->tiles[i] = tile_create (root, word[i], i);
- rack->num_tiles = i;
+ bag_shuffle (&rack->bag);
+
+ /* Keep drawing until we get 7 non-blank tiles */
+ i = 0;
+ draw = rack->bag.tiles;
+ while (i < 7) {
+ if (*draw != '?')
+ word[i++] = *draw;
+ draw++;
+ }
+ word[7] = '\0';
+
+ for (i = 0; i < 7; i++) {
+ rack->tiles[i]->letter = toupper (word[i]);
+ goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->tiles[i]->item), FALSE);
+ }
+ rack->num_tiles = 7;
+
+ dict_init (&rack->solution);
+ subanagram_expand (word, &rack->dict, &rack->solution);
+ rack->solution_total = dict_count (&rack->solution);
+ goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->solution_item), FALSE);
}
static gboolean
char guess_letter;
rack_t *rack = user_data;
+ if (event->state & GDK_CONTROL_MASK &&
+ event->keyval == GDK_c)
+ {
+ rack->done = TRUE;
+ goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->solution_item), FALSE);
+ }
+
if (event->keyval == GDK_Return) {
dict_entry_t *entry;
+ if (rack->done) {
+ rack->done = FALSE;
+ rack_new_game (rack);
+ return TRUE;
+ }
if (rack->guess_length >= 3) {
- entry = dict_lookup (rack->solution, rack->guess);
+ 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);
+ } else {
+ printf ("\a");
+ fflush (stdout);
}
}
rack_return_all (rack);
/* XXX: event->string is deprecated, but the non-deprecated
* input-method stuff (GtkIMContext) is extremely non-obvious to
* use. */
- guess_letter = tolower (event->string[0]);
+ guess_letter = toupper (event->string[0]);
for (i = 0; i < rack->num_tiles; i++) {
if (guess_letter == rack->tiles[i]->letter &&
! rack->tiles[i]->guessed)
GooCanvasItem *root;
canvas = goo_canvas_new ();
- gtk_widget_set_size_request (canvas, 400, 400);
- goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 400, 400);
+ gtk_widget_set_size_request (canvas, 400, 480);
+ goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 400, 480);
gtk_widget_show (canvas);
gtk_container_add (GTK_CONTAINER (parent), canvas);
root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
rack->solution_item = goo_demo_item_new (root,
- LETTER_PAD + SOLUTION_LINE_HEIGHT,
+ 20,
LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD),
- 400, 400 - (2 * (LETTER_SIZE + 2 * LETTER_PAD)),
- dict_paint, rack->solution,
+ 400 - 20, 480 - (LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD)),
+ dict_paint, rack,
NULL);
return GOO_CANVAS (canvas);
int
main (int argc, char *argv[])
{
- dict_t dict, solution;
struct timeval tv;
- bag_t bag;
rack_t rack;
- char word[8];
- int i;
GtkWidget *window;
GooCanvas *canvas;
gettimeofday (&tv, NULL);
srand (tv.tv_sec ^ tv.tv_usec);
- bag_init (&bag);
- bag_shuffle (&bag);
-
- memcpy (word, bag.tiles, 7);
- word[7] = '\0';
-
- for (i = 0; i < 7; i++)
- word[i] = toupper (word[i]);
-
- dict_init (&dict);
- dict_add_words_from_file (&dict, "words.txt");
-
- dict_init (&solution);
- subanagram_expand (word, &dict, &solution);
-
gtk_init (&argc, &argv);
- rack_init (&rack, &solution);
-
window = create_window (&rack);
canvas = create_canvas (window, &rack);
- rack_create_tiles (&rack, canvas, word);
+ rack_init (&rack, canvas);
+
+ rack_new_game (&rack);
gtk_main ();