From 92f671b84b9c2a2c2319aac2a52e6dffa8bc2195 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Wed, 2 Apr 2014 10:50:28 +0200 Subject: Move some attributes to the Player class --- src/esieequest/controller/Performer.java | 12 +++--- src/esieequest/model/Game.java | 60 +++++++---------------------- src/esieequest/model/Player.java | 39 +++++++++++++++++++ src/esieequest/view/text/Console.java | 4 +- src/esieequest/view/text/FileReader.java | 4 +- src/esieequest/view/text/TextInterface.java | 27 ++++++++++++- 6 files changed, 87 insertions(+), 59 deletions(-) diff --git a/src/esieequest/controller/Performer.java b/src/esieequest/controller/Performer.java index 27cbf14..96a95df 100644 --- a/src/esieequest/controller/Performer.java +++ b/src/esieequest/controller/Performer.java @@ -99,10 +99,10 @@ class Performer { * the direction of the new room */ public void goTo(final String direction) { - final Room nextRoom = this.game.getRoomExit(direction); + final Room nextRoom = this.game.getPlayer().getCurrentRoom().getExit(direction); if (nextRoom != null) { - this.game.goToRoom(nextRoom); - this.view.updateRoom(this.game.getCurrentRoom()); + this.game.getPlayer().goToRoom(nextRoom); + this.view.updateRoom(this.game.getPlayer().getCurrentRoom()); } else { this.echo(this.game.getNoExitMessage()); } @@ -112,15 +112,15 @@ class Performer { * Changes the current room to the previous one. */ public void goBack() { - this.game.goToPreviousRoom(); - this.view.updateRoom(this.game.getCurrentRoom()); + this.game.getPlayer().goToPreviousRoom(); + this.view.updateRoom(this.game.getPlayer().getCurrentRoom()); } /** * Displays informations about the current place. */ public void look() { - this.echo(this.game.getCurrentRoom().getInformations()); + this.echo(this.game.getPlayer().getCurrentRoom().getInformations()); } /** diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java index 62dfc94..0a9aa01 100644 --- a/src/esieequest/model/Game.java +++ b/src/esieequest/model/Game.java @@ -1,7 +1,6 @@ package esieequest.model; import java.util.HashMap; -import java.util.Stack; /** * Represents the game. @@ -12,17 +11,16 @@ import java.util.Stack; public class Game { private final HashMap rooms; - private Room currentRoom; - private final Stack previousRooms; + private final Player player; /** * The default constructor. */ public Game() { this.rooms = new HashMap(); - this.currentRoom = null; - this.previousRooms = new Stack(); + + this.player = new Player(); this.createRooms(); this.linkRooms(); @@ -30,6 +28,15 @@ public class Game { this.goToRoom("AmphitheaterSeat"); } + /** + * Returns the player + * + * @return the player + */ + public Player getPlayer() { + return this.player; + } + /** * Creates a new room. * @@ -184,17 +191,6 @@ public class Game { this.rooms.get("Cafeteria").addItem("Orange", new Item("An orange orange", 15)); } - /** - * 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; - } - /** * Sets the current room designated by its name. * @@ -202,37 +198,7 @@ public class Game { * the destination room name */ public void goToRoom(final String roomName) { - 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. - * - * @return the current room - */ - public Room getCurrentRoom() { - return this.currentRoom; - } - - /** - * Returns the current room's exit located in the given direction. - * - * @param direction - * the direction of the exit - * - * @return the exit room - */ - public Room getRoomExit(final String direction) { - return this.currentRoom.getExit(direction); + this.player.goToRoom(this.rooms.get(roomName)); } /** diff --git a/src/esieequest/model/Player.java b/src/esieequest/model/Player.java index 0252da3..e28edaf 100644 --- a/src/esieequest/model/Player.java +++ b/src/esieequest/model/Player.java @@ -1,5 +1,44 @@ package esieequest.model; +import java.util.Stack; + public class Player { + private Room currentRoom; + private final Stack previousRooms; + + public Player() { + this.currentRoom = null; + this.previousRooms = new Stack(); + } + + /** + * Returns the current room. + * + * @return the current room + */ + public Room getCurrentRoom() { + return this.currentRoom; + } + + /** + * 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; + } + + /** + * Sets the current room to the previous room. + */ + public void goToPreviousRoom() { + if (!this.previousRooms.empty()) { + this.currentRoom = this.previousRooms.pop(); + } + } + } diff --git a/src/esieequest/view/text/Console.java b/src/esieequest/view/text/Console.java index e7a71dd..455db81 100644 --- a/src/esieequest/view/text/Console.java +++ b/src/esieequest/view/text/Console.java @@ -15,9 +15,9 @@ public class Console extends TextInterface { @Override protected void run() { final Scanner scanner = new Scanner(System.in); - while (this.running) { + while (this.isRunning()) { System.out.print("> "); - this.gameEngine.interpret(scanner.nextLine()); + this.getController().interpret(scanner.nextLine()); } scanner.close(); } diff --git a/src/esieequest/view/text/FileReader.java b/src/esieequest/view/text/FileReader.java index 392a691..11ee895 100644 --- a/src/esieequest/view/text/FileReader.java +++ b/src/esieequest/view/text/FileReader.java @@ -31,11 +31,11 @@ public class FileReader extends TextInterface { protected void run() { try { final Scanner scanner = new Scanner(this.file); - while (this.running && scanner.hasNextLine()) { + while (this.isRunning() && scanner.hasNextLine()) { final String line = scanner.nextLine(); System.out.println("> " + line); if (!line.startsWith("//")) { - this.gameEngine.interpret(line); + this.getController().interpret(line); } } scanner.close(); diff --git a/src/esieequest/view/text/TextInterface.java b/src/esieequest/view/text/TextInterface.java index 133b03b..0361631 100644 --- a/src/esieequest/view/text/TextInterface.java +++ b/src/esieequest/view/text/TextInterface.java @@ -16,9 +16,9 @@ import esieequest.view.View; */ abstract class TextInterface implements View { - protected GameEngine gameEngine; + private GameEngine gameEngine; - protected boolean running; + private boolean running; /** * The default constructor. @@ -29,6 +29,27 @@ abstract class TextInterface implements View { protected abstract void run(); + /** + * @return the gameEngine + */ + public GameEngine getController() { + return this.gameEngine; + } + + /** + * @return the running state + */ + public boolean isRunning() { + return this.running; + } + + /** + * @param running the running to set + */ + public void setRunning(boolean running) { + this.running = running; + } + @Override public void setController(final GameEngine gameEngine) { this.gameEngine = gameEngine; @@ -77,4 +98,6 @@ abstract class TextInterface implements View { // TODO Auto-generated method stub } + + } -- cgit v1.2.3