Anonymous user
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}}==
<lang Swift>import Foundation
extension Array {
let temp = self[index1]
}
let index1 = Int(arc4random()) % self.count
let index2 = Int(arc4random()) % self.count
self.swap(index1, index2)
}
}
}
enum Direction:Int {
case north =
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
case
return
case
return
case
return
case
return
}
}
switch self
case .north:
return (0, -1)
case .south:
return (0, 1)
case .east:
return (1, 0)
case .west:
return (-1, 0)
}
}
var char:String {
switch self {
case .north:
return "N"
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
self.maze = [[Int]](
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() {
for j in 0..<y {
// Draw top edge
for
topEdge += "+"
topEdge += String(repeating: (maze[i][j] & Direction.north.rawValue) == 0 ? "-" : " ", count: cellWidth)
}
print(topEdge)
// Draw left edge
for
leftEdge += (maze[i][j] & Direction.west.rawValue) == 0 ? "|" : " "
leftEdge += String(repeating: " ", count: cellWidth)
}
print(leftEdge)
}
// Draw bottom edge
for _ in 0..<x
bottomEdge += "+"
bottomEdge += String(repeating: "-", count: cellWidth)
}
print(bottomEdge)
}
func
}
print(line)
}
func displayDirections() {
for j in 0..<y
line +=
}
print(line)
}
}
private func
for direction in Direction.allDirections {
if (value & direction.rawValue) != 0 {
line += direction.char
}
}
return line
}
}
let x = 20
let y = 10
let maze = MazeGenerator(x, y)
Line 5,883 ⟶ 5,947:
{{out}}
<pre>
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| | | | | |
+---+---+ +---+ + +---+---+---+---+---+ +---+ + +---+---+ + + +
| | | | | | | | |
+ +---+---+ + + + +---+ +---+ +---+---+---+---+---+---+---+---+ +
| | | | | | | | | | |
+---+
| | | | | | | | | | | | | |
+ +---+ + +---+ +---+ + + + + +---+---+---+ +---+ +---+ +
| | | | | | | | | | |
+ + +---+---+ + +---+---+ +---+---+---+ + +---+---+ + + +---+
| | | | | | | | | | | |
+ +---+ + +---+---+ +
| | | | | | | | | | | | |
+ + + +---+---+---+ +---+ + +---+ + + + + +---+---+---+ +
| | | | | | | | | | | | |
+ +---+---+ +---+ + + +---+---+ +
| | | | | | | | | | | | |
+---+---+ + + +---+---+ + +---+---+---+ + + + +---+---+ + +
| | | | | |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+</pre>
=={{header|Tcl}}==
|