Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you please explain in detail what is happening in the code below, I just want to know how this work. import java.io.BufferedReader; import java.io.IOException;

Can you please explain in detail what is happening in the code below, I just want to know how this work.

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Wonderland {

static Actions actions;

public static void main(String[] args) throws IOException{

BufferedReader in;

String input;

String output = "";

actions = new Actions();

in = new BufferedReader(new InputStreamReader(System.in));

actions.showIntro();

do {

System.out.print("> ");

input = in.readLine();

output = actions.runCommand(input);

System.out.println(output);

} while (!"q".equals(input));

}

}

import java.util.*;

public class Actions {

private ArrayList map;

private Player player;

public Actions() {

this.map = new ArrayList();

ItemList gardenList = new ItemList();

gardenList.add(new Item("key", "What a shiny Key, it must be useful", true, false, false));

gardenList.add(new Item("hat", "It's the Crazy Mad Hatters Hat, not much value, but I like it!", true, false, false));

gardenList.add(new Item("mushroom", "I probably shouldn't experiment", true, false, false));

ItemList safeRoomList = new ItemList();safeRoomList.add(new Item("","", false , false, false));

ItemList courtRoomList = new ItemList();courtRoomList.add(new Item("chromosphere", "This looks ancient!", true, true, false));

courtRoomList.add(new Item("sword", "With this I can protect myself!", true, false, false));

ItemList croquetList = new ItemList();croquetList.add(new Item("card", "The Queens Favourite Card, I could use itfor leverage.", true, false, false));

croquetList.add(new Item("oraculum", "This map seems to have ink thatmoves, showing some strange scenes."

, true, false, false));

ItemList duchessList = new ItemList();

duchessList.add(new Item("grinder", "A pepper grinder, never know when I'll need a perfect sandwich, this pepper might be yummy."

, true, false, false));

duchessList.add(new Item("cup", "A Fancy China Cup."

, true, false, false));

ItemList rabbitHouseList = new ItemList();

rabbitHouseList.add(new Item("watch", "An expensive pocket watch that the Rabbit forgot, might need to track the time later."

, true, false, false));

ItemList rabbitHoleList = new ItemList();

rabbitHoleList.add(new Item("bottle", "A bottle with DrinkMeBottle written on it. Should I drink this?",true, false, true));

rabbitHoleList.add(new Item("cake", "A cake with EatMeCake written on it. Should I eat this?",

true, false, true));

ItemList playerlist = new ItemList();

map.add(new Locations("in the Rabbit Hole","This is where the Rabbit went. I can't believe I fit through the hole.","Door Knob","Welcome to Wonderland, to leave you must find the Chromosphe relocated in one of the rooms. Activate it and find your way back home.But first you must get through me, choose wisely between the bottle and the cake!",Directions.NOEXIT, Directions.NOEXIT, Directions.NOEXIT,Directions.NOEXIT, 1,rabbitHoleList));

map.add(new Locations("at the Rabbit House", "A mushroom Shaped House withthat talking Rabbit from before.","White Rabbit", "I'm late, I'm Late. The Queen will have my head!You there strange creature, get out of my house!!! I'm Late, I'm Late.",

Directions.NOEXIT, Directions.NOEXIT, Directions.NOEXIT, 2,Directions.NOEXIT, rabbitHouseList));

map.add(new Locations("in the Garden", "A beautiful smelling garden.", "Mad Hatter", "Oh, what a delightful child! " +"This is an unbirthday party!",

Directions.NOEXIT, Directions.NOEXIT, 1, 3, Directions.NOEXIT,gardenList));

map.add(new Locations("at the Duchess House", "A House with Black pepper in the air, I hear sneezing.","Duchess","Achoo Oh Hello there, And the moral of that is"'Be what you would seem to be"or, if you'd like it put more simply"Never imagine yourself not to be otherwise than what it might appear to others that what you were or might have been was not otherwise than what you had been would have appeared to them to be otherwise.'",4, 5, 2, 6, Directions.NOEXIT, duchessList));

map.add(new Locations("in the Court Room", "What a glamorous Cour Room, That must be the King on the throne.",

"Red King", "I don't like the look of it at all, said the King: "+"however, it may kiss my hand, if it likes. ",Directions.NOEXIT, 3,Directions.NOEXIT, Directions.NOEXIT,Directions.NOEXIT, courtRoomList));

map.add(new Locations("in the Safe Room", "A safe room where you won't be found.","Whisper","Come home!",3, Directions.NOEXIT,Directions.NOEXIT, Directions.NOEXIT,Directions.NOEXIT,safeRoomList));

map.add(new Locations("at the Croquet PlayGround", "A playground for therich. The Queen seems a bit stuck up.","Queen of Hears", "Where is my favourite card? ...Now, where do you come from?I warn you, child... if I lose my temper, you lose your head! What do you know about this unfortunate affair regarding my card?",Directions.NOEXIT, Directions.NOEXIT, 3, Directions.NOEXIT,Directions.NOEXIT, croquetList));

player = new Player("player", "a loveable game-player", playerlist,

map.get(0));

}

private Player getPlayer() {

return player;

}

private void transferOb(Item t, ItemList fromlist, ItemList tolist) {

fromlist.remove(t);

tolist.add(t);

}

String takeOb(String obname) {

String retStr = "";

Item t = player.getLocation().getItem().thisOb(obname);

if (obname.equals("")) {

obname = "nameless object"; // if no object specified

}

if (t == null) {

retStr = "There is no " + obname + " here!";

} else {

transferOb(t, player.getLocation().getItem(), player.getItem());

retStr = obname + " taken!";

}

return retStr;

}

String dropOb(String obname) {

String retStr = "";

Item t = player.getItem().thisOb(obname);

if (obname.equals("")) {

retStr = "You'll have to tell me which object you want to drop!"; // if no object specified

} else if (t == null) {

retStr = "You haven't got one of those!";

} else {

transferOb(t, player.getItem(), player.getLocation().getItem());

retStr = obname + " dropped!";

}

return retStr;

}

// move a Player to a location.

private void moveActorTo(Player p, Locations aRoom) {

p.setLocation(aRoom);}

// move the player in direction

private int moveTo(Player anPlayer, Directions directions) {

Locations r = anPlayer.getLocation();

int exit;

switch (directions) {

case NORTH:

exit = r.getN();

break;

case SOUTH:

exit = r.getS();

break;

case EAST:

exit = r.getE();

break;

case WEST:

exit = r.getW();

break;

case DOWN:

exit = r.getDown();

break;

default:

exit = Directions.NOEXIT; // noexit - stay in same room

break;

}

if (exit != Directions.NOEXIT) {

moveActorTo(anPlayer, map.get(exit));

}

return exit;

}

void movePlayerTo(Directions directions) {

// if roomNumber = NOEXIT, display a special message, otherwise

// display text (e.g. name and description of room)

if (moveTo(player, directions) == Directions.NOEXIT) {

showStr("No Exit!");

} else {

look();

}

}

void goN() {

movePlayerTo(Directions.NORTH);

}

void goS() {

movePlayerTo(Directions.SOUTH);

}

void goW() {

movePlayerTo(Directions.WEST);

}

void goE() {

movePlayerTo(Directions.EAST);

}

void goD(){

movePlayerTo(Directions.DOWN);

}

String consumeItem(String obname) {

String retStr = "";

Item t = player.getItem().thisOb(obname);

if (obname.equals("")) {

retStr = "You'll have to tell me which object you want to consume!"; //if no object specified

} else if (t == null) {

retStr = "You haven't got one of those!";

} else if (t.isConsumable()) {

System.out.println("You consume " + obname);

System.out.println("Your Start to feel strange, the world around you is spinning, " +

"and your find yourself in a new place. ");

transferOb(t,player.getItem(), player.getLocation().getItem());

movePlayerTo(Directions.DOWN);

}

else {

System.out.println("You cannot consume that!");

}

return retStr;

}

String activateItem(String obname) {

String retStr = "";

Item t = player.getItem().thisOb(obname);

if (obname.equals("")) {

retStr = "You'll have to tell me which object you want to activate!"; // if no object specified

} else if (t == null) {

retStr = "You haven't got one of those!";

} else if (t.isActive()) {

System.out.println("You Activate " + obname);

System.out.println("Lights seem to spin, and you feel disoriented. You wake up in the park, back in your world. Good Bye! ");

System.exit(0);

}

else {

System.out.println("You cannot consume that!");

}

return retStr;

}

void look() {

showStr(" You are " + getPlayer().getLocation().describe());

}

void showStr(String s) {

System.out.println(s);

}

void showInventory() {

showStr("You have: " + getPlayer().getItem().describeThings());

}

void showMap() {

showStr("Map: \tDuchess House: Central. \tCourt Room: North \tCroquet Playground: East \tSafe Room: South \tGarden: West \tRabbit's House: West");

}

private String parseCommand(List wordlist) {

String msg;

if (wordlist.size() == 1) {

msg = Control.processAction(wordlist);

} else if (wordlist.size() == 2) {

msg = Control.processActionItem(wordlist);

} else {

msg = "Only 2word commands allowed!";

}

return msg;

}

private static List wordList(String input) {

String delims = "[ \t,.:;?!\"']+";

List strlist = new ArrayList<>();

String[] words = input.split(delims);

for (String word : words) {

strlist.add(word);

}

return strlist;

}

public void showIntro() {

String s;

s = " Welcome to the Game of Alice in Wonderland. Instructions: [Enter n,s, w or e] to move, "

+ "(or enter q to quit)."+ " You can also interact with objects by typing take, drop, activate, and eat."

+

" ---------------------------------------------------------------------"

+ " You wake up in a daze and see a White Rabbit rush past, saying 'I'm late, I'm Late' "

+ "He jumps straight into a Rabbit Hole "

+ "and you follow without thinking. ";

showStr(s);

look();

}

public String runCommand(String inputstr) {

List wordlist;

String s = "ok";

String lowstr = inputstr.trim().toLowerCase();

if (!lowstr.equals("q")) {

if (lowstr.equals("")) {

s = "You must enter a command";

} else {

wordlist = wordList(lowstr);

s = parseCommand(wordlist);

}

}

return s;

}}

import java.util.Arrays;

import java.util.List;

import java.util.*;

public class Control {

static List actions = new ArrayList<>(Arrays.asList(

"take", "drop", "look",

"n", "s", "w", "e","i","m","eat","drink","consume","activate","drop","map","l"));

static String processActionItem(List wordlist) {

String verb;

String noun;

String msg = "";

boolean error = false;

verb = wordlist.get(0);

noun = wordlist.get(1);

if (!actions.contains(verb)) {

msg = verb + " is not a known verb! ";

error = true;

}

if (!error) {

switch (verb) {

case "take":

msg = Wonderland.actions.takeOb(noun);

break;

case "drop":

msg = Wonderland.actions.dropOb(noun);

break;

case "eat":

case "drink":

case "consume":

msg = Wonderland.actions.consumeItem(noun);

case "activate":

msg = Wonderland.actions.activateItem(noun);

default:

msg += "";

break;

}

}

return msg;

}

static String processAction(List wordlist) {

String verb;

String msg = "";

verb = wordlist.get(0);

if (!actions.contains(verb)) {

msg = verb + " is not a known verb! ";

} else {

switch (verb) {

case "n":

case "north":

Wonderland.actions.goN();

break;

case "s":

case "south":

Wonderland.actions.goS();

break;

case "w":

case "west":

Wonderland.actions.goW();

break;

case "e":

case "east":

Wonderland.actions.goE();

break;

case "l":

case "look":

Wonderland.actions.look();

break;

case "inventory":

case "i":

Wonderland.actions.showInventory();

break;

case "m":

case "map":

Wonderland.actions.showMap();

default:

break;

}

}

return msg;

}}

