Question
Hi! I need help with my project! I just can't seem to make it work. And sometimes it works for the first two mazes but
Hi! I need help with my project! I just can't seem to make it work. And sometimes it works for the first two mazes but then it crashes for the next two. This is what I have so far. Also, I need to print in the path section if it went north, south, east, west. like so: Path: "NNNEEWWSSWWEEN" and then display the solved maze. The initial coordinates are given to the creature in main.cpp. The first line of the txt files represent width and height respectively and the second line represent exit row and exit column respectively.
maze. h
----------------------------------
#pragma once
#include
#include
using namespace std;
enum CELL { CLEAR, WALL, PATH, VISITED };
class Maze {
friend ostream& operator<<(ostream& out, const Maze& maze);
public:
//constructor for maze (only takes in file)
explicit Maze(const string& fileName);
//checks if position is clear
bool isClear(int row, int col) const;
//marks path with '*'
void markAsPath(int row, int col);
//mars path with '+'
void markAsVisited(int row, int col);
//checks if at exit row
int getExitRow() const;
//checks if at exit column
int getExitColumn() const;
//returns maze's height
int getHeight() const;
//returns maze's width
int getWidth() const;
private:
static const int MAX_SIZE = 100;
char field[MAX_SIZE][MAX_SIZE];
int width;
int height;
int exitRow;
int exitColumn;
};
maze.cpp
-------------------------------
#include "maze.h"
#include
#include
#include
//Overloads operator << to print out maze
ostream& operator<<(ostream& out, const Maze& maze) {
for (int row = 0; row < maze.height; row++) {
for (int col = 0; col < maze.width; col++) {
out << maze.field[row][col];
}
out << endl;
}
out << endl;
return out;
}
//Maze constructor that takes in file
//Sets values of array with file input
Maze::Maze(const string& fileName) : field{{0}}, width{0}, height{0}, exitRow{0}, exitColumn{0} {
ifstream inFile;
inFile.open(fileName);
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
inFile >> width >> height;
inFile >> exitRow >> exitColumn;
string str;
getline(inFile, str);
for (int row = 0; row < height; ++row) {
for (int col = 0; col < width; ++col) {
inFile.get(field[row][col]);
// cout << row << ", " << col << ": " << field[row][col] << endl;
}
getline(inFile, str);
}
}
//returns value of row that is the exit
int Maze::getExitRow() const { return exitRow; }
//returns value of col that is the exit
int Maze::getExitColumn() const { return exitColumn;}
//Checks if sent location is clear
bool Maze::isClear(int row, int col) const { return field[row][col] == ' '; }
//marks sent location as path with *
void Maze::markAsPath(int row, int col) { field[row][col] = '*'; }
//marks sent location as visited with +
void Maze::markAsVisited(int row, int col) { field[row][col] = '+'; }
//returns height of maze (length)
int Maze::getHeight() const { return height; }
//returns width of maze
int Maze::getWidth() const { return width; }
creature.h
---------------------------------
#pragma once
#include "maze.h"
#include
#include
class Creature {
public:
//print current location of creature
friend ostream& operator<<(ostream& out, const Creature& creature);
public:
//Shows location of creature
Creature(int row, int col);
//returns path as string such as "NNEE.."
string solve(Maze& maze);
bool atExit(const Maze& maze) const;
string goNorth(Maze& maze);
string goSouth(Maze& maze);
string goEast(Maze& maze);
string goWest(Maze& maze);
private:
int row;
int col;
stack
};
creature.cpp
----------------------------
#include "creature.h"
#include
using namespace std;
// prints current location of creature
ostream& operator<<(ostream& out, const Creature& creature){
out << "Current Location: "
<< "( " << creature.row << " , "
<< creature.col << ")";
return out;
}
//Sets starting postiton to creature
Creature::Creature(int row, int col) : row(row), col(col) {}
//Returns true if creature is at exit cordinates
bool Creature::atExit(const Maze& maze) const {
bool success = false;
success = this->row == maze.getExitRow() &&
this->col == maze.getExitColumn();
return success;
}
//returns string with succesful path
string Creature::solve(Maze& maze) {
string path;
if(atExit(maze)){
return path = " ";
}
if(goNorth(maze) == "X"){
goWest(maze);
path = goWest(maze);
ans.push(path);
if(path == "X"){
ans.pop();
goEast(maze);
path = goEast(maze);
ans.push(goEast(maze));
if(path == "X"){
ans.pop();
goSouth(maze);
path = goSouth(maze);
ans.push(path);
if(path == "X"){
ans.pop();
solve(maze);
}
}
}
}
else
{
ans.push(goNorth(maze));
solve(maze);
}
return path;
}
//Moves creature north recursively
string Creature::goNorth(Maze& maze) {
//Checks if it's still inside
if(row - 1 >= 0)
{
//Checks if it's clear or part of path
if(maze.isClear(row - 1,col) || (row-1)== '*')
{
//move one up north
this->row --;
//mark path
maze.markAsPath(row,col);
//Checks if already at exit
if(atExit(maze))
{
return "N";
} //if not at exit
else
{
//try to go up north again
string trial = goNorth(maze);
//if it hits wall try going east
if(trial == "X")
{
trial = goEast(maze);
//if it hits wall try going west
if(trial == "X")
{
trial = goWest(maze);
if(trial == "X")
{
maze.markAsVisited(row,col);
goSouth(maze); //go South
}
}
}
}
}
}
return "X";
}
//Moves creature west recursively
string Creature::goWest(Maze& maze) {
//coming from east
//check if it's inside
if(this-> col - 1 >= 0)
{
//check if it's clear
if (maze.isClear(row,col-1) || (col-1)=='*')
{
//move west and check if at exit
this -> col --;
maze.markAsPath(row,col);
if(atExit(maze))
{
return "W";
}
else
{
string tryAgain = goNorth(maze);
if( tryAgain == "X")
{
tryAgain = goWest(maze);
if( tryAgain == "X")
{
tryAgain = goSouth(maze);
if( tryAgain == "X" )
{
maze.markAsVisited(row,col);
goEast(maze);
}
}
}
}
}
}
return "X";
}
//Moves creature east recursively
string Creature::goEast(Maze& maze){
//CHECK IF STILL WITHIN maze
if (this->col + 1 <= maze.getWidth())
{
if(maze.isClear(row, col+1)|| (col+1) == '*')
{
//move east and mark path
this -> col ++;
maze.markAsPath(row, col);
//check if exit
if(atExit(maze))
{
return "E";
}
else
{
string tryAgain = goNorth(maze);
if (tryAgain == "X")
{
tryAgain = goEast(maze);
if(tryAgain == "X")
{
tryAgain = goSouth(maze);
if (tryAgain == "X")
{
//Go back west & mark as visited
maze.markAsVisited(row, col);
goWest(maze);
}
}
}
}
}
}
return "X";
}
//Moves creature south recursively
string Creature::goSouth(Maze& maze){
//Checks if it's still inside
if(row + 1 < maze.getHeight())
{
//Checks if it's clear and unvisited
if(maze.isClear(row + 1,col) || (row + 1) == '*')
{
//move one down south
this->row ++;
//mark path
maze.markAsPath(row,col);
//Checks if already at exit
if(atExit(maze))
{
return "S";
} //if not at exit
else
{
//try to go west
string newPath = goWest(maze);
//if it hits wall try going east
if(newPath == "X")
{
newPath = goSouth(maze);
//if it hits wall try going west
if(newPath == "X")
{
newPath = goEast(maze);
if(newPath == "X")
{
maze.markAsVisited(row,col);
goNorth(maze); //heads back north
}
}
}
}
}
}
return "X";
}
maze.txt
----------------------------
20 7 0 18 xxxxxxxxxxxxxxxxxx x x x xxxx x x xxxxx xxxxx xx x x xxxxx xxxxxxx xx x x x xx xx x x xxxxxxxxxx xx x xxxxxxxxxxxxxxxxxxxx
maze1.txt
----------------------------
22 7 6 2 xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxx xxx xxxxxxx xxxxxx x xxx xxx xxxxxx xxx xxx xxxxxx xxx xx xxxxxxxxxxxx xxxx xx xxxxxxxxxxxxxxxxxxx
maze2.txt
----------------------------
22 7 2 21 xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxx xxx xxxxxxx xxxx xxxx xxx xxxxxxxxx xxx xxxxxx xxx xxxxxxxxxxxxxxxx xxxx xxxxxxxxxxxxxxxxxxxxxx
maze3.txt
----------------------------
11 9 1 10 xxxxxxxxxxx x x x x xxxxx xxx x xxxxx xxx x x x xxxxxxx xxx xxxxxx xx xxxxxx xxxxxxxxxxx
main.cpp
--------------------------------
#include "creature.h"
#include "maze.h"
#include
#include
using namespace std;
//This fuction tests the first maze
void test() {
Maze m("maze.txt");
cout << "----------------------" < cout << m << endl; //Where creature starts Creature c(4, 4); assert(!c.atExit(m)); cout << c << endl; cout << "Path: " << c.solve(m) << endl; //checks if maze has the same path //assert(c.solve(m) == "EEENNNEEEEEESEESSSEEENNNNN"); cout << m << endl; cout << c << endl; cout << "----------------------" < //checks if maze is done assert(c.atExit(m)); } //This fuction tests maze1 void test2(){ Maze m("maze1.txt"); cout << "----------------------" < cout << m << endl; //Where creature starts Creature c(2, 18); cout << c << endl; assert(!c.atExit(m)); cout << "Path: " << c.solve(m) << endl; cout << m << endl; cout << c << endl; cout << "----------------------" < assert(c.atExit(m)); } //This fuction tests maze3 void test3(){ Maze m("maze3.txt"); cout << "----------------------" < cout << m << endl; //Where creature starts Creature c(5, 1); assert(!c.atExit(m)); cout << c << endl; cout << "Path: " << c.solve(m) << endl; cout << m << endl; cout << c << endl; assert(c.atExit(m)); } //This fuction tests the maze2 void test4(){ Maze m("maze2.txt"); cout << "----------------------" < cout << m << endl; //Where creature starts Creature c(4, 3); cout << c << endl; assert(!c.atExit(m)); cout << "Path: " << c.solve(m) << endl; cout << m << endl; cout << c << endl; cout << "----------------------" < } int main() { test(); test2(); test3(); test4(); cout << "Done!" << std::endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started