Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <gtk/gtk.h>
- #include <unistd.h>
- #include <stdio.h>
- #define ACE 0
- #define CARDS 13
- #define SUITS 4
- #define MAX_CARDS 10
- struct table {
- char card_path[MAX_CARDS][8];
- int points[MAX_CARDS]; /* Points for each hand */
- int total; /* total points */
- int hand; /* number of card_path dealt */
- int aces; /* N° of aces: These variable are used to attribute values 1/11 to Aces*/
- int credit;
- int bet;
- cairo_surface_t *card_image[MAX_CARDS];
- GtkWidget *vbox, *canvas;
- struct table *next;
- };
- struct {
- cairo_surface_t *image_back;
- _Bool end;
- int x[MAX_CARDS];
- } glob;
- void button_clicked(GtkWidget *widget, struct table *game);
- void init_game(struct table *game);
- static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr, struct table *player);
- static void do_drawing(cairo_t *cr, struct table *player);
- void draw_cards(struct table *game);
- void assign_points(struct table *player);
- int main (int argc, char *argv[])
- {
- GtkWidget *window, *button_hit, *button_stand, *hbox;
- struct table dealer;
- struct table player;
- dealer.next = malloc(sizeof(struct table));
- player.next = NULL;
- int i;
- srand((unsigned)time(NULL));
- init_game(&dealer);
- init_game(&player);
- glob.end = 0;
- glob.image_back = cairo_image_surface_create_from_png("back.png");
- glob.x[0] = 10;
- for (i = 1; i < MAX_CARDS; i++)
- glob.x[i] = glob.x[i-1] + 110;
- gtk_init (&argc, &argv);
- window = gtk_window_new (GTK_WINDOW_TOPLEVEL); /* create window */
- gtk_window_set_title (GTK_WINDOW (window), "Show Images");
- gtk_window_maximize (GTK_WINDOW (window));
- GtkCssProvider *css_provider = gtk_css_provider_new(); /* Apply style */
- gtk_css_provider_load_from_path (css_provider, "style.css", NULL);
- GdkScreen *myScreen = gdk_screen_get_default();
- gtk_style_context_add_provider_for_screen
- (myScreen,
- GTK_STYLE_PROVIDER (css_provider),
- GTK_STYLE_PROVIDER_PRIORITY_USER);
- player.vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
- gtk_container_add(GTK_CONTAINER (window), player.vbox);
- player.canvas = gtk_drawing_area_new();
- gtk_container_add (GTK_CONTAINER (player.vbox), player.canvas);
- g_object_set (player.canvas, "expand", TRUE, NULL); /* expand the canvas */
- hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 30); /* hbox for buttons */
- gtk_container_add (GTK_CONTAINER (player.vbox), hbox);
- button_hit = gtk_button_new_with_mnemonic ("Hit");
- gtk_container_add (GTK_CONTAINER (hbox), button_hit);
- g_signal_connect(G_OBJECT(button_hit), "clicked", G_CALLBACK(button_clicked), &dealer);
- g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
- g_signal_connect(G_OBJECT(player.canvas), "draw", G_CALLBACK(on_draw_event), &dealer);
- button_stand = gtk_button_new_with_mnemonic ("Stand");
- gtk_container_add (GTK_CONTAINER (hbox), button_stand);
- gtk_widget_show_all (window);
- gtk_main();
- for (i = 0; i < dealer.hand; i++)
- cairo_surface_destroy(dealer.card_image[i]);
- for (i = 0; i < player.hand; i++)
- cairo_surface_destroy(player.card_image[i]);
- return 0;
- }
- void init_game(struct table *player) {
- player->total = player->aces = player->hand = 0;
- draw_cards(player);
- assign_points(player);
- int i;
- for (i = 0; i < MAX_CARDS; i++)
- player->card_image[i] = NULL;
- for (i = 0; i < 2; i++) {
- player->card_image[i] = cairo_image_surface_create_from_png(player->card_path[i]);
- }
- player->hand = 1;
- }
- void button_clicked(GtkWidget *widget, struct table *player)
- {
- player->next->hand++;
- gtk_widget_queue_draw (player->canvas);
- if (chdir("cards_new") != 0)
- perror("chdir() to /card_path_new failed");
- if (player->total < 17) {
- player->hand++;
- player->card_image[player->hand] = cairo_image_surface_create_from_png(player->card_path[player->hand]);
- }
- player->next->card_image[player->next->hand] = cairo_image_surface_create_from_png(player->next->card_path[player->next->hand]);
- }
- static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr, struct table *player)
- {
- do_drawing(cr, player);
- return FALSE;
- }
- static void do_drawing(cairo_t *cr, struct table *player)
- {
- int i;
- for (i = 0; i < player->hand; i++) {
- if (i == 0 && glob.end != 1)
- cairo_set_source_surface(cr, glob.image_back, 10, 10);
- else
- cairo_set_source_surface(cr, player->card_image[i], glob.x[i], 10);
- cairo_paint(cr);
- }
- for (i = 0; i < player->next->hand; i++) {
- if (player->next->card_image[i] != NULL) {
- cairo_set_source_surface(cr, player->next->card_image[i], glob.x[i], 400);
- cairo_paint(cr);
- }
- }
- }
- void draw_cards(struct table *player) {
- int card_pick, suit_pick;
- int i = 0;
- int drawn_card[SUITS][CARDS] = {0};
- while (i < MAX_CARDS) { /* draw dealer's card_path */
- suit_pick = rand() % SUITS;
- card_pick = rand() % CARDS;
- if (drawn_card[suit_pick][card_pick] == 0) {
- sprintf(player->card_path[i], "%d%d.png", suit_pick, card_pick);
- drawn_card[suit_pick][card_pick] = 1;
- if (card_pick == ACE)
- player->points[i] = 11;
- else if (card_pick >= 9)
- player->points[i] = 10;
- else
- player->points[i] = card_pick + 1;
- i++;
- }
- }
- }
- void assign_points(struct table *player) {
- if (player->points[player->hand] == 11)
- player->aces++;
- if ( (player->aces > 0) && ( (player->total + player->points[player->hand]) > 21) ) {
- player->total += player->points[player->hand];
- player->total = player->total - 10;
- player->aces--;
- }
- else
- player->total += player->points[player->hand];
- player->hand++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement