### How do I update the maze? Hi in my algorithms class we are creating a 2D pathfinding maze where we read in the maze from a text file, and that asks the user to input where exactly they want to start and from there it finds the path while storing its path into a stack and then outputting the solution into a linked list. Right now I'm trying to get my code to update the maze after they select where they want to start and once they do that point is labeled with a '*'

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117`` `````` //Comments are used to understand the code as well as others reading it #include #include #include #include #include using namespace std; bool hedge = false; bool foot_paths = true; void options(int, int); const int ROW = 3; const int COLUMN = 6; //sets up the coordinates of the starting position where the user decides to start struct coordinate { int x, y; }; //checks to see if the starting point is valid, if they start on a hedge bool pointValidation(int row, int column){ return (row >= 0) && (row < ROW) && (column >= 0) && (column < COLUMN); } //function that reads the Maze.txt file into an array void createMaze() { char array[ROW][COLUMN]; fstream infile; string mazeLine; infile.open("Maze.txt"); if (infile.is_open()) { for (int i = 0; i < ROW; i++) { infile >> mazeLine; for (int j = 0; j < COLUMN; j++) { array[i][j] = mazeLine[j]; } } //this is for printing the maze for (int x = 0; x < ROW;x++) { for (int y = 0; y < COLUMN;y++) { cout << array[x][y]; } cout << endl; } } infile.close(); } //Check to see if the user input was valid void options(char Maze[ROW][COLUMN]) { int inputRow, inputCol; cout << "Enter ROW #(1 -3)" << endl; cin >> inputRow; inputRow -= 1; cout << "Enter COLUMN # (1 -6)" << endl; cin >> inputCol; inputCol -= 1; while (inputRow < 0 || inputRow > 3 || inputCol < 0 || inputCol > 6) { if (inputRow < 0 || inputRow > 3) { cout << "Enter ROW #(1 -3)" << endl; cin >> inputRow; inputRow -= 1; cout << "picked row" << endl; } else if (inputCol < 0 || inputCol > 6) { cout << "Enter COLUMN # (1 -6)" << endl; cin >> inputCol; inputCol -= 1; cout << "picked Column" << endl; } pointValidation(inputRow, inputCol); if (Maze[inputRow][inputCol]==inputRow,inputCol) { Maze[inputRow][inputCol] == '*'; } } } void printMaze() { } int main() { char Maze[ROW][COLUMN]; createMaze(); options(Maze); system("PAUSE");//pauses the program from flashing off quickly return 0;//excution went through fine } ``````
Last edited on In createMaze() you currently fill a local array with the maze. Instead you should pass the maze like you do for options(...).

Further more divide your `createMaze()` into `loadMaze(char Maze[ROW][COLUMN])` and `showMaze(char Maze[ROW][COLUMN])`.

Do not store the '*' within the maze array. Instead store the position separately. Whenever an update is required show the maze first and then the '*'. First off thank you for your reply! It means a lot! Secondly okay that makes sense! In the two new functions would I split the for loops as in createMaze() as well since one opens and stores the maze and the other prints it out? Well, basically you just have to move the entiry loop from line 45 into printMaze(). The problem was line 30 which shouldn't exists (instead use maze as a paramter).

printMaze() as much as createMaze() needs the parameter `char Maze[ROW][COLUMN]`. Then you may call printMaze(...) within createMaze(...).

It is generally a good idea to store the moveable objects outside the array und all non movable things in the array.
Topic archived. No new replies allowed.