Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

  1. SUMMARY
  1. 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.
  1. Secondly, track the number of moves the player makes and print that out at the end as well.
  1. This lab will involve the following new features:
    1. Singly Linked List
  1. DETAILS
  1. There won't be as much detail on the singly linked list since we went over an implementation in class.
  1. Classes and Adjustments to Lab 3.
  1. Node - This is a new class that needs to be added to Lab 3.
    1. Instance variables:
      1. xPosition - int type.
      2. yPosition - int type.
      3. nextNode - Node type.
  2. LinkedList - This class will hold the linked list.
    1. Instance variables:
      1. headNode - Node type.
    2. Methods:
      1. addHeadNode
        1. Takes in two parameters, x and y, which are integer types.
        2. Returns nothing.
        3. 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.
        4. It then assigns this new node object to the headNode variable.
      2. removeHeadNode
        1. Takes in no parameters.
        2. 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.
    1. 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.
        1. This loop will run until all LinkedList nodes have been processed.
        2. 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.
        3. 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

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

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

Pro Database Migration To Azure Data Modernization For The Enterprise

Authors: Kevin Kline, Denis McDowell, Dustin Dorsey, Matt Gordon

1st Edition

1484282299, 978-1484282298

More Books

Students also viewed these Databases questions