-
Notifications
You must be signed in to change notification settings - Fork 1
/
board.cpp
executable file
·378 lines (320 loc) · 10.4 KB
/
board.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "board.h"
//Constructor
Board::Board(sf::RenderWindow &window) :window_game(window) {
float scalex, scaley;
winlove.loadFromFile("pics/gril.png");
fullwall.loadFromFile("pics/fullwall.png");
secondplayer.loadFromFile("pics/love.png");
firstplayer.loadFromFile("pics/unlove.png");
winunlove.loadFromFile("pics/winunlove.png");
emptywall.loadFromFile("pics/emptywall.png");
emptyground.loadFromFile("pics/emptyground.png");
ptex = NULL;
delay = delay = 10;
windowWidth = window_game.getSize().x;
windowHeight = window_game.getSize().y;
costat = (windowHeight - (delay + delay))/12;
for(int i = 0; i < BOARD_X_SIZE+BOARD_X_SIZE-1; ++i) {
if(i%2 == 1)
for(int j = 0; j < BOARD_Y_SIZE+BOARD_Y_SIZE-1; ++j){
if(j%2 == 1 and i != 0 and j != 0) ground[i][j] = full_wall;
else ground[i][j] = empty_wall;
}
else {
for(int j = 0; j < BOARD_Y_SIZE+BOARD_Y_SIZE-1; ++j){
if(j%2 == 1) ground[i][j] = empty_wall;
else ground[i][j] = empty_ground;
}
}
}
build_wall(8, 1);
build_wall(8, 15);
}
//Restart
void Board::restart(){
for(int i = 0; i < BOARD_X_SIZE+BOARD_X_SIZE-1; ++i) {
if(i%2 == 1)
for(int j = 0; j < BOARD_Y_SIZE+BOARD_Y_SIZE-1; ++j){
if(j%2 == 1 and i != 0 and j != 0) ground[i][j] = full_wall;
else ground[i][j] = empty_wall;
}
else {
for(int j = 0; j < BOARD_Y_SIZE+BOARD_Y_SIZE-1; ++j){
if(j%2 == 1) ground[i][j] = empty_wall;
else ground[i][j] = empty_ground;
}
}
}
build_wall(8, 1);
build_wall(8, 15);
}
//Destructor
Board:: ~Board(){};
//Return true if there is a wall on the position x, y, of the board
bool Board::is_wall_zone (int x, int y){ /*done*/
return (ground[x][y] == empty_wall or ground[x][y] == full_wall);
}
//Return true if there is a full wall on the position x, y, of the board
bool Board::is_full_ground (int x, int y){ /*done*/
return (ground[x][y] == full_ground);
}
//Return true if there is a full wall on the position x, y, of the board
bool Board::is_full_wall (int x, int y){ /*done*/
return (ground[x][y] == full_wall);
}
//Return true if there is an empty wall on the position x, y, of the board
bool Board::is_empty_wall (int x, int y){ /*done*/
return (ground[x][y] == empty_wall);
}
//Changes the actual state to either empty or full wall in the position x, y, of the board
void Board::change_wall(int x, int y){ /*done*/
if (ground[x][y] == empty_wall) ground[x][y] = full_wall;
else ground[x][y] = empty_wall;
}
//For a given x, return true if there are no walls between the initial y and the final y
bool Board::not_a_wall_between_y (int inity, int lasty, int x){ /*done*/
int y, z;
if(inity < lasty) { y = inity; z = lasty; }
else { y = lasty; z = inity; }
for (int i = y; i < z; ++i){
if((ground[x][i] == full_wall)) return false;
}
return true;
}
//For a given y, return true if there are no walls between the initial x and the final x
bool Board::not_a_wall_between_x (int initx, int lastx, int y){ /*done*/
int x, z;
if(initx < lastx) { x = initx; z = lastx; }
else { x = lastx; z = initx; }
for (int i = x; i < z; ++i){
if((ground[i][y] == full_wall)) return false;
}
return true;
}
//Convert the input of the mouse in a position on the wall
int Board::fixed (int k){ /* PER IAs*/
/*
int b = costat + costat/3;
int fk = (k-delay)/b;
int elemPos = fk*2;
if((k-delay)%b > costat) elemPos++;
return elemPos;*/ return k;
}
int Board::fixed (int k, bool useless){
int b = costat + costat/3;
int fk = (k-delay)/b;
int elemPos = fk*2;
if((k-delay)%b > costat) elemPos++;
return elemPos;
}
//Display a player on the given position and changes the board table and the player position
void Board::draw_player (int x, int y, playerenum player){ /*done*/
ptex = &emptyground;
int pos_x, pos_y;
float scalex, scaley;
ground[x][y] = full_ground;
pos_x = delay + (x/2)*costat + (x/2)*(costat/3);
pos_y = delay + (y/2)*costat + (y/2)*(costat/3);
scalex = costat/float(ptex->getSize().x);
scaley = costat/float(ptex->getSize().y);
sprite.setTexture(*ptex);
sprite.setPosition(pos_x, pos_y);
sprite.setScale(scalex, scaley);
window_game.draw(sprite);
if(player == first) ptex = &firstplayer;
else ptex = &secondplayer;
sprite.setTexture(*ptex);
sprite.setPosition(pos_x, pos_y);
scalex = costat/float(ptex->getSize().x);
scaley = costat/float(ptex->getSize().y);
sprite.setScale(scalex, scaley);
window_game.draw(sprite);
window_game.display();
}
//Set the x, y, position of the board to empty_ground
void Board::delete_player(int x, int y){ /*done*/
ptex = &emptyground;
int pos_x, pos_y;
float scalex, scaley;
ground[x][y] = empty_ground;
pos_x = delay + (x/2)*costat + (x/2)*(costat/3);
pos_y = delay + (y/2)*costat + (y/2)*(costat/3);
scalex = costat/float(ptex->getSize().x);
scaley = costat/float(ptex->getSize().y);
sprite.setTexture(*ptex);
sprite.setPosition(pos_x, pos_y);
sprite.setScale(scalex, scaley);
window_game.draw(sprite);
window_game.display();
}
//Return true if there are no walls after the wall you want to move
bool Board::legitmove (int origx, int origy, int destx, int desty){
if(origx < 0 || origy < 0 || destx < 0 || desty < 0)return false;
if(origx > 16 or origy > 16 or destx > 16 or desty > 16)return false;
if(origx == destx) {
if(desty > origy) return (ground[origx][desty-1] == empty_wall);
if(desty < origy) return (ground[origx][origy-1] == empty_wall);
}
else if(origy == desty) {
if(destx > origx) return (ground[destx-1][origy] == empty_wall);
if(destx < origx) return (ground[origx-1][origy] == empty_wall);
}
return true;
}
//Draws the visible board with the values of the board table
void Board::draw(){ /*done*/
int pos_x , pos_y;
float scalex, scaley;
bool do_print = false;
for(int x = 0; x < 17; ++x){
for(int y = 0; y < 17; ++y){
//Set Values
if(ground[x][y] == empty_ground){ //ground
pos_x = delay + (x/2)*costat + (x/2)*(costat/3);
pos_y = delay + (y/2)*costat + (y/2)*(costat/3);
do_print = true;
ptex = &emptyground;
scalex = costat/float(ptex->getSize().x);
scaley = costat/float(ptex->getSize().y);
}
else if(x%2 == 1 and y%2 == 1 and (x != 0) and (y != 0)) { //murs fixes
pos_x = delay + (x/2+1)*costat + (x/2)*(costat/3);
pos_y = delay + (y/2+1)*costat + (y/2)*(costat/3);
do_print = true;
ptex = &fullwall;
scalex = (costat/3)/float(ptex->getSize().x);
scaley = (costat/3)/float(ptex->getSize().y);
}
else if(ground[x][y] == full_wall){ //murs construits
if(x%2 == 0){ //fullwallvert
pos_x = delay + (x/2)*costat + (x/2)*(costat/3);
pos_y = delay + (y/2+1)*costat + (y/2)*(costat/3);
do_print = true;
ptex = &fullwall;
scalex = (costat)/float(ptex->getSize().x);
scaley = (costat/3)/float(ptex->getSize().y);
}
if(x%2 == 1){ //fullwallhor
pos_x = delay + (x/2+1)*costat + (x/2)*(costat/3);
pos_y = delay + (y/2)*costat + (y/2)*(costat/3);
do_print = true;
ptex = &fullwall;
scalex = (costat/3)/float(ptex->getSize().x);
scaley = (costat)/float(ptex->getSize().y);
}
}
else if(ground[x][y] == empty_wall){ //empty wall
if(x%2 == 0){ //verticalemptywall
pos_x = delay + (x/2)*(costat) + (x/2)*(costat/3);
pos_y = delay + (y/2+1)*costat + (y/2)*(costat/3);
do_print = true;
ptex = &emptywall;
scalex = (costat)/float(ptex->getSize().x);
scaley = (costat/3)/float(ptex->getSize().y);
}
if(x%2 == 1){ //horitzontalemptywall
pos_x = delay + (x/2+1)*costat + (x/2)*(costat/3);
pos_y = delay + (y/2)*costat + (y/2)*(costat/3);
do_print = true;
ptex = &emptywall;
scalex = (costat/3)/float(ptex->getSize().x);
scaley = (costat)/float(ptex->getSize().y);
}
}
//print with values
if(do_print){
do_print = false;
sprite.setTexture(*ptex);
sprite.setPosition(pos_x, pos_y);
sprite.setScale(scalex, scaley);
window_game.draw(sprite);
}
}
}
//and display
window_game.display();
}
//Call to the function draw_wall with the boolean value which selects either if vertical or horizontal
void Board::build_wall (int x, int y){ /*done*/
bool vertical = true;
if(x%2 == 1) vertical = false;
if(vertical){
draw_wall (x, y, true);
}
else {
draw_wall (x, y, false);
}
}
//Changes the value of the position x, y, on the board to full ground
void Board::draw_wall(int x, int y, bool vertical){ /*done*/
ground[x][y] = full_wall;
}
//Changes the value of the position x, y, on the board to empty ground
void Board::unbuild_wall(int x, int y){ /*done*/
bool vertical = (x%2 != 1);
if(vertical){
undraw_wall(x, y, true);
}
else {
undraw_wall(x, y, false);
}
}
//Changes the value of the position x, y, on the board to empty ground
void Board::undraw_wall(int x, int y, bool vertical){ /*done*/
ground[x][y] = empty_wall;
}
//USELESS
void Board::print_actual_player(playerenum aplayer){
//no se on es fa servir, estaria bé buscar-ho i borrar-ho
}
//Displays the end_of_game 'animation'
void Board::end_the_game (playerenum winner){ /*done*/
if (winner == second) ptex = &winlove;
else ptex = &winunlove;
int pos_x , pos_y;
float scalex, scaley;
bool do_print = false;
for(int x = 0; x < 17; ++x){
for(int y = 0; y < 17; ++y){
if(ground[x][y] == empty_ground or ground[x][y] == full_ground){
pos_x = delay + (x/2)*costat + (x/2)*(costat/3);
pos_y = delay + (y/2)*costat + (y/2)*(costat/3);
do_print = true;
scalex = costat/float(ptex->getSize().x);
scaley = costat/float(ptex->getSize().y);
}
if(do_print){
do_print = false;
sprite.setTexture(*ptex);
sprite.setScale(scalex, scaley);
sprite.setPosition(pos_x, pos_y);
window_game.draw(sprite);
sf::Clock clock;
sf::Time timer = clock.getElapsedTime();
while (clock.getElapsedTime().asSeconds() < 0.005) { }
clock.restart();
window_game.display();
}
}
}
}
int Board::getStepsToWin(int px, int py, int obj, std::queue<sf::Vector3i>& cua, std::vector < std::vector <bool> >& visitat){
sf::Vector3i pos;
sf::Vector3i npos;
visitat[px][py] = true;
while(!cua.empty()){
pos = cua.front();
cua.pop();
visitat[pos.x][pos.y] = true;
if(pos.y == obj) return pos.z;
for(int i = 0; i < 4; ++i){
npos.x = pos.x+movx[i];
npos.y = pos.y+movy[i];
if((legitmove(pos.x, pos.y, npos.x, npos.y)) and (not is_wall_zone(npos.x, npos.y) and (not (visitat[npos.x][npos.y])))) {
npos.z = pos.z+1;
cua.push(npos);
}
}
}
return -1;
}