Hello guys, I'm new here, and this is my first post in this forums. Please bear with me if I can't explain what I'm in need of well.
Basically, this is part of my assignment, and I'm supposed to find a path from one end to the other in a maze.
The maze is pre-defined in a text file, and I've got to load it up onto the program. After loading the maze, I'll need to use a function to find the correct pathing and print out the pathing on the maze itself.
I'm a beginner in C++, so I'm kind of stuck at a particular problem.
The problem I'm facing is that, the way I use to search for the path is wrong. I've got no idea how to search for the correct path using a correct method.
A copy of the maze is attached in the file below, because posting here would be rather weird, as the whole maze is gonna look out of shape.
So far, this is my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
char gameArray[8][16];
int startX = 0;
int startY = 0;
bool positionCheck = false;
// Reminder:
// i = y-axis direction
// j = x-axis direction
void findPath(void)
{
for (int i = 0; i<8; i++)
{
for (int j = 0; j <16; j++)
{
if (gameArray[i][j] == 'M')
{
if (positionCheck == false)
{
startX = j;
startY = i;
positionCheck = true;
cout << "Mouse is at: " << i << " " << j << endl;
}
}
}
}
// moving in north
if (gameArray[startY-1][startX] == ' ' ) // checking -1 of the y axis for a space. Can be increased to check more spaces at the front.
{
cout << "Replacing path!" << endl;
gameArray[startY-1][startX] = '+'; // if the space is available, change the space to a + sign.
startY--;
findPath(); // recurs to that it'll recheck
}
// moving in south
else if (gameArray[startY+1][startX] == ' ') // same as north
{
cout << "Replacing path!" << endl;
gameArray[startY+1][startX] = '+';
startY++;
findPath();
}
// moving in west
else if (gameArray[startY][startX-1] == ' ')
{
cout << "Replacing path!" << endl;
gameArray[startY][startX-1] = '+';
startX--;
findPath();
}
else if (gameArray[startY][startX+1] == ' ')
{
cout << "Replacing path!" << endl;
gameArray[startY][startX+1] = '+';
startX++;
findPath();
}
}
char inputStandard(void)
{
ifstream myfile ("standard.txt");
if (myfile.is_open())
{
for (int i = 0; i<8; i++)
{
for (int j = 0; j < 16; j++)
{
myfile.get(gameArray[i][j]);
}
}
myfile.close();
}
return 0;
}
char inputCustom(void)
{
ifstream myfile ("custom.txt");
if (myfile.is_open())
{
for (int i = 0; i<8; i++)
{
for (int j = 0; j < 16; j++)
{
myfile.get(gameArray[i][j]);
}
}
myfile.close();
}
return 0;
}
void main(void)
{
string status_mode = "NONE";
int choice;
for (int start_loop = 1; start_loop>0; start_loop++)
{
if (status_mode != "NONE")
{
for (int indexrow=0; indexrow<8; indexrow++)
{
for (int indexcol=0; indexcol<16;indexcol++)
{
cout << gameArray[indexrow][indexcol];
}
}
cout << endl;
}
cout << "Active Maze: " << status_mode << endl;
cout << "1. Select Standard Maze" << endl;
cout << "2. Select Custom Maze" << endl;
cout << "3. Find Path" << endl;
cout << "4. Print Path" << endl;
cout << "5. End " << endl;
cin >> choice;
if (choice == 1)
{
status_mode = "STANDARD";
inputStandard();
cout << "Standard Mode Selected!" << endl << endl;
}
if (choice == 2)
{
status_mode = "CUSTOM";
inputCustom();
cout << "Custom Mode Selected!" << endl << endl;
}
if (choice == 3)
{
findPath();
}
if (choice == 4)
{
cout << "DISABLED. " << endl << endl;
}
if (choice == 5)
{
cout << "EXITING!" << endl;
break;
}
if (!cin)
{
cout << "ERROR IN INPUT!" << endl;
}
}
}
How the final product I'm required to do this somewhat like this...
[IMG]http://notcliche.com/example.PNG[/IMG]
Any help with regards to a pathing method would be greatly appreciated, and thanks in advance!