]> git.cworth.org Git - kub/blob - kub.c
Revised deck_draw by removing obsolete for loops(now that tile_draw is handling the...
[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 } game_t;
96
97 static void
98 tile_init (tile_t *tile, color_t color, int number)
99 {
100     tile->color = color;
101     tile->number = number;
102     tile->x = 0;
103     tile->y = 0;
104 }
105
106 static void tile_print(tile_t tile)
107 {
108     printf("%6s %2d\n", colors[tile.color], tile.number + 1);
109 }
110
111 static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr)
112 {
113     char number_string[3];
114     int len;
115
116     len = snprintf (number_string, 3, "%d", tile->number + 1);
117     if (len < 0 || len >= 3)
118         FATAL_ERROR ("snprintf failed");
119
120     cairo_save(cr);
121     cairo_translate(cr, tile->x, tile->y);
122     rsvg_handle_render_cairo (game->blanktile, cr);
123     
124     if (tile->color == BLACK)
125         cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
126     if (tile->color == BLUE)
127         cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);       
128     if (tile->color == RED)
129         cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
130     if (tile->color == YELLOW)
131         cairo_set_source_rgb (cr, 1.0, .843, 0.0);
132     cairo_move_to (cr, 10, 25);
133     cairo_set_font_size(cr, 25);
134     cairo_show_text (cr, number_string);
135     
136     cairo_restore(cr);
137 }
138
139 static void tile_group_init(tile_group_t *tile_group)
140 {
141     tile_group->num_tiles = 0;
142 }
143
144 static void board_init(board_t *board)
145 {
146     int i;
147     board->num_groups = 0;
148     
149     for (i = 0; i <= BOARD_MAX_TILE_GROUPS; ++i) 
150     {
151         tile_group_init(&board->groups[i]);
152     }
153 }
154
155 static void player_init(player_t *player)
156 {
157     tile_group_init(&player->hand);
158 }
159
160
161 /* If tile_one < tile_two, then return value will be negative
162    if they are equal, 0 will be returned,
163    if tile_one > tile_two, then return value will be positive */
164 static int tile_compare(const void *one, const void *two)
165 {
166     const tile_t *tile_one = one;
167     const tile_t *tile_two = two;
168     return tile_one->number - tile_two->number;
169 }
170
171 static int tile_group_is_run_one(tile_group_t *tile_group)
172 {
173     int i;
174     qsort (&tile_group->tiles[0], tile_group->num_tiles,
175            sizeof (tile_t), tile_compare);
176            
177     if (tile_group->num_tiles > 13 || tile_group->num_tiles < 3)
178     {
179         return 0;
180     }
181     for (i = 0; i < tile_group->num_tiles - 1; ++i)
182         if(tile_group->tiles[i].color != tile_group->tiles[i + 1].color)
183         {
184             return 0;
185         }
186         if(tile_group->tiles[i].number != tile_group->tiles[i + 1].number -1)
187         {
188             return 0;
189         }
190     return 1;
191 }
192
193
194 static int tile_group_is_run_two(tile_group_t *tile_group)
195 {
196     int i;
197     int lowest = 14, highest = 0;
198     color_t run_color;
199
200     /* By definition, a run must have at least 3 tiles. Also, it's
201      * impossible for any group of tiles with more than 13 tiles to be
202      * a run, (there are only 13 unique numbers so a group with more
203      * than 13 tiles must have some duplicates).
204      */
205     if (tile_group->num_tiles < 3 || tile_group->num_tiles > 13)
206     {
207         return 0;
208     }
209
210     /* Loop through all tiles in the group, ensuring that they are all
211      * the same color and finding the highest and lowest number in the
212      * group. */
213     run_color = tile_group->tiles[0].color;
214
215     for (i = 0; i < tile_group->num_tiles; i++)
216     {
217         if (tile_group->tiles[i].color != run_color)
218             return 0;
219         if (tile_group->tiles[i].number > highest)
220         {
221             highest = tile_group->tiles[i].number;
222         }
223         if (tile_group->tiles[i].number < lowest)
224         {
225             lowest = tile_group->tiles[i].number;
226         }
227     }
228
229     /* For a run, the difference between the highest and lowest tiles
230      * will always be one less than the number of tiles in the
231      * group. If not then we know it's not a run.
232      */
233     if (highest - lowest != tile_group->num_tiles - 1)
234     {
235         return 0;
236     }
237
238     /* XXX: There's a bug here. We're guessing that at this point
239      * anything we're looking at must be a run. This would be correct
240      * if there were no duplicate tiles, but since there are
241      * duplicates this us quite broken. For example consider two
242      * sequences of entirely red tiles:
243      *
244      * This is a run:   1, 2, 3, 4
245      * But this is not: 1, 3, 4, 4
246      *
247      * As currently written, this function will consider both of these
248      * groups to be a run. One possible fix is to throw away the
249      * highest - lowest heuristic and instead simply sort the tiles up
250      * front and ensure the difference between each adjacent pair is
251      * exactly 1.
252      */
253     return 1;
254 }
255
256 static int tile_group_is_set(tile_group_t *tile_group)
257 {
258     int i;
259     color_t seen_color[tile_group->num_tiles];
260     
261     if (tile_group->num_tiles > 4 || tile_group->num_tiles < 3)
262     {
263         return 0;
264     }
265     for (i = 0; i < tile_group->num_tiles - 1; ++i) 
266     {
267         if (tile_group->tiles[i].number != tile_group->tiles[i + 1].number)
268         {
269             return 0;
270         }
271     }
272     seen_color[i] = tile_group->tiles[i].color;
273     for (i = 0; i < tile_group->num_tiles; ++i)
274     {
275         seen_color[tile_group->tiles[i].color]++;
276         if (seen_color[tile_group->tiles[i].color] > 1)
277         {
278             return 0;
279         }
280     }
281     return 1;
282 }
283
284 static void deck_deal(game_t *game, deck_t *deck)
285 {
286     tile_t temp;
287     int rand_tile;
288     int i, j, newline;
289     
290     printf ("How many players(1-4) should I deal in? ");
291     game->num_players = getchar();
292     if (game->num_players == EOF)
293     {
294         printf ("\nGoodbye.\n");
295         exit (1);
296     }
297     newline = getchar();   
298     game->num_players -= '0';
299     
300     for (i = 0; i < game->num_players; ++i)
301     {
302         for (j = 0; j < 14; ++j)
303         {
304             rand_tile = ((deck->num_tiles + 1.0) * rand()) / (RAND_MAX + 1.0);
305             temp = deck->tiles[rand_tile];
306             deck->tiles[rand_tile] = deck->tiles[deck->num_tiles - 1];
307             game->players[i].hand.tiles[j] = temp;
308             deck->num_tiles -= 1;
309             game->players[i].hand.num_tiles += 1;
310         }
311     }
312     printf ("Game dealt for %d player(s)\n", game->num_players);
313 }
314
315 static void deck_init(deck_t *deck)
316 {
317     int h, i, j;  
318     deck->num_tiles = 0;
319     for (h = 0; h <= 1; ++h)
320     {
321         for (i = 0; i <= 3; ++i) 
322         {
323             for (j = 0; j <= 12; ++j) 
324             {
325                 tile_init (&deck->tiles[deck->num_tiles++], i, j);
326                 printf ("There are %d tiles in the deck\n", deck->num_tiles);
327             }
328         }
329     }
330
331
332 static void deck_shuffle(deck_t *deck)
333 {
334     tile_t temp;
335     int rand_tile;
336     int last;
337     for (last = deck->num_tiles; last > 0; --last)
338     {
339         rand_tile = ((last + 1.0) * rand()) / (RAND_MAX + 1.0);
340         temp = deck->tiles[rand_tile];
341         deck->tiles[rand_tile] = deck->tiles[last - 1];
342         deck->tiles[last - 1] = temp;
343     }
344 }
345
346 static void deck_print(deck_t *deck)
347 {
348     int h, i, j;  
349     for (h = 0; h < 2; ++h)
350     {
351         for (i = 0; i < 4; ++i)
352         {
353             for (j = 0; j < 13; ++j)
354             {
355                 tile_print(deck->tiles[j + (i * 13) + (h * 52)]);
356             }
357         }
358     }
359     printf ("There are %d tiles in the deck\n" , deck->num_tiles);
360 }
361
362
363 static void deck_draw(game_t *game, cairo_t *cr)
364 {
365     int i;
366     for (i = 0; i < game->deck.num_tiles; i++)
367     {
368             tile_draw(game, &game->deck.tiles[i], cr);
369     }
370 }   
371
372 static void hand_print(game_t *game)
373 {
374     int i;
375     for (i = 0; i < game->players[0].hand.num_tiles; ++i)
376     {
377         tile_print(game->players[0].hand.tiles[i]);
378     }
379 }
380
381 static void game_init(game_t *game)
382 {
383     int i;
384     GError *error = NULL;
385
386     game->num_players = 0;
387     
388     for (i = 0; i < GAME_MAX_PLAYERS; ++i)
389     {
390         player_init(&game->players[i]);
391         game->num_players += 1;
392     }
393     
394     board_init(&game->board);
395     deck_init(&game->deck);
396     deck_shuffle(&game->deck);
397
398     game->blanktile = rsvg_handle_new_from_file ("tiles/blanktile.svg", &error);
399     if (error)
400         FATAL_ERROR (error->message);
401
402     game->current_tile = game->deck.num_tiles - 1;
403 }
404
405 static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game)
406 {
407     cairo_t *cr;
408
409     cr = gdk_cairo_create (widget->window);
410
411     deck_draw(game, cr);
412
413     cairo_destroy (cr);
414
415     return TRUE;
416 }
417
418 static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_t *game)
419 {
420     printf ("You pressed key %d\n", event->keyval);
421
422     return TRUE;
423 }
424
425 static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
426 {
427     tile_t *tile;
428
429     tile = &game->deck.tiles[game->current_tile];
430
431     printf ("Placing tile #%d\n", game->current_tile);
432
433     /* First, invalidate the region where the tile currently is. */
434     gtk_widget_queue_draw_area (widget, tile->x, tile->y, TILE_WIDTH, TILE_HEIGHT);
435                         
436     /* Then, move the tile */
437     tile->x = event->x;
438     tile->y = event->y;
439
440     /* Finally, invalidate the region where the tile is now. */
441     gtk_widget_queue_draw_area (widget, tile->x, tile->y, TILE_WIDTH, TILE_HEIGHT);
442
443     game->current_tile--;
444     if (game->current_tile < 0)
445         game->current_tile = game->deck.num_tiles - 1;
446
447     return TRUE;
448 }
449
450 static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
451 {
452     printf ("You released button %d\n", event->button);
453
454     return TRUE;
455 }
456
457 int main(int argc, char *argv[])
458 {
459     GtkWidget *window;
460     game_t game;
461     
462     srand(time(NULL));
463
464     gtk_init (&argc, &argv);
465     
466     game_init(&game);
467     deck_print(&game.deck);
468     //deck_deal(&game, &game.deck);
469     hand_print(&game);
470     deck_print(&game.deck);
471
472     /* Create a new window */
473     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
474     gtk_window_set_default_size (GTK_WINDOW (window),
475                                  GAME_WINDOW_DEFAULT_WIDTH,
476                                  GAME_WINDOW_DEFAULT_HEIGHT);
477
478     gtk_widget_set_events (window,
479                            GDK_EXPOSURE_MASK |
480                            GDK_KEY_PRESS_MASK | 
481                            GDK_BUTTON_PRESS_MASK | 
482                            GDK_BUTTON_RELEASE_MASK);
483
484     g_signal_connect (G_OBJECT (window), "delete_event",
485                       G_CALLBACK (gtk_main_quit), NULL);
486     g_signal_connect (G_OBJECT (window), "expose_event",
487                       G_CALLBACK (on_expose_event), &game);
488     g_signal_connect (G_OBJECT (window), "key_press_event",
489                       G_CALLBACK (on_key_press_event), &game);
490     g_signal_connect (G_OBJECT (window), "button_press_event",
491                       G_CALLBACK (on_button_press_event), &game);
492     g_signal_connect (G_OBJECT (window), "button_release_event",
493                       G_CALLBACK (on_button_release_event), &game);
494
495     gtk_widget_show_all (window);
496     gtk_main ();
497
498     return 0;
499
500 }