Langton's ant: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: removed OVERFLOW from PRE html tag.)
(add swift)
Line 4,490: Line 4,490:
say "Out of bounds after #{moves} moves at (#{x}, #{y})";
say "Out of bounds after #{moves} moves at (#{x}, #{y})";
plane.map{.map {|square| square == Black ? '#' : '.' }}.each{.join.say};</lang>
plane.map{.map {|square| square == Black ? '#' : '.' }}.each{.join.say};</lang>
=={{header|Swift}}==
{{trans|C#}}
<lang Swift>import Foundation

let WIDTH = 100
let HEIGHT = 100

class Point {
var x:Int
var y:Int
init(_ x:Int, _ y:Int) {
self.x = x
self.y = y
}
}

enum Direction: Int {
case North = 0, East, West, South
}

class Langton {
let leftTurn = [Direction.West, Direction.North, Direction.South, Direction.East]
let rightTurn = [Direction.East, Direction.South, Direction.North, Direction.West]
let xInc = [0, 1,-1, 0]
let yInc = [-1, 0, 0, 1]
var isBlack:[[Bool]]!
var origin:Point!
var antPosition = Point(0, 0)
var outOfBounds = false
var antDirection = Direction.East
init(width:Int, height:Int) {
self.origin = Point(width / 2, height / 2)
self.isBlack = Array(count: width, repeatedValue: Array(count: height, repeatedValue: false))
}
func moveAnt() {
self.antPosition.x += xInc[self.antDirection.rawValue]
self.antPosition.y += yInc[self.antDirection.rawValue]
}
func step() -> Point {
if (self.outOfBounds) {
println("Ant tried to move while out of bounds.")
exit(0)
}
var ptCur = Point(self.antPosition.x + self.origin.x, self.antPosition.y + self.origin.y)
let black = self.isBlack[ptCur.x][ptCur.y]
let direction = self.antDirection.rawValue
if (black) {
self.antDirection = self.leftTurn[direction]
} else {
self.antDirection = self.rightTurn[direction]
}

self.isBlack[ptCur.x][ptCur.y] = !self.isBlack[ptCur.x][ptCur.y]
self.moveAnt()
ptCur = Point(self.antPosition.x + self.origin.x, self.antPosition.y + self.origin.y)
self.outOfBounds =
ptCur.x < 0 ||
ptCur.x >= self.isBlack.count ||
ptCur.y < 0 ||
ptCur.y >= self.isBlack[0].count
return self.antPosition
}
}


let ant = Langton(width: WIDTH, height: HEIGHT)
while (!ant.outOfBounds) {
ant.step()
}

for (var row = 0; row < WIDTH; row++) {
for (var col = 0; col < HEIGHT; col++) {
print((ant.isBlack[col][row] ? "#" : " "))
}
println()
}</lang>
{{out}}
Blank lines omitted
<pre> # #
## # #
# ### ##
#### ### #
##### # ##
# ## ## #
### # ##
# ## ## #
### # ##
# ## ## #
### # ##
# ## ## #
### # ##
# ## ## #
### # ##
# ## ## #
### # ##
# ## ## #
### # ##
# ## ## #
### # ##
# ## ## #
### # ##
# ## ## #
### # ##
# ## ## #
### # ##
# ## ## #
### # ##
# ## ## #
### # ##
# ## ## # ##
### # ## ##
# ## ## ## #
#### ### # # ###
# # # ## #### #
### # # # # ## #
### # ## # ## # ##
# # ## # # ##
# # # ##### # #
# ##### ## ######
### ## # ## # # # ## # ##
## # ####### # # ### ## #
# # ###### ## # # ## # #
# # # ## # ###### ####### #
# #### ## # #### ## ## # ## #
# #### # # ###### ## ###
# # ## # ### # ## ## ###
####### # ## ## # #
#### ## ## #### ## ## ## # #
# # # ### ## ### # #### #
### ### # # ##### # # #
# # ### #### ## # ## ### ## #
## ## #### #### # # # #
# # ## ### ### ### #
## ## ### #### # ### ## #
## # #### # # # ## ### ## #
#### ## ## #### # # # # ### #
# ## ### # # ## # # # # # #
# # # ## ## # # ### ##
## # # ##### # # # # #
# ## # # ## ## # ### ###
# # # # # # ### ## ## #
### # ##### ###### ### ####### # ##
# # # ##### ## ##### #####
# ## # # # ## ### ###
#### ##### ######### # #
## # # ### # # # ### ###
# # #### ## ### ## ### ## ##
### # ## # ##### # # # ## ###
# ##### # # ## ## # # # #
###### #### ## # # ## # # ##
## # ### ## #### # ###
# # ##### # # ## # # #
## ### ####### # # ##
# # ## ## # ## #
# # #### ### ## #
# ## ### ## ##
##
##
</pre>


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