Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

If the player spawns in the Dining Room, the game would display: Dining Room You see a table set for dinner. This auto-look behavior should

If the player spawns in the Dining Room, the game would display: Dining Room You see a table set for dinner. This auto-look behavior should only happen if the players command causes them to move to a different room. If the player enters an invalid command or enters a command that doesnt succeed in changing rooms, the game must not automatically display the rooms description. For example, if the player enters a movement command that would cause the player to move outside of the house (which the game responds to by displaying The way is blocked!), the player would not move to a different room and the rooms description would not automatically be displayed. Also note that this auto-look feature does not prevent the player from manually executing the LOOK command at any time. Additionally, I need to modify the way that the descriptions are set for each room. Presently, I can access each of the Room instances by supplying indices into the 2-Dimensional rooms array. For example, I can access the Dining Room with code such as: rooms[0][2] This mechanism is error-prone and not very readable. Nor does this type of array access lend itself to easily associating room descriptions with room names. Whats needed is an associative array an array that associates some kind of Key to a Value. Java already supports such constructs such as the JAVA class HashMap. I need to associate each Room object (the Value) with its name (the Key). In this way, Ill be able to refer to a specific room with code like: roomMap.get("Dining Room") instead of the error-prone-and-less-readable code that accesses an array element by its index.

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!"); InitializeRoomDescriptions(); SpawnPlayer(); Room currentRoom = rooms[locationX][locationY]; Room previousRoom = currentRoom; Scanner scanner = new Scanner(System.in);

Commands command = Commands.UNKNOWN; while (command != Commands.QUIT) { System.out.println(currentRoom); 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(currentRoom.getDescription()); break;

case NORTH: case SOUTH: case EAST: case WEST: Move(command); currentRoom = rooms[locationX][locationY]; if (currentRoom != previousRoom) { System.out.println(currentRoom.getDescription()); previousRoom = currentRoom; } 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; } }

private static void Move(Commands command) { if (IsDirection(command) == false) { throw new IllegalArgumentException(); }

boolean isValidMove = false; switch (command) { case NORTH: if (locationY < rooms[locationX].length - 1) { isValidMove = true; locationY++; } break;

case SOUTH: if (locationY > 0) { isValidMove = true; locationY--; } break;

case EAST: if (locationX < rooms.length - 1) { isValidMove = true; locationX++; } break;

case WEST: if (locationX > 0) { isValidMove = true; locationX--; } break; }

if (isValidMove == false) { System.out.println("The way is blocked!"); } }

private static boolean IsDirection(Commands command) { return directions.contains(command); } private static void SpawnPlayer(){ Random random = new Random(); locationX = random.nextInt(rooms.length); locationX = random.nextInt(rooms[0].length); } private static void InitializeRoomDescriptions() { rooms[0][0].setDescription("You see a computer sitting on a desk."); rooms[0][1].setDescription("You see a table set for dinner."); rooms[0][2].setDescription("You are in the bathroom. You see a toilet and a vanity."); rooms[1][0].setDescription("A rubber mat saying 'Welcome to Zork!' lies by the door."); rooms[1][1].setDescription("You see a tidy, cozy kitchen."); rooms[1][2].setDescription("The hallway is a bit dark. Maybe there's a light switch?"); rooms[2][0].setDescription("You see books. Many, many books."); rooms[2][1].setDescription("You are in the living room."); rooms[2][2].setDescription("You see a bed, a dresser, and a mirror."); } private static Room[][] rooms = { { new Room("Study"), new Room("Dining Room"), new Room("Bathroom") }, { new Room("Foyer"), new Room("Kitchen"), new Room("Hallway") }, { new Room("Library"), new Room("Living Room"), new Room("Bedroom") } }; private static int locationX = 0; private static int locationY = 0; private static final ArrayList directions = 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

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

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Sham Navathe

4th Edition

0321122267, 978-0321122261

More Books

Students also viewed these Databases questions