Skip to content

Instantly share code, notes, and snippets.

@DataWraith
Created April 14, 2010 10:12
Show Gist options
  • Save DataWraith/365642 to your computer and use it in GitHub Desktop.
Save DataWraith/365642 to your computer and use it in GitHub Desktop.
Langton Ant written with ruby-processing
# Langton Ant in ruby-processing
class LangtonAnt < Processing::App
WHITE = -1
BLACK = -16777216
DefaultMovements = {
:up => { WHITE => :right, BLACK => :left },
:down => { WHITE => :left, BLACK => :right },
:left => { WHITE => :up, BLACK => :down },
:right => { WHITE => :down, BLACK => :up },
}
DefaultColorChanges = {
WHITE => 0,
BLACK => 255
}
def setup
frame_rate 100
@direction = :up
@x = width / 2
@y = height / 2
background 255
end
def draw
# Step onto the next pixel
case @direction
when :up then
@y = (@y - 1) % height
when :down then
@y = (@y + 1) % height
when :left then
@x = (@x - 1) % width
when :right then
@x = (@x + 1) % width
end
# Change pixel color
prev_color = get(@x,@y)
new_color = DefaultColorChanges[prev_color]
set(@x, @y, color(new_color))
# Set new direction
@direction = DefaultMovements[@direction][prev_color]
end
end
LangtonAnt.new :title => "Langton Ant", :width => 300, :height => 300
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment