-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoomGenerator.java
230 lines (195 loc) · 5.21 KB
/
RoomGenerator.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
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
228
229
230
/**
*=========================================================
* RoomGenerator.java
*
* Created by: Nico Poblete/boaromayo/Quesada's Swan
*
* Initial release: 2018-05-16
*
* Initial commit: 2018-05-16
*
* Updated: 2018-05-16
*
* Changelog:
* - Initial commit - 2018-05-16
* - Started - 2018-05-15
*---------------------------------------------------------
* This generates one Zelda-esque dungeon room based on a
* given width and height.
* Adjusting certain parameters using integers corresponding
* to certain tiles, a room can be created.
* The output is shown on the console, then is saved to an
* arbitrary text file.
*=========================================================
**/
import java.io.*;
import java.util.*;
public class RoomGenerator {
// INPUT VARIABLES.
private String mapname;
private int width, height;
// TILE PARAMETERS. Add more tiles if needed.
private static final int LIMIT = 13;
private static final int BLANK = 0;
private static final int FLOOR = 1;
private static final int BLOCK = 10;
private static final int WATER = 11;
//private static final int LAVA = 12;
private static final int WALL_TOP = 2;
private static final int WALL_BOTTOM = 3;
private static final int WALL_LEFT = 4;
private static final int WALL_RIGHT = 5;
private static final int CORNER_ONE = 6;
private static final int CORNER_TWO = 7;
private static final int CORNER_THREE = 8;
private static final int CORNER_FOUR = 9;
private int[] TILES = {FLOOR, WATER, FLOOR, FLOOR,
FLOOR, BLOCK, FLOOR};
// SETTINGS.
private boolean isCSVFile = false;
// ROOM.
private int[][] room;
public RoomGenerator() {
inputGenerator();
}
public void inputGenerator() {
Scanner scanner = new Scanner(System.in);
System.out.println("Write name of map, width, and height:");
mapname = scanner.nextLine();
width = scanner.nextInt();
height = scanner.nextInt();
room = new int[height][width];
generate();
print();
scanner.close();
// Uncomment this code if console output works.
try {
writeToFile();
} catch(Exception e) {
System.err.println("ERROR: " + e.getMessage());
e.printStackTrace();
}
}
public void generate() {
Random rand = new Random();
int mid; // midpoint of map
int i, r, c;
boolean top, bottom, left, right;
boolean hasPassageNorth, hasPassageWest, hasPassageEast, hasPassageSouth;
hasPassageNorth = (rand.nextInt(LIMIT+1) % 2 == 0);
hasPassageWest = (rand.nextInt(LIMIT+1) % 6 == 0);
hasPassageEast = (rand.nextInt(LIMIT+1) % 8 == 0);
hasPassageSouth = (rand.nextInt(LIMIT+1) % 4 == 0);
for (i = 0; i < width*height; i++) {
r = i / width;
c = i % width;
top = (r == 0);
bottom = (r == height-1);
left = (c == 0);
right = (c == width-1);
// Check when on the edges of the room.
// If so, draw in the integers for the WALLs and CORNERs.
if (top) {
if (left) {
room[r][c] = CORNER_ONE;
} else if (right) {
room[r][c] = CORNER_TWO;
} else {
room[r][c] = WALL_TOP;
}
}
else if (bottom) {
if (left) {
room[r][c] = CORNER_THREE;
} else if (right) {
room[r][c] = CORNER_FOUR;
} else {
room[r][c] = WALL_BOTTOM;
}
}
else {
if (left) {
room[r][c] = WALL_LEFT;
} else if (right) {
room[r][c] = WALL_RIGHT;
} else {
room[r][c] =
TILES[rand.nextInt(LIMIT+1) % TILES.length];
}
}
}
// If a passage is made available,
// replace walls with floor.
if (hasPassageNorth) {
mid = (int)width/2;
room[0][mid] = FLOOR;
room[0][mid+1] = FLOOR;
}
if (hasPassageWest) {
mid = (int)height/2;
room[mid][0] = FLOOR;
room[mid+1][0] = FLOOR;
}
if (hasPassageEast) {
mid = (int)height/2;
room[mid][width-1] = FLOOR;
room[mid+1][width-1] = FLOOR;
}
if (hasPassageSouth) {
mid = (int)width/2;
room[height-1][mid] = FLOOR;
room[height-1][mid+1] = FLOOR;
}
}
public void print() {
int i, r, c;
boolean top, left;
for (i = 0; i < width*height; i++) {
r = i / width;
c = i % width;
top = (r == 0);
left = (c == 0);
// Go to next row when column resets
if (!top && left) {
System.out.println();
}
if (isCSVFile) {
System.out.print(room[r][c] + ",");
} else {
System.out.print(room[r][c] + " ");
}
}
}
public void writeToFile() throws FileNotFoundException {
String filename = mapname + ".txt";
File output = new File(filename);
PrintWriter writer = new PrintWriter(output);
int i, r, c;
boolean top, left, edge;
for (i = 0; i < width*height; i++) {
r = i / width;
c = i % width;
top = (r == 0);
left = (c == 0);
edge = (c == width-1);
if (!top && left) {
writer.println();
}
// Don't leave whitespace at right edge of room
if (edge) {
writer.print(room[r][c]);
continue;
}
if (isCSVFile) {
writer.print(room[r][c] + ",");
} else {
writer.print(room[r][c] + " ");
}
}
writer.flush();
writer.close();
}
public static void main(String[] args) {
new RoomGenerator();
}
}