Skip to content

Commit 4a98320

Browse files
author
Mykolas Krupauskas
committed
add fifteen start
1 parent 1790e53 commit 4a98320

File tree

1 file changed

+203
-4
lines changed

1 file changed

+203
-4
lines changed

chapter3/fifteen.c

Lines changed: 203 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,206 @@
1-
#include <stdio.h>
1+
/**
2+
* fifteen.c
3+
*
4+
* CS50 AP
5+
* Fifteen
6+
*
7+
* Implements Game of Fifteen (generalized to d x d).
8+
*
9+
* Usage: fifteen d
10+
*
11+
* whereby the board's dimensions are to be d x d,
12+
* where d must be in [DIM_MIN,DIM_MAX]
13+
*
14+
* Note that usleep is obsolete, but it offers more granularity than
15+
* sleep and is simpler to use than nanosleep; `man usleep` for more.
16+
*
17+
* Extra features including printing an actual grid to make it look more
18+
* tile-like, and using ANSI color sequences for some additional customizing
19+
*/
20+
21+
#define _XOPEN_SOURCE 500
22+
223
#include <cs50.h>
24+
#include <stdio.h>
25+
#include <stdlib.h>
26+
#include <unistd.h>
27+
28+
// constants
29+
#define DIM_MIN 3
30+
#define DIM_MAX 9
31+
32+
// ansi escape sequence to print grid color
33+
// replace the number beteen [ and m with 31 for red, 32 for green, 33 for brown,
34+
// 34 for blue, 35 for purple, 36 for cyan, 37 for gray
35+
#define COLOR "\033[32m"
36+
37+
// board
38+
int board[DIM_MAX][DIM_MAX];
39+
40+
// dimensions
41+
int d;
42+
43+
// saved locations of the blank tile
44+
int blank_row;
45+
int blank_col;
46+
47+
// prototypes
48+
void clear(void);
49+
void greet(void);
50+
void init(void);
51+
void draw(void);
52+
bool move(int tile);
53+
bool won(void);
54+
void swap(int *a, int *b);
55+
void print_grid_row(int d);
56+
void print_tile(int tile);
57+
58+
int main(int argc, string argv[])
59+
{
60+
// ensure proper usage
61+
if (argc != 2)
62+
{
63+
printf("Usage: fifteen d\n");
64+
return 1;
65+
}
66+
67+
// ensure valid dimensions
68+
d = atoi(argv[1]);
69+
if (d < DIM_MIN || d > DIM_MAX)
70+
{
71+
printf("Board must be between %i x %i and %i x %i, inclusive.\n",
72+
DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX);
73+
return 2;
74+
}
75+
76+
// open log
77+
FILE *file = fopen("log.txt", "w");
78+
if (file == NULL)
79+
{
80+
return 3;
81+
}
82+
83+
// greet user with instructions
84+
greet();
85+
86+
// initialize the board
87+
init();
88+
89+
// accept moves until game is won
90+
while (true)
91+
{
92+
// clear the screen
93+
clear();
94+
95+
// draw the current state of the board
96+
draw();
97+
98+
// log the current state of the board (for testing)
99+
for (int i = 0; i < d; i++)
100+
{
101+
for (int j = 0; j < d; j++)
102+
{
103+
fprintf(file, "%i", board[i][j]);
104+
if (j < d - 1)
105+
{
106+
fprintf(file, "|");
107+
}
108+
}
109+
fprintf(file, "\n");
110+
}
111+
fflush(file);
112+
113+
// check for win
114+
if (won())
115+
{
116+
printf("ftw!\n");
117+
break;
118+
}
119+
120+
// prompt for move
121+
printf("Tile to move: ");
122+
int tile = get_int();
123+
124+
// quit if user inputs 0 (for testing)
125+
if (tile == 0)
126+
{
127+
break;
128+
}
129+
130+
// log move (for testing)
131+
fprintf(file, "%i\n", tile);
132+
fflush(file);
133+
134+
// move if possible, else report illegality
135+
if (!move(tile))
136+
{
137+
printf("\nIllegal move.\n");
138+
usleep(500000);
139+
}
140+
141+
// sleep thread for animation's sake
142+
usleep(50000);
143+
}
144+
145+
// close log
146+
fclose(file);
147+
148+
// success
149+
return 0;
150+
}
151+
152+
/**
153+
* Clears screen using ANSI escape sequences.
154+
*/
155+
void clear(void)
156+
{
157+
printf("\033[2J");
158+
printf("\033[%d;%dH", 0, 0);
159+
}
160+
161+
/**
162+
* Greets player.
163+
*/
164+
void greet(void)
165+
{
166+
clear();
167+
printf("WELCOME TO GAME OF FIFTEEN\n");
168+
usleep(2000000);
169+
}
170+
171+
/**
172+
* Initializes the game's board with tiles numbered 1 through d*d - 1
173+
* (i.e., fills 2D array with values but does not actually print them).
174+
*/
175+
void init(void)
176+
{
177+
// TODO
178+
}
179+
180+
/**
181+
* Prints the board in its current state.
182+
*/
183+
void draw(void)
184+
{
185+
// TODO
186+
}
187+
188+
/**
189+
* If tile borders empty space, moves tile and returns true, else
190+
* returns false.
191+
*/
192+
bool move(int tile)
193+
{
194+
// TODO
195+
return false;
196+
}
3197

4-
int main ()
198+
/**
199+
* Returns true if game is won (i.e., board is in winning configuration),
200+
* else false.
201+
*/
202+
bool won(void)
5203
{
6-
7-
}
204+
// TODO
205+
return false;
206+
}

0 commit comments

Comments
 (0)