Skip to content

Commit 5ae754d

Browse files
committed
my update (game works yay)
1 parent 0efeccd commit 5ae754d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+551
-496
lines changed

Debug/ROBOT3.exe

-140 KB
Binary file not shown.

Debug/ROBOT3.exe.recipe

-11
This file was deleted.

Debug/ROBOT3.ilk

-1.25 MB
Binary file not shown.

Debug/ROBOT3.log

-2
This file was deleted.

Debug/ROBOT3.pdb

-1.91 MB
Binary file not shown.

Debug/ROBOT3.tlog/CL.command.1.tlog

-9.66 KB
Binary file not shown.

Debug/ROBOT3.tlog/CL.read.1.tlog

-300 KB
Binary file not shown.

Debug/ROBOT3.tlog/CL.write.1.tlog

-5.38 KB
Binary file not shown.

Debug/ROBOT3.tlog/ROBOT3.lastbuildstate

-2
This file was deleted.

Debug/ROBOT3.tlog/link.command.1.tlog

-2.92 KB
Binary file not shown.

Debug/ROBOT3.tlog/link.read.1.tlog

-5.69 KB
Binary file not shown.

Debug/ROBOT3.tlog/link.write.1.tlog

-1.64 KB
Binary file not shown.

Debug/ROBOT3.vcxproj.FileListAbsolute.txt

-1
This file was deleted.

Debug/keep_playing.obj

-193 KB
Binary file not shown.

Debug/load_map.obj

-49.3 KB
Binary file not shown.

Debug/main.obj

-150 KB
Binary file not shown.

Debug/maze_check.obj

-215 KB
Binary file not shown.

Debug/maze_choose.obj

-68.8 KB
Binary file not shown.

Debug/maze_name.obj

-166 KB
Binary file not shown.

Debug/player.obj

-52.5 KB
Binary file not shown.

Debug/post.obj

-51.6 KB
Binary file not shown.

Debug/read_rules.obj

-243 KB
Binary file not shown.

Debug/read_times.obj

-256 KB
Binary file not shown.

Debug/read_title.obj

-240 KB
Binary file not shown.

Debug/robot.obj

-54.4 KB
Binary file not shown.

Debug/select_option_game.obj

-191 KB
Binary file not shown.

Debug/tempCodeRunnerFile.obj

-129 KB
Binary file not shown.

Debug/vc142.pdb

-884 KB
Binary file not shown.

Game.cpp

+149-88
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@
1717
#include "Player.h"
1818
#include "Robot.h"
1919
#include "Post.h"
20+
#include "Gate.h"
2021
#include "Move_pos.h"
2122

2223
//constructor
2324
Game::Game(const std::string & filename){
2425

25-
gameWin = false; //onyl altered if player wins the game;
26+
gameWin = false; //only altered if player wins the game
2627

2728
std::ifstream mazefile;
28-
mazefile.open(filename);
29+
mazefile.open(filename);//open maze file
2930

3031
std::string line;
3132

@@ -42,11 +43,13 @@ Game::Game(const std::string & filename){
4243
mazefile.ignore ( std::numeric_limits<std::streamsize>::max(), 'x' );
4344
mazefile >> pos_y;
4445

46+
//set maze variables
4547
maze.setnumRows(pos_x);
4648
maze.setnumCols(pos_y);
4749

4850
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)
4951

52+
//read the lines of the fize to extract information
5053
while (getline(mazefile, line)) {
5154
for (int i = 0; i!=line.length(); i++) {
5255

@@ -77,11 +80,11 @@ Game::Game(const std::string & filename){
7780

7881
//read doors in map
7982
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);
8285
}
8386
}
84-
line_counter++;
87+
line_counter++;//jump to next line
8588
}
8689
}
8790
mazefile.close();
@@ -92,44 +95,83 @@ Maze Game::getMaze() const { return maze; }
9295
Player Game::getPlayer() const { return player; }
9396
std::vector<Robot> Game::getRobots() const { return robots; }
9497
std::vector<Post> Game::getPosts() const { return posts; }
98+
std::vector<Gate> Game::getGates() const { return gates; }
9599
bool Game::getGameWin() const { return gameWin; }
96100

97101
//setter
98102
void Game::setGameWin(bool gameCond) {
99103
gameWin = gameCond;
100104
}
101105

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+
102123
//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
105133
showGameDisplay();
134+
135+
//player_movement
106136
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+
}
110149
//detect player_lost
111150
}
151+
152+
showGameDisplay(); //show display after the game ended
153+
154+
return gameWin;
112155
}
113156

114-
//function to print the map
157+
//function to print the map (private)
115158
void Game::showGameDisplay() {
116159

117160
Maze maze_s = getMaze();
118161
Player player_s = getPlayer();
119162
std::vector<Robot> robots_s = getRobots();
120163
std::vector<Post> posts_s = getPosts();
121-
122-
//std::cout << maze.getnumRows() << ' ' << maze.getnumCols() << std::endl;
164+
std::vector<Gate> gates_s = getGates();
123165

124166
bool printed = false; //see if the char has already been printed
125167
int size_line = maze_s.getnumRows(); //number of rows in map
126168
int size_col = maze_s.getnumCols(); //number of columns in map
127169
int player_row, player_col; //detect player place in map
128170
int robot_row, robot_col; //detect place of a robot in map
129171
int post_row, post_col; //detect place of post in map
172+
int gate_row, gate_col; //detect place of door in map
130173

131-
std::cout << size_line << ' ' << size_col << std::endl;
132-
174+
//run the map coordinates, see what is in the position and prints it
133175
for (int row_number=0;row_number<size_line;row_number++){
134176
for (int column_number=0;column_number<size_col;column_number++){
135177

@@ -157,7 +199,7 @@ void Game::showGameDisplay() {
157199
}
158200
}
159201

160-
//print posts and doors in the map
202+
//print posts in the map
161203
for (Post post_store : posts_s) {
162204
post_row = post_store.getRow();
163205
post_col = post_store.getCol();
@@ -169,6 +211,18 @@ void Game::showGameDisplay() {
169211
}
170212
}
171213

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+
172226
//if is none prints a space
173227
if(!printed){
174228
std::cout << ' ';
@@ -181,6 +235,7 @@ void Game::showGameDisplay() {
181235
}
182236
}
183237

238+
//function to do player movement (private)
184239
void Game::player_movement() {
185240

186241
std::string player_move; //variable to detect player movement
@@ -255,6 +310,7 @@ void Game::player_movement() {
255310
if (robots[i].isAlive()) {
256311
player.move(player_movement);//puts player where he died
257312
player.setAsDead();
313+
setGameWin(game_lost); //assures the player has lost
258314
movement_completed = true;
259315
}
260316
else {
@@ -263,25 +319,29 @@ void Game::player_movement() {
263319
}
264320
}
265321

266-
//player tris to move into a post
322+
//player tries to move into a post
267323
for (unsigned int i = 0; i < posts.size(); i++) {
268324
if ((posts[i].getRow() == player.getRow() + player_movement.dRow) && (posts[i].getCol() == player.getCol() + player_movement.dCol)) {
269325
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
272327
player.setAsDead();
328+
setGameWin(game_lost); //assures the player has lost
273329
movement_completed = true;
274330
}
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-
}
280331
else if (!posts[i].isElectrified()) {
281332
valid_move = detect_invalid_move;
282333
}
283334
}
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+
}
285345

286346
//player moves normally
287347
if (!movement_completed && valid_move) { //if movement has still not be completed move normally
@@ -297,80 +357,81 @@ void Game::player_movement() {
297357
}
298358
}
299359

300-
void Game::robot_movement() {
360+
//function to do robot movement (private)
361+
void Game::robot_movement(int robot_index) {
301362

302-
for (int i = 0; i < robots.size(); i++) {
363+
if (robots[robot_index].isAlive()) {
303364

304-
if (robots[i].isAlive()) {
365+
//variables to determine where the robot will move
366+
bool robot_has_moved = false;
367+
Movement robot_movement;
305368

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;
309372

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+
}
313380

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+
}
321388

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
329390

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)
354405
}
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)
368410
}
369411
}
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+
}
373430
}
374431
}
432+
//robots move normally
433+
if (!robot_has_moved) {
434+
robots[robot_index].robotMove(robot_movement);
435+
}
375436
}
376437
}

0 commit comments

Comments
 (0)