publicclassInventoryextendsItem {

privateItemList items = new ItemList();

publicInventory(String aName, String aDescription, ItemList tl) {

super(aName, aDescription);

items = tl;

}

publicInventory(String aName, String aDescription,booleancanTake,

booleancanConsume,booleancanActive, ItemList tl) {

super(aName, aDescription, canTake, canActive, canConsume);

items = tl;

}

publicItemList getItem() {

return items;

}

publicvoidsetItem(ItemList items) {

this.items = items;

}

}

publicclassItem {

// Basic Thing type that defines all objects in the Adventure

privateString name;

privateString description;

privatebooleantakable;

privatebooleanactive;

privatebooleanconsumable;

publicItem(String aName, String aDescription) {

// constructor

this.name = aName;

this.description = aDescription;

}

publicItem(String aName, String aDescription,booleancanTake,boolean

canActive,booleancanConsume) {

// constructor

this.name = aName;

this.description = aDescription;

this.takable = canTake;

this.active = canActive;

this.consumable = canConsume;

}

publicString getName() {

return name;

}

publicvoidsetName(String name) {

this.name = name;

}

publicString getDescription() {

return description;

}

publicvoidsetDescription(String description) {

this.description = description;

}

publicbooleanisTakable() {

return takable;

}

publicvoidsetTakable(booleantakable) {

this.takable = takable;

}

publicbooleanisActive() {

return active;

}

publicvoidsetActive(booleanactive) {

this.active = active;

}

publicbooleanisConsumable() {

return consumable;

}

publicvoidsetConsumable(booleanconsumable) {

this.consumable = consumable;

}

publicString describe(){

return name + " " + description;

}

publicstaticbooleancontains(String verb) {

return false;

}

}

