Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 3 a. Build an app that randomly creates a 10 by 10 grid of 0s and 1s where the 0s represent open spaces and

Problem 3

a. Build an app that randomly creates a 10 by 10 grid of 0s and 1s where the 0s represent open spaces and 1s represent blocked spaces (like we did in class), and then see if user can navigate from position 0,0 to where either the first index or second index equal 9 w/o hitting a 1. In other words the user must move clear across the grid without hitting a 1 or the game is over.

b. This problem will involve the following new features: java.util.Random class, Two-dimensional arrays

2. Details

Classes:

GameGrid_Test

Will have main method and will instantiate instance of GameGrid and call run... that's it.

GameGrid

This will have one public "run" method and this will be called by the GameGrid_Test class's main method. All code can be in the "run" method.

Create game grid: This method will create a game grid (two-dimensional int array) and fill each cell with 1 or 0 randomly using the Random class.

There should be a LESS CHANCE to have a 1 fill the spot than a 0.

1. Reminder Tip:

a. Set an int variable called iWallChance to a number like 3, and then use the bound parameter of the Random classs nextInt method to control the range of possible numbers just like we did in class.Then you'd compare the returned random number to see if it's less than your iWallChance variable. If it was less, you'd set that cell to 1... if not, then the cell would be set to 0.

b. Change the location [0][0] of the grid to 0 so that the first space is open and not a wall because that's where we'll start our user.

c. Create two int variables for the user's position... iUserRow and iUserCol... and set each to 0.

d. Create a boolean variable for the following loop to know when to exit.

e. Create while loop for moving: This loop will keep asking user whether to move down or to the right.

i. If the user chooses down, then that will affect the first array value (row); and if the user chooses right, that will affect the second array value.

ii. With each user answer, adjust the iUserRow or iUserCol positions depending whether down or right by increasing the correct variable by 1.

iii. Check if this new user position is a 1 in the grid. If it's a 1, tell the user they failed and exit loop.

iv. If it was not a 1, then check if either user position is now a 9.

1. If one of them is 9, they have reached either the right side or bottom and tell them they have won and exit loop.

f. After loop is over, then print out the grid

i. With each cell, before you print out 0 or 1, check if that cell (x and y) equals the user position (iUserRow and iUserCol).

Classes and Adjustments to Problem 3.

Add to Problem 3 by tracking the players moves in a linked list. Then, before printing out the maze at the end, cycle through all of the nodes of the linked list and change the cell value of each grid spot where the user has been to a number like 3 or 4. Track the number of moves the player makes and print that out at the end as well.

i. Node

- This is a new class that needs to be added to Problem3.

1.Instance variables:

a. xPosition - int type.

b. yPosition - int type.

c. nextNode - Node type.

ii. LinkedList

-This class will hold the linked list.

1. Instance variables:

a. headNode - Node type.

2. Methods:

a. addHeadNode

i. Takes in two parameters, x and y, which are integer types.

ii. Returns nothing.

iii. With the x and y params, it creates a new node object and sets the nextNode on the new node object to the existing headNode object.

iv. It then assigns this new node object to the headNode variable.

b. removeHeadNode

i. Takes in no parameters.

1. It needs to change the current head node to the node that is in the head node's nextNode variable; and it needs to return the current head node to the method caller.

2. Essentially, you're taking the head node out of the linked list and returning it and setting the next node in line as the new head node.

iii. Updating run method in GameGrid class

1. You need to instantiate a LinkedList instance

2. In the while loop, use the iUserRow and iUserCol values to call the addHeadNode method on the LinkedList instance to add a node for every user move. 3. Create a second while loop: After teh existing while loop, but before the grid print out.

a. This loop will run until all LinkedList nodes have been processed.

b. In the loop, call the removeHeadNode method on the LinkedList instance and use the returned node's xPosition and yPosition to update the grid at that position with any number you choose besides 0 and 1.

c. Use this loop to count the number of player moves.

4. Print out the total number of player moves at the end when printing the grid.

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_2

Step: 3

blur-text-image_3

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

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

5. A review of the key behaviors is included.

Answered: 1 week ago