SlideShare a Scribd company logo
Modified Code: Game.java:
-import java.util.*;
public class Game
{
private Parser parser;
private Room currentRoom;
private Stack route;
/**
* Create the game and initialize its internal map.
*/
public Game()
{
createRooms();
parser = new Parser();
route=new Stack();
}
/**
* Create all the rooms and link their exits together.
*/
private void createRooms()
{
Room outside, theatre, pub, lab, office, basement;
// create the rooms
outside = new Room("outside the main entrance of the university");
theatre = new Room("in a lecture theatre");
pub = new Room("in the campus pub");
lab = new Room("in a computing lab");
office = new Room("in the computing admin office");
basement = new Room("in the basement");
// initialise room exits
outside.setExit("east", theatre);
outside.setExit("south", lab);
outside.setExit("west", pub);
theatre.setExit("west", outside);
pub.setExit("east", outside);
lab.setExit("north", outside);
lab.setExit("east", office);
lab.setExit("down", basement);
basement.setExit("up", lab);
office.setExit("west", lab);
currentRoom = outside; // start game outside
}
/**
* Main play routine. Loops until end of play.
*/
public void play()
{
printWelcome();
// Enter the main command loop. Here we repeatedly read commands and
// execute them until the game is over.
boolean finished = false;
while (! finished)
{
Command command = parser.getCommand();
finished = processCommand(command);
}
System.out.println("Thank you for playing. Good bye.");
}
/**
* Print out the opening message for the player.
*/
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());
}
/**
* Given a command, process (that is: execute) the command.
* If this command ends the game, true is returned, otherwise false is
* returned.
*/
private boolean processCommand(Command command)
{
boolean wantToQuit = false;
if(command.isUnknown())
{
System.out.println("I don't know what you mean...");
return false;
}
String commandWord = command.getCommandWord();
if (commandWord.equals("help")){
printHelp();
}
else if (commandWord.equals("go")) {
goRoom(command);
}
else if (commandWord.equals("back")) {
goBack();
}
else if (commandWord.equals("quit")) {
wantToQuit = quit(command);
}
return wantToQuit;
}
// implementations of user commands:
/**
* Print out some help information.
* Here we print some stupid, cryptic message and a list of the
* command words.
*/
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();
}
/**
* Try to go to one direction. If there is an exit, enter the new
* room, otherwise print an error message.
*/
private void goRoom(Command command)
{
if(!command.hasSecondWord())
{
// if there is no second word, we don't know where to go...
System.out.println("Go where?");
return;
}
String direction = command.getSecondWord();
// Try to leave current room.
Room nextRoom = currentRoom.getExit(direction);
if (nextRoom == null)
System.out.println("There is no door!");
else
{
route.push(currentRoom);
currentRoom = nextRoom;
System.out.println(currentRoom.getLongDescription());
}
}
//Try to go to the previous room
private void goBack()
{
//Check if route exist
if(route.isEmpty())
{
/*If route doesnot exist. Stay to the current room itself */
System.out.println("No direction");
return;
}
//If route exist
else
{
//Store the currentRoom
Room tt=currentRoom;
//Go to the previous room
currentRoom=route.pop();
//Store the previous current room
route.push(tt);
//Get currentRoom description System.out.println(currentRoom.getLongDescription());
}
}
/**
* "Quit" was entered. Check the rest of the command to see
* whether we really quit the game. Return true, if this command
* quits the game, false otherwise.
*/
private boolean quit(Command command)
{
if(command.hasSecondWord())
{
System.out.println("Quit what?");
return false;
}
else
return true; // signal that we want to quit
}
}
Solution
Modified Code: Game.java:
-import java.util.*;
public class Game
{
private Parser parser;
private Room currentRoom;
private Stack route;
/**
* Create the game and initialize its internal map.
*/
public Game()
{
createRooms();
parser = new Parser();
route=new Stack();
}
/**
* Create all the rooms and link their exits together.
*/
private void createRooms()
{
Room outside, theatre, pub, lab, office, basement;
// create the rooms
outside = new Room("outside the main entrance of the university");
theatre = new Room("in a lecture theatre");
pub = new Room("in the campus pub");
lab = new Room("in a computing lab");
office = new Room("in the computing admin office");
basement = new Room("in the basement");
// initialise room exits
outside.setExit("east", theatre);
outside.setExit("south", lab);
outside.setExit("west", pub);
theatre.setExit("west", outside);
pub.setExit("east", outside);
lab.setExit("north", outside);
lab.setExit("east", office);
lab.setExit("down", basement);
basement.setExit("up", lab);
office.setExit("west", lab);
currentRoom = outside; // start game outside
}
/**
* Main play routine. Loops until end of play.
*/
public void play()
{
printWelcome();
// Enter the main command loop. Here we repeatedly read commands and
// execute them until the game is over.
boolean finished = false;
while (! finished)
{
Command command = parser.getCommand();
finished = processCommand(command);
}
System.out.println("Thank you for playing. Good bye.");
}
/**
* Print out the opening message for the player.
*/
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());
}
/**
* Given a command, process (that is: execute) the command.
* If this command ends the game, true is returned, otherwise false is
* returned.
*/
private boolean processCommand(Command command)
{
boolean wantToQuit = false;
if(command.isUnknown())
{
System.out.println("I don't know what you mean...");
return false;
}
String commandWord = command.getCommandWord();
if (commandWord.equals("help")){
printHelp();
}
else if (commandWord.equals("go")) {
goRoom(command);
}
else if (commandWord.equals("back")) {
goBack();
}
else if (commandWord.equals("quit")) {
wantToQuit = quit(command);
}
return wantToQuit;
}
// implementations of user commands:
/**
* Print out some help information.
* Here we print some stupid, cryptic message and a list of the
* command words.
*/
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();
}
/**
* Try to go to one direction. If there is an exit, enter the new
* room, otherwise print an error message.
*/
private void goRoom(Command command)
{
if(!command.hasSecondWord())
{
// if there is no second word, we don't know where to go...
System.out.println("Go where?");
return;
}
String direction = command.getSecondWord();
// Try to leave current room.
Room nextRoom = currentRoom.getExit(direction);
if (nextRoom == null)
System.out.println("There is no door!");
else
{
route.push(currentRoom);
currentRoom = nextRoom;
System.out.println(currentRoom.getLongDescription());
}
}
//Try to go to the previous room
private void goBack()
{
//Check if route exist
if(route.isEmpty())
{
/*If route doesnot exist. Stay to the current room itself */
System.out.println("No direction");
return;
}
//If route exist
else
{
//Store the currentRoom
Room tt=currentRoom;
//Go to the previous room
currentRoom=route.pop();
//Store the previous current room
route.push(tt);
//Get currentRoom description System.out.println(currentRoom.getLongDescription());
}
}
/**
* "Quit" was entered. Check the rest of the command to see
* whether we really quit the game. Return true, if this command
* quits the game, false otherwise.
*/
private boolean quit(Command command)
{
if(command.hasSecondWord())
{
System.out.println("Quit what?");
return false;
}
else
return true; // signal that we want to quit
}
}

More Related Content

PDF
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
PDF
mainpublic class AssignmentThree {    public static void ma.pdf
DOCX
Example of JAVA Program
PDF
Chapter06 designing class-zuul bad
PDF
TASK #1In the domain class you will create a loop that will prompt.pdf
PDF
Simple 27 Java Program on basic java syntax
PDF
public interface Game Note interface in place of class { .pdf
PDF
Please read the comment ins codeExpressionTree.java-------------.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
Example of JAVA Program
Chapter06 designing class-zuul bad
TASK #1In the domain class you will create a loop that will prompt.pdf
Simple 27 Java Program on basic java syntax
public interface Game Note interface in place of class { .pdf
Please read the comment ins codeExpressionTree.java-------------.pdf

Similar to Modified Code Game.java­import java.util.;public class Game.pdf (16)

PDF
Simple Java Program for beginner with easy method.pdf
PDF
Java Programpublic class Fraction {   instance variablesin.pdf
PDF
Workshop 10: ECMAScript 6
PDF
Node.js API pitfalls
PDF
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
PDF
Here is the code for youimport java.util.Scanner; import java.u.pdf
PDF
4. Обработка ошибок, исключения, отладка
PDF
PPTX
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
PDF
Please observe the the code and validations of user inputs.import .pdf
PDF
Security Meetup 22 октября. «PHP Unserialize Exploiting». Павел Топорков. Лаб...
PDF
Security Meetup 22 октября. «Реверс-инжиниринг в Enterprise». Алексей Секрето...
PPTX
Add an interactive command line to your C++ application
PPT
Whats new in_csharp4
PDF
The following is my code for a connectn program. When I run my code .pdf
PDF
Rootkit on Linux X86 v2.6
Simple Java Program for beginner with easy method.pdf
Java Programpublic class Fraction {   instance variablesin.pdf
Workshop 10: ECMAScript 6
Node.js API pitfalls
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdf
4. Обработка ошибок, исключения, отладка
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
Please observe the the code and validations of user inputs.import .pdf
Security Meetup 22 октября. «PHP Unserialize Exploiting». Павел Топорков. Лаб...
Security Meetup 22 октября. «Реверс-инжиниринг в Enterprise». Алексей Секрето...
Add an interactive command line to your C++ application
Whats new in_csharp4
The following is my code for a connectn program. When I run my code .pdf
Rootkit on Linux X86 v2.6
Ad

More from galagirishp (20)

PDF
alcohols also do not give tollens test.... only a.pdf
PDF
Acid Buffer - Weak acid + plus its salt Basic B.pdf
PDF
Total vapor pressure of mixture = P(benzene) + P(.pdf
PDF
some data is missin .pdf
PDF
reaction is missing .pdf
PDF
Not necessarily. It could very well mean that you.pdf
PDF
For Li, B, and F they only formed single bond bet.pdf
PDF
Enzyme Induction- the synthesis of enzymes in res.pdf
PDF
Buffer Capacity is the ranges of volume of acid o.pdf
PDF
a. CH4 has no free valence electrons so it can no.pdf
PDF
We are living in a world where cyber security is a top priority for .pdf
PDF
highest bond energy molecule is CO because its bond order is = 3.pdf
PDF
Urine is concentrated byc) facilitated diffusion in the collecting d.pdf
PDF
The two fund theorem is the establishment of two efficient Funds.pdf
PDF
the nuclide is147N which is nitrogenSolutionthe nuclide .pdf
PDF
The Programmer has created multiple entry point which can be used to.pdf
PDF
The halide is iodine as it gives pale yellow precipitate with AgNO3 .pdf
PDF
The answer isd. The pi electrons are distributed over the entire .pdf
PDF
(a)Solution(a).pdf
PDF
import java.awt.FlowLayout;import java.awt.event.KeyEvent;import.pdf
alcohols also do not give tollens test.... only a.pdf
Acid Buffer - Weak acid + plus its salt Basic B.pdf
Total vapor pressure of mixture = P(benzene) + P(.pdf
some data is missin .pdf
reaction is missing .pdf
Not necessarily. It could very well mean that you.pdf
For Li, B, and F they only formed single bond bet.pdf
Enzyme Induction- the synthesis of enzymes in res.pdf
Buffer Capacity is the ranges of volume of acid o.pdf
a. CH4 has no free valence electrons so it can no.pdf
We are living in a world where cyber security is a top priority for .pdf
highest bond energy molecule is CO because its bond order is = 3.pdf
Urine is concentrated byc) facilitated diffusion in the collecting d.pdf
The two fund theorem is the establishment of two efficient Funds.pdf
the nuclide is147N which is nitrogenSolutionthe nuclide .pdf
The Programmer has created multiple entry point which can be used to.pdf
The halide is iodine as it gives pale yellow precipitate with AgNO3 .pdf
The answer isd. The pi electrons are distributed over the entire .pdf
(a)Solution(a).pdf
import java.awt.FlowLayout;import java.awt.event.KeyEvent;import.pdf
Ad

Recently uploaded (20)

PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Classroom Observation Tools for Teachers
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Cell Structure & Organelles in detailed.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
2.FourierTransform-ShortQuestionswithAnswers.pdf
Classroom Observation Tools for Teachers
Chinmaya Tiranga quiz Grand Finale.pdf
A systematic review of self-coping strategies used by university students to ...
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
FourierSeries-QuestionsWithAnswers(Part-A).pdf
human mycosis Human fungal infections are called human mycosis..pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Final Presentation General Medicine 03-08-2024.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Microbial disease of the cardiovascular and lymphatic systems
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Complications of Minimal Access Surgery at WLH
O7-L3 Supply Chain Operations - ICLT Program
VCE English Exam - Section C Student Revision Booklet
Cell Structure & Organelles in detailed.
Microbial diseases, their pathogenesis and prophylaxis
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

Modified Code Game.java­import java.util.;public class Game.pdf

  • 1. Modified Code: Game.java: -import java.util.*; public class Game { private Parser parser; private Room currentRoom; private Stack route; /** * Create the game and initialize its internal map. */ public Game() { createRooms(); parser = new Parser(); route=new Stack(); } /** * Create all the rooms and link their exits together. */ private void createRooms() { Room outside, theatre, pub, lab, office, basement; // create the rooms outside = new Room("outside the main entrance of the university"); theatre = new Room("in a lecture theatre"); pub = new Room("in the campus pub"); lab = new Room("in a computing lab"); office = new Room("in the computing admin office"); basement = new Room("in the basement"); // initialise room exits outside.setExit("east", theatre); outside.setExit("south", lab); outside.setExit("west", pub);
  • 2. theatre.setExit("west", outside); pub.setExit("east", outside); lab.setExit("north", outside); lab.setExit("east", office); lab.setExit("down", basement); basement.setExit("up", lab); office.setExit("west", lab); currentRoom = outside; // start game outside } /** * Main play routine. Loops until end of play. */ public void play() { printWelcome(); // Enter the main command loop. Here we repeatedly read commands and // execute them until the game is over. boolean finished = false; while (! finished) { Command command = parser.getCommand(); finished = processCommand(command); } System.out.println("Thank you for playing. Good bye."); } /** * Print out the opening message for the player. */ 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.");
  • 3. System.out.println("Type 'help' if you need help."); System.out.println(); System.out.println(currentRoom.getLongDescription()); } /** * Given a command, process (that is: execute) the command. * If this command ends the game, true is returned, otherwise false is * returned. */ private boolean processCommand(Command command) { boolean wantToQuit = false; if(command.isUnknown()) { System.out.println("I don't know what you mean..."); return false; } String commandWord = command.getCommandWord(); if (commandWord.equals("help")){ printHelp(); } else if (commandWord.equals("go")) { goRoom(command); } else if (commandWord.equals("back")) { goBack(); } else if (commandWord.equals("quit")) { wantToQuit = quit(command); } return wantToQuit; } // implementations of user commands: /** * Print out some help information. * Here we print some stupid, cryptic message and a list of the
  • 4. * command words. */ 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(); } /** * Try to go to one direction. If there is an exit, enter the new * room, otherwise print an error message. */ private void goRoom(Command command) { if(!command.hasSecondWord()) { // if there is no second word, we don't know where to go... System.out.println("Go where?"); return; } String direction = command.getSecondWord(); // Try to leave current room. Room nextRoom = currentRoom.getExit(direction); if (nextRoom == null) System.out.println("There is no door!"); else { route.push(currentRoom); currentRoom = nextRoom; System.out.println(currentRoom.getLongDescription()); } } //Try to go to the previous room
  • 5. private void goBack() { //Check if route exist if(route.isEmpty()) { /*If route doesnot exist. Stay to the current room itself */ System.out.println("No direction"); return; } //If route exist else { //Store the currentRoom Room tt=currentRoom; //Go to the previous room currentRoom=route.pop(); //Store the previous current room route.push(tt); //Get currentRoom description System.out.println(currentRoom.getLongDescription()); } } /** * "Quit" was entered. Check the rest of the command to see * whether we really quit the game. Return true, if this command * quits the game, false otherwise. */ private boolean quit(Command command) { if(command.hasSecondWord()) { System.out.println("Quit what?"); return false; } else return true; // signal that we want to quit }
  • 6. } Solution Modified Code: Game.java: -import java.util.*; public class Game { private Parser parser; private Room currentRoom; private Stack route; /** * Create the game and initialize its internal map. */ public Game() { createRooms(); parser = new Parser(); route=new Stack(); } /** * Create all the rooms and link their exits together. */ private void createRooms() { Room outside, theatre, pub, lab, office, basement; // create the rooms outside = new Room("outside the main entrance of the university"); theatre = new Room("in a lecture theatre"); pub = new Room("in the campus pub"); lab = new Room("in a computing lab"); office = new Room("in the computing admin office"); basement = new Room("in the basement"); // initialise room exits
  • 7. outside.setExit("east", theatre); outside.setExit("south", lab); outside.setExit("west", pub); theatre.setExit("west", outside); pub.setExit("east", outside); lab.setExit("north", outside); lab.setExit("east", office); lab.setExit("down", basement); basement.setExit("up", lab); office.setExit("west", lab); currentRoom = outside; // start game outside } /** * Main play routine. Loops until end of play. */ public void play() { printWelcome(); // Enter the main command loop. Here we repeatedly read commands and // execute them until the game is over. boolean finished = false; while (! finished) { Command command = parser.getCommand(); finished = processCommand(command); } System.out.println("Thank you for playing. Good bye."); } /** * Print out the opening message for the player. */ private void printWelcome() {
  • 8. 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()); } /** * Given a command, process (that is: execute) the command. * If this command ends the game, true is returned, otherwise false is * returned. */ private boolean processCommand(Command command) { boolean wantToQuit = false; if(command.isUnknown()) { System.out.println("I don't know what you mean..."); return false; } String commandWord = command.getCommandWord(); if (commandWord.equals("help")){ printHelp(); } else if (commandWord.equals("go")) { goRoom(command); } else if (commandWord.equals("back")) { goBack(); } else if (commandWord.equals("quit")) { wantToQuit = quit(command); } return wantToQuit; } // implementations of user commands:
  • 9. /** * Print out some help information. * Here we print some stupid, cryptic message and a list of the * command words. */ 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(); } /** * Try to go to one direction. If there is an exit, enter the new * room, otherwise print an error message. */ private void goRoom(Command command) { if(!command.hasSecondWord()) { // if there is no second word, we don't know where to go... System.out.println("Go where?"); return; } String direction = command.getSecondWord(); // Try to leave current room. Room nextRoom = currentRoom.getExit(direction); if (nextRoom == null) System.out.println("There is no door!"); else { route.push(currentRoom); currentRoom = nextRoom; System.out.println(currentRoom.getLongDescription());
  • 10. } } //Try to go to the previous room private void goBack() { //Check if route exist if(route.isEmpty()) { /*If route doesnot exist. Stay to the current room itself */ System.out.println("No direction"); return; } //If route exist else { //Store the currentRoom Room tt=currentRoom; //Go to the previous room currentRoom=route.pop(); //Store the previous current room route.push(tt); //Get currentRoom description System.out.println(currentRoom.getLongDescription()); } } /** * "Quit" was entered. Check the rest of the command to see * whether we really quit the game. Return true, if this command * quits the game, false otherwise. */ private boolean quit(Command command) { if(command.hasSecondWord()) { System.out.println("Quit what?"); return false; }
  • 11. else return true; // signal that we want to quit } }