17
17
#include " Player.h"
18
18
#include " Robot.h"
19
19
#include " Post.h"
20
+ #include " Gate.h"
20
21
#include " Move_pos.h"
21
22
22
23
// constructor
23
24
Game::Game (const std::string & filename){
24
25
25
- gameWin = false ; // onyl altered if player wins the game;
26
+ gameWin = false ; // only altered if player wins the game
26
27
27
28
std::ifstream mazefile;
28
- mazefile.open (filename);
29
+ mazefile.open (filename);// open maze file
29
30
30
31
std::string line;
31
32
@@ -42,11 +43,13 @@ Game::Game(const std::string & filename){
42
43
mazefile.ignore ( std::numeric_limits<std::streamsize>::max (), ' x' );
43
44
mazefile >> pos_y;
44
45
46
+ // set maze variables
45
47
maze.setnumRows (pos_x);
46
48
maze.setnumCols (pos_y);
47
49
48
50
int line_counter = -1 ;// point to the line of the maze we are reading (-1 becaue of the first line in the file that is the size of map)
49
51
52
+ // read the lines of the fize to extract information
50
53
while (getline (mazefile, line)) {
51
54
for (int i = 0 ; i!=line.length (); i++) {
52
55
@@ -77,11 +80,11 @@ Game::Game(const std::string & filename){
77
80
78
81
// read doors in map
79
82
else if (line[i] == door){
80
- Post post_store (line_counter, i, door );
81
- posts .push_back (post_store );
83
+ Gate gate_store (line_counter, i);
84
+ gates .push_back (gate_store );
82
85
}
83
86
}
84
- line_counter++;
87
+ line_counter++;// jump to next line
85
88
}
86
89
}
87
90
mazefile.close ();
@@ -92,44 +95,83 @@ Maze Game::getMaze() const { return maze; }
92
95
Player Game::getPlayer () const { return player; }
93
96
std::vector<Robot> Game::getRobots () const { return robots; }
94
97
std::vector<Post> Game::getPosts () const { return posts; }
98
+ std::vector<Gate> Game::getGates () const { return gates; }
95
99
bool Game::getGameWin () const { return gameWin; }
96
100
97
101
// setter
98
102
void Game::setGameWin (bool gameCond) {
99
103
gameWin = gameCond;
100
104
}
101
105
106
+ // function to detect player win or lose
107
+ bool Game::ask_game_ended () {
108
+ // see if player won game
109
+ if (gameWin) {
110
+ return game_ended;
111
+ }
112
+ // see if player lost game
113
+ else if (!player.isAlive ()) {
114
+ return game_ended;
115
+ }
116
+ // if neither continue game
117
+ else {
118
+ return game_running;
119
+ }
120
+ }
121
+
122
+
102
123
// public function to play the game;
103
- bool Game::play (){
104
- while (true ) {
124
+ bool Game::play () {
125
+
126
+ setGameWin (game_start_cond);// ensures the gameWin condiiton is set to false
127
+
128
+ bool game_running = true ;// initiates varviable to loop the game
129
+
130
+ while (game_running) {
131
+
132
+ // print the game
105
133
showGameDisplay ();
134
+
135
+ // player_movement
106
136
player_movement ();
107
- // detect player win
108
- // detect player lost
109
- robot_movement ();
137
+ // detect if game ended (if player has won or lost)
138
+ game_running = ask_game_ended ();
139
+
140
+ // robots_movements
141
+ for (size_t i = 0 ; i < robots.size (); i++) {
142
+ robot_movement (i);
143
+ // detect if game ended (if player has won or lost)
144
+ game_running = ask_game_ended ();
145
+ if (!game_running) {
146
+ break ;
147
+ }
148
+ }
110
149
// detect player_lost
111
150
}
151
+
152
+ showGameDisplay (); // show display after the game ended
153
+
154
+ return gameWin;
112
155
}
113
156
114
- // function to print the map
157
+ // function to print the map (private)
115
158
void Game::showGameDisplay () {
116
159
117
160
Maze maze_s = getMaze ();
118
161
Player player_s = getPlayer ();
119
162
std::vector<Robot> robots_s = getRobots ();
120
163
std::vector<Post> posts_s = getPosts ();
121
-
122
- // std::cout << maze.getnumRows() << ' ' << maze.getnumCols() << std::endl;
164
+ std::vector<Gate> gates_s = getGates ();
123
165
124
166
bool printed = false ; // see if the char has already been printed
125
167
int size_line = maze_s.getnumRows (); // number of rows in map
126
168
int size_col = maze_s.getnumCols (); // number of columns in map
127
169
int player_row, player_col; // detect player place in map
128
170
int robot_row, robot_col; // detect place of a robot in map
129
171
int post_row, post_col; // detect place of post in map
172
+ int gate_row, gate_col; // detect place of door in map
130
173
131
- std::cout << size_line << ' ' << size_col << std::endl;
132
-
174
+ // run the map coordinates, see what is in the position and prints it
133
175
for (int row_number=0 ;row_number<size_line;row_number++){
134
176
for (int column_number=0 ;column_number<size_col;column_number++){
135
177
@@ -157,7 +199,7 @@ void Game::showGameDisplay() {
157
199
}
158
200
}
159
201
160
- // print posts and doors in the map
202
+ // print posts in the map
161
203
for (Post post_store : posts_s) {
162
204
post_row = post_store.getRow ();
163
205
post_col = post_store.getCol ();
@@ -169,6 +211,18 @@ void Game::showGameDisplay() {
169
211
}
170
212
}
171
213
214
+ // print gates in the map
215
+ for (Gate gate_store : gates_s) {
216
+ gate_row = gate_store.getRow ();
217
+ gate_col = gate_store.getCol ();
218
+ if (gate_row == row_number && gate_col == column_number ) {
219
+ if (!printed) {
220
+ std::cout << gate_store.getSymbol ();
221
+ }
222
+ printed = true ;
223
+ }
224
+ }
225
+
172
226
// if is none prints a space
173
227
if (!printed){
174
228
std::cout << ' ' ;
@@ -181,6 +235,7 @@ void Game::showGameDisplay() {
181
235
}
182
236
}
183
237
238
+ // function to do player movement (private)
184
239
void Game::player_movement () {
185
240
186
241
std::string player_move; // variable to detect player movement
@@ -255,6 +310,7 @@ void Game::player_movement() {
255
310
if (robots[i].isAlive ()) {
256
311
player.move (player_movement);// puts player where he died
257
312
player.setAsDead ();
313
+ setGameWin (game_lost); // assures the player has lost
258
314
movement_completed = true ;
259
315
}
260
316
else {
@@ -263,25 +319,29 @@ void Game::player_movement() {
263
319
}
264
320
}
265
321
266
- // player tris to move into a post
322
+ // player tries to move into a post
267
323
for (unsigned int i = 0 ; i < posts.size (); i++) {
268
324
if ((posts[i].getRow () == player.getRow () + player_movement.dRow ) && (posts[i].getCol () == player.getCol () + player_movement.dCol )) {
269
325
if (posts[i].isElectrified ()) {
270
- // posts.erase(posts.begin() + i);
271
- player.move (player_movement);// puts player where he died
326
+ // player doesnt move to electrified post
272
327
player.setAsDead ();
328
+ setGameWin (game_lost); // assures the player has lost
273
329
movement_completed = true ;
274
330
}
275
- else if (posts[i].isDoor ()){
276
- player.move (player_movement);// puts player in door frame
277
- setGameWin (game_won);// signifies play won
278
-
279
- }
280
331
else if (!posts[i].isElectrified ()) {
281
332
valid_move = detect_invalid_move;
282
333
}
283
334
}
284
- }
335
+ }
336
+
337
+ // player tries to move into gate
338
+ for (unsigned int i = 0 ; i < gates.size (); i++) {
339
+ if ((gates[i].getRow () == player.getRow () + player_movement.dRow ) && (gates[i].getCol () == player.getCol () + player_movement.dCol )) {
340
+ player.move (player_movement);// puts player in door frame
341
+ setGameWin (game_won);// signifies play won
342
+ movement_completed = true ;
343
+ }
344
+ }
285
345
286
346
// player moves normally
287
347
if (!movement_completed && valid_move) { // if movement has still not be completed move normally
@@ -297,80 +357,81 @@ void Game::player_movement() {
297
357
}
298
358
}
299
359
300
- void Game::robot_movement () {
360
+ // function to do robot movement (private)
361
+ void Game::robot_movement (int robot_index) {
301
362
302
- for ( int i = 0 ; i < robots. size (); i++ ) {
363
+ if ( robots[robot_index]. isAlive () ) {
303
364
304
- if (robots[i].isAlive ()) {
365
+ // variables to determine where the robot will move
366
+ bool robot_has_moved = false ;
367
+ Movement robot_movement;
305
368
306
- // variables to determine where the robot will move
307
- bool robot_has_moved = false ;
308
- Movement robot_movement ;
369
+ // defaut robot movement (no movement)
370
+ robot_movement. dRow = no_movement ;
371
+ robot_movement. dCol = no_movement ;
309
372
310
- // defaut robot movement (no movement)
311
- robot_movement.dRow = no_movement;
312
- robot_movement.dCol = no_movement;
373
+ // determines if the robot will move up or down (will chase the player)
374
+ if (robots[robot_index].getRow () - player.getRow () > robot_move_place) {
375
+ robot_movement.dRow = move_position_negative;
376
+ }
377
+ else if (robots[robot_index].getRow () - player.getRow () < robot_move_place) {
378
+ robot_movement.dRow = move_position_positive;
379
+ }
313
380
314
- // determines if the robot will move up or down (will chase the player)
315
- if (robots[i]. getRow () - player.getRow () > robot_move_place) {
316
- robot_movement.dRow = move_position_negative;
317
- }
318
- else if (robots[i]. getRow () - player.getRow () > robot_move_place) {
319
- robot_movement.dRow = move_position_positive;
320
- }
381
+ // determines if the robot will move left or right (will chase the player)
382
+ if (robots[robot_index]. getCol () - player.getCol () > robot_move_place) {
383
+ robot_movement.dCol = move_position_negative;
384
+ }
385
+ else if (robots[robot_index]. getCol () - player.getCol () < robot_move_place) {
386
+ robot_movement.dCol = move_position_positive;
387
+ }
321
388
322
- // determines if the robot will move left or right (will chase the player)
323
- if (robots[i].getCol () - player.getCol () > robot_move_place) {
324
- robot_movement.dCol = move_position_negative;
325
- }
326
- else if (robots[i].getCol () - player.getCol () > robot_move_place) {
327
- robot_movement.dCol = move_position_positive;
328
- }
389
+ // MAKE THE ROBOTS MOVE
329
390
330
- // MAKE THE ROBOTS MOVE
331
- // robot catches player and player dies
332
- if ((robots[i].getRow () == player.getRow ()) && (robots[i].getCol () == player.getCol ())) {
333
- robots[i].robotMove (robot_movement);
334
- player.setAsDead ();
335
- robot_has_moved = true ; // sinaliaing the robot has already moved (so no defaut movement)
336
- }
337
- // robot goes into posts
338
- for (unsigned int j = 0 ; j < posts.size (); j++) {
339
- if ((posts[j].getRow () == robots[i].getRow () + robot_movement.dRow ) && (posts[j].getCol () == robots[i].getCol () + robot_movement.dCol )) {
340
- if (posts[j].isElectrified ()) {
341
- // posts.erase(posts.begin() + i);
342
- robots[i].robotMove (robot_movement);// puts player where he died
343
- robots[i].setAsDead ();
344
- robot_has_moved = true ; // sinaliaing the robot has already moved (so no defaut movement)
345
- }
346
- else if (posts[j].isDoor ()) {
347
- // do nothing
348
- robot_has_moved = true ; // sinaliaing the robot has already moved (so no defaut movement)
349
- }
350
- else if (!posts[j].isElectrified ()) {
351
- // do nothing
352
- robot_has_moved = true ; // sinaliaing the robot has already moved (so no defaut movement)
353
- }
391
+ // robot catches player and player dies
392
+ if ((robots[robot_index].getRow () + robot_movement.dRow == player.getRow ()) && (robots[robot_index].getCol () + robot_movement.dCol == player.getCol ())) {
393
+ robots[robot_index].robotMove (robot_movement);
394
+ player.setAsDead ();
395
+ setGameWin (game_lost); // assures the player has lost
396
+ robot_has_moved = true ; // sinaliaing the robot has already moved (so no defaut movement)
397
+ }
398
+ // robot goes into posts
399
+ for (unsigned int j = 0 ; j < posts.size (); j++) {
400
+ if ((posts[j].getRow () == robots[robot_index].getRow () + robot_movement.dRow ) && (posts[j].getCol () == robots[robot_index].getCol () + robot_movement.dCol ) && !robot_has_moved) {
401
+ if (posts[j].isElectrified ()) {
402
+ // robot doesnt move
403
+ robots[robot_index].setAsDead (); // robot dies
404
+ robot_has_moved = true ; // sinaliaing the robot has already moved (so no defaut movement)
354
405
}
355
- }
356
- // robot colides with another robot
357
- for (unsigned int j = 0 ; j < robots.size (); j++) {
358
- if ((robots[j].getRow () == robots[i].getRow () + robot_movement.dRow ) && (robots[j].getCol () == robots[i].getCol () + robot_movement.dCol )) {
359
- if (robots[j].getID () != robots[i].getID ()) {
360
- robots[i].robotMove (robot_movement);// puts player where he died
361
- robots[i].setAsDead ();
362
- robots[j].setAsDead ();
363
- robot_has_moved = true ; // sinaliaing the robot has already moved (so no defaut movement)
364
- }
365
- else {
366
- // do nothing
367
- }
406
+ else if (!posts[j].isElectrified () && !robot_has_moved) {
407
+ robots[robot_index].robotMove (robot_movement);// puts robot where he died
408
+ robots[robot_index].setAsDead ();
409
+ robot_has_moved = true ; // sinaliaing the robot has already moved (so no defaut movement)
368
410
}
369
411
}
370
- // robots move normally
371
- if (!robot_has_moved) {
372
- robots[i].robotMove (robot_movement);
412
+ }
413
+ // robot goes into door
414
+ for (unsigned int j = 0 ; j < gates.size (); j++) {
415
+ if ((gates[j].getRow () == robots[robot_index].getRow () + robot_movement.dRow ) && (gates[j].getCol () == robots[robot_index].getCol () + robot_movement.dCol ) && !robot_has_moved) {
416
+ // do nothing (robot cant move to door so it doesnt move)
417
+ robot_has_moved = true ; // sinaliaing the robot has already moved (so no defaut movement)
418
+ }
419
+ }
420
+ // robot colides with another robot
421
+ for (unsigned int j = 0 ; j < robots.size (); j++) {
422
+ if ((robots[j].getRow () == robots[robot_index].getRow () + robot_movement.dRow ) && (robots[j].getCol () == robots[robot_index].getCol () + robot_movement.dCol ) && !robot_has_moved) {
423
+ // only do it if it doesnt collide with itself
424
+ if (robots[j].getID () != robots[robot_index].getID () && !robot_has_moved) {
425
+ robots[robot_index].robotMove (robot_movement);// puts player where he died
426
+ robots[robot_index].setAsDead ();
427
+ robots[j].setAsDead ();
428
+ robot_has_moved = true ; // sinaliaing the robot has already moved (so no defaut movement)
429
+ }
373
430
}
374
431
}
432
+ // robots move normally
433
+ if (!robot_has_moved) {
434
+ robots[robot_index].robotMove (robot_movement);
435
+ }
375
436
}
376
437
}
0 commit comments