-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberCrash_v2.c
More file actions
312 lines (296 loc) · 10.7 KB
/
Copy pathNumberCrash_v2.c
File metadata and controls
312 lines (296 loc) · 10.7 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 100
void generateNew(int (*)[],int,int); /// fill empty blocks with random numbers
void pullDown(int (*)[],int,int); /// pull non empty blocks down
void checkForCrash(int (*)[],int ,int ,int ,int *,int, int , int , int); /// check 4-connectivity for crashing items
int traverseMatrix(int (*)[],int ,int ,int *); /// traverse matrix items
void exchange(int (*)[],int , int ,int ,int ,int ,int ); /// exchange 2 items
void matrixShow(int (*)[],int , int , int); /// display matrix
void main()
{
system("COLOR F0");
srand(time(NULL));
int N;
int M;
int i;
int j;
int crashed=0;
int moved=0;
int lives=1;
int table[SIZE][SIZE];
int score,row_1,column_1,row_2,column_2;
char again,quit;
srand(time(NULL));
/*Entering the size of matrix*/
printf("Enter the size of table ( N*M, N>5, M>5 )");
printf("\n\tN: ");
scanf("%d",&N);
while(N<4 || N>SIZE){
printf("\n**Please check your input and enter again**\n\tN: ");
scanf("%d",&N);
}
printf("\tM: ");
scanf("%d",&M);
while(M<4 || M>SIZE){
printf("\n**Please check your input and enter again**\n\tM: ");
scanf("%d",&M);
}
// N=6;
// M=5;
printf("\n\tNumber Crash\n");
do{
/** Initializing board **/
/*setting random numbers on the board*/
for(i=0;i<N;i++){
for(j=0;j<M;j++){
table[i][j]=rand()%10;
}
}
// table[0][0] = 5;
// table[0][1] = 5;
// table[1][0] = 5;
// table[1][1] = 5;
// table[2][2] = 2;
// table[3][2] = 2;
// table[3][3] = 2;
// table[3][4] = 2;
// table[4][3] = 2;
/** GAME BEGINS HERE! **/
moved = 0;
while(lives>0){
matrixShow(table,N,M,0);
crashed = traverseMatrix(table,N,M,&score);
if(moved && !crashed){
printf("\nOoops! No crashed, live -1\n");
lives--;
//printf("\nLives: %d\n",lives);
}
if(!crashed){
printf("\nLives: %d\n",lives);
printf("\nChange: ");
scanf("%d %d %d %d",&row_1,&column_1,&row_2,&column_2);
exchange(table,N,M,row_1,column_1,row_2,column_2);
moved=1;
}
} /*While looping ends here*/
printf("Your Score: %d",score);
lives=3;
score=0;
printf("\nWant to play again? (y/n) ");
scanf(" %c",&again);
}while(again=='y' || again=='Y');
printf("Game terminated");
system("pause");
}
/**
function of displaying the board.
@param N row size of matrix
@param M column size of matrix
@param doDelay 1 - delay for a bit, then continue, 0 - user should press key to continue
@param table[SIZE][SIZE] board with numbers
*/
void matrixShow(int table[SIZE][SIZE],int N, int M, int doDelay){
int i,j;
/*printing column indexes of matrix (x-axis) to make easy understand which number we have chosen*/
printf("\n\t ");
for(i=0;i<M;i++){
printf("%2d",i);
}
printf("\n\t ");
for(i=0;i<M;i++){
printf("--");
}
printf("\n"); /*columns finished*/
for(i=0;i<N;i++){
printf("\t%2d|",i);/* printing row indexes */
for(j=0;j<M;j++){/*start to printing matrix's elements*/
if(table[i][j]>=0){
// switch(table[i][j]){
// case 0: printf("\033[1;30m %2d",table[i][j]); break;
// case 1: printf("\033[1;31m %2d",table[i][j]); break;
// case 2: printf("\033[1;32m %2d",table[i][j]); break;
// case 3: printf("\033[1;33m %2d",table[i][j]); break;
// case 4: printf("\033[1;34m %2d",table[i][j]); break;
// case 5: printf("\033[1;35m %2d",table[i][j]); break;
// case 6: printf("\033[1;36m %2d",table[i][j]); break;
// case 7: printf("\033[1;37m %2d",table[i][j]); break;
// case 8: printf("\033[1;38m %2d",table[i][j]); break;
// case 9: printf("\033[1;39m %2d",table[i][j]); break;
// }
printf("%2d",table[i][j]);
}
else{
printf("%2c",table[i][j]);
}
}
printf("\n");
}
printf("\n");
if(doDelay){
for (i = 0; i <= 32767; i++)
for (j = 0; j <= 12767; j++)
{}
}
else{
system("pause");
}
}
/**
function of exchanging two values of matrix.
@param N row size of matrix
@param M column size of matrix
@param table[SIZE][SIZE] board with numbers
*/
void exchange(int table[SIZE][SIZE],int N, int M,int row_1,int column_1,int row_2,int column_2){
int temp;
if((row_1<0 || row_1>=N) || (row_2<0 || row_2>=N)){
printf("\n Out of bounds \n"); return;
}
if((column_1<0 || column_1>=M) || (column_2<0 || column_2>=M)){
printf("\n Out of bounds \n"); return;
}
temp=table[row_1][column_1];
table[row_1][column_1]=table[row_2][column_2];
table[row_2][column_2]=temp;
}
/**
****** Traverse matrix or crashing items ******
@param n row size of matrix
@param m column size of matrix
@param *score game score
@param table[SIZE][SIZE] board with numbers
______________________________________________________
*/
int traverseMatrix(int table[SIZE][SIZE],int n,int m,int *score){
int i,j,temp,crashed=0,length;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
length = 0;
if(table[i][j]>=0){
checkForCrash(table,n,m,table[i][j],&length,-1,-1,i,j);
//printf("\n \t length: %d",length);
}
if(length){
//matrixShow(table,n,m,1);
crashed+=length;
printf("\n \t crashed items: %d\t\t",crashed);
system("pause");
}
}
}
if(crashed){
*score = (*score) + (crashed*crashed);
printf("\n \t Your Score: %d\t\t",*score);
pullDown(table,n,m);
matrixShow(table,n,m,0);
generateNew(table,n,m);
matrixShow(table,n,m,0);
}
return crashed;
}
/**
****** Crash items if possible ******
@param n row size of matrix
@param m column size of matrix
@param object check neighbors for this item
@param *length crashed item count
@param loc_x_prev previous item location row
@param loc_y_prev previous item location column
@param loc_x current item location row
@param loc_y current item location column
@param table[SIZE][SIZE] board with numbers
______________________________________________________
!!!!!!! In the function used pullDown function;!!!!!!!
*/
void checkForCrash(int table[SIZE][SIZE],int n,int m, int object,int *length,
int loc_x_prev, int loc_y_prev,int loc_x, int loc_y){
int selfCrash = 0;
if((loc_x<0 || loc_x>=n) || (loc_y<0 || loc_y>=m) || (table[loc_x][loc_y]!=object) ){
//printf(" loc %d %d returning 0",loc_x,loc_y);
return ;
}
else{
//printf("\n\tchecking [%d, %d]",loc_x,loc_y);
if( table[loc_x][loc_y] != -1 && (loc_y_prev != loc_y+1) && loc_y+1 != m && (table[loc_x][loc_y+1]==object) ){
table[loc_x][loc_y] = -1;
(*length)++;
selfCrash = 1;
printf("\t\t[%d, %d] %d crashed, going right",loc_x,loc_y,object);
matrixShow(table,n,m,1);
checkForCrash(table,n,m,object,length,loc_x,loc_y,loc_x,loc_y+1);
}
if( table[loc_x][loc_y] != -1 && (loc_y_prev != loc_y-1) && loc_y-1 != -1 && (table[loc_x][loc_y-1]==object) ){
table[loc_x][loc_y] = -1;
(*length)++;
selfCrash = 1;
printf("\t\t[%d, %d] %d crashed, going left",loc_x,loc_y,object);
matrixShow(table,n,m,1);
checkForCrash(table,n,m,object,length,loc_x,loc_y,loc_x,loc_y-1);
}
if( table[loc_x][loc_y] != -1 && (loc_x_prev != loc_x-1) && loc_x-1 != -1 &&(table[loc_x-1][loc_y]==object)){
table[loc_x][loc_y] = -1;
(*length)++;
selfCrash = 1;
printf("\t\t[%d, %d] %d crashed, going up",loc_x,loc_y,object);
matrixShow(table,n,m,1);
checkForCrash(table,n,m,object,length,loc_x,loc_y,loc_x-1,loc_y);
}
if( table[loc_x][loc_y] != -1 && (loc_x_prev != loc_x+1) && loc_x+1 != n && (table[loc_x+1][loc_y]==object)){
table[loc_x][loc_y] = -1;
(*length)++;
printf("\t\t[%d, %d] %d crashed, going down",loc_x,loc_y,object);
matrixShow(table,n,m,1);
selfCrash = 1;
checkForCrash(table,n,m,object,length,loc_x,loc_y,loc_x+1,loc_y);
}
if(table[loc_x][loc_y] != -1 && table[loc_x][loc_y]==object && *length!=0){
table[loc_x][loc_y] = -1;
printf("\t[%d, %d] crashed",loc_x,loc_y);
matrixShow(table,n,m,1);
(*length)++;
}
if(selfCrash){
checkForCrash(table,n,m,object,length,loc_x,loc_y,loc_x,loc_y-1);
checkForCrash(table,n,m,object,length,loc_x,loc_y,loc_x-1,loc_y);
checkForCrash(table,n,m,object,length,loc_x,loc_y,loc_x+1,loc_y);
}
}
}
/**
function of pulling numbers down after detecting empty cells below.
@param n row size of matrix
@param m column size of matrix
@param table[100][100] board with numbers
*/
void pullDown(int table[SIZE][SIZE],int n,int m){
int i,j,k,temp;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if (table[i][j]==-1 && 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;
}
}
}
}
}
/**
Generates numbers on empty cells;
@param n row size of matrix
@param m column size of matrix
@param table[100][100] board with numbers
*/
void generateNew(int table[SIZE][SIZE],int n,int m){
int i,j,k,temp;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(table[i][j]==-1){
table[i][j]=rand()%10;
}
}
}
}