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
/
Copy pathmain.c
250 lines (221 loc) · 6.77 KB
/
main.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include "card_pile.h"
#include "basic.h"
#include "animation.h"
#include "log.h"
#include "MUL_ENABLED.h"
#ifdef GTK
#include "mgtk.h"
#endif
#ifndef GTK
void game(int r,int d,int num,int c,FILE* log,int m)
{
//initial setup
int dir=1;//originally turning counter-clock wise
int card_counter[CardTotal];//card_counter is a container that will automatically fill every time there is s reshuffle
int cur_r;
int i,pl;//temp
int **card;
int *score;
//int array[5]={0,4,5,8};
//int *test;
//test=&array[0];
pile dock_pile;
pile disc_pile;
table t;
game_status st;
t.n=num;
t.win=1;
score=malloc((size_t)num*sizeof(int));
if(score==NULL){
fprintf(stderr,"Cannot allocate memory\n.");
exit(1);
}
for(i=0;i<num;i++)
score[i]=0;
for(cur_r=1;cur_r<=r;cur_r++)
{
/**
* Game Settings Initialized
*/
init_table(&t);
init_pile(&dock_pile);
init_pile(&disc_pile);
for (i=0;i<CardTotal;i++)
{
card_counter[i]=d;
}
printf("Shuffling cards...\n");
printf("Shuffle result: # shuffle result only displayed in log and demo mode\n");
if(log!=NULL)
{
fprintf(log,"Shuffling cards...\n");
fprintf(log,"Shuffle result: # shuffle result only displayed in log and demo mode\n");
}
shuffle_card(card_counter,&dock_pile);//initializing dock_pile
read_pile(&dock_pile,log,150);
printf("Dealing cards...\n");
if(log!=NULL)
fprintf(log,"Dealing cards...\n");
/**
* initializing personal card
*/
card=(int**)malloc(sizeof(int*)*(size_t)num);
if(card==NULL){
fprintf(stderr,"Cannot allocate memory\n.");
exit(1);
}
for(pl=0;pl<num;pl++)
{
card[pl]=(int*)malloc((size_t)c*sizeof(int));
if(card[pl]==NULL){
fprintf(stderr,"Cannot allocate memory\n.");
exit(1);
}
//begin=temp;
for(i=0;i<c+1;i++)
{
card[pl][i]=pull_card(&dock_pile,&disc_pile,card_counter,1,log);
push_card(&disc_pile,card[pl][i]);
}
qsort(card[pl],(size_t)c+1,sizeof(int),cmpf);
add_player(&t,init_player(c,card[pl],score[pl],m),dir);
}
if(log!=NULL)
fprintf(log,"\n# only display current user for a real game, server and demo mode show all players\n");
move(&t,-dir);
read_table(&t,dir,log,1,NULL,0);
printf("Determining the playing order...\n");
if(log!=NULL)
fprintf(log,"Determining the playing order...\n");
t=init_order(&t,&disc_pile,log);
//add_card(t.next,test,4);
//read_table(&t,dir,log,1);
/**
* Game Starts!
*/
init_status(&st);
st.dir=1;
animation(&t,&st,&dock_pile,&disc_pile,card_counter,log,t.pl);
/**
* Game Ended!
*/
/***
* displaying the scores of every player
*/
printf("---- Stats ----\n"
"Round %d result:\n",cur_r);
if(log!=NULL)
fprintf(log,"---- Stats ----\n"
"Round %d result:\n",cur_r);
read_table(&t,1,log,2,score,1);
create_score("score.log",score,pl);
printf("Round %d ends.\n\n",cur_r);
if(log!=NULL)
fprintf(log,"Round %d ends.\n\n",cur_r);
///clear all the memory
free(card);
clear_table(&t);
clear_pile(&dock_pile);
clear_pile(&disc_pile);
}
return;
}
#endif
void init_start(int argc, char *argv[])
{
int option;
char *arg;
int r=1,d=2,num=4,c=5;
int m=1;
FILE *log=NULL;
static struct option long_opts[]=
{
{"help",no_argument,NULL,'h'},
{"log",required_argument,NULL,'l'},
{"player-number=",required_argument,NULL,'n'},
{"initial-cards=",required_argument,NULL,'c'},
{"decks=",required_argument,NULL,'d'},
{"rounds=",required_argument,NULL,'r'},
{"auto",no_argument,NULL,'a'},
#ifdef MUL
{"move=",required_argument,NULL,'m'},
#endif
{NULL,0,NULL,0},
};
setvbuf(stdout, NULL, _IONBF, 0);
while(1) {
#ifndef MUL
option= getopt_long(argc,argv,"hn:c:d:r:a",long_opts,NULL);
#endif
#ifdef MUL
option= getopt_long(argc,argv,"hn:c:d:r:am:",long_opts,NULL);
#endif
if(option==-1) break;
//printf("%c\n",option);
//printf("%s",optarg);
switch(option)
{
case 'h': printf("help\n"); print_help(); break;
case 'n': arg=optarg; sscanf(arg,"%d",&num); break;
case 'c': arg=optarg; sscanf(arg,"%d",&c); break;
case 'r': arg=optarg; sscanf(arg,"%d",&r); break;
case 'd': arg=optarg; sscanf(arg,"%d",&d); break;
case 'a': read_log("demo.log"); break;
#ifdef MUL
case 'm': arg=optarg; sscanf(arg,"%d",&m); break;
#endif
case 'l': {
arg=optarg;
printf("Creating Log\n");
log=create_log(arg,log);
break;
}
default:;
}
}
printf("########################\n"
"# #\n"
"# Welcome to One Card! #\n"
"# #\n"
"########################\n");
printf("---- Initial setup ----\n"
"Number of rounds: %d\n"
"Number of decks: %d\n"
"Number of players: %d\n"
"Initial number of cards offered to each player:%d\n",r,d,num,c);
if(log!=NULL)
fprintf(log,"---- Initial setup ----\n"
"Number of rounds: %d\n"
"Number of decks: %d\n"
"Number of players: %d\n"
"Initial number of cards offered to each player:%d\n",r,d,num,c);
#ifdef GTK
GtkWidget* window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"Onecard Game");
gtk_window_set_default_size(GTK_WINDOW(window),1920,1080);
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
g_signal_connect(window, "destroy",
G_CALLBACK(destroy), NULL);
gtk_container_set_border_width(GTK_CONTAINER(window), 20);
/**
* init setting!
*/
init_ggame(window);
gtk_widget_show_all(window);
gtk_main();
#endif
#ifndef GTK
game(r, d, num, c, log,m);
#endif
close_log(log);
}
int main(int argc,char *argv[])
{
init_start(argc,argv);
return 0;
}