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 '*'


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  //Comments are used to understand the code as well as others reading it 
#include <iostream>
#include<string>
#include<vector>
#include<fstream>
#include<cstdlib>

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.