-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap1.java
More file actions
76 lines (65 loc) · 2.54 KB
/
Map1.java
File metadata and controls
76 lines (65 loc) · 2.54 KB
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public final class Map1 {
private final Map<Integer, Map<Integer, Room>> map = new HashMap<Integer, Map<Integer, Room>>();
private Room currentRoom;
private int positiontX = 0;
private int positionY = 0;
private Map1() {
}
private void putRoom(int x, int y, Room room) {
if (!map.containsKey(x)) {
map.put(x, new HashMap<Integer, Room>());
}
map.get(x).put(y, room);
}
private Room getRoom(int x, int y) {
return map.get(x).get(y);
}
private boolean roomExists(int x, int y) {
if (!map.containsKey(x)) {
return false;
}
return map.get(x).containsKey(y);
}
private boolean isComplete() {
return currentRoom.isComplete();
}
public void movePlayer(Player player) throws IOException {
boolean northPossible = roomExists(positiontX, positionY + 1);
boolean southPossible = roomExists(positiontX, positionY - 1);
boolean eastPossible = roomExists(positiontX + 1, positionY);
boolean westPossible = roomExists(positiontX - 1, positionY);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String direction = in.readLine();
if (direction.equals("go north") && northPossible) {
positionY++;
} else if (direction.equals("go south") && southPossible) {
positionY--;
} else if (direction.equals("go east") && eastPossible) {
positiontX++;
} else if (direction.equals("go west") && westPossible) {
positiontX--;
}
currentRoom = getRoom(positiontX, positionY);
currentRoom.enter(player);
}
public static Map1 newInstance() {
Map1 dungeon = new Map1();
dungeon.putRoom(0, 0, Room.newRegularInstance());
dungeon.putRoom(-1, 1, Room.newRegularInstance());
dungeon.putRoom(0, 1, Room.newRegularInstance());
dungeon.putRoom(1, 1, Room.newRegularInstance());
dungeon.putRoom(-1, 2, Room.newRegularInstance());
dungeon.putRoom(1, 2, Room.newRegularInstance());
dungeon.putRoom(-1, 3, Room.newRegularInstance());
dungeon.putRoom(0, 3, Room.newRegularInstance());
dungeon.putRoom(1, 3, Room.newRegularInstance());
dungeon.putRoom(0, 4, Room.newRegularInstance());
dungeon.currentRoom = dungeon.getRoom(0, 0);
return dungeon;
}
}