1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
package esieequest.model;
import lombok.Getter;
import lombok.Setter;
import net.pacien.util.CleanJSONObject;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import esieequest.controller.utils.SerialisableObject;
import esieequest.model.characters.Character;
import esieequest.model.doors.Door;
import esieequest.model.doors.HiddenDoor;
import esieequest.model.doors.LockedDoor;
import esieequest.model.doors.TransporterDoor;
import esieequest.model.doors.TrapDoor;
import esieequest.model.items.Item;
import esieequest.model.map.Direction;
import esieequest.model.map.Room;
/**
* The Game model.
*
* @author Pacien TRAN-GIRARD
*/
public class Game implements SerialisableObject {
public static final int DEFAULT_INVENTORY_LIMIT = 10;
public static final int DEFAULT_STEPS_LIMIT = 0;
public static final int CHALLENGE_STEPS_LIMIT = 50;
public static final Direction DEFAULT_DIRECTION = Direction.NORTH;
public static final Room DEFAULT_ROOM = Room.AMPHITHEATER_SEAT;
private static final String CHALLENGE_LABEL = "L";
private final boolean challenge;
private static final String PLAYER_LABEL = "P";
@Getter
@Setter
private Player player;
private static final String ROOMS_LABEL = "R";
private static final String ITEMS_LABEL = "I";
private static final String CHARACTERS_LABEL = "C";
/**
* Creates a Game with a step limit for the Player.
*
* @param maxNbSteps
* the step limit for the Player
*/
public Game(final boolean challenge) {
this.challenge = challenge;
}
/**
* Creates a Game.
*/
public Game() {
this(false);
}
/**
* Initialises a new Game.
*/
public void newGame(final boolean populate, final boolean challengeMode) {
Room.createAllSides();
this.connectRooms();
this.setPlayer(new Player(Game.DEFAULT_ROOM, Game.DEFAULT_DIRECTION, Game.DEFAULT_INVENTORY_LIMIT, challengeMode ? Game.CHALLENGE_STEPS_LIMIT : Game.DEFAULT_STEPS_LIMIT));
if (populate) {
this.addItems();
this.addCharacters();
}
}
/**
* Connects Room-s together using Door-s.
*/
public void connectRooms() {
this.d(Room.AMPHITHEATER_SEAT, Direction.NORTH, Room.AMPHITHEATER_STAGE);
this.d(Room.AMPHITHEATER_STAGE, Direction.WEST, Room.CAFETERIA);
this.d(Room.CAFETERIA, Direction.NORTH, Room.CAFETERIA_STREET);
this.d(Room.CAFETERIA_STREET, Direction.EAST, Room.ESIEESPACE_STREET);
this.d(Room.ESIEESPACE_STREET, Direction.SOUTH, Room.ESIEESPACE_FRONT);
this.d(Room.ESIEESPACE_FRONT, Direction.EAST, Room.ESIEESPACE_ENTRANCE);
this.d(Room.ESIEESPACE_ENTRANCE, Direction.NORTH, Room.ESIEESPACE);
this.d(Room.ESIEESPACE_STREET, Direction.EAST, Room.ENTRANCE_STREET);
this.d(Room.ENTRANCE_STREET, Direction.SOUTH, Room.ENTRANCE_STAIRS);
this.d(Room.ENTRANCE_STAIRS, Direction.SOUTH, Room.ENTRANCE_ROUNDABOUT);
this.d(Room.ENTRANCE_STREET, Direction.EAST, Room.WING_STREET);
this.d(Room.WING_STREET, Direction.NORTH, Room.WING_FLOOR_ONE);
this.d(Room.WING_FLOOR_ONE, Direction.WEST, Room.WING_STAIRS_ONE);
this.d(Room.WING_STAIRS_ONE, Direction.SOUTH, Room.WING_STAIRS_TWO);
this.d(Room.WING_STAIRS_ONE, Direction.UP, new HiddenDoor(Room.WING_STAIRS_TWO));
this.d(Room.WING_STAIRS_TWO, Direction.DOWN, new HiddenDoor(Room.WING_STAIRS_ONE));
this.d(Room.WING_STAIRS_TWO, Direction.EAST, Room.WING_FLOOR_TWO);
this.d(Room.WING_FLOOR_TWO, Direction.NORTH, Room.WING_OFFICE_CORRIDOR);
this.d(Room.WING_OFFICE_CORRIDOR, Direction.EAST, Room.WING_OFFICE);
this.d(Room.WING_STREET, Direction.EAST, Room.CLUBNIX_STREET);
this.d(Room.CLUBNIX_STREET, Direction.SOUTH, Room.CLUBNIX_FRONT);
this.d(Room.CLUBNIX_FRONT, Direction.EAST, Room.CLUBNIX_ENTRANCE);
this.d(Room.CLUBNIX_ENTRANCE, Direction.NORTH, Room.CLUBNIX);
this.d(Room.WING_FLOOR_ONE, Direction.EAST, new HiddenDoor(Room.SECRET_CORRIDOR_ENTRANCE));
this.d(Room.SECRET_CORRIDOR_ENTRANCE, Direction.WEST, new Door(Room.WING_FLOOR_ONE));
this.d(Room.SECRET_CORRIDOR_ENTRANCE, Direction.NORTH, Room.STORAGE_ROOM);
this.d(Room.SECRET_CORRIDOR_ENTRANCE, Direction.SOUTH, Room.SECRET_LAB);
this.d(Room.SECRET_CORRIDOR_ENTRANCE, Direction.EAST, Room.SECRET_CORRIDOR_END);
this.d(Room.SECRET_CORRIDOR_END, Direction.NORTH, new TrapDoor(Room.DEAD_END));
final LockedDoor lockedDoorEnter = new LockedDoor(Room.LOCKED_ROOM);
lockedDoorEnter.setKey(Item.KEYCARD);
this.d(Room.SECRET_CORRIDOR_END, Direction.SOUTH, lockedDoorEnter);
final LockedDoor lockedDoorExit = new LockedDoor(Room.SECRET_CORRIDOR_END);
lockedDoorExit.setKey(Item.KEYCARD);
this.d(Room.LOCKED_ROOM, Direction.NORTH, lockedDoorExit);
this.d(Room.SECRET_CORRIDOR_END, Direction.EAST, new TransporterDoor(Room.values()));
}
/**
* Adds two Door that connect two Room-s in both ways.
*
* @param source
* the source Room
* @param direction
* the direction of the Door, seen from the source Room
* @param destination
* the destination Room
*/
private void d(final Room source, final Direction direction, final Room destination) {
source.getSide(direction).setDoor(new Door(destination));
destination.getSide(direction.getOpposite()).setDoor(new Door(source));
}
/**
* Adds a Door to a Room.
*
* @param room
* the source Room
* @param direction
* the Direction of the Door in the given Room
* @param door
* the Door
*/
private void d(final Room room, final Direction direction, final Door door) {
room.getSide(direction).setDoor(door);
}
/**
* Adds Item-s in the map.
*/
public void addItems() {
this.i(Room.STORAGE_ROOM, Direction.WEST, Item.STORAGE_CUBE);
this.i(Room.STORAGE_ROOM, Direction.EAST, Item.SAFETY_CUBE);
this.i(Room.STORAGE_ROOM, Direction.NORTH, Item.BLACK_HOLE);
this.i(Room.SECRET_LAB, Direction.SOUTH, Item.BEAMER);
this.i(Room.DEAD_END, Direction.NORTH, Item.KEYCARD);
}
/**
* Adds a given Item in a Room with a Direction.
*
* @param room
* the Room
* @param direction
* the Direction
* @param item
* the Item
*/
private void i(final Room room, final Direction direction, final Item item) {
room.getSide(direction).setItem(item);
}
/**
* Adds Character-s to the map.
*/
public void addCharacters() {
this.c(Room.LOCKED_ROOM, Direction.SOUTH, Character.SUMOBOT);
}
/**
* Adds a Character in a Room with a Direction.
*
* @param room
* the Room
* @param direction
* the Direction
* @param character
* the Character
*/
private void c(final Room room, final Direction direction, final Character character) {
room.getSide(direction).setCharacter(character);
}
@Override
public JSONObject serialise() {
final CleanJSONObject o = new CleanJSONObject();
o.put(Game.CHALLENGE_LABEL, this.challenge);
o.put(Game.PLAYER_LABEL, this.player.serialise());
o.put(Game.ROOMS_LABEL, Room.serialiseAll());
o.put(Game.ITEMS_LABEL, Item.serialiseAll());
o.put(Game.CHARACTERS_LABEL, Character.serialiseAll());
return o;
}
@Override
public void deserialise(final JSONObject o) {
this.player.deserialise((JSONObject) o.get(Game.PLAYER_LABEL));
Room.deserialiseAll((JSONArray) o.get(Game.ROOMS_LABEL));
Item.deserialiseAll((JSONArray) o.get(Game.ITEMS_LABEL));
Character.deserialiseAll((JSONArray) o.get(Game.CHARACTERS_LABEL));
}
}
|