-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfifteen.c
315 lines (267 loc) · 6.08 KB
/
fifteen.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
309
310
311
312
313
314
315
/**
* fifteen.c
*
* Computer Science 50
* Problem Set 3
*
* Implements Game of Fifteen (generalized to d x d).
*
* Usage: fifteen d
*
* whereby the board's dimensions are to be d x d,
* where d must be in [DIM_MIN,DIM_MAX]
*
* Note that usleep is obsolete, but it offers more granularity than
* sleep and is simpler to use than nanosleep; `man usleep` for more.
*/
#define _XOPEN_SOURCE 500
#include <cs50.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;
//global variables
int space;
int tile_x;
int tile_y;
// prototypes
void clear(void);
void greet(void);
void init(void);
void draw(void);
bool move(int tile);
bool won(void);
int main(int argc, string argv[])
{
// ensure proper usage
if (argc != 2)
{
printf("Usage: fifteen d\n");
return 1;
}
// ensure valid dimensions
d = atoi(argv[1]);
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 (true)
{
// clear the screen
clear();
// 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("ftw!\n");
break;
}
// prompt for move
printf("Tile to move: ");
int tile = GetInt();
// quit if user inputs 0 (for testing)
if (tile == 0)
{
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);
// success
return 0;
}
/**
* Clears screen using ANSI escape sequences.
*/
void clear(void)
{
printf("\033[2J");
printf("\033[%d;%dH", 0, 0);
}
/**
* Greets player.
*/
void greet(void)
{
clear();
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 mult = d * d;
int counter = 0;
// loop throug two-dimensional array
for(int i = 0; i < d; i ++)
{
for(int j = 0; j < d; j ++)
{
//Make array count friom largest element to smalest
counter++;
board[i][j] = mult - counter;
}
}
//If d is even swap 1 and 2
if ((d % 2) == 0)
{
int tmp = board[d - 1][d - 2];
board[d - 1][d - 2] = board[d - 1][d - 3];
board[d - 1][d - 3] = tmp;
}
}
/**
* Prints the board in its current state.
*/
void draw(void)
{
// TODO
int counter = 0;
//Loop through 2 dim array
for(int i = 0; i < d; i ++)
{
for(int j = 0; j < d; j ++)
{
counter ++;
// print numbers for all tiles but not for "0"
if(board[i][j] > 0)
{
printf("| %2d", board[i][j]);
}
//print 2 spaces for "0"
if(board[i][j] == 0)
{
printf("| __");
}
}
//print 2 lines to create smth like board
printf("|\n \n");
}
}
/**
* If tile borders empty space, moves tile and returns true, else
* returns false.
*/
bool move(int tile)
{
// TODO
int tile_x = 0;
int tile_y = 0;
//Loop through rows to find user's tile
for(int i = 0; i < d; i++)
{
//Loop through columns to find user's tile
for(int j = 0; j < d; j++)
{
// Find "0"
if (board[i][j] == 0)
{
tile_x = i;
tile_y = j;
}
}
}
//If title is adjescent to space, swap tile with space
if (tile == board[tile_x][tile_y - 1])
{
board[tile_x][tile_y] = tile;
board[tile_x][tile_y-1] = 0;
return true;
}
else if (tile == board[tile_x][tile_y + 1])
{
board[tile_x][tile_y] = tile;
board[tile_x][tile_y+1] = 0;
return true;
}
else if (tile == board[tile_x - 1][tile_y])
{
board[tile_x][tile_y] = tile;
board[tile_x - 1][tile_y] = 0;
return true;
}
else if (tile == board[tile_x + 1][tile_y])
{
board[tile_x][tile_y] = tile;
board[tile_x + 1][tile_y] = 0;
return true;
}
return false;
}
/**
* Returns true if game is won (i.e., board is in winning configuration),
* else false.
*/
bool won(void)
{
// TODO
int counter = 1;
//Loop through the board
for(int i = 0; i < d; i++)
{
for(int j = 0; j < d; j++)
{
if(board[i][j] == 0)
{
counter = 0;
}
//If tile is not equal to any counter, return false
if(board[i][j] != counter)
{
return false;
}
counter++;
}
}
//in another case return true
return true;
}