Будет довольно просто приспособить его под любой язык программирования.
Вот код!😁😁😁
👇🏻👇🏻👇🏻👇🏻👇🏻
// Create a 2D array to represent your map, setting all cells in the map as walls.
Cell[][] map = new Cell[width][height];
for (h in height) {
for (w in width) {
map[w][h].make_wall();
}
}
// Choose a random cell with odd x and y coordinates and clear it.
int x = random_int(0, width / 2) * 2 + 1;
int y = random_int(0, height / 2) * 2 + 1;
map[x][y].clear_cell();
// Create an array and add valid cells that are two orthogonal spaces away from the cell you just cleared.
Point[] to_check = new Point[0];
if (y - 2 >= 0) {
to_check.append(new Point(x, y - 2));
}
if (y + 2 < height) {
to_check.append(new Point(x, y + 2));
}
if (x - 2 >= 0) {
to_check.append(new Point(x - 2, y));
}
if (x + 2 < width) {
to_check.append(new Point(x + 2, y));
}
// While there are cells in your growable array, choose choose one at random, clear it, and remove it from the growable array.
while (to_check.size() > 0) {
int index = random_int(0, to_check.size());
Point cell = to_check.get(index);
x = cell.get_x();
y = cell.get_y();
map[x][y].make_clear();
to_check.remove(index);
// The cell you just cleared needs to be connected with another clear cell.
// Look two orthogonal spaces away from the cell you just cleared until you find one that is not a wall.
// Clear the cell between them.
Direction[] d = {Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST};
while (d.size() > 0) {
int dir_index = random_int(0, d.size());
switch (d[dir_index]) {
case Direction.NORTH:
if (y - 2 >= 0 && map[x][y - 2].is_clear()) {
map[x][y - 1].make_clear();
d.remove_all();
}
break;
case Direction.SOUTH:
if (y + 2 < height && map[x][y + 2].is_clear()) {
map[x][y + 1].clear();
d.remove_all();
}
break;
case Direction.EAST:
if (x - 2 >= 0 && map[x - 2][y].is_clear()) {
map[x - 1][y].make_clear();
d.remove_all();
}
break;
case Direction.WEST:
if (x + 2 < width && map[x + 2][y].is_clear()) {
map[x + 1][y].make_clear();
d.remove_all();
}
break;
}
d.remove(dir_index);
}
// Add valid cells that are two orthogonal spaces away from the cell you cleared.
if (y - 2 >= 0 && map[x][y - 2].is_wall()) {
to_check.append(new Point(x, y - 2));
}
if (y + 2 < height && map[x][y + 2].is_wall()) {
to_check.append(new Point(x, y + 2));
}
if (x - 2 >= 0 && map[x - 2][y].is_wall()) {
to_check.append(new Point(x - 2, y));
}
if (x + 2 < width && map[x + 2][y].is_wall()) {
to_check.append(new Point(x + 2, y));
}
}
Присоединяйтесь — мы покажем вам много интересного
Присоединяйтесь к ОК, чтобы подписаться на группу и комментировать публикации.
Нет комментариев