The program simulates water flood in a maze. The water outflows from the point i
ID: 3733517 • Letter: T
Question
The program simulates water flood in a maze. The water outflows from the point indicated by 'S' and covers all blanks at the end.
1.The flood water covers the nearby areas first and gradually flows to other remote areas. This is more similar to the real flood.
2. The flood should be marked with '~'
3. nearby areas being the four directions from starting position 'S' (up, down, left, right) and then from those '~' look for their four directions
4. 'X' is a wall
Also the "maze_flood.txt" is the image below.
#include
#include
#include
using namespace std;
const int SIZE = 15;
class Maze
{
private:
char maze2D[SIZE][SIZE];
public:
int startX, startY;
Maze()
{
ifstream mapfile("maze_flood.txt");
if (mapfile.is_open())
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
mapfile.get(maze2D[i][j]);
if (maze2D[i][j] == 'S')
{
startX = j;
startY = i;
}
}
mapfile.ignore();
}
mapfile.close();
}
else
cout << "File cannot be opened." << endl;
}
void show()
{
for (int i = 0; i < SIZE; i++)
{
cout.write(maze2D[i],SIZE);
cout << endl;
}
cout << endl;
}
void flood()
{
/// TO DO: Add your code here
}
};
int main()
{
Maze mz;
mz.flood();
mz.show();
return 0;
}
Explanation / Answer
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
// Defines size as constant and its size to 15
const int SIZE = 15;
// Class Maze definition
class Maze
{
private:
// Creates an character array of constant row and column of SIZE
char maze2D[SIZE][SIZE];
public:
// To store startX and startY position of 'S'
int startX, startY;
// Default constructor definition
Maze()
{
// Opens the file for reading
ifstream mapfile("maze_flood.txt");
// Checks if the file can be opened
if (mapfile.is_open())
{
// Loops till SIZE for rows
for (int i = 0; i < SIZE; i++)
{
// Loops till SIZE for columns
for (int j = 0; j < SIZE; j++)
{
// Extracts character from the file and stores it in maze2D index position i for row and j for column
mapfile.get(maze2D[i][j]);
// Checks if the current cell contents of maze2D is character 'S'
if (maze2D[i][j] == 'S')
{
// Store the row and column index as startX and startY
startX = j;
startY = i;
}// End of inner if condition
}// End of inner for loop
mapfile.ignore();
}// End of outer for loop
// Close the file
mapfile.close();
}// End of outer if condition
// Otherwise display error message and stop the program
else
{
cout << "File cannot be opened." << endl;
// Stop the program
exit(0);
}// End of else
}// End of function
// Function to display the maze
void show()
{
// Loops till SIZE
for (int i = 0; i < SIZE; i++)
{
// Write the data to console output for each row till SIZE
cout.write(maze2D[i], SIZE);
cout << endl;
}// End of for loop
cout << endl;
}// End of function
// Function to show the water flow direction
void flood()
{
// Loops from startY position to SIZE
for (int i = startY; i < SIZE; i++)
{
// Loops from startX position to SIZE
for (int j = startX+1; j < SIZE; j++)
{
// Checks if the current position character is space
if(maze2D[i][j] == ' ')
// Adds a '~' symbol
maze2D[i][j] = '~';
// Checks if the current position character is character 'X' then come out of the inner loop
if(maze2D[i][j] == 'X')
break;
}// End of inner for loop
}// End of outer for loop
}// End of function
};// End of class
// main function definition
int main()
{
// Creates an object of the class Maze
Maze mz;
// Calls the function to create water flow
mz.flood();
// Calls the function to display the Maze flow
mz.show();
return 0;
}// End of main function
Sample Output:
XXXXXXXXXXXXXXX
X XXXX XXXXX
X X XX X XXX
XX X X XX X X X
X X X X
X XXXXX XXX XXX
X X XXX X X X
X X XXX X XXX X
X X X X
XX S~~X X X
XX XXXXX~~X X X
XX X ~X X X
X X ~X X X
X X ~~~~X X
XXXXXXXXXXXXXXX
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.