This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
card_pile.c
203 lines (195 loc) · 4.85 KB
/
card_pile.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// Created by Ziming on 2021/10/11.
//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "card_pile.h"
#include "log.h"
#include "GTK_ENABLED.h"
#ifdef GTK
#include "mgtk.h"
#endif
//initialize
void init_pile(pile* q)
{
q->first=q->last=NULL;
}
//check empty
int pile_empty(pile* q)
{
return q->first==NULL && q->last==NULL;
}
//read the top card of the pile without picking it up
#ifndef GTK
void read_pile(pile *q,FILE* log,int n)
{
int i=0;
node *temp=q->first;
if(pile_empty(q))
{
fprintf(stderr,"Error#101: Card Pile Empty. Cannot read the pile.\n");
close_log(log);
exit(EXIT_FAILURE);
}
while(temp->next!=NULL){
tell_card(temp->card_num,log,0);
i++;
temp=temp->next;
if(i==n){
printf("... # more results skipped here\n");
if(log!=NULL)
fprintf(log,"... # more results skipped here\n");
break;
}
}
printf("\n");
if(log!=NULL)
fprintf(log,"\n");
}
#endif
#ifdef GTK
void read_pile(GtkWidget *window,GtkWidget *fixed,pile *q,FILE* log,int n)
{
if(window==NULL){}
int i=0;
node *temp=q->first;
GtkWidget *label;
if(pile_empty(q))
{
fprintf(stderr,"Error#101: Card Pile Empty. Cannot read the pile.\n");
close_log(log);
exit(EXIT_FAILURE);
}
while(temp->next!=NULL){
tell_card(temp->card_num,log);
disp_card(temp->card_num,i%7*200, (int)(100+floor(i/7)*200),fixed,-1,NULL);
//gtk_widget_show_all(window);
i++;
temp=temp->next;
if(i==n){
printf("... # more results skipped here\n");
if(log!=NULL)
fprintf(log,"... # more results skipped here\n");
break;
}
}
label= gtk_label_new("... # more results skipped here");
gtk_fixed_put(GTK_FIXED(fixed), label,0 , (int)(250+ floor(i/7)*200));
gtk_widget_set_size_request(label, 300, 35);
printf("\n");
if(log!=NULL)
fprintf(log,"\n");
}
#endif
void push_card(pile *q,int new_card)
{
node *s;
/***
* COMMENT: PAY SPECIAL ATTENTION TO SEGMENT FAULT caused by not distributing space
*/
s=(node*)malloc(sizeof(node));
if(s==NULL){
fprintf(stderr,"Cannot allocate memory\n.");
exit(1);
}
s->card_num=new_card;
s->next=NULL;
if(pile_empty(q))
{
q->first=q->last=s;
}
else
{
//THINK just care about the last one
q->last->next=s;
q->last=s;
}
}
int pull_card(pile *q,pile *disc,int counter[],int state,FILE *log)
{
int i;
int temp;
node *s;
if(pile_empty(q) && state!=2)
{
//the current card pile is empty resort to the discard pile
if(pile_empty(disc) && state)
{
fprintf(stderr,"Error#101: ALL Cards missing. Please Checkout the error!\n");
close_log(log);
exit(EXIT_FAILURE);
}
else{
printf("Stock pile exhausted. Shuffling the discard pile and restore the stock pile\n");
if(log!=NULL)
fprintf(log,"Stock pile exhausted. Shuffling the discard pile and restore the stock pile\n");
do{
temp=pull_card(disc,NULL,NULL,2,log);
//printf("%d\n",temp);
counter[temp]++;
}while(!pile_empty(disc));
shuffle_card(counter,q);
}
}
s=q->first;
i=s->card_num;
//be discreet
if(q->first==q->last)
q->first=q->last=NULL;
else
q->first=q->first->next;
free(s);
return i;
}
void clear_pile(pile *q)
{
while(!pile_empty(q)){
pull_card(q,q,NULL,1,NULL);
}
}
void tell_card(int x,FILE *log,int state)
{
char *suit[5]={"Spades","Hearts","Diamonds","Clubs"};
/**
* COMMENT: REMEMBER in C a string array could be directly defined by char*
*/
char *rank[14]={"1","2","3","4","5","6","7","8","9","10","J","Q","K"};
if(state)
printf("%s %s \t",suit[get_suits(x)],rank[get_rank(x)]);
if(log!=NULL)
fprintf(log,"%s %s \t",suit[get_suits(x)],rank[get_rank(x)]);
}
void shuffle_card(int card_counter[],pile* P)
{
//initialize the counter;
int i,j,eff=0;
int s,ct;
for(j=0;j<CardTotal;j++)
{
if(card_counter[j])
eff++;
}
//randomly choose element from the counter and push it to the queue
i=0;
srand((unsigned int)time(NULL));
while(eff)
{
ct=0;
s=rand()%(eff);
for(j=0;j<CardTotal;j++)
if(card_counter[j])
{
if(ct==s)
{
card_counter[j]--;
push_card(P,j);
if(card_counter[j]==0)
eff--;
break;
}
ct++;
}
i++;
}
}