Maze generation: Difference between revisions

Content added Content deleted
(→‎{{header|Swift}}: Swift 3 - depth-first algorithm using recursion)
Line 5,771: Line 5,771:


=={{header|Swift}}==
=={{header|Swift}}==

{{trans|Java}}
<lang Swift>import Foundation
<lang Swift>import Foundation


extension Array {
class Direction {
var vect:(Int, Int, Int)
mutating func swap(_ index1:Int, _ index2:Int) {
let temp = self[index1]
var bit:Int {
let (bit, dx, dy) = vect
self[index1] = self[index2]
return bit
self[index2] = temp
}
}

var dx:Int {
mutating func shuffle() {
let (bit, dx, dy) = vect
for _ in 0..<self.count {
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
return dy
case north = 1
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 {
var opposite:Direction {
switch (vect) {
switch self {
case (1, 0, -1):
case .north:
return Direction(bit: 2, dx: 0, dy: 1)
return .south
case (2, 0, 1):
case .south:
return Direction(bit: 1, dx: 0, dy: -1)
return .north
case (4, 1, 0):
case .east:
return Direction(bit: 8, dx: -1, dy: 0)
return .west
case (8, -1, 0):
case .west:
return Direction(bit: 4, dx: 1, dy: 0)
return .east
default:
return Direction(bit: 0, dx: 0, dy: 0)
}
}
}
}

init(bit:Int, dx:Int, dy:Int) {
var diff:(Int, Int) {
self.vect = (bit, dx, dy)
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 {
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 {
class MazeGenerator {
let x:Int!
let x:Int
let y:Int!
let y:Int
var maze:[[Int]]!
var maze:[[Int]]

init(_ x:Int, _ y:Int) {
init(_ x:Int, _ y:Int) {
self.x = x
self.x = x
self.y = y
self.y = y
var col = [Int](count: y, repeatedValue: 0)
let column = [Int](repeating: 0, count: y)
self.maze = [[Int]](count: x, repeatedValue: col)
self.maze = [[Int]](repeating: column, count: x)
generateMaze(0, 0)
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() {
func display() {
for i in 0..<y {
let cellWidth = 3
for j in 0..<y {
// Draw top edge
// Draw top edge
for j in 0..<x {
var topEdge = ""
print((maze[j][i] & 1) == 0 ? "+---" : "+ ")
for i in 0..<x {
topEdge += "+"
topEdge += String(repeating: (maze[i][j] & Direction.north.rawValue) == 0 ? "-" : " ", count: cellWidth)
}
}
println("+")
topEdge += "+"
print(topEdge)

// Draw left edge
// Draw left edge
for j in 0..<x {
var leftEdge = ""
print((maze[j][i] & 8) == 0 ? "| " : " ")
for i in 0..<x {
leftEdge += (maze[i][j] & Direction.west.rawValue) == 0 ? "|" : " "
leftEdge += String(repeating: " ", count: cellWidth)
}
}
println("|")
leftEdge += "|"
print(leftEdge)
}
}

// Draw bottom edge
// Draw bottom edge
for j in 0..<x {
var bottomEdge = ""
print("+---")
for _ in 0..<x {
bottomEdge += "+"
bottomEdge += String(repeating: "-", count: cellWidth)
}
}
println("+")
bottomEdge += "+"
print(bottomEdge)
}
}
func generateMaze(cx:Int, _ cy:Int) {
func displayInts() {
var dirs = [N, S, E, W] as NSMutableArray
for j in 0..<y {
for i in 0..<dirs.count {
var line = ""
let x1 = Int(arc4random()) % dirs.count
for i in 0..<x {
let x2 = Int(arc4random()) % dirs.count
line += String(maze[i][j]) + "\t"
}
dirs.exchangeObjectAtIndex(x1, withObjectAtIndex: x2)
print(line)
}
}
}

for dir in dirs {
func displayDirections() {
var dir = dir as Direction
let nx = cx + dir.dx
for j in 0..<y {
let ny = cy + dir.dy
var line = ""
if (between(nx, self.x) && between(ny, self.y) && (maze[nx][ny] == 0)) {
for i in 0..<x {
maze[cx][cy] |= dir.bit
line += getDirectionsAsString(maze[i][j]) + "\t"
maze[nx][ny] |= dir.opposite.bit
generateMaze(nx, ny)
}
}
print(line)
}
}
}
}

func between(v:Int, _ upper:Int) -> Bool {
private func getDirectionsAsString(_ value:Int) -> String {
return (v >= 0) && (v < upper)
var line = ""
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 y = 10
let maze = MazeGenerator(x, y)
let maze = MazeGenerator(x, y)
Line 5,883: Line 5,947:
{{out}}
{{out}}
<pre>
<pre>
+---+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| | | |
| | | | | |
+ + + +---+---+---+---+ +---+ +
+---+---+ +---+ + +---+---+---+---+---+ +---+ + +---+---+ + + +
| | | | | |
| | | | | | | | |
+ +---+ +---+---+ +---+---+---+ +
+ +---+---+ + + + +---+ +---+ +---+---+---+---+---+---+---+---+ +
| | | | |
| | | | | | | | | | |
+---+---+---+ + +---+ +---+ + +
+---+ + +---+ + + + +---+ +---+---+---+ + +---+---+ + + +
| | | | | |
| | | | | | | | | | | | | |
+---+ + +---+---+ + +---+ + +
+ +---+ + +---+ +---+ + + + + +---+---+---+ +---+ +---+ +
| | | | | | | |
| | | | | | | | | | |
+ +---+---+ + + + + + + +
+ + +---+---+ + +---+---+ +---+---+---+ + +---+---+ + + +---+
| | | | | | | | |
| | | | | | | | | | | |
+ + +---+---+ + +---+ + + +
+ +---+ + +---+---+ +---+---+---+ + + + + + +---+ +---+ +
| | | | | | | | |
| | | | | | | | | | | | |
+ + + + +---+---+ + +---+ +
+ + + +---+---+---+ +---+ + +---+ + + + + +---+---+---+ +
| | | | | | |
| | | | | | | | | | | | |
+ + + +---+---+ +---+---+ +---+
+ +---+---+ +---+ + + +---+---+ + +---+ + + + +---+---+---+
| | | | | | | |
| | | | | | | | | | | | |
+ +---+ + + + + + + + +
+---+---+ + + +---+---+ + +---+---+---+ + + + +---+---+ + +
| | | | |
| | | | | |
+---+---+---+---+---+---+---+---+---+---+</pre>
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+</pre>


=={{header|Tcl}}==
=={{header|Tcl}}==