import java.util.ArrayList;

publicclassItemListextendsArrayList {

publicString describeThings() {

String s = "";

if (this.size() == 0) {

s = "";

} else {

for (Item t : this) {

s = s + t.getName() + ": " + t.getDescription() + " ";

}

}

return s;

}

publicItem thisOb(String aName) {

Item athing = null;

String thingName = "";

String aNameLowCase = aName.trim().toLowerCase();

for (Item t : this) {

thingName = t.getName().trim().toLowerCase();

if (thingName.equals(aNameLowCase)) {

athing = t;

}

}

return athing;

}

}

publicclassLocationsextendsInventory {

privateintn, s, w, e, down;

privateString charDescription;

privateString charName;

publicLocations(String aName, String aDescription, String cName, String aCDescription,intaN,intaS,intaW,intaE,intaDown,ItemList tl) {

super(aName, aDescription, tl);

this.charDescription = aCDescription;

this.charName = cName;

this.n = aN;

this.s = aS;

this.w = aW;

this.e = aE;

this.down = aDown;

}

publicintgetN() {

return n;

}

publicvoidsetN(intn) {

this.n = n;

}

publicintgetS() {

return s;

}

publicvoidsetS(ints) {

this.s = s;

}

publicintgetW() {

return w;

}

publicvoidsetW(intw) {

this.w = w;

}

publicintgetE() {

return e;

}

publicvoidsetE(inte) {

this.e = e;

}

publicintgetDown() {

return down;

}

publicvoidsetDown(intdown) {

this.down = down;

}

publicString getCharName(){return charName;}

publicString getCharDescription() {

return charDescription;

}

publicString describe() {

String roomdesc;

String thingsdesc;

roomdesc = String.format("%s. %s %s: \"%s.\"", getName(),

getDescription(), getCharName(), getCharDescription());

thingsdesc = getItem().describeThings();

if (!thingsdesc.isEmpty()) {

roomdesc += " >>>><<<<"+" Items here: " + thingsdesc;

}

return roomdesc;

}

}

publicclassPlayerextendsInventory{

privateLocations location;// the Room where the Person is at present

publicPlayer(String aName, String aDescription, ItemList tl, Locations aRoom)

{

super(aName, aDescription, tl);// init super class

this.location = aRoom;

}

publicvoidsetLocation(Locations aRoom) {

this.location = aRoom;

}

publicLocations getLocation() {

return this.location;

}

}

publicenumDirections {

NORTH,

SOUTH,

EAST,

WEST,

DOWN,;

publicstaticfinalintNOEXIT = -1;

}

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

Cornerstones Of Cost Management

Authors: Don R. Hansen, Maryanne M. Mowen

3rd Edition

9781305147102, 1285751787, 1305147103, 978-1285751788

More Books

Students also viewed these Programming questions