-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.java
135 lines (109 loc) · 3.1 KB
/
part2.java
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
import java.util.HashMap;
import java.util.Map;
enum Direction {
NORTH,
EAST,
SOUTH,
WEST
}
class Wall {
}
class DoorWall extends Wall {
private Room r1;
private Room r2;
private boolean isOpen;
public DoorWall(Room r1, Room r2) {
this.r1 = r1;
this.r2 = r2;
this.isOpen = false;
}
public Room getOtherRoom(Room currentRoom) {
if (currentRoom == r1) {
return r2;
} else {
return r1;
}
}
}
class Room {
private Map<Direction, Wall> sides = new HashMap<>();
private int roomNo;
public Room(int roomNo) {
this.roomNo = roomNo;
}
public int getRoomNo() {
return roomNo;
}
public Wall getSide(Direction direction) {
return sides.get(direction);
}
public void setSide(Direction direction, Wall wall) {
sides.put(direction, wall);
}
}
class Maze {
private Map<Integer, Room> rooms = new HashMap<>();
public void addRoom(Room r) {
rooms.put(r.getRoomNo(), r);
}
public Room roomNo(int r) {
return rooms.get(r);
}
public Iterable<Integer> roomNumbers() {
return rooms.keySet();
}
}
interface MazeBuilder {
void buildRooms();
Maze getMaze();
}
class OldMazeBuilder implements MazeBuilder {
private Maze maze;
public OldMazeBuilder() {
this.maze = new Maze();
}
@Override
public void buildRooms() {
Room r1 = new Room(1);
Room r2 = new Room(2);
DoorWall d = new DoorWall(r1, r2);
maze.addRoom(r1);
maze.addRoom(r2);
r1.setSide(Direction.NORTH, d);
r1.setSide(Direction.EAST, new Wall());
r1.setSide(Direction.SOUTH, new Wall());
r1.setSide(Direction.WEST, new Wall());
r2.setSide(Direction.NORTH, new Wall());
r2.setSide(Direction.EAST, new Wall());
r2.setSide(Direction.SOUTH, d);
r2.setSide(Direction.WEST, new Wall());
}
@Override
public Maze getMaze() {
return maze;
}
}
class MazeGame {
public static void main(String[] argv) {
Maze maze = createMaze(new OldMazeBuilder()); // Creating the old game
displayMaze(maze);
}
private static Maze createMaze(MazeBuilder builder) {
builder.buildRooms();
return builder.getMaze();
}
private static void displayMaze(Maze maze) {
for (int roomNo : maze.roomNumbers()) {
Room room = maze.roomNo(roomNo);
System.out.println("Room " + room.getRoomNo() + ":");
for (Direction direction : Direction.values()) {
Wall wall = room.getSide(direction);
if (wall instanceof DoorWall) {
System.out.println("\t" + direction + " -> Door to Room " + ((DoorWall) wall).getOtherRoom(room).getRoomNo());
} else {
System.out.println("\t" + direction + " -> Wall");
}
}
}
}
}