-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamefunction.h
263 lines (231 loc) · 7.98 KB
/
gamefunction.h
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
251
252
253
254
255
256
257
258
259
260
261
262
263
#ifndef GAMEFUNCTION_H_INCLUDED
#define GAMEFUNCTION_H_INCLUDED
#include "printgame.h"
#include "scannum.h"
#include "check_boxes.h"
#include <time.h>
#include "undo.h"
#include "computerturn.h"
void sortusers ( users list[50]) // sort the user according to score used in displaying top 10
{
int i, j;
users temp;
for(i = 0; i < 50-1; i++)
{
for(j = 0; j < 50-1-i; j++)
if(list[j].score < list[j+1].score) //descending way
{
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}
}
int gameloop(int nb,int mode,int size,char gamearr[size][size],int totallines,int nofmoves,int *score1,int *score2, int moves1, int moves2,int lenname1,char name1[lenname1],int lenname2,char name2[lenname2],int gamer,int movesplayed[totallines][7])
{
time_t t1 =time(0);
int noundo =0; //30/12/2021
//initializing the arrays of complete game
int j, i,availablemove=1;
char h = 205, v = 186, dot = 254; //205 186 254
char compgame[size][size];
for(j=0; j < size; j++)
{
compgame[0][j] = j;
compgame[j][0] = j;
if(j%2 != 0)
{
for(i=1; i < size; i=i+2)
{
compgame[i][j] = dot;
compgame[i+1][j] =v;
}
}
else if(j%2 == 0)
{
for(i=1; i < size; i = i+2)
{
compgame[i][j] = h;
compgame[i+1][j] =' ';
}
}
}
int row,col,gameon=1,check=0,ai = 1 ;
printgrid(size,gamearr,name1,name2,moves1,moves2,*score1,*score2,totallines,nofmoves,t1,movesplayed);
while(gameon)
{
printf("\n\n\tenter number of row and column by hexadecimal digits\nenter 0, 0 for undo\t1, 1 for redo\t 2, 2 for save\t 3, 3 for main menu\n");
if(gamer==1)
SetColor(Blue);
else
SetColor(Red);
printf("\n\tplayer %d's turn\n\n",gamer);
if(gamer==2 && mode == 1) //computer turn function
{
compturn(&row,&col,size,gamearr,compgame,gamer,totallines,nofmoves,movesplayed,v,h,dot);
}
else
{
if(gamer==1)
{
SetColor(Blue);
}
else
{
SetColor(Red);
}
printf("\tENTER ROW: ");
row = readint();
printf(("\tENTER COL: "));
col = readint();
}
SetColor(White);
system("cls");
ai =0;
if(row==0 && col==0) //function of undo
{
undo ( &row, &col,&noundo,totallines,movesplayed,size,gamearr,&moves1,&moves2,&nofmoves,score1,score2,&gamer, mode ); //30/12/2021
}
else if(row == 2 && col == 2) //save game
{
int s_n;
printf("chose file you want to save \n (1 or 2 or 3):\n ");
while(1)
{ s_n=readint();
if (s_n==1||s_n==2||s_n==3)
{
char fname[5];
sprintf(fname,"%d.txt",s_n);
FILE *save=fopen(fname,"w");
fwrite(&nb, sizeof(int), 1, save);
fwrite(&mode,sizeof(int), 1, save);
fwrite(&nofmoves,sizeof(int), 1, save);
fwrite(&moves1,sizeof(int), 1, save);
fwrite(&moves2,sizeof(int), 1, save);
fwrite(score1,sizeof(int), 1, save);
fwrite(score2,sizeof(int), 1, save);
fwrite(&gamer,sizeof(int), 1, save);
fwrite(movesplayed,sizeof(int), totallines * 7, save);
fwrite(gamearr, sizeof(char), size * size, save);
fwrite(&lenname1, sizeof(int), 1, save);
fwrite(name1, sizeof(char), lenname1, save);
if(mode == 2)
fwrite(&lenname2, sizeof(int), 1, save);
fwrite(name2, sizeof(char), lenname2, save);
fclose(save);
printf("saved to %d.txt\n", s_n);
system("pause");
printgrid(size,gamearr,name1,name2,moves1,moves2,*score1,*score2,totallines,nofmoves,t1,movesplayed);
system("cls");
break;
}
else
{
printf("\ninvalid file\n");
}
}
}
else if (row == 3 && col == 3) //if user want to return to main menu
{
gamer =0;
return gamer;
}
else if( row > 0 && row < size && col > 0 && col < size )
{
if(compgame[row][col] == h || compgame[row][col] == v || (row == 1 && col == 1))
{
if (row == 1 && col == 1) //for redo
{
if (noundo>0)
{
row =movesplayed[nofmoves][0];
col=movesplayed[nofmoves][1];
gamer=movesplayed[nofmoves][2];
noundo--;
}
else
{
printf("there is no moves to redo ");
printgrid(size,gamearr,name1,name2,moves1,moves2,*score1,*score2,totallines,nofmoves,t1,movesplayed);
continue;
}
}
else if ( gamer==1)
{
noundo=0;
}
//check if we played this move before
for(i=0; i < nofmoves; i++)
{
if(row == movesplayed[i][0] && col == movesplayed[i][1])
{
availablemove = 0;
printf("\nnot available move\n\n");
break;
}
}
if(availablemove)
{
gamearr[row][col]=compgame[row][col];
movesplayed[nofmoves][0]=row;
movesplayed[nofmoves][1]=col;
movesplayed[nofmoves][2]=gamer;
if(gamer == 1)
moves1++;
else
moves2++;
nofmoves++;
check=checkboxes ( row, col, size,compgame, gamearr, gamer,totallines,movesplayed,nofmoves,ai);
if(check)
{
if(gamer == 1)
*score1 += check;
else
*score2 += check;
}
else
{
if(gamer==1)
gamer=2;
else
gamer=1;
}
}
else
availablemove = 1;
}
else
printf("\nnot available move\n\n");
}
else
printf("\nnot available move\n\n");
printgrid(size,gamearr,name1,name2,moves1,moves2,*score1,*score2,totallines,nofmoves,t1,movesplayed);
if(nofmoves == totallines) //end the game
{
gameon = 0;
printf("\n\n\t\t\t\t END GAME\n\n");
if(*score1 > *score2)
{
SetColor(Blue);
printf("congratulation %s you are the winner", name1);
SetColor(Green);
gamer = 1;
}
else if(*score2 > *score1)
{
SetColor(Red);
printf("congratulation %s you are the winner", name2);
SetColor(Green);
gamer = 2;
}
else
{
printf("Tie Game");
gamer = 0;
}
return gamer;
}
}
return gamer;
}
#endif // GAMEFUNCTION_H_INCLUDED