forked from achintyapataskar/card-games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cards.c
91 lines (91 loc) · 2.45 KB
/
cards.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*the game of Blackjack.This is my miniproject for 2015
* Copyright (C) 2015 Achintya Pataskar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.*/
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include "cards.h"
/*all .png images are stored in a file called cards_png
*they are coded as <suite><rank>.png*/
void initdeck(deck *d) {
int i = 0;
int k;
char str[32], x;
while(i < 13) {
x = 'c';
d->arr[i].rank = i + 1;
d->arr[i].suite = x;
sprintf(str, "cards_png/%c%d.png", x, i + 1);
d->arr[i].image = gtk_image_new_from_file(str);
i++;
}
while(i < 26) {
x = 'd';
k = i % 13;
d->arr[i].rank = k + 1;
d->arr[i].suite = x;
sprintf(str, "cards_png/%c%d.png", x, k + 1);
d->arr[i].image = gtk_image_new_from_file(str);
i++;
}
while(i < 39) {
x = 'h';
k = i % 13;
d->arr[i].rank = k + 1;
d->arr[i].suite = x;
sprintf(str, "cards_png/%c%d.png", x, k + 1);
d->arr[i].image = gtk_image_new_from_file(str);
i++;
}
while(i < 52) {
x = 's';
k = i % 13;
d->arr[i].suite = x;
d->arr[i].rank = k + 1;
sprintf(str, "cards_png/%c%d.png", x, k + 1);
d->arr[i].image = gtk_image_new_from_file(str);
i++;
}
d->index = 0;
return;
}
/*shuffles the deck by swapping cards in two different indices*/
void shuffle(deck *d) {
int i, j, k;
i = 0;
card x;
while(i < 50) {
j = rand() % 52;
k = rand() % 52;
x = d->arr[j];
d->arr[j] = d->arr[k];
d->arr[k] = x;
i++;
}
return;
}
/*takes a pointer to GtkWidget and GtkGrid, also int x and y for position in grid and card c for an image*/
void dispcard(GtkWidget *window, GtkGrid *grid, int x, int y, card c) {
gtk_grid_attach(grid, c.image, x, y, 1, 1);
gtk_widget_show_all(window);
return;
}
/*returns a card after sequentially drawing from the deck*/
card drawcard(deck *d) {
card x;
x = d->arr[(d->index)];
d->index++;
return x;
}