-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
294 lines (246 loc) · 6.37 KB
/
main.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
//
//#define _XOPEN_SOURCE 500
#include<ctype.h>
#include<stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
// constants
#define DIM_MIN 3
#define DIM_MAX 9
// board
int board[DIM_MAX][DIM_MAX];
// dimensions
int d;
// prototypes
void greet(void);
void init(void);
void draw(void);
int move(int tile);
int won(void);
int main()
{ char choice;
do{int n;
system("cls");
printf("***********************************************************WELCOME TO GAME OF FIFTEEN!**********************************\n");
printf("\nINSTRUCTIONS FOR USAGE!\n\n1)THE OBJECTIVE: The grid of numbers is to be arranged in ascending order.\n\n2)THE CONTROLS: Simply press the number of the tile to be moved. Game allows only those tiles to be moved which are :\n\na)Horizontally adjacent or\n\nb)Vertically adjacent\n\nto the blank tile!");
printf("\n\n3)NOTE: In case of improper usage, you'll be notified and asked for the proper move. \n\n\nALL THE BEST!!!\n\n\n");
printf("Enter Difficulty: \n\n1)Easy(3x3)\n\n2)Medium(4x4)\n\n3)Difficult(5x5)\n");
scanf("%d",&n);
if(n==1)
d=3;
else if(n==2)
d=4;
else if(n==3)
d=5;
else
{printf("Please enter correct choice.");return 2;}
// ensure valid dimensions
if (d < DIM_MIN || d > DIM_MAX)
{
printf("Board must be between %i x %i and %i x %i, inclusive.\n",
DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX);
return 2;
}
// open log
FILE *file = fopen("log.txt", "w");
if (file == NULL)
{
return 3;
}
// greet user with instructions
greet();
// initialize the board
init();
// accept moves until game is won
while (1)
{
// clear the screen
// clear();
system("cls");
// draw the current state of the board
draw();
// log the current state of the board (for testing)
for (int i = 0; i < d; i++)
{ for (int j = 0; j < d; j++)
{
fprintf(file, "%i", board[i][j]);
if (j < d - 1)
{
fprintf(file, "|");
}
}
fprintf(file, "\n");
}
fflush(file);
// check for win
if (won())
{
printf("\n\n!!!!!!!!!!!!!!!!!!!!!!CONGRATULATIONS!!!!!!!!!!!!!!!!!!!\n\nYOU HAVE SOLVED THE PUZZLE!!\n\nPress Y to play again, N to exit! \n\n");
fflush(stdin);scanf(" %c",&choice);
break;
}
// prompt for move
printf("\n\nTile to move: ");
int tile;
scanf("%d",&tile);
// quit if user inputs 0 (for testing)
if (tile == 0)
{ printf("\n\nYou have successfully left the previous game! \n\nPress Y to restart the game or N to exit! ");
fflush(stdin);scanf(" %c",&choice);
break;
}
// log move (for testing)
fprintf(file, "%i\n", tile);
fflush(file);
// move if possible, else report illegality
if (!move(tile))
{
printf("\nIllegal move.\n");
usleep(500000);
}
// sleep thread for animation's sake
usleep(500000);
}
// close log
fclose(file);
}while(choice=='Y' || choice=='y');
// success
return 0;
}
/**
* Greets player.
*/
void greet(void)
{
// clear();
system("cls");
printf("WELCOME TO GAME OF FIFTEEN\n");
usleep(2000000);
}
/**
* Initializes the game's board with tiles numbered 1 through d*d - 1
* (i.e., fills 2D array with values but does not actually print them).
*/
void init(void)
{
// TODO
int p=d*d;
for(int i=0;i<d;i++)
{
for(int j=0;j<d;j++)
{
board[i][j]=--p;
}
}
if(d%2==0)
{
board[d-1][d-3]=1;
board[d-1][d-2]=2;
}
board[d-1][d-1]='_';
}
/**
* Prints the board in its current state.
*/
void draw(void)
{ printf("!!!!!!!!!!!!PRESS 0 TO QUIT!!!!!!!!!!!");
printf("\n");for(int i=0;i<=(d*16);i++)printf("-");
for(int i=0; i<d; i++)
{ char ch='_';printf("\n");
for(int j=0; j<d; j++)
{
if(board[i][j]!='_')
printf("\t%3i\t|",board[i][j]);
else
printf("\t%3c\t|",ch);
}
printf("\n");for(int i=0;i<=(d*16);i++)printf("-");
}
}
/**
* If tile borders empty space, moves tile and returns true, else
* returns false.
*/
int move(int tile)
{ int br,bc;
for(int i=0;i<d;i++)
{
for(int j=0;j<d;j++)
{
if(board[i][j]=='_')
{
br=i;
bc=j;
}
}
}
for(int i=0;i<d;i++)
{
for(int j=0;j<d;j++)
{
if(board[i][j]==tile)
{
if(br==i)
{
if(bc==j-1)
{
int tmp = board[i][j];
board[i][j]=board[br][bc];
board[br][bc]=tmp;
return 1;
}
else if(bc==j+1)
{
int tmp = board[i][j];
board[i][j]=board[br][bc];
board[br][bc]=tmp;
return 1;
}
else
return 0;
}
else if(bc==j)
{
if(br==i-1)
{
int tmp = board[i][j];
board[i][j]=board[br][bc];
board[br][bc]=tmp;
return 1;
}
else if(br==i+1)
{
int tmp = board[i][j];
board[i][j]=board[br][bc];
board[br][bc]=tmp;
return 1;
}
else
return 0;
}
}
}
}
return 0;
}
/**
* Returns true if game is won (i.e., board is in winning configuration),
* else
.
*/
int won(void)
{
int arr[d*d],k=1;
for(int i=1;i<=(d*d);i++)
arr[i]=i;arr[(d*d)]='_';
for(int i=0;i<d;i++)
{
for(int j=0;j<d;j++)
{
if(board[i][j]!=arr[k++])
return 0;
}
}
return 1;
}