-
Notifications
You must be signed in to change notification settings - Fork 0
/
myGame.c
308 lines (258 loc) · 7.99 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <windows.h>
#include <conio.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
// #define ANSI_COLOR_BLUE "\x1b[34m"
// #define ANSI_COLOR_MAGENTA "\x1b[35m"
// #define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
// 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("");
}
// Reads the user input character and return ascii value of that
int readEnteredKey()
{
char c;
c = getch();
if (c == -32)
c = getch();
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;
// Successfully swapped two numbers !
swap(&arr[i][j], &arr[i + 1][j]);
return 1; // 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("cls");
int i, j, k = 1;
printf("\t\t MATRIX PUZZLE\n");
printf("\n");
printf(ANSI_COLOR_RED " RULE OF THIS GAME \n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_RED "\n1.You can move only 1 step at a time by arrow key " ANSI_COLOR_RESET);
printf("\n\tMove Up : by Up arrow key ");
printf("\n\tMove Down : by Down arrow key");
printf("\n\tMove Left : by Left arrow key");
printf("\n\tMove Right: by Right arrow key");
printf(ANSI_COLOR_RED "\n2.You can move number at empty position only " ANSI_COLOR_RESET);
printf("\n");
printf(ANSI_COLOR_RED "\n3. For each valid move : your total number of move will decreased by 1 \n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_RED "4. Wining situation : " ANSI_COLOR_RESET);
printf(ANSI_COLOR_RED " Number in a 4*4 matrix should be in order from 1 to 15 " ANSI_COLOR_RESET);
printf("\n\n winning situation: \n");
printf(ANSI_COLOR_YELLOW "---------------------\n" ANSI_COLOR_RESET);
for (i = 0; i < 4; i++)
{
printf(ANSI_COLOR_YELLOW "| " ANSI_COLOR_RESET);
for (j = 0; j < 4; j++)
{
if (arr[i][j] != 0)
printf(ANSI_COLOR_YELLOW "%2d | " ANSI_COLOR_RESET, 4 * i + j + 1);
else
printf(ANSI_COLOR_YELLOW " |" ANSI_COLOR_RESET);
}
printf("\n");
}
printf(ANSI_COLOR_YELLOW "---------------------\n" ANSI_COLOR_RESET);
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("\nEnter any key to start..... ");
int x = readEnteredKey();
}
// main function
int main()
{
int arr[4][4]; // matrix
int maxTry = 1000; // maximum move
char name[20];
for (int k = 0; k < 3; k++)
printf("\n");
printf("Player Name: ");
scanf("%s", name);
system("cls"); // to clear screen
while (1)
{
createMatrix(arr); // calling function to create matrix
gameRule(arr); // game rule function calling
while (!winner(arr)) // checking for winning situation
{
system("cls");
if (!maxTry) // for remaining zero move
break;
printf("\n\n Player Name: %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 101: // ascii of E
case 69: // 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();
return 0;
case 72: // arrow up
if (shiftUp(arr))
maxTry--;
break;
case 80: // arrow down
if (shiftDown(arr))
maxTry--;
break;
case 77: // arrow right
if (shiftRight(arr))
maxTry--;
break;
case 75: // arrow left
if (shiftLeft(arr))
maxTry--;
break;
default:
printf("\n\n \a\a Not Allowed \a");
}
}
if (!maxTry)
printf(ANSI_COLOR_RED "\n\a YOU LOSE ! \a\a\n" ANSI_COLOR_RESET);
else
printf(ANSI_COLOR_GREEN "\n\a!!!!!!!!!!!!!Congratulations %s for winning this game !!!!!!!!!!!!!\n\a" ANSI_COLOR_RESET, name);
fflush(stdin); // Will clear the buffer
char check;
printf(ANSI_COLOR_GREEN "\nWant to play again ? \n" ANSI_COLOR_RESET);
printf("enter 'y' to play again: ");
scanf("%c", &check);
// Leave the game here itself !
if ((check != 'y') && (check != 'Y'))
break;
maxTry = 4;
}
return 0;
}