Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help to this Java Program please! Continue the following Room and Game program codes! Change all exits attributes in Room class by using HashMap .

Help to this Java Program please!

Continue the following Room and Game program codes!

Change all exits attributes in Room class by using HashMap.

Add the following methods to Room class

public void setExits(HashMap) public HaspMap getExits() public void addExit(String direction, Room room) public Room getExit(String direction)

Modify code in Room and Game class to adapt to the change.

CODE Room:

import java.util.Set; import java.util.HashMap; public class Room { private String description; private HashMap exits; private HashMap items; public Room(String description) { this.description = description; exits = new HashMap(); items = new HashMap(); } public void setExit(String direction, Room neighbor) { exits.put(direction, neighbor); } public void addItem(String name, Item item) { items.put(name, item); } public String getShortDescription() { return description; } public String getLongDescription() { return "You are " + description + ". " + getFullItemDescriptionList() + ". " + getExitString(); } private String getExitString() { String returnString = "Exits:"; Set keys = exits.keySet(); for(String exit : keys) { returnString += " " + exit; } return returnString; } private String getItemList() { String returnString = "There is the following item(s):"; Set keys = items.keySet(); for(String itemName : keys) { returnString += " " + items.get(itemName).getName(); } return returnString; } private String getItemDescriptionList() { String returnString = "There is the following item(s):"; Set keys = items.keySet(); for(String itemName : keys) { returnString += " " + items.get(itemName).getDescription(); } return returnString; } private String getFullItemDescriptionList() { String returnString = "There is the following item(s): "; Set keys = items.keySet(); for(String itemName : keys) { Item i = items.get(itemName); returnString += " " + i.getName() + ", " + i.getDescription() + ", weight " + i.getWeight() + " "; } return returnString; } public Room getExit(String direction) { return exits.get(direction); } public Item getItem(String name) { return items.get(name); } }

CODE Game:

public class Game { } private Parser parser; private Room currentRoom; private Stack previousRoom; public Game() { createRooms(); parser = new Parser(); previousRoom = new Stack(); } private void createRooms() { Room outside, theater, pub, lab, office; outside = new Room("outside the main entrance of the university"); theater = new Room("in a lecture theater"); pub = new Room("in the campus pub"); lab = new Room("in a computing lab"); office = new Room("in the computing admin office"); outside.setExit("east", theater); outside.setExit("south", lab); outside.setExit("west", pub); theater.setExit("west", outside); pub.setExit("east", outside); lab.setExit("north", outside); lab.setExit("east", office); office.setExit("west", lab); outside.addItem("torch", new Item("torch", "lights your path in the darkness", 1)); outside.addItem("knife", new Item("Knife of Ratslaying", "Really good at murdering those poor defenseless rats", 2)); pub.addItem("beer", new Item("beer", "a rich lager", 1)); currentRoom = outside; // it's to star the game outside, but I'm not sure if it's correct } public static void main(String[] args) { Game game = new Game(); game.play(); } public void play() { printWelcome(); boolean finished = false; while (! finished) { Command command = parser.getCommand(); finished = processCommand(command); } System.out.println("Thank you for playing. Good bye."); } private void printWelcome() { System.out.println(); System.out.println("Welcome to the World of Zuul!"); System.out.println("World of Zuul is a new, incredibly boring adventure game."); System.out.println("Type 'help' if you need help."); System.out.println(); System.out.println(currentRoom.getLongDescription()); } private boolean processCommand(Command command) { boolean wantToQuit = false; CommandWord commandWord = command.getCommandWord(); switch (commandWord) { case UNKNOWN: System.out.println("I don't know what you mean..."); break; case HELP: printHelp(); break; case G: goRoom(command); break; case LOOK: look(); break; case STOP: wantToQuit = quit(command); break; case BACK: goBack(); break; } return wantToQuit; } private void printHelp() { System.out.println("You are lost. You are alone. You wander"); System.out.println("around at the university."); System.out.println(); System.out.println("Your command words are:"); parser.showCommands(); } private void goRoom(Command command) { if(!command.hasSecondWord()) { System.out.println("Go where?"); return; } String direction = command.getSecondWord(); Room nextRoom = currentRoom.getExit(direction); if (nextRoom == null) { System.out.println("There is no door!"); } else { previousRoom.push(currentRoom); currentRoom = nextRoom; System.out.println(currentRoom.getLongDescription()); } } private void goBack() { if (previousRoom.empty() == false) { currentRoom = previousRoom.pop(); System.out.println(currentRoom.getLongDescription()); } else { System.out.println("There is no room to return to!"); } } private void look() { System.out.println(currentRoom.getLongDescription()); } private boolean quit(Command command) { if(command.hasSecondWord()) { System.out.println("Quit what?"); return false; } else { return true; } } }

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions