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