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(-) 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 From 07c3d56b24c9693ec060399823dd233184c054a3 Mon Sep 17 00:00:00 2001 From: Benoît LUBRANO DI SBARAGLIONE Date: Wed, 12 Mar 2014 10:51:36 +0100 Subject: Update zuul with history report. --- report/progression.tex | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/report/progression.tex b/report/progression.tex index 6600430..cf4f3d2 100644 --- a/report/progression.tex +++ b/report/progression.tex @@ -204,12 +204,27 @@ to perform actions on a particular one (take, drop, use, etc\ldots). \subsection{back} +The back command was implemented as part of commit f218bf5fa. + \subsection{back test} +The back command works even with any parameter. We will check this later with +"zuul-with-enums". + \subsection{back back} +When the user enters back twice, he goes two rooms backward. This is working +correctly. + \subsection{Stack} +The Stack class represents a last-in-first-out (LIFO) stack of objects. The push() +method allows to add and element at the top of the stack and the pop() method +allows to retrieve the top item and removes it. + +A condition checks whether the Stack is empty before calling the pop() method, +so that the program does not throw an Exception. This resolve the case when the +player is at the starting point. \section{Zuul with tests} -- cgit v1.2.3