-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumberCrash.c
More file actions
481 lines (458 loc) · 17.8 KB
/
Copy pathnumberCrash.c
File metadata and controls
481 lines (458 loc) · 17.8 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/**
@file
BBG2 2014-2015 spring semester, semester project.
NumberCrash game.
@author
Name: Sardor Hazratov
Student no: 14011901
Date: 08/06/2015
E-Mail: serdarjan1995@gmail.com
Compiler used: GCC
IDE: CodeBlocks
Operating System Windows 8.1
*/
#include <stdio.h>
#include <stdlib.h>
/**
function of pulling numbers down after detecting any structure ( recursive ).
Generates numbers on empty cells;
@param n size of matrix
@param numbers range of numbers (2<x<10)
@param table[100][100] board with numbers
*/
void pullDown(int table[100][100],int n,int numbers){
int i,j,k,temp;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(table[i][j]==0 && i==0){ /*if there are any empty cell on the first row, fill it with new random number*/
table[i][j]=rand()%(numbers-1)+2;
}
else if (table[i][j]==0 && i>0){ /*if there are any empty cells, push them up.*/
for(k=i;k>0;k--){
temp=table[k][j];
table[k][j]=table[k-1][j];
table[k-1][j]=temp;
}
pullDown(table,n,numbers); /*After pushing the empty cell, start function again with changed matrix*/
}
}
}
}
/**
function of exchanging position of two numbers.
@param row row where exchanging number is plant (y-axis).
@param column column where exchanging number is plant (x-axis).
@param direction direction of exchanging. (8-up; 6-right; 4-left; 2-down;)
@param table[100][100] board with numbers
*/
void exchange(int table[100][100],int row,int column,int direction){
int temp;
switch(direction){
case 8/*up*/ : temp=table[row][column];
table[row][column]=table[row-1][column];
table[row-1][column]=temp; break;
case 6/*right*/: temp=table[row][column];
table[row][column]=table[row][column+1];
table[row][column+1]=temp; break;
case 2/*down*/: temp=table[row][column];
table[row][column]=table[row+1][column];
table[row+1][column]=temp; break;
case 4/*left*/: temp=table[row][column];
table[row][column]=table[row][column-1];
table[row][column-1]=temp; break;
}
}
/**
function of displaying the board.
@param N size of matrix
@param table[100][100] board with numbers
*/
void matrixShow(int table[100][100],int N){
int i,j;
/*printing column indexes of matrix (x-axis) to make easy understand which number we have chosen*/
printf("\n ");
for(i=0;i<N;i++){
printf("%2d",i);
}
printf("\n ");
for(i=0;i<N;i++){
printf("--");
}
printf("\n"); /*columns finished*/
for(i=0;i<N;i++){
printf("%2d|",i);/* printing row indexes */
for(j=0;j<N;j++){/*start to printing matrix's elements*/
if(table[i][j]){
printf("%2d",table[i][j]);
}
else{
printf("%2c",table[i][j]);
}
}
printf("\n");
}
printf("\n");
}
/**
****** STRUCTURE I ******
@param n size of matrix
@param *score game score
@param numbers range of numbers (2<x<10) (required by pullDown function)
@param table[100][100] board with numbers
______________________________________________________
!!!!!!! In the function used pullDown function;!!!!!!!
*/
int strI(int table[100][100],int n,int *score,int numbers){
int i,j,temp,found=0;
for(i=0;i<n-2;i++){
for(j=0;j<n-2;j++){
if(table[i][j]==table[i+1][j] && table[i][j]==table[i+2][j]){
found++;
temp=table[i][j]+table[i+1][j]+table[i+2][j];
*score+=temp;
table[i][j]=NULL;
table[i+1][j]=NULL;
table[i+2][j]=NULL;
matrixShow(table,n);
pullDown(table,n,numbers); /*send the condition to pullDown function*/
printf("\nYou hit I structure +%d points.\nscore: %d\n",temp,*score);
matrixShow(table,n);
}
}
}
return found;
}
/**
****** STRUCTURE T ******
@param n size of matrix
@param *score game score
@param numbers range of numbers (2<x<10) (required by pullDown function)
@param table[100][100] board with numbers
______________________________________________________
!!!!!!! In the function used pullDown function;!!!!!!!
*/
int strT(int table[100][100],int n,int *score,int numbers){
int i,j,temp,found=0;
for(i=0;i<n-2;i++){
for(j=0;j<n-2;j++){
if(table[i][j]==table[i][j+1] && table[i][j]==table[i][j+2] && table[i][j]==table[i+1][j+1] && table[i][j]==table[i+2][j+1]){
found++;
temp=table[i][j]+table[i][j+1]+table[i][j+2]+table[i+1][j+1]+table[i+2][j+1];
*score+=temp;
table[i][j]=NULL;
table[i][j+1]=NULL;
table[i][j+2]=NULL;
table[i+1][j+1]=NULL;
table[i+2][j+1]=NULL;
matrixShow(table,n);
pullDown(table,n,numbers);/*send the condition to pullDown function*/
printf("\nYou hit T structure +%d points.\nscore: %d\n",temp,*score);
matrixShow(table,n);
}
}
}
return found;
}
/**
****** STRUCTURE L ******
@param n size of matrix
@param *score game score
@param numbers range of numbers (2<x<10) (required by pullDown function)
@param table[100][100] board with numbers
______________________________________________________
!!!!!!! In the function used pullDown function;!!!!!!!
*/
int strL(int table[100][100],int n,int *score,int numbers){
int i,j,temp,found=0;
for(i=0;i<n-2;i++){
for(j=0;j<n-2;j++){
if(table[i][j]==table[i+1][j] && table[i][j]==table[i+2][j] && table[i][j]==table[i+2][j+1] && table[i][j]==table[i+2][j+2]){
found++;
temp=table[i][j]+table[i+1][j]+table[i+2][j]+table[i+2][j+1]+table[i+2][j+2];
*score+=temp;
table[i][j]=NULL;
table[i+1][j]=NULL;
table[i+2][j]=NULL;
table[i+2][j+1]=NULL;
table[i+2][j+2]=NULL;
matrixShow(table,n);
pullDown(table,n,numbers);/*send the condition to pullDown function*/
printf("\nYou hit L structure +%d points.\nscore: %d\n",temp,*score);
matrixShow(table,n);
}
}
}
return found;
}
/**
****** STRUCTURE C ******
@param n size of matrix
@param *score game score
@param numbers range of numbers (2<x<10) (required by pullDown function)
@param table[100][100] board with numbers
______________________________________________________
!!!!!!! In the function used pullDown function;!!!!!!!
*/
int strC(int table[100][100],int n,int *score,int numbers){
int i,j,temp,found=0;
for(i=0;i<n-2;i++){
for(j=0;j<n-2;j++){
if(table[i][j]==table[i+1][j] && table[i][j]==table[i+2][j] && table[i][j]==table[i+2][j+1]
&& table[i][j]==table[i+2][j+2] && table[i][j]==table[i][j+1] && table[i][j]==table[i][j+2]){
found++;
temp=table[i][j]+table[i+1][j]+table[i+2][j]+table[i+2][j+1]+table[i+2][j+2]+table[i][j+1]+table[i][j+2];
*score+=temp;
table[i][j]=NULL;
table[i+1][j]=NULL;
table[i+2][j]=NULL;
table[i+2][j+1]=NULL;
table[i+2][j+2]=NULL;
table[i][j+1]=NULL;
table[i][j+2]=NULL;
matrixShow(table,n);
pullDown(table,n,numbers);/*send the condition to pullDown function*/
printf("\nYou hit C structure +%d points.\nscore: %d\n",temp,*score);
matrixShow(table,n);
}
}
}
return found;
}
/**
****** STRUCTURE U ******
@param n size of matrix
@param *score game score
@param numbers range of numbers (2<x<10) (required by pullDown function)
@param table[100][100] board with numbers
______________________________________________________
!!!!!!! In the function used pullDown function;!!!!!!!
*/
int strU(int table[100][100],int n,int *score,int numbers){
int i,j,temp,found=0;
for(i=0;i<n-2;i++){
for(j=0;j<n-2;j++){
if(table[i][j]==table[i+1][j] && table[i][j]==table[i+2][j] && table[i][j]==table[i+2][j+1]
&& table[i][j]==table[i+2][j+2] && table[i][j]==table[i+1][j+2] && table[i][j]==table[i][j+2]){
found++;
temp=table[i][j]+table[i+1][j]+table[i+2][j]+table[i+2][j+1]+table[i+2][j+2]+table[i+1][j+2]+table[i][j+2];
*score+=temp;
table[i][j]=NULL;
table[i+1][j]=NULL;
table[i+2][j]=NULL;
table[i+2][j+1]=NULL;
table[i+2][j+2]=NULL;
table[i+1][j+2]=NULL;
table[i][j+2]=NULL;
matrixShow(table,n);
pullDown(table,n,numbers);/*send the condition to pullDown function*/
printf("\nYou hit U structure +%d points.\nscore: %d\n",temp,*score);
matrixShow(table,n);
}
}
}
return found;
}
/**
****** STRUCTURE O ******
@param n size of matrix
@param *score game score
@param numbers range of numbers (2<x<10) (required by pullDown function)
@param table[100][100] board with numbers
______________________________________________________
!!!!!!! In the function used pullDown function;!!!!!!!
*/
int strO(int table[100][100],int n,int *score,int numbers){
int i,j,temp,found=0;
for(i=0;i<n-2;i++){
for(j=0;j<n-2;j++){
if(table[i][j]==table[i+1][j] && table[i][j]==table[i+2][j] && table[i][j]==table[i+2][j+1]
&& table[i][j]==table[i+2][j+2] && table[i][j]==table[i][j+1] && table[i][j]==table[i][j+2]
&& table[i][j]==table[i+1][j+2]){
found++;
temp=table[i][j]+table[i+1][j]+table[i+2][j]+table[i+2][j+1]+table[i+2][j+2]+table[i][j+1]+table[i][j+2]+table[i+1][j+2];
*score+=temp;
table[i][j]=NULL;
table[i+1][j]=NULL;
table[i+2][j]=NULL;
table[i+2][j+1]=NULL;
table[i+2][j+2]=NULL;
table[i][j+1]=NULL;
table[i][j+2]=NULL;
table[i+1][j+2]=NULL;
matrixShow(table,n);
pullDown(table,n,numbers);/*send the condition to pullDown function*/
printf("\nYou hit O structure +%d points.\nscore: %d\n",temp,*score);
matrixShow(table,n);
}
}
}
return found;
}
/**
****** STRUCTURE E ******
@param n size of matrix
@param *score game score
@param numbers range of numbers (2<x<10) (required by pullDown function)
@param table[100][100] board with numbers
______________________________________________________
!!!!!!! In the function used pullDown function;!!!!!!!
*/
int strE(int table[100][100],int n,int *score,int numbers){
int i,j,temp,found=0;
for(i=0;i<n-2;i++){
for(j=0;j<n-2;j++){
if(table[i][j]==table[i+1][j] && table[i][j]==table[i+2][j] && table[i][j]==table[i+2][j+1]
&& table[i][j]==table[i+2][j+2] && table[i][j]==table[i][j+1] && table[i][j]==table[i][j+2]
&& table[i][j]==table[i+1][j+1]){
found++;
temp=table[i][j]+table[i+1][j]+table[i+2][j]+table[i+2][j+1]+table[i+2][j+2]+table[i][j+1]+table[i][j+2]+table[i+1][j+1];
*score+=temp;
table[i][j]=NULL;
table[i+1][j]=NULL;
table[i+2][j]=NULL;
table[i+2][j+1]=NULL;
table[i+2][j+2]=NULL;
table[i][j+1]=NULL;
table[i][j+2]=NULL;
table[i+1][j+1]=NULL;
matrixShow(table,n);
pullDown(table,n,numbers);/*send the condition to pullDown function*/
printf("\nYou hit E structure +%d points.\nscore: %d\n",temp,*score);
matrixShow(table,n);
}
}
}
return found;
}
/**
****** STRUCTURE F ******
@param n size of matrix
@param *score game score
@param numbers range of numbers (2<x<10) (required by pullDown function)
@param table[100][100] board with numbers
______________________________________________________
!!!!!!! In the function used pullDown function;!!!!!!!
*/
int strF(int table[100][100],int n,int *score,int numbers){
int i,j,temp,found=0;
for(i=0;i<n-2;i++){
for(j=0;j<n-2;j++){
if(table[i][j]==table[i+1][j] && table[i][j]==table[i+2][j] && table[i][j]==table[i][j+1] && table[i][j]==table[i][j+2] && table[i][j]==table[i+1][j+1]){
found++;
temp=table[i][j]+table[i+1][j]+table[i+2][j]+table[i][j+1]+table[i][j+2]+table[i+1][j+1];
*score+=temp;
table[i][j]=NULL;
table[i+1][j]=NULL;
table[i+2][j]=NULL;
table[i][j+1]=NULL;
table[i][j+2]=NULL;
table[i+1][j+1]=NULL;
matrixShow(table,n);
pullDown(table,n,numbers);/*send the condition to pullDown function*/
printf("\nYou hit F structure +%d points.\nscore: %d\n",temp,*score);
matrixShow(table,n);
}
}
}
return found;
}
/**
Main function. Reads parameters and controls the game.
*/
int main(){
int table[100][100],activeStructs[8];
int k=3,control=0,lastControl=0,newControl=1;
int i=0,j,N,numbers,score,row,column,direction,highScore=0;
char again,quit,highScoreName[20],structs[8],defaultStructs[8]={'O','E','U','C','F','L','T','I','\0'};
srand(time(NULL));
do{
for(i=0;i<9;i++){ /*Resetting matrix*/
activeStructs[i]=0;
}
/*Entering the size of matrix*/
printf("Enter the size of table ( N*N, N>5 ) N: ");
scanf("%d",&N);
while(N<6 || N>100){
printf("\n**Please check your input and enter again**\nN: ");
scanf("%d",&N);
}
/*Entering range of number*/
printf("Enter the range of numbers ( 2<numbers<10 ) : ");
scanf("%d",&numbers);
while(numbers<3 || numbers>9){
printf("\n**Please check your input and enter again**\nnumbers: ");
scanf("%d",&numbers);
}
/*Entering the structures*/
printf("Enter the structures ( O,E,U,C,F,L,T,I )\n=> ");
scanf("%s",structs);
i=0;
while(structs[i]){
for(j=0;j<8;j++){
if(structs[i]==defaultStructs[j]){
activeStructs[j]=1;
}
else if((structs[i]+'A'-'a')==defaultStructs[j]){
activeStructs[j]=1;
}
}
i++;
}
/*displaying active structures in binary model. 1-active; 0-off*/
for(j=0;j<8;j++){
printf("%d",activeStructs[j]);
}
printf("\n");
/** Initializing board **/
/*setting random numbers on the board*/
for(i=0;i<N;i++){
for(j=0;j<N;j++){
table[i][j]=rand()%(numbers-1)+2;
}
}
/** GAME BEGINS HERE! **/
matrixShow(table,N);
while(k>0){ /*k is the chances*/
printf("\nChange ");
scanf("%d %d %d",&row,&column,&direction);
exchange(table,row,column,direction);
newControl=1;
lastControl=0;
while(newControl!=lastControl){
lastControl=newControl;
/*check matrix as probabilities of structures*/
/* control=0 if there are any structure */
if(activeStructs[0]!=0) {control+=strO(table,N,&score,numbers);}
if(activeStructs[1]!=0) {control+=strE(table,N,&score,numbers);}
if(activeStructs[2]!=0) {control+=strU(table,N,&score,numbers);}
if(activeStructs[3]!=0) {control+=strC(table,N,&score,numbers);}
if(activeStructs[4]!=0) {control+=strF(table,N,&score,numbers);}
if(activeStructs[5]!=0) {control+=strL(table,N,&score,numbers);}
if(activeStructs[6]!=0) {control+=strT(table,N,&score,numbers);}
if(activeStructs[7]!=0) {control+=strI(table,N,&score,numbers);}
newControl=control;
}
if(control==0){ /*if control equals to 0, decline k(chances)*/
k--;
printf("\n**There was not any match\n");
}
matrixShow(table,N);/*display the table*/
printf("\nLives: %d\n",k);
control=0;
} /*While looping ends here*/
if(highScore<score){
highScore=score;
printf("\nScore: %d",score);
printf("\nYou hit new high score!!! Enter the name: ");
scanf("%s",&highScoreName);
}
k=3;
score=0;
newControl=1;
lastControl=0;
printf("\nHigh score:\t %d played by %s",highScore,highScoreName);
printf("\nWant to play again? (y/n) ");
scanf(" %c",&again);
}while(again=='y' || again=='Y');
system("pause");
}