-
Notifications
You must be signed in to change notification settings - Fork 0
/
fifteen.c
255 lines (219 loc) · 4.81 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
/**
* fifteen.c
*
* Usage: fifteen d
*
* where the board's dimensions are to be d x d,
* where d must be in [DIM_MIN,DIM_MAX]
*/
#define _XOPEN_SOURCE 500
#include <stdbool.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 clear(void);
void greet(void);
void init(void);
void draw(void);
bool move(int tile);
bool won(void);
int main(int argc, char *argv[])
{
// ensure proper usage
int tile;
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: ");
scanf("%d",&tile);
// 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)
{
int a = d*d-1;
for(int row = 0; row < d; row++)
{
for(int col = 0; col < d; col++)
{
board[row][col] = a;
a--;
}
}
if(d%2 == 0)
{
board[d-1][d-2] = 2;
board[d-1][d-3] = 1;
}
}
/**
* Prints the board in its current state.
*/
void draw(void)
{
for (int i = 0; i < d; i++)
{
for(int j = 0; j < d; j++)
printf("%2d ",board[i][j]);
printf("\n");
}
}
/**
* If tile borders empty space, moves tile and returns true, else
* returns false.
*/
bool move(int tile)
{
int row0,col0;
for (int row = 0; row < d; row++)
{
for(int col = 0; col < d; col++)
{
if(board[row][col] == 0)
{
row0 = row;
col0 = col;
}
}
}
if (board[row0][col0 - 1] == tile)
{
board[row0][col0 - 1] = 0;
board[row0][col0] = tile;
return true;
}
else if(board[row0][col0 + 1] == tile)
{
board[row0][col0 + 1] = 0;
board[row0][col0] = tile;
return true;
}
else if(board[row0-1][col0] == tile)
{
board[row0-1][col0] = 0;
board[row0][col0] = tile;
return true;
}
else if(board[row0+1][col0] == tile)
{
board[row0+1][col0] = 0;
board[row0][col0] = tile;
return true;
}
return false;
}
/**
* Returns true if game is won (i.e., board is in winning configuration),
* else false.
*/
bool won(void)
{
int win[d][d];
for (int row = 0; row < d; row++)
{
for(int col = 0; col < d; col++)
{
win[row][col] = (row*d) + (col+1);
win[d-1][d-1] = 0;
if(board[row][col] != win[row][col])
return false;
}
}
return true;
}