aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2014-04-02 10:50:28 +0200
committerPacien TRAN-GIRARD2014-04-02 10:50:28 +0200
commit92f671b84b9c2a2c2319aac2a52e6dffa8bc2195 (patch)
tree0556aa0d65d52e03562235d11088e1aef6a7928b
parent52b649c665ac9f35b3e4d553bd964442dc5224c1 (diff)
downloadesieequest-92f671b84b9c2a2c2319aac2a52e6dffa8bc2195.tar.gz
Move some attributes to the Player class
-rw-r--r--src/esieequest/controller/Performer.java12
-rw-r--r--src/esieequest/model/Game.java60
-rw-r--r--src/esieequest/model/Player.java39
-rw-r--r--src/esieequest/view/text/Console.java4
-rw-r--r--src/esieequest/view/text/FileReader.java4
-rw-r--r--src/esieequest/view/text/TextInterface.java27
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 {
99 * the direction of the new room 99 * the direction of the new room
100 */ 100 */
101 public void goTo(final String direction) { 101 public void goTo(final String direction) {
102 final Room nextRoom = this.game.getRoomExit(direction); 102 final Room nextRoom = this.game.getPlayer().getCurrentRoom().getExit(direction);
103 if (nextRoom != null) { 103 if (nextRoom != null) {
104 this.game.goToRoom(nextRoom); 104 this.game.getPlayer().goToRoom(nextRoom);
105 this.view.updateRoom(this.game.getCurrentRoom()); 105 this.view.updateRoom(this.game.getPlayer().getCurrentRoom());
106 } else { 106 } else {
107 this.echo(this.game.getNoExitMessage()); 107 this.echo(this.game.getNoExitMessage());
108 } 108 }
@@ -112,15 +112,15 @@ class Performer {
112 * Changes the current room to the previous one. 112 * Changes the current room to the previous one.
113 */ 113 */
114 public void goBack() { 114 public void goBack() {
115 this.game.goToPreviousRoom(); 115 this.game.getPlayer().goToPreviousRoom();
116 this.view.updateRoom(this.game.getCurrentRoom()); 116 this.view.updateRoom(this.game.getPlayer().getCurrentRoom());
117 } 117 }
118 118
119 /** 119 /**
120 * Displays informations about the current place. 120 * Displays informations about the current place.
121 */ 121 */
122 public void look() { 122 public void look() {
123 this.echo(this.game.getCurrentRoom().getInformations()); 123 this.echo(this.game.getPlayer().getCurrentRoom().getInformations());
124 } 124 }
125 125
126 /** 126 /**
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 @@
1package esieequest.model; 1package esieequest.model;
2 2
3import java.util.HashMap; 3import java.util.HashMap;
4import java.util.Stack;
5 4
6/** 5/**
7 * Represents the game. 6 * Represents the game.
@@ -12,17 +11,16 @@ import java.util.Stack;
12public class Game { 11public class Game {
13 12
14 private final HashMap<String, Room> rooms; 13 private final HashMap<String, Room> rooms;
15 private Room currentRoom;
16 14
17 private final Stack<Room> previousRooms; 15 private final Player player;
18 16
19 /** 17 /**
20 * The default constructor. 18 * The default constructor.
21 */ 19 */
22 public Game() { 20 public Game() {
23 this.rooms = new HashMap<String, Room>(); 21 this.rooms = new HashMap<String, Room>();
24 this.currentRoom = null; 22
25 this.previousRooms = new Stack<Room>(); 23 this.player = new Player();
26 24
27 this.createRooms(); 25 this.createRooms();
28 this.linkRooms(); 26 this.linkRooms();
@@ -31,6 +29,15 @@ public class Game {
31 } 29 }
32 30
33 /** 31 /**
32 * Returns the player
33 *
34 * @return the player
35 */
36 public Player getPlayer() {
37 return this.player;
38 }
39
40 /**
34 * Creates a new room. 41 * Creates a new room.
35 * 42 *
36 * @param name 43 * @param name
@@ -185,54 +192,13 @@ public class Game {
185 } 192 }
186 193
187 /** 194 /**
188 * Sets the current room and stacks previous rooms.
189 *
190 * @param room
191 * the destination room
192 */
193 public void goToRoom(final Room room) {
194 this.previousRooms.push(this.currentRoom);
195 this.currentRoom = room;
196 }
197
198 /**
199 * Sets the current room designated by its name. 195 * Sets the current room designated by its name.
200 * 196 *
201 * @param roomName 197 * @param roomName
202 * the destination room name 198 * the destination room name
203 */ 199 */
204 public void goToRoom(final String roomName) { 200 public void goToRoom(final String roomName) {
205 this.currentRoom = this.rooms.get(roomName); 201 this.player.goToRoom(this.rooms.get(roomName));
206 }
207
208 /**
209 * Sets the current room to the previous room.
210 */
211 public void goToPreviousRoom() {
212 if (!this.previousRooms.empty()) {
213 this.currentRoom = this.previousRooms.pop();
214 }
215 }
216
217 /**
218 * Returns the current room.
219 *
220 * @return the current room
221 */
222 public Room getCurrentRoom() {
223 return this.currentRoom;
224 }
225
226 /**
227 * Returns the current room's exit located in the given direction.
228 *
229 * @param direction
230 * the direction of the exit
231 *
232 * @return the exit room
233 */
234 public Room getRoomExit(final String direction) {
235 return this.currentRoom.getExit(direction);
236 } 202 }
237 203
238 /** 204 /**
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 @@
1package esieequest.model; 1package esieequest.model;
2 2
3import java.util.Stack;
4
3public class Player { 5public class Player {
4 6
7 private Room currentRoom;
8 private final Stack<Room> previousRooms;
9
10 public Player() {
11 this.currentRoom = null;
12 this.previousRooms = new Stack<Room>();
13 }
14
15 /**
16 * Returns the current room.
17 *
18 * @return the current room
19 */
20 public Room getCurrentRoom() {
21 return this.currentRoom;
22 }
23
24 /**
25 * Sets the current room and stacks previous rooms.
26 *
27 * @param room
28 * the destination room
29 */
30 public void goToRoom(final Room room) {
31 this.previousRooms.push(this.currentRoom);
32 this.currentRoom = room;
33 }
34
35 /**
36 * Sets the current room to the previous room.
37 */
38 public void goToPreviousRoom() {
39 if (!this.previousRooms.empty()) {
40 this.currentRoom = this.previousRooms.pop();
41 }
42 }
43
5} 44}
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 {
15 @Override 15 @Override
16 protected void run() { 16 protected void run() {
17 final Scanner scanner = new Scanner(System.in); 17 final Scanner scanner = new Scanner(System.in);
18 while (this.running) { 18 while (this.isRunning()) {
19 System.out.print("> "); 19 System.out.print("> ");
20 this.gameEngine.interpret(scanner.nextLine()); 20 this.getController().interpret(scanner.nextLine());
21 } 21 }
22 scanner.close(); 22 scanner.close();
23 } 23 }