Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Programming : Description Set the scene. You re a woodworker, and you have an assortment of rats as pets. You love your dear pets

Java Programming : Description
Set the scene. Youre a woodworker, and you have an assortment of rats as pets. You love your
dear pets but cant seem to keep up with carving them new race tracks (mazes) by hand. Instead
youve enlisted the help of a robot to do the carving. The problem is you need to provide these
randomized tracks in the form of a text document to the robot.
How does one go about creating a map as a text document to do this? We will need some rules
You can carve tracks into the wood provided:
1. No tracks cross over into another track.
2. You move in 1 mm increments in directions chosen at random.
3. If you end up in a place where you cant carve in any direction (or you cross over
into another track), you need to back up (backtracking!) and try a different
direction.
4. There are only 4 directions you can carve into the wood. Think of them as north,
south, east and west.
5. You need to specify and beginning point (entry) and an ending point (the cheese).
Exercises
1. Download the following class source files:
TrackCarver.java
TrackGenerator.java
2. In Eclipse, create a new Java project called "Project 1", and import the above 2 files.
3. Compile and run the program. Make sure it works (although it wont do much beyond
asking you the dimensions of the array).
4. The class TrackCarver has been implemented for you. This is the main class.
5. The class TrackGenerator needs to be implemented by you. Specifically the following
methods need to be filled in:
private static void fill2DArray( char[][] wood)
The purpose of this method is to fill the 2 dimensional array with all one character
to represent a fresh piece of wood.
public static void carveTrack(char[][] wood, int x, int y)
This method is the recursive method. wood should already be filled with the
character that you chose to fill it with in fill2DArray (you dont need to call it). x
and y is the location your algorithm is currently at. At this point, your virtual
chisel has to check to see if it can move in any direction. Randomly chisel in a
direction provided you can (see above rules). This means chiseling through two
spots of the array by setting those cells to blank spaces and passing off this new
location to a recursive call of this method. When it can no longer make any moves
(it has no neighbors), the method simply returns.
To help with carveTrack, I have provided you with 5 methods.
1. hasWestNeighbor
2. hasEastNeighbor
3. hasNorthNeighbor
4. hasSouthNeighbor
5. hasNoNeighbors
You can use these methods to help you determine if there are neighbors and which
neighbors it has. Again, neighbors are defined as a place you can chisel to, that will
not chisel into an already existing tunnel. import java.io.File;
import java.io.PrintWriter;
import java.util.Random;
public class TrackGenerator {
private static Random rand = new Random();
private static void fill2DArray(char [][]wood)
{
//Fill the 2D array 'wood' with non-space characters
/*********************************************
* TODO FILL IN CODE HERE
*********************************************/
}
private static void carveTrack(char [][] wood, int x, int y)
{
/*********************************************
* TODO FILL IN CODE HERE
*********************************************/
}
private static boolean hasWestNeighbor(char[][] wood, int x, int y)
{
return (x-2)>=1 && wood[x-2][y]!='';
}
private static boolean hasEastNeighbor(char[][] wood, int x, int y)
{
return (x+2)=1 && wood[x][y-2]!='';
}
private static boolean hasSouthNeighbor(char[][] wood, int x, int y)
{
return (y+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 And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago