From f218bf5fa6f47b00800d068097b3b75c86fa1e39 Mon Sep 17 00:00:00 2001 From: Benoît LUBRANO DI SBARAGLIONE Date: Tue, 11 Mar 2014 09:47:27 +0100 Subject: Add back command --- src/esieequest/controller/Interpreter.java | 3 +++ src/esieequest/controller/Performer.java | 8 ++++++++ src/esieequest/model/Game.java | 21 ++++++++++++++++----- 3 files changed, 27 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/esieequest/controller/Interpreter.java b/src/esieequest/controller/Interpreter.java index 5c60c0b..586b329 100644 --- a/src/esieequest/controller/Interpreter.java +++ b/src/esieequest/controller/Interpreter.java @@ -63,6 +63,9 @@ class Interpreter { case "go": this.performer.goTo(command.getOption()); return; + case "back": + this.performer.goBack(); + return; case "look": this.performer.look(); return; diff --git a/src/esieequest/controller/Performer.java b/src/esieequest/controller/Performer.java index 6da3901..0ac194f 100644 --- a/src/esieequest/controller/Performer.java +++ b/src/esieequest/controller/Performer.java @@ -108,6 +108,14 @@ class Performer { } } + /** + * Changes the current room to the previous one. + */ + public void goBack() { + this.game.goToPreviousRoom(); + this.echo(this.game.getLocationInfo()); + } + /** * Displays informations about the current place. */ diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java index 99afa7c..5f2049c 100644 --- a/src/esieequest/model/Game.java +++ b/src/esieequest/model/Game.java @@ -1,6 +1,7 @@ package esieequest.model; import java.util.HashMap; +import java.util.Stack; /** * Represents the game. @@ -12,6 +13,7 @@ public class Game { private final HashMap rooms; private Room currentRoom; + private Stack previousRooms; /** * The default constructor. @@ -19,6 +21,7 @@ public class Game { public Game() { this.rooms = new HashMap(); this.currentRoom = null; + this.previousRooms = new Stack(); this.createRooms(); this.linkRooms(); @@ -48,8 +51,7 @@ public class Game { * @param exitRoomName * the name of the exit room */ - private void setRoomExit(final String roomName, final String direction, - final String exitRoomName) { + private void setRoomExit(final String roomName, final String direction, final String exitRoomName) { this.rooms.get(roomName).addExit(direction, this.rooms.get(exitRoomName)); } @@ -98,8 +100,7 @@ public class Game { this.createRoom("OffscriptLock", "somewhere implementing a doorlock"); this.createRoom("OffscriptLockLockedroom", "in a locked room that is not anymore"); this.createRoom("OffscriptAlea", "somewhere implementing alea"); - this.createRoom("OffscriptAleaRoomrandomizer", - "in a weird room that will transport you somewhere else"); + this.createRoom("OffscriptAleaRoomrandomizer", "in a weird room that will transport you somewhere else"); this.createRoom("OffscriptMovingcharacter", "somewhere implementing a moving character"); this.createRoom("OffscriptMovingcharacterMo", "in M-O's room"); @@ -199,12 +200,13 @@ public class Game { } /** - * Sets the current room. + * Sets the current room and stacks previous rooms. * * @param room * the destination room */ public void goToRoom(final Room room) { + this.previousRooms.push(this.currentRoom); this.currentRoom = room; } @@ -218,6 +220,15 @@ public class Game { this.currentRoom = this.rooms.get(roomName); } + /** + * Sets the current room to the previous room. + */ + public void goToPreviousRoom() { + if (!this.previousRooms.empty()) { + this.currentRoom = this.previousRooms.pop(); + } + } + /** * Returns the current room's exit located in the given direction. * -- cgit v1.2.3