]> git.cworth.org Git - kub/blob - kub.c
Allow selection of a tile by including any corner
[kub] / kub.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include <gtk/gtk.h>
5 #include <librsvg/rsvg.h>
6 #include <librsvg/rsvg-cairo.h>
7
8 /*
9  *   TILE_NUMBER_X_OFFSET = 3
10  * //
11  * || TILE_NUMBER_WIDTH = 34
12  * ||/   /
13  * ||   |  TILE_WIDTH = 40
14  * ||   | /
15  * |     |
16  * +-----+ - TILE_NUMBER_Y_OFFSET = 3   -
17  * |+---+| -                            |
18  * ||   ||                              |
19  * ||   ||   TILE_NUMBER_HEIGHT = 24    |
20  * |+---+| -                            |
21  * |  _  | _                            |-TILE_HEIGHT = 50
22  * | / \ |                              |
23  * ||   ||   TILE_SUN_HEIGHT = 20       |
24  * | \_/ | _                            |
25  * |     |   TILE_SUN_Y_OFFSET = 3      |
26  * +-----+ -                            -
27  * ||   | TILE_SUN_WIDTH = 20
28  * ||
29  * || TILE_SUN_X_OFFSET = 10
30  */
31 #define TILE_WIDTH              40
32 #define TILE_HEIGHT             50
33
34 #define TILE_NUMBER_X_OFFSET    3
35 #define TILE_NUMBER_Y_OFFSET    3
36 #define TILE_NUMBER_WIDTH       34
37 #define TILE_NUMBER_HEIGHT      24
38
39 #define TILE_SUN_X_OFFSET       10
40 #define TILE_SUN_Y_OFFSET       3
41 #define TILE_SUN_WIDTH          20
42 #define TILE_SUN_HEIGHT         20
43
44 #define FATAL_ERROR(msg)                                                \
45     do { fprintf (stderr, "Error: %s\n", msg); exit (1); } while (0)
46
47 char *colors[] = {"Black", "Blue", "Red", "Yellow"};
48
49 typedef enum {BLACK, BLUE, RED, YELLOW} color_t;
50
51 typedef struct tile {
52     color_t color;
53     int number;
54     int x;
55     int y;
56 } tile_t;
57
58 #define DECK_MAX_TILES 104
59
60 typedef struct deck {
61     tile_t tiles[DECK_MAX_TILES];
62     int num_tiles;
63 } deck_t;
64
65 #define TILE_GROUP_MAX_TILES DECK_MAX_TILES
66
67 typedef struct tile_group {
68     tile_t tiles[TILE_GROUP_MAX_TILES];
69     int num_tiles;
70 } tile_group_t;
71
72 #define BOARD_MAX_TILE_GROUPS (DECK_MAX_TILES / 3)
73
74 typedef struct board {
75     tile_group_t groups[BOARD_MAX_TILE_GROUPS];
76     int num_groups;
77 } board_t;
78
79 typedef struct player {
80     tile_group_t hand;
81 } player_t;
82
83 typedef struct selection_box {
84     int x1, x2, y1, y2, visible;
85 } selection_box_t;
86
87 #define GAME_MAX_PLAYERS 4
88 #define GAME_WINDOW_DEFAULT_WIDTH  800
89 #define GAME_WINDOW_DEFAULT_HEIGHT 600
90
91 typedef struct game {
92     player_t players[GAME_MAX_PLAYERS];
93     int num_players;
94     board_t board;
95     deck_t deck;
96     selection_box_t selection_box;
97     RsvgHandle *blanktile;
98
99     int current_tile;
100     int select_mode;
101     int diff_x, diff_y;
102     int click_x, click_y;
103     int release_x, release_y;    /*Currently unused*/
104 } game_t;
105
106 static void selection_box_init(selection_box_t *box)
107 {
108     box->x1 = 0;
109     box->x2 = 0;
110     box->y1 = 0;
111     box->y2 = 0;
112     box->visible = 0;
113 }
114
115 static void selection_box_draw(selection_box_t *box, cairo_t *cr)
116 {
117     cairo_rectangle (cr, box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1);
118     cairo_set_source_rgba(cr, 0.0, 0.1, 0.2, 0.5);
119     cairo_fill (cr);
120 }
121
122 static void tile_init (tile_t *tile, color_t color, int number)
123 {
124     tile->color = color;
125     tile->number = number;
126     tile->x = 0;
127     tile->y = 0;
128 }
129
130 static void tile_set_x_y (tile_t *tile, int x, int y)
131 {
132     tile->x = x;
133     tile->y = y;
134 }
135
136 static void tile_print(tile_t tile)
137 {
138     printf("%6s %2d\n", colors[tile.color], tile.number + 1);
139 }
140
141 static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr, GdkRegion *region)
142 {
143     char number_string[3];
144     int len;
145     GdkRectangle rectangle;
146
147     rectangle.x = tile->x - 1;
148     rectangle.y = tile->y - 1;
149     rectangle.width = TILE_WIDTH + 2;
150     rectangle.height = TILE_HEIGHT + 2;
151     if (gdk_region_rect_in (region, &rectangle) == GDK_OVERLAP_RECTANGLE_OUT)
152         return;
153
154     len = snprintf (number_string, 3, "%d", tile->number + 1);
155     if (len < 0 || len >= 3)
156         FATAL_ERROR ("snprintf failed");
157
158     cairo_save(cr);
159     cairo_translate(cr, tile->x, tile->y);
160     rsvg_handle_render_cairo (game->blanktile, cr);
161
162     if (tile->color == BLACK)
163         cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
164     if (tile->color == BLUE)
165         cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
166     if (tile->color == RED)
167         cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
168     if (tile->color == YELLOW)
169         cairo_set_source_rgb (cr, 1.0, .843, 0.0);
170     if (tile->number + 1 > 9)
171         cairo_move_to (cr, 1, 25);
172     else
173         cairo_move_to (cr, 10, 25);
174     cairo_set_font_size(cr, 25);
175     cairo_show_text (cr, number_string);
176
177     cairo_restore(cr);
178 }
179
180 static void tile_group_init(tile_group_t *tile_group)
181 {
182     tile_group->num_tiles = 0;
183 }
184
185 static void board_init(board_t *board)
186 {
187     int i;
188     board->num_groups = 0;
189
190     for (i = 0; i <= BOARD_MAX_TILE_GROUPS; ++i)
191     {
192         tile_group_init(&board->groups[i]);
193     }
194 }
195
196 static void player_init(player_t *player)
197 {
198     tile_group_init(&player->hand);
199 }
200
201
202 /* If tile_one < tile_two, then return value will be negative
203    if they are equal, 0 will be returned,
204    if tile_one > tile_two, then return value will be positive */
205 static int tile_compare(const void *one, const void *two)
206 {
207     const tile_t *tile_one = one;
208     const tile_t *tile_two = two;
209     return tile_one->number - tile_two->number;
210 }
211
212 static int tile_group_is_run_one(tile_group_t *tile_group)
213 {
214     int i;
215     qsort (&tile_group->tiles[0], tile_group->num_tiles,
216            sizeof (tile_t), tile_compare);
217
218     if (tile_group->num_tiles > 13 || tile_group->num_tiles < 3)
219     {
220         printf("fail run - invalid num tiles; ");
221         return 0;
222     }
223     for (i = 0; i < tile_group->num_tiles - 1; ++i)
224     {
225         if(tile_group->tiles[i].color != tile_group->tiles[i + 1].color)
226         {
227             printf("fail run - colors don't match; ");
228             return 0;
229         }
230         if( tile_group->tiles[i].number != tile_group->tiles[i + 1].number -1 &&
231             i+1 != tile_group->num_tiles)
232         {
233             printf("fail run - invalid number sequence; ");
234             return 0;
235         }
236     }
237     return 1;
238 }
239
240
241 static int tile_group_is_run_two(tile_group_t *tile_group)
242 {
243     int i;
244     int lowest = 14, highest = 0;
245     color_t run_color;
246
247     /* By definition, a run must have at least 3 tiles. Also, it's
248      * impossible for any group of tiles with more than 13 tiles to be
249      * a run, (there are only 13 unique numbers so a group with more
250      * than 13 tiles must have some duplicates).
251      */
252     if (tile_group->num_tiles < 3 || tile_group->num_tiles > 13)
253     {
254         return 0;
255     }
256
257     /* Loop through all tiles in the group, ensuring that they are all
258      * the same color and finding the highest and lowest number in the
259      * group. */
260     run_color = tile_group->tiles[0].color;
261
262     for (i = 0; i < tile_group->num_tiles; i++)
263     {
264         if (tile_group->tiles[i].color != run_color)
265             return 0;
266         if (tile_group->tiles[i].number > highest)
267         {
268             highest = tile_group->tiles[i].number;
269         }
270         if (tile_group->tiles[i].number < lowest)
271         {
272             lowest = tile_group->tiles[i].number;
273         }
274     }
275
276     /* For a run, the difference between the highest and lowest tiles
277      * will always be one less than the number of tiles in the
278      * group. If not then we know it's not a run.
279      */
280     if (highest - lowest != tile_group->num_tiles - 1)
281     {
282         return 0;
283     }
284
285     /* XXX: There's a bug here. We're guessing that at this point
286      * anything we're looking at must be a run. This would be correct
287      * if there were no duplicate tiles, but since there are
288      * duplicates this us quite broken. For example consider two
289      * sequences of entirely red tiles:
290      *
291      * This is a run:   1, 2, 3, 4
292      * But this is not: 1, 3, 4, 4
293      *
294      * As currently written, this function will consider both of these
295      * groups to be a run. One possible fix is to throw away the
296      * highest - lowest heuristic and instead simply sort the tiles up
297      * front and ensure the difference between each adjacent pair is
298      * exactly 1.
299      */
300     return 1;
301 }
302
303 static int tile_group_is_set(tile_group_t *tile_group)
304 {
305     int i;
306     color_t seen_color[4];
307     for (i = 0; i < 4; i++)
308         seen_color[i] = 0;
309
310     if (tile_group->num_tiles > 4 || tile_group->num_tiles < 3)
311     {
312         printf("fail set - invalid num tiles; ");
313         return 0;
314     }
315     for (i = 0; i <= tile_group->num_tiles - 1; ++i)
316     {
317         if (tile_group->tiles[i].number != tile_group->tiles[i + 1].number &&
318             i+1 != tile_group->num_tiles)
319         {
320             printf("fail set - numbers don't match; ");
321             return 0;
322         }
323         seen_color[tile_group->tiles[i].color] += 1;
324     }
325     for (i = 0; i < 4; i++)
326     {
327         if (seen_color[i] > 1)
328         {
329             printf("fail set - repeat color; ");
330             return 0;
331         }
332     }
333     return 1;
334 }
335
336 static void deck_deal(game_t *game, deck_t *deck)
337 {
338     tile_t temp;
339     int rand_tile;
340     int i, j, newline;
341
342     printf ("How many players(1-4) should I deal in? ");
343     game->num_players = getchar();
344     if (game->num_players == EOF)
345     {
346         printf ("\nGoodbye.\n");
347         exit (1);
348     }
349     newline = getchar();
350     game->num_players -= '0';
351
352     for (i = 0; i < game->num_players; ++i)
353     {
354         for (j = 0; j < 14; ++j)
355         {
356             rand_tile = ((deck->num_tiles + 1.0) * rand()) / (RAND_MAX + 1.0);
357             temp = deck->tiles[rand_tile];
358             deck->tiles[rand_tile] = deck->tiles[deck->num_tiles - 1];
359             game->players[i].hand.tiles[j] = temp;
360             deck->num_tiles -= 1;
361             game->players[i].hand.num_tiles += 1;
362         }
363     }
364     printf ("Game dealt for %d player(s)\n", game->num_players);
365 }
366
367 static void deck_init(deck_t *deck)
368 {
369     int h, i, j;
370     deck->num_tiles = 0;
371     for (h = 0; h <= 1; ++h)
372     {
373         for (i = 0; i <= 3; ++i)
374         {
375             for (j = 0; j <= 12; ++j)
376             {
377                 tile_init (&deck->tiles[deck->num_tiles++], i, j);
378                 printf ("There are %d tiles in the deck\n", deck->num_tiles);
379             }
380         }
381     }
382 }
383
384 static void deck_shuffle(deck_t *deck)
385 {
386     tile_t temp;
387     int rand_tile;
388     int last;
389     for (last = deck->num_tiles; last > 0; --last)
390     {
391         rand_tile = ((last + 1.0) * rand()) / (RAND_MAX + 1.0);
392         temp = deck->tiles[rand_tile];
393         deck->tiles[rand_tile] = deck->tiles[last - 1];
394         deck->tiles[last - 1] = temp;
395     }
396 }
397
398 static void deck_print(deck_t *deck)
399 {
400     int h, i, j;
401     for (h = 0; h < 2; ++h)
402     {
403         for (i = 0; i < 4; ++i)
404         {
405             for (j = 0; j < 13; ++j)
406             {
407                 tile_print(deck->tiles[j + (i * 13) + (h * 52)]);
408             }
409         }
410     }
411     printf ("There are %d tiles in the deck\n" , deck->num_tiles);
412 }
413
414 static void deck_spread(deck_t *deck)
415 {
416     int i, j;
417     for (i = 0; i < 8; i++)
418     {
419         for (j = 0; j < 13; j++)
420         {
421             deck->tiles[j + (i * 13)].x = j * 50;
422             deck->tiles[j + (i * 13)].y = i * 60;
423         }
424     }
425 }
426
427 static void deck_draw(game_t *game, cairo_t *cr, GdkRegion *region)
428 {
429     int i;
430     for (i = 0; i < game->deck.num_tiles; i++)
431     {
432         tile_draw(game, &game->deck.tiles[i], cr, region);
433     }
434 }
435
436 static void hand_print(game_t *game, int player)
437 {
438     int i;
439     for (i = 0; i < game->players[player].hand.num_tiles; i++)
440     {
441         tile_print(game->players[player].hand.tiles[i]);
442     }
443 }
444
445 static void hand_draw(game_t *game, int player, cairo_t *cr, GdkRegion *region)
446 {
447     int i;
448     int gwdw = GAME_WINDOW_DEFAULT_WIDTH;
449     int gwdh = GAME_WINDOW_DEFAULT_HEIGHT;
450     for (i = 0; i < game->players[player].hand.num_tiles; i++)
451     {
452         tile_set_x_y(&game->players[player].hand.tiles[i],
453                      ((gwdw / game->players[player].hand.num_tiles)) * i,
454                      (gwdh - TILE_HEIGHT - 6) );
455     }
456     for (i = 0; i < game->players[player].hand.num_tiles; i++)
457     {
458         tile_draw(game, &game->players[player].hand.tiles[i], cr, region);
459     }
460 }
461
462 static void game_init(game_t *game)
463 {
464     int i;
465     GError *error = NULL;
466
467     game->num_players = 0;
468
469     for (i = 0; i < GAME_MAX_PLAYERS; ++i)
470     {
471         player_init(&game->players[i]);
472         game->num_players += 1;
473     }
474     
475     selection_box_init(&game->selection_box);
476     board_init(&game->board);
477     deck_init(&game->deck);
478     deck_shuffle(&game->deck);
479
480     game->blanktile = rsvg_handle_new_from_file ("tiles/blanktile.svg", &error);
481     if (error)
482         FATAL_ERROR (error->message);
483
484     game->current_tile = game->deck.num_tiles - 1;
485     game->select_mode = 1;
486
487     game->diff_x = game->diff_y = 0;
488 }
489
490 static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game)
491 {
492     cairo_t *cr;
493
494     cr = gdk_cairo_create (widget->window);
495
496     deck_draw(game, cr, event->region);
497
498     if (game->selection_box.visible)
499         selection_box_draw(&game->selection_box, cr);
500
501     hand_draw(game, 0, cr, event->region);
502
503     cairo_destroy (cr);
504
505     return TRUE;
506 }
507
508 static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_t *game)
509 {
510     printf ("You pressed key %d\n", event->keyval);
511
512     return TRUE;
513 }
514
515 static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
516 {
517     int i, tile_x, tile_y;
518
519     for (i = 0; i < game->deck.num_tiles; i++)
520     {
521         tile_x = game->deck.tiles[i].x;
522         tile_y = game->deck.tiles[i].y;
523         if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
524             event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
525         {
526             game->select_mode = 0;
527             game->current_tile = i;
528             game->diff_x = event->x - tile_x;
529             game->diff_y = event->y - tile_y;
530         }
531     }
532     if (game->select_mode)
533     {
534         game->selection_box.visible = 1;
535         /*These next two lines are likely to be replaced by...*/
536         game->click_x = event->x;
537         game->click_y = event->y;
538         /*...these two lines*/
539         game->selection_box.x1 = event->x;
540         game->selection_box.x2 = event->x;
541         game->selection_box.y1 = event->y;
542         game->selection_box.y2 = event->y;
543     }
544     return TRUE;
545 }
546
547 static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
548 {
549     if (game->select_mode)
550     {
551         game->select_mode = 0;
552         selection_box_t *box;
553         box = &game->selection_box;
554
555         int x_min = MIN(box->x1, box->x2);
556         int x_max = MAX(box->x1, box->x2);
557         int y_min = MIN(box->y1, box->y2);
558         int y_max = MAX(box->y1, box->y2);
559         int width = abs(box->x2 - box->x1);
560         int height = abs(box->y2 - box->y1);
561
562         box->visible = 0;       
563         gtk_widget_queue_draw_area (widget, x_min, y_min, width, height);
564
565         tile_group_t group;
566         group.num_tiles = 0;
567
568         int i, tile_x, tile_y, tile_x2, tile_y2;
569         for (i = 0; i < game->deck.num_tiles; i++)
570         {
571             tile_x = game->deck.tiles[i].x;
572             tile_y = game->deck.tiles[i].y;
573             tile_x2 = tile_x + TILE_WIDTH;
574             tile_y2 = tile_y + TILE_HEIGHT;
575             if (/*If top-left corner*/
576                 (tile_x >= x_min && tile_x <= x_max &&
577                  tile_y >= y_min && tile_y <= y_max) ||
578                 /*or bottom-right corner*/
579                 (tile_x2 >= x_min && tile_x2 <= x_max &&
580                  tile_y2 >= y_min && tile_y2 <= y_max) ||
581                 /*or bottom-left corner*/
582                 (tile_x >= x_min && tile_x <= x_max &&
583                  tile_y2 >= y_min && tile_y2 <= y_max) ||
584                 /*or top-right corner of tile selected*/
585                 (tile_x2 >= x_min && tile_x2 <= x_max &&
586                  tile_y >= y_min && tile_y <= y_max) )
587             {
588                 group.tiles[group.num_tiles] = game->deck.tiles[i];
589                 group.num_tiles++;
590             }
591         }
592         printf("is run %d\n", tile_group_is_run_one(&group) );
593         printf("is set %d\n", tile_group_is_set(&group) );
594         for (i = 0; i < group.num_tiles; i++)
595             tile_print(group.tiles[i]);
596     }
597     
598     game->select_mode = 1;
599
600     return TRUE;
601 }
602
603 static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event,
604                                         game_t *game, cairo_t *cr)
605 {
606     if (game->select_mode)
607     {
608         selection_box_t *box;
609         box = &game->selection_box;
610         box->visible = 1;
611
612         gtk_widget_queue_draw_area ( widget,  MIN(box->x1, box->x2), MIN(box->y1, box->y2), abs(box->x2 - box->x1), abs(box->y2 - box->y1) );
613
614         box->x2 = event->x;
615         box->y2 = event->y;
616
617         gtk_widget_queue_draw_area ( widget, MIN(box->x1, box->x2), MIN(box->y1, box->y2), abs(box->x2 - box->x1), abs(box->y2 - box->y1) );
618     }
619     else
620     {
621         tile_t *tile;
622         tile = &game->deck.tiles[game->current_tile];
623
624         /* First, invalidate the region where the tile currently is. */
625         gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
626
627     /* Then, move the tile */
628         tile->x = event->x - game->diff_x;
629         tile->y = event->y - game->diff_y;
630
631     /* Finally, invalidate the region where the tile is now. */
632         gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
633     }
634     return TRUE;
635 }
636
637 int main(int argc, char *argv[])
638 {
639     GtkWidget *window;
640     game_t game;
641
642     srand(time(NULL));
643
644     gtk_init (&argc, &argv);
645
646     game_init(&game);
647     deck_print(&game.deck);
648     deck_spread(&game.deck);
649     deck_deal(&game, &game.deck);
650     //hand_print(&game, 0); //With Zero being passed, will print hand for player 1(players[0])
651     //deck_print(&game.deck);
652
653     /* Create a new window */
654     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
655     gtk_window_set_default_size (GTK_WINDOW (window),
656                                  GAME_WINDOW_DEFAULT_WIDTH,
657                                  GAME_WINDOW_DEFAULT_HEIGHT);
658
659     gtk_widget_set_events (window,
660                            GDK_EXPOSURE_MASK |
661                            GDK_KEY_PRESS_MASK |
662                            GDK_BUTTON_MOTION_MASK |
663                            GDK_BUTTON_PRESS_MASK |
664                            GDK_BUTTON_RELEASE_MASK);
665
666     g_signal_connect (G_OBJECT (window), "delete_event",
667                       G_CALLBACK (gtk_main_quit), NULL);
668     g_signal_connect (G_OBJECT (window), "expose_event",
669                       G_CALLBACK (on_expose_event), &game);
670     g_signal_connect (G_OBJECT (window), "key_press_event",
671                       G_CALLBACK (on_key_press_event), &game);
672     g_signal_connect (G_OBJECT (window), "button_press_event",
673                       G_CALLBACK (on_button_press_event), &game);
674     g_signal_connect (G_OBJECT (window), "button_release_event",
675                       G_CALLBACK (on_button_release_event), &game);
676     g_signal_connect (G_OBJECT (window), "motion_notify_event",
677                       G_CALLBACK (on_button_motion_event), &game);
678
679
680     gtk_widget_show_all (window);
681     gtk_main ();
682
683     return 0;
684
685 }