Anonymous user
Langton's ant: Difference between revisions
m
→{{header|Swift}}
(add swift) |
|||
Line 4,497:
let HEIGHT = 100
var x:Int
var y:Int
}
Line 4,516 ⟶ 4,511:
let xInc = [0, 1,-1, 0]
let yInc = [-1, 0, 0, 1]
var isBlack:[[Bool]]
var origin:Point
var antPosition = Point(x:0, y:0)
var outOfBounds = false
var antDirection = Direction.East
init(width:Int, height:Int) {
self.origin = Point(x:width / 2, y:height / 2)
self.isBlack = Array(count: width, repeatedValue: Array(count: height, repeatedValue: false))
}
Line 4,533 ⟶ 4,528:
func step() -> Point {
if
println("Ant tried to move while out of bounds.")
exit(0)
}
var ptCur = Point(x:self.antPosition.x + self.origin.x, y:self.antPosition.y + self.origin.y)
let black = self.isBlack[ptCur.x][ptCur.y]
let direction = self.antDirection.rawValue
▲ self.antDirection = self.rightTurn[direction]
self.isBlack[ptCur.x][ptCur.y] = !self.isBlack[ptCur.x][ptCur.y]
self.moveAnt()
ptCur = Point(x:self.antPosition.x + self.origin.x, y:self.antPosition.y + self.origin.y)
self.outOfBounds =
ptCur.x < 0 ||
Line 4,564 ⟶ 4,555:
let ant = Langton(width: WIDTH, height: HEIGHT)
while
ant.step()
}
for
for
print((ant.isBlack[col][row] ? "#" : " "))
}
|