-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dungeon.java
58 lines (50 loc) · 1.44 KB
/
Dungeon.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
/*
* DUNGEON
* Helper Class
*
* Maintains jagged array of rooms representing different challenges
*/
public class Dungeon {
// FIELDS
// rooms array is jagged
Room[][] rooms;
int difficulty;
String name;
// CONSTRUCTOR
// generate new random dungeon
public Dungeon(int difficulty, String name) {
this.difficulty = difficulty;
this.name = name;
// generate number of levels
// jagged array to allow for lower levels to have more rooms
int numLevels = Dice.rollDice(1, 4) + difficulty;
rooms = new Room[numLevels][];
int numRooms = Dice.rollDice(1, 2);
// generate number of rooms in each level
for (int i = 0; i < rooms.length; i++) {
rooms[i] = new Room[numRooms];
// fill level with rooms
for (int j = 0; j < rooms[i].length; j++) {
rooms[i][j] = new Room(difficulty);
}
// next level adds 0, 1, or 2 more rooms
numRooms += Dice.rollDice(1, 3) - 1;
}
// decide a random room in each level to have the stairs down
// Stairs location not yet a requirement for advancing to lower levels
for (Room[] r : rooms) {
int stairsIndex = Dice.rollDice(1, r.length) - 1;
r[stairsIndex].hasStairs = true;
}
}
// prints the dungeon so it is easy for player to tell where they are and what dungeon looks like
public void printDungeon() {
System.out.println(name);
for (Room[] level : rooms) {
for (Room r : level) {
System.out.print(r);
}
System.out.println();
}
}
}