]> git.cworth.org Git - kub/blob - kub.c
69be1d22e989480c196e2a3b1f708d0efdb89892
[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         return 0;
199     }
200     for (i = 0; i < tile_group->num_tiles - 1; ++i)
201         if(tile_group->tiles[i].color != tile_group->tiles[i + 1].color)
202         {
203             return 0;
204         }
205         if(tile_group->tiles[i].number != tile_group->tiles[i + 1].number -1)
206         {
207             return 0;
208         }
209     return 1;
210 }
211
212
213 static int tile_group_is_run_two(tile_group_t *tile_group)
214 {
215     int i;
216     int lowest = 14, highest = 0;
217     color_t run_color;
218
219     /* By definition, a run must have at least 3 tiles. Also, it's
220      * impossible for any group of tiles with more than 13 tiles to be
221      * a run, (there are only 13 unique numbers so a group with more
222      * than 13 tiles must have some duplicates).
223      */
224     if (tile_group->num_tiles < 3 || tile_group->num_tiles > 13)
225     {
226         return 0;
227     }
228
229     /* Loop through all tiles in the group, ensuring that they are all
230      * the same color and finding the highest and lowest number in the
231      * group. */
232     run_color = tile_group->tiles[0].color;
233
234     for (i = 0; i < tile_group->num_tiles; i++)
235     {
236         if (tile_group->tiles[i].color != run_color)
237             return 0;
238         if (tile_group->tiles[i].number > highest)
239         {
240             highest = tile_group->tiles[i].number;
241         }
242         if (tile_group->tiles[i].number < lowest)
243         {
244             lowest = tile_group->tiles[i].number;
245         }
246     }
247
248     /* For a run, the difference between the highest and lowest tiles
249      * will always be one less than the number of tiles in the
250      * group. If not then we know it's not a run.
251      */
252     if (highest - lowest != tile_group->num_tiles - 1)
253     {
254         return 0;
255     }
256
257     /* XXX: There's a bug here. We're guessing that at this point
258      * anything we're looking at must be a run. This would be correct
259      * if there were no duplicate tiles, but since there are
260      * duplicates this us quite broken. For example consider two
261      * sequences of entirely red tiles:
262      *
263      * This is a run:   1, 2, 3, 4
264      * But this is not: 1, 3, 4, 4
265      *
266      * As currently written, this function will consider both of these
267      * groups to be a run. One possible fix is to throw away the
268      * highest - lowest heuristic and instead simply sort the tiles up
269      * front and ensure the difference between each adjacent pair is
270      * exactly 1.
271      */
272     return 1;
273 }
274
275 static int tile_group_is_set(tile_group_t *tile_group)
276 {
277     int i;
278     color_t seen_color[tile_group->num_tiles];
279     
280     if (tile_group->num_tiles > 4 || tile_group->num_tiles < 3)
281     {
282         return 0;
283     }
284     for (i = 0; i < tile_group->num_tiles - 1; ++i) 
285     {
286         if (tile_group->tiles[i].number != tile_group->tiles[i + 1].number)
287         {
288             return 0;
289         }
290     }
291     seen_color[i] = tile_group->tiles[i].color;
292     for (i = 0; i < tile_group->num_tiles; ++i)
293     {
294         seen_color[tile_group->tiles[i].color]++;
295         if (seen_color[tile_group->tiles[i].color] > 1)
296         {
297             return 0;
298         }
299     }
300     return 1;
301 }
302
303 static void deck_deal(game_t *game, deck_t *deck)
304 {
305     tile_t temp;
306     int rand_tile;
307     int i, j, newline;
308     
309     printf ("How many players(1-4) should I deal in? ");
310     game->num_players = getchar();
311     if (game->num_players == EOF)
312     {
313         printf ("\nGoodbye.\n");
314         exit (1);
315     }
316     newline = getchar();   
317     game->num_players -= '0';
318     
319     for (i = 0; i < game->num_players; ++i)
320     {
321         for (j = 0; j < 14; ++j)
322         {
323             rand_tile = ((deck->num_tiles + 1.0) * rand()) / (RAND_MAX + 1.0);
324             temp = deck->tiles[rand_tile];
325             deck->tiles[rand_tile] = deck->tiles[deck->num_tiles - 1];
326             game->players[i].hand.tiles[j] = temp;
327             deck->num_tiles -= 1;
328             game->players[i].hand.num_tiles += 1;
329         }
330     }
331     printf ("Game dealt for %d player(s)\n", game->num_players);
332 }
333
334 static void deck_init(deck_t *deck)
335 {
336     int h, i, j;  
337     deck->num_tiles = 0;
338     for (h = 0; h <= 1; ++h)
339     {
340         for (i = 0; i <= 3; ++i) 
341         {
342             for (j = 0; j <= 12; ++j) 
343             {
344                 tile_init (&deck->tiles[deck->num_tiles++], i, j);
345                 printf ("There are %d tiles in the deck\n", deck->num_tiles);
346             }
347         }
348     }
349
350
351 static void deck_shuffle(deck_t *deck)
352 {
353     tile_t temp;
354     int rand_tile;
355     int last;
356     for (last = deck->num_tiles; last > 0; --last)
357     {
358         rand_tile = ((last + 1.0) * rand()) / (RAND_MAX + 1.0);
359         temp = deck->tiles[rand_tile];
360         deck->tiles[rand_tile] = deck->tiles[last - 1];
361         deck->tiles[last - 1] = temp;
362     }
363 }
364
365 static void deck_print(deck_t *deck)
366 {
367     int h, i, j;  
368     for (h = 0; h < 2; ++h)
369     {
370         for (i = 0; i < 4; ++i)
371         {
372             for (j = 0; j < 13; ++j)
373             {
374                 tile_print(deck->tiles[j + (i * 13) + (h * 52)]);
375             }
376         }
377     }
378     printf ("There are %d tiles in the deck\n" , deck->num_tiles);
379 }
380
381 static void deck_spread(deck_t *deck)
382 {
383     int i, j;
384     for (i = 0; i < 8; i++)
385     {
386         for (j = 0; j < 13; j++)
387         {
388             deck->tiles[j + (i * 13)].x = j * 50;
389             deck->tiles[j + (i * 13)].y = i * 60;
390         }
391     }
392 }
393
394 static void deck_draw(game_t *game, cairo_t *cr, GdkRegion *region)
395 {
396     int i;
397     for (i = 0; i < game->deck.num_tiles; i++)
398     {
399             tile_draw(game, &game->deck.tiles[i], cr, region);
400     }
401 }   
402
403 static void hand_print(game_t *game, int player)
404 {
405     int i;
406     for (i = 0; i < game->players[player].hand.num_tiles; i++)
407     {
408         tile_print(game->players[player].hand.tiles[i]);
409     }
410 }
411
412 static void hand_draw(game_t *game, int player, cairo_t *cr, GdkRegion *region)
413 {
414     int i;
415     int gwdw = GAME_WINDOW_DEFAULT_WIDTH;
416     int gwdh = GAME_WINDOW_DEFAULT_HEIGHT;
417     for (i = 0; i < game->players[player].hand.num_tiles; i++)    
418     {
419         tile_set_x_y(&game->players[player].hand.tiles[i],  
420                      ((gwdw / game->players[player].hand.num_tiles)) * i,
421                      (gwdh - TILE_HEIGHT - 6) );
422     }
423     for (i = 0; i < game->players[player].hand.num_tiles; i++)
424     {
425         tile_draw(game, &game->players[player].hand.tiles[i], cr, region);
426     }
427 }
428
429 static void game_init(game_t *game)
430 {
431     int i;
432     GError *error = NULL;
433
434     game->num_players = 0;
435     
436     for (i = 0; i < GAME_MAX_PLAYERS; ++i)
437     {
438         player_init(&game->players[i]);
439         game->num_players += 1;
440     }
441     
442     board_init(&game->board);
443     deck_init(&game->deck);
444     deck_shuffle(&game->deck);
445
446     game->blanktile = rsvg_handle_new_from_file ("tiles/blanktile.svg", &error);
447     if (error)
448         FATAL_ERROR (error->message);
449
450     /*This line appears to be useless, has been replaced by line below*/
451     //game->current_tile = game->deck.num_tiles - 1;    
452     game->current_tile = -1;
453     
454     game->diff_x = game->diff_y = 0;
455 }
456
457 static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game)
458 {
459     cairo_t *cr;
460
461     cr = gdk_cairo_create (widget->window);
462
463     deck_draw(game, cr, event->region);
464
465     hand_draw(game, 0, cr, event->region);
466
467     cairo_destroy (cr);
468
469     return TRUE;
470 }
471
472 static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_t *game)
473 {
474     printf ("You pressed key %d\n", event->keyval);
475
476     return TRUE;
477 }
478
479 static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
480 {
481     int i, tile_x, tile_y;
482     
483     for (i = 0; i < game->deck.num_tiles; i++)
484     {
485         tile_x = game->deck.tiles[i].x;
486         tile_y = game->deck.tiles[i].y;
487         if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
488             event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
489         {
490             game->current_tile = i;
491             game->diff_x = event->x - tile_x;
492             game->diff_y = event->y - tile_y;
493         }
494     }
495     if (game->current_tile == -1)
496     {            
497             game->click_x = event->x;
498             game->click_y = event->y;
499     }
500     return TRUE;
501 }
502
503 static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
504 {
505     if (game->current_tile == -1)
506     {
507         tile_group_t group;
508         group.num_tiles = 0;
509         
510         int i, tile_x, tile_y;
511         for (i = 0; i < game->deck.num_tiles; i++)
512         {
513             tile_x = game->deck.tiles[i].x;
514             tile_y = game->deck.tiles[i].y;
515             if ( (event->x >= tile_x && game->click_x <= tile_x &&
516                  event->y >= tile_y && game->click_y <= tile_y) ||
517                  (event->x >= tile_x && game->click_x <= tile_x &&
518                  event->y <= (tile_y + TILE_HEIGHT) && game->click_y >= tile_y) )
519             {
520                 group.tiles[group.num_tiles] = game->deck.tiles[i];
521                 group.num_tiles++;
522             }
523         }
524         for (i = 0; i < group.num_tiles; i++)
525             tile_print(group.tiles[i]);
526     }
527     
528     game->current_tile = -1;
529     printf ("You released button %d\n", event->button);
530
531     return TRUE;
532 }
533
534 static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event, 
535                                         game_t *game, cairo_t *cr)
536 {
537     tile_t *tile;
538     
539     tile = &game->deck.tiles[game->current_tile];
540     
541     /* First, invalidate the region where the tile currently is. */
542     gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
543     
544     /* Then, move the tile */
545     tile->x = event->x - game->diff_x;
546     tile->y = event->y - game->diff_y;
547     
548     /* Finally, invalidate the region where the tile is now. */
549     gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
550     
551     return TRUE;
552 }
553
554 int main(int argc, char *argv[])
555 {
556     GtkWidget *window;
557     game_t game;
558     
559     srand(time(NULL));
560
561     gtk_init (&argc, &argv);
562     
563     game_init(&game);
564     deck_print(&game.deck);
565     deck_spread(&game.deck);
566     deck_deal(&game, &game.deck);
567     //hand_print(&game, 0); //With Zero being passed, will print hand for player 1(players[0])
568     //deck_print(&game.deck);
569
570     /* Create a new window */
571     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
572     gtk_window_set_default_size (GTK_WINDOW (window),
573                                  GAME_WINDOW_DEFAULT_WIDTH,
574                                  GAME_WINDOW_DEFAULT_HEIGHT);
575
576     gtk_widget_set_events (window,
577                            GDK_EXPOSURE_MASK |
578                            GDK_KEY_PRESS_MASK | 
579                            GDK_BUTTON_MOTION_MASK |
580                            GDK_BUTTON_PRESS_MASK | 
581                            GDK_BUTTON_RELEASE_MASK);
582
583     g_signal_connect (G_OBJECT (window), "delete_event",
584                       G_CALLBACK (gtk_main_quit), NULL);
585     g_signal_connect (G_OBJECT (window), "expose_event",
586                       G_CALLBACK (on_expose_event), &game);
587     g_signal_connect (G_OBJECT (window), "key_press_event",
588                       G_CALLBACK (on_key_press_event), &game);
589     g_signal_connect (G_OBJECT (window), "button_press_event",
590                       G_CALLBACK (on_button_press_event), &game);
591     g_signal_connect (G_OBJECT (window), "button_release_event",
592                       G_CALLBACK (on_button_release_event), &game);
593     g_signal_connect (G_OBJECT (window), "motion_notify_event",
594                       G_CALLBACK (on_button_motion_event), &game);
595
596
597     gtk_widget_show_all (window);
598     gtk_main ();
599
600     return 0;
601
602 }