-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmyGame.c
executable file
·289 lines (249 loc) · 7.4 KB
/
myGame.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
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
//How to compile : gcc -o game myGame.c -lcurses
// how to run: ./game
// header files
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<string.h>
//#include<ncurses.h>
#include<curses.h> // for getch() equivalent in linux
// own heder files
#include"getchInLinux.h" // contain definition of getch() equivalent in linux i.e getchInLinux()
// create matrix 4*4
void createMatrix(int arr[][4])
{
int n=15;
int num[n],i,j;
for(i=0;i<15;i++) // These 1-15 number will be in th matrix
num[i]=i+1;
srand(time(NULL)); // for random number generation
int lastIndex=n-1,index;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(lastIndex>=0)
{ index=rand()%(lastIndex+1); // idea : performing % operation by (lastIndex+1)
arr[i][j]=num[index]; // will give index , so put that num[index] number in matrix
num[index]=num[lastIndex--]; // and replace last number with this indexed positioned number
} // and finally lastIndex--
}
arr[i-1][j-1]=0; // last number is zero
}
// showing matrix
void showMatrix(int arr[][4])
{
int i,j;
printf("----------------------\n");
for(i=0;i<4;i++)
{ printf("༏ ");
for(j=0;j<4;j++)
{ if(arr[i][j]!=0)
printf("%-2d ༏ ",arr[i][j]);
else
printf(" ༏ ");
}
printf("\n");
}
printf("----------------------\n");
}
// winning check by this function
int winner(int arr[][4])
{
int i,j,k=1;
for(i=0;i<4;i++)
{ for(j=0;j<4;j++,k++)
if(arr[i][j]!=k && k!=16)
break;
if(j<4)
break;
}
if(j<4)
return 0;
return 1;
}
// swap function to swap two numbers
void swap(int *x,int *y)
{
*x=*x+*y;
*y=*x-*y;
*x=*x-*y;
printf("\a\a\a");
}
// Reads the user input character and return ascii value of that
int readEnteredKey()
{
char c;
c = getchInLinux();
if(c==27 || c==91) // this is just for scan code
{ c = getchInLinux();
if(c==27 || c==91) // also for scan code
c=getchInLinux();
}
return c;
}
// shift up function
int shiftUp(int arr[][4])
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
if(arr[i][j]==0)
break;
if(j<4)
break;
}
if(i==3) // shifting not possible
return 0;
swap(&arr[i][j],&arr[i+1][j]);
return 1; // shift up success
}
int shiftDown(int arr[][4])
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
if(arr[i][j]==0)
break;
if(j<4)
break;
}
if(i==0) // shifting not possible
return 0;
swap(&arr[i][j],&arr[i-1][j]); // swap numbers
return 1; // shift up success
}
int shiftRight(int arr[][4])
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
if(arr[i][j]==0)
break;
if(j<4)
break;
}
if(j==0) // shifting not possible
return 0;
swap(&arr[i][j],&arr[i][j-1]);
return 1; // shift up success
}
int shiftLeft(int arr[][4])
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
if(arr[i][j]==0)
break;
if(j<4)
break;
}
if(j==3) // shifting not possible
return 0;
swap(&arr[i][j],&arr[i][j+1]);
return 1; // shift up success
}
// Game rules
void gameRule(int arr[][4])
{
system("clear");
int x=readEnteredKey();
int i,j,k=1;
printf(" RULE OF THIS GAME \n");
printf("\n1.You can move only 1 step at a time by arrow key ");
printf("\nMove Up : by Up arrow key ");
printf("\nMove Down : by Down arrow key");
printf("\nMove Left : by Left arrow key");
printf("\nMove Right: by Right arrow key");
printf("\n2.You can move number at empty position only ");
printf("\n");
printf("\n3.For each valid move : your total number of move will decreased by 1 \n");
printf("4.Wining situation : ");
printf("number in a 4*4 matrix should be in order from 1 to 15 ");
printf("\n\n winning situation: \n");
printf("----------------------\n");
for(i=0;i<4;i++)
{ printf("༏ ");
for(j=0;j<4;j++)
{ if(arr[i][j]!=0)
printf("%-2d ༏ ",4*i+j+1);
else
printf(" ༏ ");
}
printf("\n");
}
printf("----------------------\n");
printf("\n5.You can exit the game at any time by pressing 'E' or 'e' ");
printf("\nSo Try to win in minimum no of move \n");
printf("\n Happy gaming , Good Luck\n");
printf("\nEnter any key to start..... 🆗 : ");
x=readEnteredKey();
}
// main function
int main()
{
int arr[4][4]; // matrix
int maxTry=400; // maximum move
char name[20];
system("clear"); // to clear screen
printf("Enter Your Good Name: ");
scanf("%s",name);
while(1)
{ createMatrix(arr); // calling funcn to create matrix
gameRule(arr); // game rule function calling
while(!winner(arr)) // checking for winning situation
{
system("clear");
if(!maxTry) // for remaing zero move
break;
printf("\n\n welcome %s , Move remaining : %d\n\n",name,maxTry);
showMatrix(arr); // show matrix
int key=readEnteredKey(); // this will return ascii code of user entered key
switch(key) // maping
{
case 69: // ascii of E
case 101: // ascii of e
printf("\a\a\a\a\a\a\n Thanks for Playing ! \n\a");
printf("\nHit 'Enter' to exit the game \n");
key=readEnteredKey();
exit(0);
case 65: // arrow up
if(shiftUp(arr))
maxTry--;
break;
case 66: // arrow down
if(shiftDown(arr))
maxTry--;
break;
case 67: // arrow right
if(shiftRight(arr))
maxTry--;
break;
case 68: // arrow left
if(shiftLeft(arr))
maxTry--;
break;
default:
printf("\n\n \a\a❌ Not Allowed \a\a\a\a");
}
}
if(!maxTry)
printf("\n\a YOU LOSE ! \a\a\n");
else
{ printf("\n\a!!!!!!!!!!!!!Congratualization !!!!!!!!!!!!!\n\a");
printf("\a You have Won \a\a\a ");
}
char check;
printf("\nwant to play again ? \n");
printf("enter 'y' to play again: ");
scanf("%c",&check);
if(check!='y' && check!='Y')
break;
maxTry=400;
}
return 0;
}