RosettaCodeData/Task/Langtons-ant/11l/langtons-ant.11l
2023-07-01 13:44:08 -04:00

62 lines
1.5 KiB
Text
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

T.enum Dir
UP
RIGHT
DOWN
LEFT
-V color_WHITE = Char( )
-V color_BLACK = Char(#)
F invert_color(&grid, x, y)
Invert the color of grid at x, y coordinate.
I grid[y][x] == :color_BLACK
grid[y][x] = :color_WHITE
E
grid[y][x] = :color_BLACK
F next_direction(grid, x, y, =direction)
Compute next direction according to current position and direction.
V turn_right = grid[y][x] != :color_BLACK
V direction_index = Int(direction)
I turn_right
direction_index = (direction_index + 1) % 4
E
direction_index = (direction_index + 4 - 1) % 4
V directions = [Dir.UP, Dir.RIGHT, Dir.DOWN, Dir.LEFT]
direction = directions[direction_index]
R direction
F next_position(=x, =y, direction)
Compute next position according to direction.
I direction == UP
y--
E I direction == RIGHT
x--
E I direction == DOWN
y++
E I direction == LEFT
x++
R (x, y)
F print_grid(grid)
Display grid.
print(80 * #)
print(grid.map(row -> row.join()).join("\n"))
F ant(width, height, max_nb_steps)
Langton's ant.
V grid = [[:color_WHITE] * width] * height
V x = width I/ 2
V y = height I/ 2
V direction = Dir.UP
V i = 0
L i < max_nb_steps & x C 0 .< width & y C 0 .< height
invert_color(&grid, x, y)
direction = next_direction(grid, x, y, direction)
(x, y) = next_position(x, y, direction)
i++
print_grid(grid)
ant(width' 75, height' 52, max_nb_steps' 12000)