-
Notifications
You must be signed in to change notification settings - Fork 0
/
Animate.cpp
281 lines (213 loc) · 7.71 KB
/
Animate.cpp
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
/*
* Flicker-free implementation of the 8-puzzle board game using the BGI
* graphics library for Windows.
*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "graphics.h"
using namespace std;
#define OUTPUT_LENGTH 2 /* Length of output string. */
const int HEIGHT = 400; /**< Height of board for rendering in pixels. */
const int WIDTH = 400; /**< Width of board for rendering in pixels. */
/**
* Update the board and draw it to the screen. This function displays the
* board updates in a flicker-free way.
*
* @param board 3 x 3 array containing the current board state,
* 0 indicates an empty space.
*/
//////////////////////////////////////////////////////
// Function prototypes
void displayBoard(string const elements);
//
void AnimateSolution(string const initialState, string path);
void update(int **board);
//////////////////////////////////////////////////////
void update(int **board) {
/* Setting up the graphics. */
static bool setup = false;
if(!setup) {
int graphDriver = 0;
int graphMode = 0;
initgraph(&graphDriver, &graphMode, "", WIDTH, HEIGHT);
setup = true;
}
/* Variables for the function. */
int xIncrement = (WIDTH - 40) / 3; /* Grid's raster width. */
int yIncrement = ((HEIGHT - 6) - 40) / 3; /* Grid's raster height. */
int x = 0; /* Temporary x positions. */
int y = 0; /* Temporary y positions. */
char outputString[OUTPUT_LENGTH]; /* Holder for output strings in the GUI. */
static bool visual; /* Indicator which visual page to draw to
* to prevent flickers. */
/* Initalising the variables. */
strncpy(outputString, "", OUTPUT_LENGTH);
/* Even though this is not necessary here the protected version of "strcpy"
is used in this case. It should ALWAYS be used to prevent boundary
overwrites! */
/* Initialising the GUI. */
setactivepage(visual);
setbkcolor(BLACK);
cleardevice();
setfillstyle(SOLID_FILL, WHITE);
settextstyle(GOTHIC_FONT, HORIZ_DIR, 6);
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* Display different coloured squares for different numbers. */
y = 10;
for(int i = 0; i < 3; i++) {
x = 10;
for(int j = 0; j < 3; j++) {
if(board[i][j] != 0) {
setcolor(board[i][j]);
bar(x, y, x + xIncrement, y + yIncrement);
}
x += 10;
x += xIncrement;
}
y += 10;
y += yIncrement;
}
/* Display the actual numbers. */
y = 8 * HEIGHT / 40;
for(int i = 0; i < 3; i++) {
x = WIDTH / 6;
for(int j = 0; j < 3; j++) {
setcolor(WHITE);
setbkcolor(board[i][j]);
if(board[i][j] != 0) {
snprintf(outputString, OUTPUT_LENGTH, "%d", board[i][j]);
/* Even though this is also not necessary here the protected
version of "sprintf" is used in this case. It should ALWAYS
be used to prevent boundary overwrites! */
outtextxy(x, y, outputString);
moveto(0, 0);
}
x += 2 * (WIDTH / 6);
}
y += 13 * HEIGHT / 40;
}
/* Set the page to display. */
setvisualpage(visual);
visual = !visual;
delay(1000);
}
void displayBoard(string const elements) {
/* Setting up the graphics. */
int board[3][3];
int n=0;
for(int i=0; i < 3;i++){
for(int j=0; j < 3;j++){
board[i][j] = elements.at(n) - '0';
n++;
}
}
static bool setup = false;
if(!setup) {
int graphDriver = 0;
int graphMode = 0;
initgraph(&graphDriver, &graphMode, "", WIDTH, HEIGHT);
setup = true;
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 3);
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(getmaxx()/2,getmaxy()/2,"press any key to start.");
cout << endl << endl << "press any key to start." << endl << endl;
getch();
}
/* Variables for the function. */
int xIncrement = (WIDTH - 40) / 3; /* Grid's raster width. */
int yIncrement = ((HEIGHT - 6) - 40) / 3; /* Grid's raster height. */
int x = 0; /* Temporary x positions. */
int y = 0; /* Temporary y positions. */
char outputString[OUTPUT_LENGTH]; /* Holder for output strings in the GUI. */
static bool visual; /* Indicator which visual page to draw to
* to prevent flickers. */
/* Initalising the variables. */
strncpy(outputString, "", OUTPUT_LENGTH);
/* Even though this is not necessary here the protected version of "strcpy"
is used in this case. It should ALWAYS be used to prevent boundary
overwrites! */
/* Initialising the GUI. */
setactivepage(visual);
setbkcolor(BLACK);
cleardevice();
setfillstyle(SOLID_FILL, WHITE);
settextstyle(GOTHIC_FONT, HORIZ_DIR, 6);
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* Display different coloured squares for different numbers. */
y = 10;
for(int i = 0; i < 3; i++) {
x = 10;
for(int j = 0; j < 3; j++) {
if(board[i][j] != 0) {
setcolor(board[i][j]);
bar(x, y, x + xIncrement, y + yIncrement);
}
x += 10;
x += xIncrement;
}
y += 10;
y += yIncrement;
}
/* Display the actual numbers. */
y = 8 * HEIGHT / 40;
for(int i = 0; i < 3; i++) {
x = WIDTH / 6;
for(int j = 0; j < 3; j++) {
setcolor(WHITE);
setbkcolor(board[i][j]);
if(board[i][j] != 0) {
snprintf(outputString, OUTPUT_LENGTH, "%d", board[i][j]);
/* Even though this is also not necessary here the protected
version of "sprintf" is used in this case. It should ALWAYS
be used to prevent boundary overwrites! */
// outtextxy(x, y, (char *)elements[i+j*3]);
outtextxy(x, y, outputString);
moveto(0, 0);
}
x += 2 * (WIDTH / 6);
}
y += 13 * HEIGHT / 40;
}
/* Set the page to display. */
setvisualpage(visual);
visual = !visual;
delay(1000);
}
void AnimateSolution(string const initialState, string path){
int step=1;
// cout << endl << "********************************************************************" << endl;
cout << endl << "--------------------------------------------------------------------" << endl;
if (path==""){
cout << endl << "Nothing to animate." << endl;
} else {
cout << endl << "Animating solution..." << endl;
cout << "Solution path = " << path << endl;
}
Puzzle *p = new Puzzle(initialState, "123804765");
string strState;
strState = p->toString();
displayBoard(strState);
cout << "--------------------------------------------------------------------" << endl;
for(int i=0; i < path.length(); i++){
cout << endl << "Step #" << step << ") ";
switch(path[i]){
case 'U': p = p->moveUp(); cout << "[UP]" << endl;
break;
case 'D': p = p->moveDown(); cout << "[DOWN]" << endl;
break;
case 'L': p = p->moveLeft(); cout << "[LEFT]" << endl;
break;
case 'R': p = p->moveRight(); cout << "[RIGHT]" << endl;
break;
}
strState = p->toString();
displayBoard(strState);
step++;
}
delete p;
cout << endl << "Done." << endl;
cout << "--------------------------------------------------------------------" << endl;
getch();
}