Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In c++. You will have to create 3 files: main.cpp, maze.h, maze.cpp Create a maze generator where you ask the user for the number of

In c++. You will have to create 3 files: main.cpp, maze.h, maze.cpp

Create a maze generator where you ask the user for the number of rows and columns and a random seed generator.

At each iteration a wall is removed to connect two adjacent cells. This iterative process must follow these rules:

  • Walls to be removed should be selected randomly.
    • Use std::rand() (read more) to generate random numbers and std::srand() (read more) to provide a seed to the random number generator
  • There should be exactly one path connecting the starting and ending cells
  • Every cell must be reachable from the starting cell

image text in transcribed

Picking a random neighbor must follow this procedure:

  1. Check the neighbors of a cell in N-S-E-W order and
  2. append the neighbors that were not visited yet into an empty vector neighbors
  3. Then use the index idx, as defined below, to pick a random neighbor with neighbors[idx]
idx = std::rand() / ((RAND_MAX + 1u) / neighbors.size());

You would need to be able to ask the user the following things

 the seed value for the random number generator number of rows in the grid N > 0 number of cols in the grid M > 0 file name for the output 

The seed argument is very important as it initializes the random number generator. If you change the seed, you will generate a different maze. In your code make sure you call this function exactly once before generating the maze:

std::srand(seed);

The file format for saving the maze is a two dimensional array of integers, where each integer is used to represent a cell and its walls. Each integer in the matrix ranges from 0 to 15. The idea behind this representation is that the walls are encoded using 4 bits (a nibble), and the integers are their corresponding values in decimal notation. The figure below illustrates the encoding, with 4 of the possible 16 possibilities.

image text in transcribedWhen saving the grid, the output file must be a text file in which cell values are separated by a single whitespace, and organized in n rows and m columns (the grid dimensions). In the example below Is shows the output textfile and what it visually would look like.

image text in transcribed

create empty dynamic array (henceforth referred to as A) mark cell [0,0] as visited insert cell [0,0] at the end of A while A is not empty remove last element from A (henceforth referred to as current) current's neighbors not visited yet (henceforth referred to as neighbors) if neighbors is not empty insert current at the end of A pick a random neighbor from neighbors (henceforth referred to as neigh) remove the wall between current and neigh mark neigh as visited insert neigh at the end of A endir endwhile N SEW N 15 E w s BITSTRING DECinse WOLLS 0 1 1 1 H F 100, = 9 3 13 12 12 10 3 9 12 10 3 3 3 13 4 2 3 5 10 11 3 5 12 6 5 2 create empty dynamic array (henceforth referred to as A) mark cell [0,0] as visited insert cell [0,0] at the end of A while A is not empty remove last element from A (henceforth referred to as current) current's neighbors not visited yet (henceforth referred to as neighbors) if neighbors is not empty insert current at the end of A pick a random neighbor from neighbors (henceforth referred to as neigh) remove the wall between current and neigh mark neigh as visited insert neigh at the end of A endir endwhile N SEW N 15 E w s BITSTRING DECinse WOLLS 0 1 1 1 H F 100, = 9 3 13 12 12 10 3 9 12 10 3 3 3 13 4 2 3 5 10 11 3 5 12 6 5 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Access Patterns Database Interactions In Object Oriented Applications

Authors: Clifton Nock

1st Edition

0321555627, 978-0321555625

More Books

Students also viewed these Databases questions

Question

What are the major social responsibilities of business managers ?

Answered: 1 week ago

Question

What are the skills of management ?

Answered: 1 week ago

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago