Question
SUMMARY 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
- SUMMARY
- 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.
- Secondly, track the number of moves the player makes and print that out at the end as well.
- This lab will involve the following new features:
- Singly Linked List
- DETAILS
- There won't be as much detail on the singly linked list since we went over an implementation in class.
- Classes and Adjustments to Lab 3.
- Node - This is a new class that needs to be added to Lab 3.
- Instance variables:
- xPosition - int type.
- yPosition - int type.
- nextNode - Node type.
- Instance variables:
- LinkedList - This class will hold the linked list.
- Instance variables:
- headNode - Node type.
- Methods:
- addHeadNode
- Takes in two parameters, x and y, which are integer types.
- Returns nothing.
- 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.
- It then assigns this new node object to the headNode variable.
- removeHeadNode
- Takes in no parameters.
- This is tricky so you need to carefully think through how to do this, but the following is what it needs to accomplish:
- 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.
- 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.
- addHeadNode
- Updating run method in GameGrid class.
- You need to instantiate a LinkedList instance.
- 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.
- Create second while loop: AFTER the existing while loop exits but BEFORE the grid print out.
- This loop will run until all LinkedList nodes have been processed.
- 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.
- You can also use this loop to count the number of player moves.
- Print out the total number of player moves at the end when printing the grid
- Instance variables:
heres lab 3
import java.security.SecureRandom;
import java.util.Random;
import java.util.Scanner;
public class GameGrid {
public static void gRun() {
rGrid(); }
public static void rGrid() { Scanner input = new Scanner(System.in);
int rGrid[][] = new int[10][10]; int iUserRow = 0; int iUserCol = 0; int iWallChance = 30; SecureRandom oRand = new SecureRandom();
for (int y = 0; y < rGrid[0].length; y++) {
for (int x = 0; x < rGrid.length; x++) {
int result = oRand.nextInt(99);
if (iWallChance > result) { rGrid[x][y] = 1;
} rGrid[0][0] = 0;
System.out.print(rGrid[x][y] + " "); } System.out.println();
}
int playerA = 0; int playerB = 0; int decision; boolean exit = false;
do { System.out.println("Press 1 to move to the right or press 2 to go down: "); decision = input.nextInt();
if (decision == 1) { playerB++; } if (decision == 2) { playerA++; }
if (rGrid[playerA][playerB] == 1) { System.out.println("You lose "); exit = true; } if (rGrid[playerA][playerB] == 9) { System.out.println("You win "); exit = true; }
} while (!exit); for (int y = 0; y < rGrid[0].length; y++) {
for (int x = 0; x < rGrid.length; x++) { if (playerA == x && playerB == y) { System.out.print(" X "); } else { System.out.print(rGrid[x][y] + " "); } } System.out.println(); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started