Hello everyone,
I am quite new to C++ and I am currently trying to complete a project in creating a program that solves a maze of a given dimension.The text has to be read in and the first element of the file defines the size of the array,its an integer .I have defined this first element as 'size'.
example:
10
x x x x x x x x x x
x x x x p x x p g x
x p p p p x x p x x
x x x p p x x p p x
x p p p x x x x p x
x x x p p p p p p x
x p p p x x x x x x
x x x p x x x p p x
x x x p p p p p x x
x x x x x x x x x x
My idea is to create a dynamic array using vectors ,which I have done and it compiles alright.However I am a bit stumped when I try to pass the array to the maze solving function. I get this error:
'solveMaze' : cannot convert parameter 3 from 'std::vector<_Ty>' to 'char **'
I hope you guys could offer me some pointers on how to solve this problem.Thank you.
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
#define False 0
#define True 1
int solveMaze(int x,int y,char **maze,int size);//function declaration
int main()
{
int m,n,i,x,y,size;
vector< vector<char> >mazeArray;
string filename="C:\\maze.txt";// file path
ifstream mazeFile(filename.c_str());
if (mazeFile.is_open())
{
mazeFile>>size;
cout<<size<<endl;
mazeArray.resize(size); //resize the vector based on the size read in .
for (i = 0; i <size; ++i)
mazeArray[i].resize(size);
while (!mazeFile.eof())
{
for (m=0;m<size;m++){
for(n=0;n<size;n++)
mazeFile>>mazeArray[m][n];}
}
}
else {cout<<"There has been an error in opening the file"<<endl;}
mazeFile.close();
//Prints the maze in the console
for (m=0;m<size;m++)
{for(n=0;n<size;n++)
cout<<mazeArray[m][n]<<" ";
cout<<endl; }
cout<<"Please enter the starting coordinate(x):"<<endl;
cin>>x;
cout<<"Please enter the starting coordinate(y):"<<endl;
cin>>y;
solveMaze(y,x,mazeArray,size);
}
int solveMaze(int y,int x,char **maze,int size)
{
if (x<0||x > size-1|| y<0|| y> size-1)//choosen cord is out of range.
return False;
if (maze[y][x]=='x') //path not open.
return False;
if(maze[y][x]=='g') //goal has been found!
return True;
maze[y][x]='f'; //if all conditions are met then mark the starting coordinate as valid
//Go north
if (solveMaze(x,y-1,maze,size)==True)
return True;
//Go east
if (solveMaze(x+1,y,maze,size)==True)
return True;
//Go south
if (solveMaze(x,y+1,maze,size)==True)
return True;
//Go back to west
if(solveMaze(x-1,y,maze,size)==True)
return True;
maze[y][x]='c'; //cancels the path as part of solution
return False;
}