Question: You will use loops and random number generation to develop a simulation of the Wiccans search for herbs Start with an ItemLocation.java object. This will

You will use loops and random number generation to develop a simulation of the Wiccans search for herbs

Start with an ItemLocation.java object. This will keep track of a single cell of your grid where your items: Wiccan, Herbs and Wolves can be. So this just needs two instances variables, one for x and one for y. Write your getters and setters and two helper methods:

public boolean equalTo(ItemLocation itemIn) Used to compare two item locations. One will be implicit and one explicit in your call to this method. Return true if the explicit ItemLocation is equal to both the x and y instance variables of the implicit.

public boolean notEqualTo(ItemLocation itemIn) Used to compare two item locations. One will be implicit and one explicit in your call to this method. Return false if the explicit ItemLocation is not equal to either the x and y instance variables of the implicit.

(Yes you could do this with one method but these two will make the other code you will need to write easier to read. Recall the compareTo method for Integers this also uses implicit and explicit of Integer to compare)

Now Create your Wiccan.java object. You will need an instance variable for each: wiccan, herb and wolves (2). So 4 variables that keep track of their location. What is their type?

The grid is conceptual so we just need to define a variable that keeps track of its size. We will work with an NxN grid for the Wiccan to search for her herbs so you can use a single static instance variable to set the conceptual size of the grid. Start with 5 for development.

private static int GRID_SIZE = 5;

Wiccan constructor:

1) Randomly place a batch of herbs (setting the instance variable herbLocation) in a random X,Y square of the grid. this means you set the x and y of your herbLocation variable to a random number between 1 and your GRID_SIZE.

2) Now collecting herbs in the forest is a dangerous endeavor so you will also randomly place two wolves. The wolves cannot be placed in the same location as the herbs or as each other to start. (HINT: use a while loops to make sure you have a different X,Y for herbs and also each wolf). This is a bit tricky to figure out so take your time here and put on your thinking cap! Your helper methods in the ItemLocation class will be VERY beneficial.

3) Randomly place the wiccan such that she is not in either wolf or herb location.

Ready to Go: Now you are ready for the Wiccan to begin searching for her herbs!

1) First in Wiccan lets create a printLocations method that will nicely print the locations of the wolves, the herbs and the witch with some labels. Here is a good sample format:

The herbs are at: x: 7 and y: 5

Wolf one is at: x: 7 and y: 1

Wolf two is at: x: 5 and y: 1

The wiccan is at: x: 3 and y: 2

2) In your main in your Wiccan class call your Wiccan constructor and then your printLocations method.

3) You will now write your moveWitch method in the Wiccan class. Keep randomly placing the Wiccan in an X,Y location of the grid. Sounds like another good spot for a loop. You will stop the loop if the Wiccan finds her herbs or sadly finds a wolf instead. At each iteration of the loop if the Wiccan has not found her herbs or the wolves print an appropriate message:

The Wiccan is at X,Y and is still searching

When the Wiccan finds her herbs or a wolf the loop will exit and print an appropriate message as to whether she found her herbs or a wolf.

Begin the search by printing in main (after your printLocations call): The Wiccan is searching for her herbs. Good Luck!

Your code should pause for terminal input here to give the user a moment to see the location of all items. Prompt for the enter key.

call moveWitch next.

At each iteration also print a message that is relative to her position and the herbs or the wolf.

That is if the wiccan is within 3 spaces of either coordinate to the wolf: LOOKOUT A WOLF IS CLOSE

Or if the wiccan is within 3 spaces of either coordinate to the herbs: YOU ARE ALMOST THERE

Here is a sample interaction with a 9x9 grid and the 100% level implemented:

The herbs are at: x: 7 and y: 5

Wolf one is at: x: 7 and y: 1

Wolf two is at: x: 5 and y: 1

The wiccan is at: x: 3 and y: 2

The Wiccan is searching for her herbs. Good Luck!

Press enter to continue...

WATCH OUT A WOLF!

The herbs are at: x: 7 and y: 5

Wolf one is at: x: 7 and y: 1

Wolf two is at: x: 5 and y: 1

The wiccan is at: x: 1 and y: 0

Press enter to continue...

WATCH OUT A WOLF!

The herbs are at: x: 7 and y: 5

Wolf one is at: x: 7 and y: 1

Wolf two is at: x: 5 and y: 1

The wiccan is at: x: 4 and y: 0

Press enter to continue...

YOU ARE ALMOST TO THE HERBS!

WATCH OUT A WOLF!

The herbs are at: x: 7 and y: 5

Wolf one is at: x: 7 and y: 1

Wolf two is at: x: 5 and y: 1

The wiccan is at: x: 3 and y: 3

Press enter to continue...

WATCH OUT A WOLF!

The herbs are at: x: 7 and y: 5

Wolf one is at: x: 7 and y: 1

Wolf two is at: x: 5 and y: 1

The wiccan is at: x: 1 and y: 0

Press enter to continue...

YOU ARE ALMOST TO THE HERBS!

The herbs are at: x: 7 and y: 5

Wolf one is at: x: 7 and y: 1

Wolf two is at: x: 5 and y: 1

The wiccan is at: x: 0 and y: 5

Press enter to continue...

WATCH OUT A WOLF!

The herbs are at: x: 7 and y: 5

Wolf one is at: x: 7 and y: 1

Wolf two is at: x: 5 and y: 1

The wiccan is at: x: 2 and y: 0

Press enter to continue...

YOU ARE ALMOST TO THE HERBS!

WATCH OUT A WOLF!

The herbs are at: x: 7 and y: 5

Wolf one is at: x: 7 and y: 1

Wolf two is at: x: 5 and y: 1

The wiccan is at: x: 7 and y: 8

Press enter to continue...

The herbs are at: x: 7 and y: 5

Wolf one is at: x: 7 and y: 1

Wolf two is at: x: 5 and y: 1

The wiccan is at: x: 7 and y: 1

Press enter to continue...

Oh no a wolf!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!