Need assistance with small piece of larger assignment writing a recursive division algorithm (Java).
The assignment is to write a recursive division algorithm to build a maze in a 2D array.
I have code that will randomly generate vertical and horizontal walls and randomly place doorways in the walls.
Requirements:
The maze should be broken into sub-regions on each recursive method call until the sub-region is no less than 3x3.
My recursive calls only seem to work on the top left sub-region of each call and I can't seem to get it to work on every sub-region in the entire maze board.
Outline of my code:
1) BASE CASE - If the width of sub region is less than 3 and height of a sub-region is less than 3 (smaller than 3x3), do not run recursive method again. {
(below code is inside base case if statement)
variable X = randomly select a x value for vertical wall in the range of left boundary to right boundary
variable Y = randomly select a y value for horizontal wall in range of top boundary to bottom boundary
for loop to build vertical wall
randomly place passage somewhere along vertical wall
for loop to build horizontal wall
randomly place passage on horizontal wall LEFT of vertical wall
randomly place passage on horizontal wall RIGHT of vertical wall
run recursive method
}
Thoughts:
Should one recursive method call go inside of the base case if statement, then another call outside of it? How can I ensure EVERY sub-region greater than 3x3 is being divided to the base case.
Dear Learner,
The solution is in the question itself. The outline of the code says that IF the width of sub region is less than 3 and height of a sub-region is less than 3 (smaller than 3x3), do not run recursive method again. We can use this to stop our recursive calls.
This can be done in the following manner -
Passing the dimension and coordinates as parameters
You can pass the dimensions of the region during the function call and reduce it everytime.
begin
{
m = width of board //to set the dimensions of the region (we will keep on reducing this during our recursive calls)
n = height of board
while x<=number of columns and y<=number of rows
{ //this part will help us to traverse through all the regions
if (x==number of columns)
CreateMaze(1,y++,m,n) //go to next row first column
else
CreateMaze(x++,y,m,n) //shift left
}
}
------------------------------------------
CreateMaze(int x, int y,int m,int n) //assuming the base case is m*n dimension with its top left coordinate as x,y
{
if(m==3 && n ==3) //this will be the base condition
{
variable X = randomly select a x value for vertical wall in the range of left boundary to right boundary
variable Y = randomly select a y value for horizontal wall in range of top boundary to bottom boundary
for loop to build vertical wall
randomly place passage somewhere along vertical wall
for loop to build horizontal wall
randomly place passage on horizontal wall LEFT of vertical wall
randomly place passage on horizontal wall RIGHT of vertical wall
return;
}
else CreateMaze(x,y,m-1,n-1)) //this will ensure that you call every subregions
}
This should perform the way you are expecting.
I hope I have answered the question the way you were expecting. If you still have any doubts or any other questions regarding this, feel free to ask us in the comment section.
Please leave a like if this was helpful.
Thanks
Happy Studying
Need assistance with small piece of larger assignment writing a recursive division algorithm (Java). The assignment is t...
This lab will use 2D arrays, recursive algorithms, and logical thinking. The following grid of hashes(#) and dots(.) is a 2D array representation of a maze # # # # # # # # # # # # # . . . # . . . . . . # . . # . # . # # # # . # # # # . # . . . . # . # # . . . . #...
Make a program using Java that asks the user to input an integer
"size". That integer makes and prints out an evenly spaced, size by
size 2D array (ex: 7 should make an index of 0-6 for col and rows).
The array must be filled with random positive integers less than
100. Then, using recursion, find a "peak" and print out its number
and location. (A peak is basically a number that is bigger than all
of its "neighbors" (above,...
JAVA 3 LECTURE REVIEW PLEASE NEED ANSWERS ASAP. DUE IN AN HOUR!!! Question 12 points The best-case performance for a shell sort is: --- O(1) O(n2) O(n) O(n log n) Signaler cette question Question 22 points The best-case performance for an array of n items using insertion sort is: --- O(n2) O(n) O(1) there is no best-case Signaler cette question Question 3 2 points A recursive method that processes a chain of linked nodes --- uses the first node in...
Please to indent and follow structure!!!!!
Assignment 3 - The card game: War Due Date: June 9th, 2018 @
23:55
Percentage overall grade: 5% Penalties: No late assignments
allowed
Maximum Marks: 10
Pedagogical Goal: Refresher of Python and hands-on experience
with algorithm coding, input validation, exceptions, file reading,
Queues, and data structures with encapsulation.
The card game War is a card game that is played with a deck of
52 cards. The goal is to be the first player to...
This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...
I have this case study to solve. i want to ask which
type of case study in this like problem, evaluation or decision? if
its decision then what are the criterias and all?
Stardust Petroleum Sendirian Berhad: how to inculcate the pro-active safety culture? Farzana Quoquab, Nomahaza Mahadi, Taram Satiraksa Wan Abdullah and Jihad Mohammad Coming together is a beginning; keeping together is progress; working together is success. - Henry Ford The beginning Stardust was established in 2013 as a...