Skip to content

Commit 0efeccd

Browse files
committed
My latest files
1 parent 98cae6f commit 0efeccd

13 files changed

+687
-154
lines changed

Game.cpp

+376
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,376 @@
1+
//T09_G12
2+
3+
#include <iostream>
4+
#include <fstream>
5+
#include <string>
6+
#include <vector>
7+
#include <limits>
8+
#include <algorithm>
9+
#include <stdio.h>
10+
#include <chrono>
11+
#include <iomanip>
12+
#include <algorithm>
13+
14+
//our files
15+
#include "Game.h"
16+
#include "Maze.h"
17+
#include "Player.h"
18+
#include "Robot.h"
19+
#include "Post.h"
20+
#include "Move_pos.h"
21+
22+
//constructor
23+
Game::Game(const std::string & filename){
24+
25+
gameWin = false; //onyl altered if player wins the game;
26+
27+
std::ifstream mazefile;
28+
mazefile.open(filename);
29+
30+
std::string line;
31+
32+
if(!mazefile.is_open()){
33+
std::cout << "ERROR!" << std::endl;
34+
}
35+
else{
36+
37+
int robot_counter = 0; //count the number of robots added (also pass the id)
38+
39+
//get maze variables
40+
int pos_x, pos_y;
41+
mazefile >> pos_x;
42+
mazefile.ignore ( std::numeric_limits<std::streamsize>::max(), 'x' );
43+
mazefile >> pos_y;
44+
45+
maze.setnumRows(pos_x);
46+
maze.setnumCols(pos_y);
47+
48+
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+
50+
while (getline(mazefile, line)) {
51+
for (int i = 0; i!=line.length(); i++) {
52+
53+
//read the robots in the map
54+
if (line[i] == alive_robot) {
55+
robot_counter++;
56+
Robot robot_store(robot_counter, line_counter, i);
57+
robots.push_back(robot_store);
58+
}
59+
60+
//read the player in the map
61+
else if (line[i] == alive_player) {
62+
player.setRow(line_counter);
63+
player.setCol(i);
64+
}
65+
66+
//read the electric posts in the map
67+
else if (line[i] == electric_post){
68+
Post post_store(line_counter, i, electric_post);
69+
posts.push_back(post_store);
70+
}
71+
72+
//read the non electric posts in map
73+
else if (line[i] == normal_post){
74+
Post post_store(line_counter, i, normal_post);
75+
posts.push_back(post_store);
76+
}
77+
78+
//read doors in map
79+
else if (line[i] == door){
80+
Post post_store(line_counter, i, door);
81+
posts.push_back(post_store);
82+
}
83+
}
84+
line_counter++;
85+
}
86+
}
87+
mazefile.close();
88+
}
89+
90+
//getters
91+
Maze Game::getMaze() const { return maze; }
92+
Player Game::getPlayer() const { return player; }
93+
std::vector<Robot> Game::getRobots() const { return robots; }
94+
std::vector<Post> Game::getPosts() const { return posts; }
95+
bool Game::getGameWin() const { return gameWin; }
96+
97+
//setter
98+
void Game::setGameWin(bool gameCond) {
99+
gameWin = gameCond;
100+
}
101+
102+
//public function to play the game;
103+
bool Game::play(){
104+
while (true) {
105+
showGameDisplay();
106+
player_movement();
107+
//detect player win
108+
//detect player lost
109+
robot_movement();
110+
//detect player_lost
111+
}
112+
}
113+
114+
//function to print the map
115+
void Game::showGameDisplay() {
116+
117+
Maze maze_s = getMaze();
118+
Player player_s = getPlayer();
119+
std::vector<Robot> robots_s = getRobots();
120+
std::vector<Post> posts_s = getPosts();
121+
122+
//std::cout << maze.getnumRows() << ' ' << maze.getnumCols() << std::endl;
123+
124+
bool printed = false; //see if the char has already been printed
125+
int size_line = maze_s.getnumRows(); //number of rows in map
126+
int size_col = maze_s.getnumCols(); //number of columns in map
127+
int player_row, player_col; //detect player place in map
128+
int robot_row, robot_col; //detect place of a robot in map
129+
int post_row, post_col; //detect place of post in map
130+
131+
std::cout << size_line << ' ' << size_col << std::endl;
132+
133+
for (int row_number=0;row_number<size_line;row_number++){
134+
for (int column_number=0;column_number<size_col;column_number++){
135+
136+
printed = false; //set printed char to false
137+
138+
//print player on the map
139+
player_row = player_s.getRow();
140+
player_col = player_s.getCol();
141+
if (player_row == row_number && player_col == column_number){
142+
if (!printed) {
143+
std::cout << player_s.getSymbol();
144+
}
145+
printed = true;
146+
}
147+
148+
//print robots on the map
149+
for (Robot robot_store : robots_s) {
150+
robot_row = robot_store.getRow();
151+
robot_col = robot_store.getCol();
152+
if (robot_row == row_number && robot_col == column_number) {
153+
if (!printed) {
154+
std::cout << robot_store.getSymbol();
155+
}
156+
printed = true;
157+
}
158+
}
159+
160+
//print posts and doors in the map
161+
for (Post post_store : posts_s) {
162+
post_row = post_store.getRow();
163+
post_col = post_store.getCol();
164+
if (post_row == row_number && post_col == column_number) {
165+
if (!printed) {
166+
std::cout << post_store.getSymbol();
167+
}
168+
printed = true;
169+
}
170+
}
171+
172+
//if is none prints a space
173+
if(!printed){
174+
std::cout << ' ';
175+
}
176+
177+
}
178+
179+
std::cout << std::endl; //goes to the next line
180+
181+
}
182+
}
183+
184+
void Game::player_movement() {
185+
186+
std::string player_move; //variable to detect player movement
187+
bool movement_completed = false;
188+
189+
Movement player_movement;
190+
191+
int s_move = 0; //verifies if it is an s input (choose not to move)
192+
bool valid_move = valid_move_reset; //if move is valid we do it and break from the loop
193+
194+
std::cout << std::endl << "Where do you want to go?" << std::endl;
195+
while (!movement_completed) {
196+
197+
//reset move validation and move positions
198+
valid_move = valid_move_reset;//flag for invalid movement
199+
200+
//get player input
201+
std::cin >> player_move;
202+
std::cin.ignore(255, '\n');
203+
204+
//end the program if user press control + z
205+
if (std::cin.eof()) {
206+
std::exit(0);
207+
}
208+
209+
//load defaut movemetn (no movement)
210+
player_movement.dRow = no_movement;
211+
player_movement.dCol = no_movement;
212+
213+
//determine which char the player chose and validates it
214+
if (player_move == "q" || player_move == "Q") {
215+
player_movement.dRow = move_position_negative;
216+
player_movement.dCol = move_position_negative;
217+
}
218+
else if (player_move == "w" || player_move == "W") {
219+
player_movement.dRow = move_position_negative;
220+
}
221+
else if (player_move == "e" || player_move == "E") {
222+
player_movement.dRow = move_position_negative;
223+
player_movement.dCol = move_position_positive;
224+
}
225+
else if (player_move == "a" || player_move == "A") {
226+
player_movement.dCol = move_position_negative;
227+
}
228+
else if (player_move == "s" || player_move == "S") {
229+
s_move = detect_s_move;
230+
}
231+
else if (player_move == "d" || player_move == "D") {
232+
player_movement.dCol = move_position_positive;
233+
}
234+
else if (player_move == "z" || player_move == "Z") {
235+
player_movement.dRow = move_position_positive;
236+
player_movement.dCol = move_position_negative;
237+
}
238+
else if (player_move == "x" || player_move == "X") {
239+
player_movement.dRow = move_position_positive;
240+
}
241+
else if (player_move == "c" || player_move == "C") {
242+
player_movement.dRow = move_position_positive;
243+
player_movement.dCol = move_position_positive;
244+
}
245+
else {
246+
valid_move = detect_invalid_move;//flag for invalid movement
247+
}
248+
249+
//if valid checks where it is going and its consequences
250+
if (valid_move == valid_move_reset) {
251+
252+
//player tries to move into a robot
253+
for (unsigned int i = 0; i < robots.size(); i++) {
254+
if ((robots[i].getRow() == player.getRow() + player_movement.dRow) && (robots[i].getCol() == player.getCol() + player_movement.dCol)) {
255+
if (robots[i].isAlive()) {
256+
player.move(player_movement);//puts player where he died
257+
player.setAsDead();
258+
movement_completed = true;
259+
}
260+
else {
261+
valid_move = detect_invalid_move;
262+
}
263+
}
264+
}
265+
266+
//player tris to move into a post
267+
for (unsigned int i = 0; i < posts.size(); i++) {
268+
if ((posts[i].getRow() == player.getRow() + player_movement.dRow) && (posts[i].getCol() == player.getCol() + player_movement.dCol)) {
269+
if (posts[i].isElectrified()) {
270+
//posts.erase(posts.begin() + i);
271+
player.move(player_movement);//puts player where he died
272+
player.setAsDead();
273+
movement_completed = true;
274+
}
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+
else if (!posts[i].isElectrified()) {
281+
valid_move = detect_invalid_move;
282+
}
283+
}
284+
}
285+
286+
//player moves normally
287+
if (!movement_completed && valid_move) { //if movement has still not be completed move normally
288+
player.move(player_movement);
289+
movement_completed = true;
290+
}
291+
}
292+
293+
//if not valid tells the user and restart the loop
294+
if (!valid_move) {
295+
std::cout << "Movement invalid, please choose another one" << std::endl;
296+
}
297+
}
298+
}
299+
300+
void Game::robot_movement() {
301+
302+
for (int i = 0; i < robots.size(); i++) {
303+
304+
if (robots[i].isAlive()) {
305+
306+
//variables to determine where the robot will move
307+
bool robot_has_moved = false;
308+
Movement robot_movement;
309+
310+
//defaut robot movement (no movement)
311+
robot_movement.dRow = no_movement;
312+
robot_movement.dCol = no_movement;
313+
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+
}
321+
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+
}
329+
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+
}
354+
}
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+
}
368+
}
369+
}
370+
//robots move normally
371+
if (!robot_has_moved) {
372+
robots[i].robotMove(robot_movement);
373+
}
374+
}
375+
}
376+
}

0 commit comments

Comments
 (0)