Jump to content

Maze generation: Difference between revisions

→‎{{header|Swift}}: Swift 3 - depth-first algorithm using recursion
(→‎{{header|Swift}}: Swift 3 - depth-first algorithm using recursion)
Line 5,771:
 
=={{header|Swift}}==
 
{{trans|Java}}
<lang Swift>import Foundation
 
extension Array {
class Direction {
varmutating vect:func swap(Int,_ index1:Int, _ index2:Int) {
let temp = self[index1]
var bit:Int {
let (bit, dx, dy)self[index1] = vectself[index2]
returnself[index2] bit= temp
}
 
varmutating dx:Intfunc shuffle() {
letfor (bit,_ dx,in dy)0..<self.count = vect{
let index1 = Int(arc4random()) % self.count
return dx
let index2 = Int(arc4random()) % self.count
self.swap(index1, index2)
}
}
}
 
var dy:Int {
enum Direction:Int {
let (bit, dx, dy) = vect
case north = return dy1
case south = 2
case east = 4
case west = 8
 
static var allDirections:[Direction] {
return [Direction.north, Direction.south, Direction.east, Direction.west]
}
 
var opposite:Direction {
switch (vect)self {
case (1, 0, -1).north:
return Direction(bit: 2, dx: 0, dy: 1).south
case (2, 0, 1).south:
return Direction(bit: 1, dx: 0, dy: -1).north
case (4, 1, 0).east:
return Direction(bit: 8, dx: -1, dy: 0).west
case (8, -1, 0).west:
return Direction(bit: 4, dx: 1, dy: 0).east
default:
return Direction(bit: 0, dx: 0, dy: 0)
}
}
 
init(bit:Int,var dxdiff:(Int, dy:Int) {
switch self.vect = (bit, dx, dy){
case .north:
return (0, -1)
case .south:
return (0, 1)
case .east:
return (1, 0)
case .west:
return (-1, 0)
}
}
}
 
var char:String {
let N = Direction(bit: 1, dx: 0, dy: -1)
switch self {
let S = Direction(bit: 2, dx: 0, dy: 1)
case .north:
let E = Direction(bit: 4, dx: 1, dy: 0)
return "N"
let W = Direction(bit: 8, dx: -1, dy: 0)
case .south:
return "S"
case .east:
return "E"
case .west:
return "W"
}
}
 
}
 
class MazeGenerator {
let x:Int!
let y:Int!
var maze:[[Int]]!
 
init(_ x:Int, _ y:Int) {
self.x = x
self.y = y
varlet colcolumn = [Int](countrepeating: y0, repeatedValuecount: 0y)
self.maze = [[Int]](countrepeating: xcolumn, repeatedValuecount: colx)
generateMaze(0, 0)
}
 
private func generateMaze(_ cx:Int, _ cy:Int) {
var directions = Direction.allDirections
directions.shuffle()
for direction in directions {
let (dx, dy) = direction.diff
let nx = cx + dx
let ny = cy + dy
if inBounds(nx, ny) && maze[nx][ny] == 0 {
maze[cx][cy] |= direction.rawValue
maze[nx][ny] |= direction.opposite.rawValue
generateMaze(nx, ny)
}
}
}
 
private func inBounds(_ testX:Int, _ testY:Int) -> Bool {
return inBounds(value:testX, upper:self.x) && inBounds(value:testY, upper:self.y)
}
 
private func inBounds(value:Int, upper:Int) -> Bool {
return (value >= 0) && (value < upper)
}
 
func display() {
forlet icellWidth in= 0..<y {3
for j in 0..<y {
// Draw top edge
forvar jtopEdge in= 0..<x {""
for print((maze[j][i] & 1) ==in 0..<x ? "+---" : "+ "){
topEdge += "+"
topEdge += String(repeating: (maze[i][j] & Direction.north.rawValue) == 0 ? "-" : " ", count: cellWidth)
}
println(topEdge += "+")
print(topEdge)
 
// Draw left edge
forvar jleftEdge in= 0..<x {""
for print((maze[j][i] & 8) ==in 0..<x ? "| " : " "){
leftEdge += (maze[i][j] & Direction.west.rawValue) == 0 ? "|" : " "
leftEdge += String(repeating: " ", count: cellWidth)
}
println(leftEdge += "|")
print(leftEdge)
}
 
// Draw bottom edge
forvar jbottomEdge in= 0..<x {""
for _ in 0..<x print("+---"){
bottomEdge += "+"
bottomEdge += String(repeating: "-", count: cellWidth)
}
println(bottomEdge += "+")
print(bottomEdge)
}
func generateMazedisplayInts(cx:Int, _ cy:Int) {
varfor dirsj =in [N,0..<y S, E, W] as NSMutableArray{
for i in 0..<dirs.count {var line = ""
letfor x1i =in Int(arc4random()) % dirs0.count.<x {
let x2 line += IntString(arc4random()maze[i][j]) %+ dirs.count"\t"
}
dirs.exchangeObjectAtIndex(x1, withObjectAtIndex: x2)
print(line)
}
}
 
for dir in dirs {
func displayDirections() {
var dir = dir as Direction
for j in 0..<y let nx = cx + dir.dx{
letvar nyline = cy + dir.dy""
iffor (between(nx,i selfin 0..<x) && between(ny, self.y) && (maze[nx][ny] == 0)) {
line += getDirectionsAsString(maze[cxi][cyj]) |=+ dir.bit"\t"
maze[nx][ny] |= dir.opposite.bit
generateMaze(nx, ny)
}
print(line)
}
}
 
private func betweengetDirectionsAsString(v:Int, _ uppervalue:Int) -> BoolString {
returnvar (vline >= 0) && (v < upper)""
for direction in Direction.allDirections {
if (value & direction.rawValue) != 0 {
line += direction.char
}
}
return line
}
}
 
 
let x = 10
let x = 20
let y = 10
let maze = MazeGenerator(x, y)
Line 5,883 ⟶ 5,947:
{{out}}
<pre>
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| | | | | |
+---+---+ +---+ + +---+---+---+---+---+ +---+ + +---+---+ + + +
| | | | | | | | |
+ +---+---+ + + + +---+ +---+ +---+---+---+---+---+---+---+---+ +
| | | | | | | | | | |
+---+--- + +---+ + + + +---+ +---+---+---+ + +---+---+ + + +
| | | | | | | | | | | | | |
+ +---+ + +---+ +---+ + + + + +---+---+---+ +---+ +---+ +
| | | | | | | | | | |
+ + +---+---+ + +---+---+ +---+---+---+ + +---+---+ + + +---+
| | | | | | | | | | | |
+ +---+ + +---+---+ + ---+---+---+ + + + + + +---+ +---+ +
| | | | | | | | | | | | |
+ + + +---+---+---+ +---+ + +---+ + + + + +---+---+---+ +
| | | | | | | | | | | | |
+ +---+---+ +---+ + + +---+---+ +--- +---+ + + + +---+---+---+
| | | | | | | | | | | | |
+---+---+ + + +---+---+ + +---+---+---+ + + + +---+---+ + +
| | | | | |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+</pre>
 
=={{header|Tcl}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.