#include <iostream>
#include <fstream>
#include <string>
using namespace std;
char gameArray[8][16];
int startX = 0;
int startY = 0;
bool moveNorth = true; // checks if it is able to move north
bool moveEast = true; // checks if it's able to move east
bool positionCheck = false; // used to check if Mouse's (M) position is located
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] == ' ')
{
moveEast = false;
cout << "Replacing path!" << endl;
gameArray[startY-1][startX] = '+';
startY--;
findPath();
}
// moves in the east direction
if (gameArray[startY][startX+1] == ' ')
{
moveNorth = false;
cout << "Replacing path!" << endl;
gameArray[startY][startX+1] = '+';
startX++;
findPath();
}
// moves in the west direction
if (gameArray[startY][startX-1] == ' ')
{
moveEast = false;
moveNorth = false;
cout << "Replacing path!" << endl;
gameArray[startY][startX-1] = '+';
startX--;
findPath();
}
// backtracking
else if (gameArray[startY][startX] == '+')
{
if (moveNorth == false)
{
// backtracking in north direction
cout << "Back tracking!" << endl;
gameArray[startY][startX] = 'X';
startX--;
}
else if (moveEast == false)
{
// backtracking in east direction
cout << "Back tracking!" << endl;
gameArray[startY][startX] = 'X';
startY++;
}
}
// check if north reaches a dead end
else if (gameArray[startY-1][startX] == '*' && moveNorth == true)
{
cout << "Max for north reached!" << endl;
gameArray[startY][startX] = 'X';
startY++;
findPath();
}
// checks if east reaches a dead end
else if (gameArray[startY][startX] == '+' && moveEast == true)
{
cout << "Backtracking" << endl;
gameArray[startY][startX] = 'X';
startY++;
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)
{
cout << "DISABLED." << endl << endl;
findPath();
}
if (choice == 4)
{
cout << "DISABLED. " << endl << endl;
}
if (choice == 5)
{
cout << "EXITING!" << endl;
break;
}
if (!cin)
{
cout << "ERROR IN INPUT!" << endl;
}
}
}