Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. #include <gtk/gtk.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4.  
  5. #define ACE 0
  6. #define CARDS 13
  7. #define SUITS 4
  8. #define MAX_CARDS 10
  9.  
  10. struct table {
  11.  
  12. char card_path[MAX_CARDS][8];
  13.  
  14. int points[MAX_CARDS]; /* Points for each hand */
  15.  
  16. int total; /* total points */
  17.  
  18. int hand; /* number of card_path dealt */
  19.  
  20. int aces; /* N° of aces: These variable are used to attribute values 1/11 to Aces*/
  21.  
  22. int credit;
  23.  
  24. int bet;
  25.  
  26. cairo_surface_t *card_image[MAX_CARDS];
  27.  
  28. GtkWidget *vbox, *canvas;
  29.  
  30. struct table *next;
  31. };
  32.  
  33. struct {
  34. cairo_surface_t *image_back;
  35. _Bool end;
  36. int x[MAX_CARDS];
  37. } glob;
  38.  
  39. void button_clicked(GtkWidget *widget, struct table *game);
  40. void init_game(struct table *game);
  41. static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr, struct table *player);
  42. static void do_drawing(cairo_t *cr, struct table *player);
  43. void draw_cards(struct table *game);
  44. void assign_points(struct table *player);
  45.  
  46. int main (int argc, char *argv[])
  47. {
  48. GtkWidget *window, *button_hit, *button_stand, *hbox;
  49.  
  50. struct table dealer;
  51. struct table player;
  52.  
  53. dealer.next = malloc(sizeof(struct table));
  54.  
  55. player.next = NULL;
  56.  
  57. int i;
  58.  
  59. srand((unsigned)time(NULL));
  60.  
  61. init_game(&dealer);
  62. init_game(&player);
  63.  
  64. glob.end = 0;
  65.  
  66. glob.image_back = cairo_image_surface_create_from_png("back.png");
  67.  
  68. glob.x[0] = 10;
  69.  
  70. for (i = 1; i < MAX_CARDS; i++)
  71. glob.x[i] = glob.x[i-1] + 110;
  72.  
  73. gtk_init (&argc, &argv);
  74.  
  75. window = gtk_window_new (GTK_WINDOW_TOPLEVEL); /* create window */
  76. gtk_window_set_title (GTK_WINDOW (window), "Show Images");
  77. gtk_window_maximize (GTK_WINDOW (window));
  78.  
  79. GtkCssProvider *css_provider = gtk_css_provider_new(); /* Apply style */
  80. gtk_css_provider_load_from_path (css_provider, "style.css", NULL);
  81.  
  82. GdkScreen *myScreen = gdk_screen_get_default();
  83. gtk_style_context_add_provider_for_screen
  84. (myScreen,
  85. GTK_STYLE_PROVIDER (css_provider),
  86. GTK_STYLE_PROVIDER_PRIORITY_USER);
  87.  
  88. player.vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
  89. gtk_container_add(GTK_CONTAINER (window), player.vbox);
  90.  
  91. player.canvas = gtk_drawing_area_new();
  92. gtk_container_add (GTK_CONTAINER (player.vbox), player.canvas);
  93. g_object_set (player.canvas, "expand", TRUE, NULL); /* expand the canvas */
  94.  
  95. hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 30); /* hbox for buttons */
  96. gtk_container_add (GTK_CONTAINER (player.vbox), hbox);
  97.  
  98. button_hit = gtk_button_new_with_mnemonic ("Hit");
  99. gtk_container_add (GTK_CONTAINER (hbox), button_hit);
  100.  
  101. g_signal_connect(G_OBJECT(button_hit), "clicked", G_CALLBACK(button_clicked), &dealer);
  102. g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
  103. g_signal_connect(G_OBJECT(player.canvas), "draw", G_CALLBACK(on_draw_event), &dealer);
  104.  
  105. button_stand = gtk_button_new_with_mnemonic ("Stand");
  106. gtk_container_add (GTK_CONTAINER (hbox), button_stand);
  107.  
  108. gtk_widget_show_all (window);
  109.  
  110. gtk_main();
  111.  
  112. for (i = 0; i < dealer.hand; i++)
  113. cairo_surface_destroy(dealer.card_image[i]);
  114.  
  115. for (i = 0; i < player.hand; i++)
  116. cairo_surface_destroy(player.card_image[i]);
  117.  
  118. return 0;
  119. }
  120.  
  121. void init_game(struct table *player) {
  122.  
  123. player->total = player->aces = player->hand = 0;
  124.  
  125. draw_cards(player);
  126.  
  127. assign_points(player);
  128.  
  129. int i;
  130.  
  131. for (i = 0; i < MAX_CARDS; i++)
  132. player->card_image[i] = NULL;
  133.  
  134. for (i = 0; i < 2; i++) {
  135. player->card_image[i] = cairo_image_surface_create_from_png(player->card_path[i]);
  136. }
  137.  
  138. player->hand = 1;
  139. }
  140.  
  141. void button_clicked(GtkWidget *widget, struct table *player)
  142. {
  143. player->next->hand++;
  144.  
  145. gtk_widget_queue_draw (player->canvas);
  146.  
  147. if (chdir("cards_new") != 0)
  148. perror("chdir() to /card_path_new failed");
  149.  
  150. if (player->total < 17) {
  151. player->hand++;
  152. player->card_image[player->hand] = cairo_image_surface_create_from_png(player->card_path[player->hand]);
  153. }
  154.  
  155. player->next->card_image[player->next->hand] = cairo_image_surface_create_from_png(player->next->card_path[player->next->hand]);
  156. }
  157.  
  158. static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr, struct table *player)
  159. {
  160. do_drawing(cr, player);
  161.  
  162. return FALSE;
  163. }
  164.  
  165. static void do_drawing(cairo_t *cr, struct table *player)
  166. {
  167. int i;
  168.  
  169. for (i = 0; i < player->hand; i++) {
  170.  
  171. if (i == 0 && glob.end != 1)
  172. cairo_set_source_surface(cr, glob.image_back, 10, 10);
  173.  
  174. else
  175. cairo_set_source_surface(cr, player->card_image[i], glob.x[i], 10);
  176.  
  177. cairo_paint(cr);
  178. }
  179.  
  180. for (i = 0; i < player->next->hand; i++) {
  181. if (player->next->card_image[i] != NULL) {
  182. cairo_set_source_surface(cr, player->next->card_image[i], glob.x[i], 400);
  183. cairo_paint(cr);
  184. }
  185. }
  186. }
  187.  
  188. void draw_cards(struct table *player) {
  189.  
  190. int card_pick, suit_pick;
  191. int i = 0;
  192.  
  193. int drawn_card[SUITS][CARDS] = {0};
  194.  
  195. while (i < MAX_CARDS) { /* draw dealer's card_path */
  196.  
  197. suit_pick = rand() % SUITS;
  198. card_pick = rand() % CARDS;
  199.  
  200. if (drawn_card[suit_pick][card_pick] == 0) {
  201.  
  202. sprintf(player->card_path[i], "%d%d.png", suit_pick, card_pick);
  203. drawn_card[suit_pick][card_pick] = 1;
  204.  
  205. if (card_pick == ACE)
  206. player->points[i] = 11;
  207.  
  208. else if (card_pick >= 9)
  209. player->points[i] = 10;
  210.  
  211. else
  212. player->points[i] = card_pick + 1;
  213.  
  214. i++;
  215. }
  216. }
  217. }
  218.  
  219. void assign_points(struct table *player) {
  220.  
  221. if (player->points[player->hand] == 11)
  222. player->aces++;
  223.  
  224. if ( (player->aces > 0) && ( (player->total + player->points[player->hand]) > 21) ) {
  225. player->total += player->points[player->hand];
  226.  
  227. player->total = player->total - 10;
  228. player->aces--;
  229. }
  230.  
  231. else
  232. player->total += player->points[player->hand];
  233.  
  234. player->hand++;
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement