#include <stdio.h>
#include <stdlib.h>
#include <time.h>
+#include <gtk/gtk.h>
+#include <librsvg/rsvg.h>
+#include <librsvg/rsvg-cairo.h>
+
+#define FATAL_ERROR(msg) \
+ do { fprintf (stderr, "Error: %s\n", msg); exit (1); } while (0)
char *colors[] = {"Black", "Blue", "Red", "Yellow"};
int num_cards;
} deck_t;
-#define CARD_GROUP_MAX_CARDS 14
+#define CARD_GROUP_MAX_CARDS DECK_MAX_CARDS
typedef struct card_group {
- card_t cards[HAND_MAX_CARDS];
+ card_t cards[CARD_GROUP_MAX_CARDS];
int num_cards;
} card_group_t;
typedef struct board {
card_group_t groups[BOARD_MAX_CARD_GROUPS];
int num_groups;
-} board_t
+} board_t;
typedef struct player {
card_group_t hand;
} player_t;
#define GAME_MAX_PLAYERS 4
+#define GAME_WINDOW_DEFAULT_WIDTH 800
+#define GAME_WINDOW_DEFAULT_HEIGHT 600
typedef struct game {
player_t players[GAME_MAX_PLAYERS];
int num_players;
board_t board;
deck_t deck;
+ RsvgHandle *blanktile;
} game_t;
-void card_print(card_t card)
+static void card_print(card_t card)
{
printf("%6s %2d\n", colors[card.color], card.number + 1);
}
-void card_group_init(card_group_t *card_group)
+static void card_group_init(card_group_t *card_group)
{
card_group->num_cards = 0;
}
-void board_init(board_t *board)
+static void board_init(board_t *board)
{
int i;
board->num_groups = 0;
}
}
-void player_init(player_t *player)
+static void player_init(player_t *player)
{
card_group_init(&player->hand);
}
-void game_init(game_t *game)
+
+/* If card_one < card_two, then return value will be negative
+ if they are equal, 0 will be returned,
+ if card_one > card_two, then return value will be positive */
+static int card_compare(const void *one, const void *two)
+{
+ const card_t *card_one = one;
+ const card_t *card_two = two;
+ return card_one->number - card_two->number;
+}
+
+static int card_group_is_run_one(card_group_t *card_group)
{
int i;
- game->num_players = 0
-
- for (i = 0; i < GAME_MAX_PLAYERS; ++i)
+ qsort (&card_group->cards[0], card_group->num_cards,
+ sizeof (card_t), card_compare);
+
+ if (card_group->num_cards > 13 || card_group->num_cards < 3)
+ {
+ return 0;
+ }
+ for (i = 0; i < card_group->num_cards - 1; ++i)
+ if(card_group->cards[i].color != card_group->cards[i + 1].color)
{
- player_init(&game->players[i]);
- game->num_players += 1;
+ return 0;
}
-
- board_init(&game->board);
- deck_init(&game->deck);
- deck_shuffle(&game->deck);
+ if(card_group->cards[i].number != card_group->cards[i + 1].number -1)
+ {
+ return 0;
+ }
+ return 1;
}
-int card_group_is_set(card_group_t *card_group)
+
+static int card_group_is_run_two(card_group_t *card_group)
{
- int i, j;
- color_t seen_color[card_group->num_cards]
-
- if (card_group->num_cards > 4 || card_group->num_cards < 3)
- {
+ int i;
+ int lowest = 14, highest = 0;
+ color_t run_color;
+
+ /* By definition, a run must have at least 3 cards. Also, it's
+ * impossible for any group of cards with more than 13 cards to be
+ * a run, (there are only 13 unique numbers so a group with more
+ * than 13 cards must have some duplicates).
+ */
+ if (card_group->num_cards < 3 || card_group->num_cards > 13)
+ {
+ return 0;
+ }
+
+ /* Loop through all cards in the group, ensuring that they are all
+ * the same color and finding the highest and lowest number in the
+ * group. */
+ run_color = card_group->cards[0].color;
+
+ for (i = 0; i < card_group->num_cards; i++)
+ {
+ if (card_group->cards[i].color != run_color)
return 0;
+ if (card_group->cards[i].number > highest)
+ {
+ highest = card_group->cards[i].number;
+ }
+ if (card_group->cards[i].number < lowest)
+ {
+ lowest = card_group->cards[i].number;
}
+ }
+
+ /* For a run, the difference between the highest and lowest cards
+ * will always be one less than the number of cards in the
+ * group. If not then we know it's not a run.
+ */
+ if (highest - lowest != card_group->num_cards - 1)
+ {
+ return 0;
+ }
+
+ /* XXX: There's a bug here. We're guessing that at this point
+ * anything we're looking at must be a run. This would be correct
+ * if there were no duplicate cards, but since there are
+ * duplicates this us quite broken. For example consider two
+ * sequences of entirely red cards:
+ *
+ * This is a run: 1, 2, 3, 4
+ * But this is not: 1, 3, 4, 4
+ *
+ * As currently written, this function will consider both of these
+ * groups to be a run. One possible fix is to throw away the
+ * highest - lowest heuristic and instead simply sort the cards up
+ * front and ensure the difference between each adjacent pair is
+ * exactly 1.
+ */
+ return 1;
+}
+
+static int card_group_is_set(card_group_t *card_group)
+{
+ int i;
+ color_t seen_color[card_group->num_cards];
+
+ if (card_group->num_cards > 4 || card_group->num_cards < 3)
+ {
+ return 0;
+ }
for (i = 0; i < card_group->num_cards - 1; ++i)
{
if (card_group->cards[i].number != card_group->cards[i + 1].number)
seen_color[i] = card_group->cards[i].color;
for (i = 0; i < card_group->num_cards; ++i)
{
- color_t color;
-
seen_color[card_group->cards[i].color]++;
+ if (seen_color[card_group->cards[i].color] > 1)
+ {
+ return 0;
+ }
}
return 1;
}
-void deck_deal(deck_t *deck)
+static void deck_deal(game_t *game, deck_t *deck)
{
card_t temp;
int rand_card;
- int i;
- for (i = 0; i < Players )
+ int i, j, newline;
+
+ printf ("How many players(1-4) should I deal in? ");
+ game->num_players = getchar();
+ if (game->num_players == EOF)
{
- rand_card = ((last + 1.0) * rand()) / (RAND_MAX + 1.0);
- temp = deck->cards[rand_card];
- deck->cards[rand_card] = deck->cards[last];
- deck->cards[last] = temp;
+ printf ("\nGoodbye.\n");
+ exit (1);
}
+ newline = getchar();
+ game->num_players -= '0';
+
+ for (i = 0; i < game->num_players; ++i)
+ {
+ for (j = 0; j < 14; ++j)
+ {
+ rand_card = ((deck->num_cards + 1.0) * rand()) / (RAND_MAX + 1.0);
+ temp = deck->cards[rand_card];
+ deck->cards[rand_card] = deck->cards[deck->num_cards - 1];
+ game->players[i].hand.cards[j] = temp;
+ deck->num_cards -= 1;
+ game->players[i].hand.num_cards += 1;
+ }
+ }
+ printf ("Game dealt for %d player(s)\n", game->num_players);
}
-
-void deck_init(deck_t *deck)
+static void deck_init(deck_t *deck)
{
int h, i, j;
deck->num_cards = 0;
deck->cards[j + (i * 13) + (h * 52)].color = i;
deck->cards[j + (i * 13) + (h * 52)].number = j;
deck->num_cards += 1;
+ printf ("There are %d tiles in the deck\n", deck->num_cards);
}
}
}
}
-void deck_shuffle(deck_t *deck)
+static void deck_shuffle(deck_t *deck)
{
card_t temp;
int rand_card;
{
rand_card = ((last + 1.0) * rand()) / (RAND_MAX + 1.0);
temp = deck->cards[rand_card];
- deck->cards[rand_card] = deck->cards[last];
- deck->cards[last] = temp;
+ deck->cards[rand_card] = deck->cards[last - 1];
+ deck->cards[last - 1] = temp;
}
}
-void deck_print(deck_t *deck)
+static void deck_print(deck_t *deck)
{
int h, i, j;
for (h = 0; h <= 1; ++h)
}
}
}
+ printf ("There are %d tiles in the deck\n" , deck->num_cards);
}
-int main()
+static void hand_print(game_t *game)
{
-#define MAX_PLAYERS 4
-#define HANDSIZE 14
-#define PLAYERS 4
+ int i;
+ for (i = 0; i < game->players[0].hand.num_cards; ++i)
+ {
+ card_print(game->players[0].hand.cards[i]);
+ }
+}
+
+static void card_draw(game_t *game, card_t *card, cairo_t *cr, int x, int y)
+{
+ char number_string[2];
+ number_string[0] = card->number;
+ number_string[1] = '\0';
+
+ cairo_save(cr);
+ cairo_translate(cr, x, y);
+ rsvg_handle_render_cairo (game->blanktile, cr);
- deck_t deck;
+ if (card->color == BLACK)
+ cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
+ if (card->color == BLUE)
+ cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
+ if (card->color == RED)
+ cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
+ if (card->color == YELLOW)
+ cairo_set_source_rgb (cr, 1.0, 1.0, 0.0);
+ cairo_move_to (cr, 0, 25);
+ cairo_set_font_size(cr, 25);
+ cairo_show_text (cr, number_string);
+
+ cairo_restore(cr);
+}
+
+static void game_init(game_t *game)
+{
+ int i;
+ GError *error = NULL;
+
+ game->num_players = 0;
+
+ for (i = 0; i < GAME_MAX_PLAYERS; ++i)
+ {
+ player_init(&game->players[i]);
+ game->num_players += 1;
+ }
+
+ board_init(&game->board);
+ deck_init(&game->deck);
+ deck_shuffle(&game->deck);
+
+ game->blanktile = rsvg_handle_new_from_file ("tiles/blanktile.svg", &error);
+ if (error)
+ FATAL_ERROR (error->message);
+}
+
+static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game)
+{
+ cairo_t *cr;
+
+ cr = gdk_cairo_create (widget->window);
+
+ rsvg_handle_render_cairo (game->blanktile, cr);
+ card_draw(game, &game->deck.cards[0], cr, 320, 240);
+
+ cairo_destroy (cr);
+
+ return TRUE;
+}
+
+static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_t *game)
+{
+ printf ("You pressed key %d\n", event->keyval);
+
+ return TRUE;
+}
+
+static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
+{
+ printf ("You pressed button %d\n", event->button);
+
+ return TRUE;
+}
+
+static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
+{
+ printf ("You released button %d\n", event->button);
+
+ return TRUE;
+}
+
+int main(int argc, char *argv[])
+{
+ GtkWidget *window;
+ game_t game;
srand(time(NULL));
+
+ gtk_init (&argc, &argv);
game_init(&game);
- deck_print(&deck);
-
+ deck_print(&game.deck);
+ //deck_deal(&game, &game.deck);
+ hand_print(&game);
+ deck_print(&game.deck);
+
+ /* Create a new window */
+ window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+ gtk_window_set_default_size (GTK_WINDOW (window),
+ GAME_WINDOW_DEFAULT_WIDTH,
+ GAME_WINDOW_DEFAULT_HEIGHT);
+
+ gtk_widget_set_events (window,
+ GDK_EXPOSURE_MASK |
+ GDK_KEY_PRESS_MASK |
+ GDK_BUTTON_PRESS_MASK |
+ GDK_BUTTON_RELEASE_MASK);
+
+ g_signal_connect (G_OBJECT (window), "delete_event",
+ G_CALLBACK (gtk_main_quit), NULL);
+ g_signal_connect (G_OBJECT (window), "expose_event",
+ G_CALLBACK (on_expose_event), &game);
+ g_signal_connect (G_OBJECT (window), "key_press_event",
+ G_CALLBACK (on_key_press_event), &game);
+ g_signal_connect (G_OBJECT (window), "button_press_event",
+ G_CALLBACK (on_button_press_event), &game);
+ g_signal_connect (G_OBJECT (window), "button_release_event",
+ G_CALLBACK (on_button_release_event), &game);
+
+ gtk_widget_show_all (window);
+ gtk_main ();
+
return 0;
-
+
+}