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