Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. SUMMARY a. Add to Lab 3 by tracking the player's moves in a linked list. Then, before printing out the maze at the end,

1. SUMMARY

a.

Add to Lab 3 by tracking the player's moves in a linked list. Then, before printing out the maze at the

end, cycle through all the nodes of the linked list and change the cell value of each grid spot where

the user has been to a number like 8 or 5 or 3 or any integer other than 0 or 1. This way the grid

print out at the end will show the player's path.

b. Secondly, track the number of moves the player makes and print that out at the end as well.

c.

This lab will involve the following new features:

i. Singly Linked List

2. DETAILS

a.

There won't be as much detail on the singly linked list since we went over an implementation in class.

b. Classes and Adjustments to Lab 3.

i.

Node

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

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.

ii. This is tricky so you need to carefully think through how to do this, but the following is

what it needs to accomplish:

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 that previous 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 second while loop: AFTER the existing while loop exits 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.

You can also 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.

This is lab 3:

1. SUMMARY

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 lab will involve the following new features:

i. java.util.SecureRandom class

ii. Two-dimensional arrays

2. DETAILS

a.

There won't be as much detail on the grid creation part of this as we already created an example of

the grid in class together.

b. Classes

i. GameGrid_Test

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

ii. GameGrid

1. 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.

a.

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.

i. 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 30, and then use the

bound parameter of the SecureRandom classs nextInt method to control the

range of possible numbers just like we did in class.

b. Then you'd compare the returned random number to see if it's less than your

iWallChance variable.

c.

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 as we did in class (nested for loop (which is a for

loop within a for loop)).

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).

1. If it does, print out a capital X in that spot.

2. Otherwise, simply print out the value of that spot in the array.

3. Printing tip: Remember in class that we used System.out.print... within the inner

for loop for printing so no line break was done. Then use System.out.println("");

after the closing curly bracket of the inner for loop but before the closing curly

bracket of the outer for loop. That way each row will be printed out on a separate

line for a nice readable layout.

The lab3 code:

GameGrid.java: import java.util.Random; import java.util.Scanner; public class GameGrid { static Scanner scanner = new Scanner(System.in); static Random random = new Random(); int grid[][] = new int[10][10]; int iUserRow = 0; int iUserCol = 0; void loadGrid() { for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[i].length; j++) { grid[i][j] = getRandom(); } } grid[0][0] = 0; // mark first cell open

} private int getRandom() { // below statement returns either 1, 2 or 3 int a = random.nextInt(3) + 1; // so 0 has 66% chance(Odd number), while 1 has 33% chance(even number) return (a % 2 == 0) ? 1 : 0; } private void printGrid() { for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[i].length; j++) { if (i == iUserRow && j == iUserCol) { System.out.print("X "); } else { System.out.print(grid[i][j] + " "); } }

System.out.println(); } } public void run() { loadGrid(); while (true) { printGrid(); System.out.print(" Enter (r)ight or (d)own: "); char ch = Character.toLowerCase(scanner.next().charAt(0)); if (ch == 'r') { iUserCol++; if(iUserCol == 10) { System.out.println("You went out of Grid. Invalid move!");

iUserCol--; }

} else if (ch == 'd') { iUserRow++; if(iUserRow == 10) { System.out.println("You went out of Grid. Invalid move!"); iUserRow--; } } else { System.out.println("invalid selection."); continue; } if (grid[iUserRow][iUserCol] == 1) { System.out.println("You have failed to cross successfully."); break; } if (iUserCol == 9 || iUserRow == 9) { System.out.println("you have successfully crossed.");

break; } } } public static void main(String[] args) { GameGrid gameGrid = new GameGrid(); gameGrid.run(); } }

Use IntelliJ Please

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

Implementing Ai And Machine Learning For Business Optimization

Authors: Robert K Wiley

1st Edition

B0CPQJW72N, 979-8870675855

More Books

Students also viewed these Databases questions