Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can someone help me fix this? It seems every time I fix one bit of code I get three more errors. import java.util.*; enum Commands
Can someone help me fix this? It seems every time I fix one bit of code I get three more errors.
import java.util.*; enum Commands { UNKNOWN, QUIT, LOOK, NORTH, SOUTH, EAST, WEST } public class Main { public static void main(String[] args) { System.out.println("Welcome to Zork!"); SpawnPlayer(); Scanner scanner = new Scanner(System.in); Commands command = Commands.UNKNOWN; while (command != Commands.QUIT) { System.out.println(rooms[locationRow][locationColumn]); System.out.print(">"); String inputString = scanner.nextLine(); command = ToCommand(inputString); switch (command) { case QUIT: System.out.println("Thank you for playing!"); break; case LOOK: System.out.println("A rubber mat saying 'Welcome to Zork!' lies by the door."); break; // North, South, East and West have all the same behaviors case NORTH: case SOUTH: case EAST: case WEST: // Move command passes the command to the method to navigate throughout the maze Move(command); break; default: System.out.println("Unknown command."); break; } } } private static Commands ToCommand(String commandString) { try { return Commands.valueOf(commandString.trim().toUpperCase()); } catch (IllegalArgumentException ex) { return Commands.UNKNOWN; } } // New Move method that uses commands to navigate throughout the maze // uses a boolean method IsDirection which passes back private static void Move(Commands command) { if (IsDirection(command) == false) { throw new IllegalArgumentException(); } boolean isValidMove; switch (command) { case NORTH: if (locationRow > 0) { isValidMove = true; locationRow--; } break; case SOUTH: if (locationRow < rooms[locationColumn].length() - 1) { isValidMove = true; locationRow++; } case EAST: if (locationColumn < rooms.length - 1) { isValidMove = true; locationColumn++; } else { isValidMove = false; } break; case WEST: if (locationColumn > 0) { isValidMove = true; locationColumn--; } else { isValidMove = false; } break; default: isValidMove = false; break; } if (isValidMove == false) { System.out.println("The way is blocked!"); } } // IsDirection method to process the command private static boolean IsDirection(Commands command) { return directions.contains(command); } // Final Static array which contains the rooms // Foyer is at poistion 0 private static void SpawnPlayer(){ Random random = new Random(); locationRow = random.nextInt(rooms.length); System.out.println("Location1: " + locationRow); locationRow = random.nextInt(rooms[0].length()); System.out.println("Location1: " + locationRow); } private static final String[][] rooms = { { "Foyer", "Kitchen", "Dining Room" }, { "Living Room", "Bedroom", "Bathroom" }, { "Game Room", "Laundry Room", "Porch" } }; // Sets the starting room location to 0 which is the Foyer private static int locationColumn = 0; private static int locationRow = 0; // Array with a list of valid commands private static final ArrayListdirections = new ArrayList (Arrays.asList( Commands.NORTH, Commands.SOUTH, Commands.EAST, Commands.WEST)); }
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