Maze generation: Difference between revisions
Content added Content deleted
m (→{{header|Sidef}}: modified the code to work with Sidef 2.30) |
(Kotlin entry) |
||
Line 3,320: | Line 3,320: | ||
┣━╸ ┗━╸ ┗━━━┓ ╹ ┗━┓ ╹ ┗━┛ ┣━━━━━┛ ╻ ┗━━━┛ ┗━╸ ╹ ┃ ╺━┛ ╺━┛ ╻ ┃ |
┣━╸ ┗━╸ ┗━━━┓ ╹ ┗━┓ ╹ ┗━┛ ┣━━━━━┛ ╻ ┗━━━┛ ┗━╸ ╹ ┃ ╺━┛ ╺━┛ ╻ ┃ |
||
┗━━━━━━━━━━━┻━━━━━┻━━━━━━━┻━━━━━━━┻━━━━━━━━━━━━━┻━━━━━━━━━┻━┛</pre> |
┗━━━━━━━━━━━┻━━━━━┻━━━━━━━┻━━━━━━━┻━━━━━━━━━━━━━┻━━━━━━━━━┻━┛</pre> |
||
=={{header|Kotlin}}== |
|||
{{trans|Java}} |
|||
<lang scala>import shuffle.shuffle |
|||
class Maze_generator(val x: Int, val y: Int) { |
|||
fun generate(cx: Int, cy: Int) { |
|||
val dirs = arrayOf(*DIR.values()).shuffle() |
|||
for (dir in dirs) { |
|||
val nx = cx + dir.dx |
|||
val ny = cy + dir.dy |
|||
if (between(nx, x) && between(ny, y) && maze[nx][ny] == 0) { |
|||
maze[cx][cy] = maze[cx][cy] or dir.bit |
|||
maze[nx][ny] = maze[nx][ny] or dir.opposite!!.bit |
|||
generate(nx, ny) |
|||
} |
|||
} |
|||
} |
|||
fun display() { |
|||
for (i in 0..y - 1) { |
|||
// draw the north edge |
|||
for (j in 0..x - 1) |
|||
print(if (maze[j][i] and 1 == 0) "+---" else "+ ") |
|||
println('+') |
|||
// draw the west edge |
|||
for (j in 0..x - 1) |
|||
print(if (maze[j][i] and 8 == 0) "| " else " ") |
|||
println('|') |
|||
} |
|||
// draw the bottom line |
|||
for (j in 0..x - 1) print("+---") |
|||
println('+') |
|||
} |
|||
private enum class DIR(val bit: Int, val dx: Int, val dy: Int) { |
|||
N(1, 0, -1), S(2, 0, 1), E(4, 1, 0),W(8, -1, 0); |
|||
var opposite: DIR? = null |
|||
companion object { |
|||
init { |
|||
N.opposite = S |
|||
S.opposite = N |
|||
E.opposite = W |
|||
W.opposite = E |
|||
} |
|||
} |
|||
} |
|||
private fun between(v: Int, upper: Int) = v >= 0 && v < upper |
|||
private val maze = Array(x) { IntArray(y) } |
|||
} |
|||
fun main(args: Array<String>) { |
|||
val x = if (args.size >= 1) args[0].toInt() else 8 |
|||
val y = if (args.size == 2) args[1].toInt() else 8 |
|||
val maze = Maze_generator(x, y) |
|||
maze.generate(0, 0) |
|||
maze.display() |
|||
}</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |