#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; } } }
Alright, noted and changed my void main() to int main(). Thanks for the advice!
I've use structs before, but not stacks. Haven't learned of that as of yet.
I'm still trying to figure out how to backtrack, as of what VernonDozier had suggested, but I'm not getting far at the moment...
If it's not too much, could I get an example of how it's like? Thanks again!
******* *** * * * ***** * * ******* Here is the correct path. ******* oo***oo *ooooo* * ***** * * ******* Here is the attempt where you keep the wall to your right. ******* oo*** *o * *o***** *oooo@* ******* Oops, I hit a wall with a dead end. I have nowhere else to go, so I backtrack (i.e. go where I went before). I know that any spot where I've gone before and where I have no blank spaces and no option to switch directions is a dead end. Mark that dead end with an x so I don't go over it again. ******* oo*** *o * *o***** *ooo@x* ******* ******* oo*** *o * *o***** *oo@xx* ******* and so on till I get to a spot where I have a new option. ******* oo*** *@ * *x***** *xxxxx* ******* I'm back where I was before. Go right again. ******* oo*** *o@ * *x***** *xxxxx* ******* Keep going. ******* oo***oo *ooooo* *x***** *xxxxx* *******
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] == ' ') { cout << "Replacing path!" << endl; gameArray[startY-1][startX] = '+'; startY--; findPath(); } // moving in east else if (gameArray[startY][startX+1] == ' ') { cout << "Replacing path!" << endl; gameArray[startY][startX+1] = '+'; startX++; findPath(); } // backtracking else if (gameArray[startY][startX] == '+') { cout << "Backtracking" << endl; gameArray[startY][startX] = 'X'; if (moveEast == false) { startX--; } else if (moveNorth == false) { startY++; } findPath(); } // checking for max north else if (gameArray[startY-1][startX] == '*' && moveNorth == true) { moveNorth = false; cout << "Max for north reached!" << endl; gameArray[startY][startX] = 'X'; startY++; findPath(); } // checking for max east else if (gameArray[startY][startX+1] == '*' && moveEast == true) { moveEast = false; cout << "Max for east reached!" << endl; gameArray[startY][startX] = 'X'; startX--; findPath(); } }
#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; } } }
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; } } }
int main () , not void main () , even if your compiler lets you get away with it. You have what appears to be an infinite loop. See red code above. If that is intentional, you should probably change it to a while (true) loop so it's more obvious that it is intentional. | DaniWeb Message | |
| Cancel Changes | |