Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MAZE.H
- #ifndef __CS_235__Maze__
- #define __CS_235__Maze__
- #include <iostream>
- #include <vector>
- enum class RoomType { Start, Wall, EmptyRoom, Outside, \
- Treasure, Visited };
- class Maze{
- public:
- Maze();
- RoomType GetRoomType(int x, int y);
- void MarkPath(int x, int y);
- void ClearPath(int x, int y);
- void GetStartCoordinates(int& x, int& y);
- friend std::ostream & operator<<(std::ostream& out, c\
- onst Maze& maze);
- std::vector< std::vector<char> > _theMaze;
- int _startX;
- int _startY;
- };
- #endif /* defined(__CS_235__Maze__) */
- MAZE.CPP
- #include <vector>
- #include <iostream>
- #include <initializer_list>
- using namespace std;
- #include "Maze.h"
- Maze::Maze() : _startX(1), _startY(1)
- {
- vector<char> r1 = {'#','#','#','#','#','#','#','#','#\
- ','\n'};
- vector<char> r2 = {'#','S','#',' ',' ',' ','#','T','#\
- ','\n'};
- vector<char> r3 = {'#',' ','#',' ','#',' ',' ',' ','#\
- ','\n'};
- vector<char> r4 = {'#',' ',' ',' ','#',' ','#',' ','#\
- ','\n'};
- vector<char> r5 = {'#','#','#','#','#','#','#','#','#\
- ','\n'};
- _theMaze.push_back(r1);
- _theMaze.push_back(r2);
- _theMaze.push_back(r3);
- _theMaze.push_back(r4);
- _theMaze.push_back(r5);
- }
- void Maze::GetStartCoordinates(int& x, int& y)
- {
- x = _startX;
- y = _startY;
- }
- RoomType Maze::GetRoomType(int x, int y)
- {
- if(y > _theMaze.size() || x > _theMaze[y].size())
- return RoomType::Outside;
- if(_theMaze[y][x] == '#')
- return RoomType::Wall;
- if(_theMaze[y][x] == 'S')
- return RoomType::Start;
- if(_theMaze[y][x] == 'T')
- return RoomType::Treasure;
- if(_theMaze[y][x] == '.')
- return RoomType::Visited;
- return RoomType::EmptyRoom;
- }
- void Maze::MarkPath(int x, int y){
- RoomType rt = GetRoomType(x, y);
- if(rt == RoomType::EmptyRoom || rt == RoomType::Start\
- || rt == RoomType::Treasure)
- _theMaze[y][x] = '.';
- }
- void Maze::ClearPath(int x, int y)
- {
- RoomType rt = GetRoomType(x, y);
- if(x == _startX && y == _startY)
- _theMaze[y][x] = 'S';
- else if(rt == RoomType::Visited)
- _theMaze[y][x] = ' ';
- }
- // Overloaded for stream output
- std::ostream & operator<<(std::ostream& out, const Maze\
- & maze)
- {
- for(int y = 0; y < maze._theMaze.size(); ++y){
- std::vector<char> mazeRow = maze._theMaze[y];
- for(int x = 0; x < mazeRow.size(); ++x){
- out << mazeRow[x];
- }
- }
- out << std::endl;
- return out;
- }
- MAIN.CPP
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std; //cout, endl
- #include "Maze.h"
- void GetRoomType(int x,int y);
- void GetStartCoordinates(int x,int y);
- void GetRoomType(int x, int y);
- bool FindPath(Maze& m, int x, int y)
- {
- RoomType r;
- if(r == RoomType::Treasure)
- {
- cout << "You won!";
- return true;
- FindPath(m,x,y);
- }
- if(r == RoomType::Outside)
- {
- return false;
- FindPath(m,x,y);
- }
- if(r == RoomType::Wall)
- {
- return false;
- FindPath(m,x,y);
- }
- if(r == RoomType::Visited)
- {
- return false;
- FindPath(m,x,y);
- }
- m.MarkPath(x,y);
- if(m._theMaze[x][y++])
- {
- FindPath(m,x,y);
- return true;
- }
- if(m._theMaze[x][y--])
- {
- cout << x << "," << y << endl;
- FindPath(m,x,y);
- cout << m << endl;
- return true;
- }
- if(m._theMaze[x--][y])
- {
- cout << x << "," << y << endl;
- FindPath(m,x,y);
- cout << m << endl;
- return true;
- }
- if(m._theMaze[x++][y])
- {
- cout << x << "," << y << endl;
- FindPath(m,x,y);
- cout << m << endl;
- return true;
- }
- else
- {
- m.ClearPath(x,y);
- return false;
- }
- }
- }
- if(m._theMaze[x++][y])
- {
- cout << x << "," << y << endl;
- FindPath(m,x,y);
- cout << m << endl;
- return true;
- }
- else
- {
- m.ClearPath(x,y);
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement