Skip to content

Commit

Permalink
move attr_writers to readers. Cleanup redudantness
Browse files Browse the repository at this point in the history
  • Loading branch information
josephwilk committed Sep 4, 2009
1 parent 64f869e commit 2672aeb
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions ruby/ant/ant.rb
@@ -1,10 +1,10 @@
require 'forwardable'

class LangtonAnt
class LangtonAnt
extend Forwardable

class Ant
attr_accessor :direction, :position
attr_reader :direction, :position

CLOCKWISE = {
:north => :east,
Expand All @@ -14,7 +14,7 @@ class Ant
}

ANTI_CLOCKWISE = CLOCKWISE.invert

def initialize(cordinates = [0,0], direction = :north)
@direction = direction
@position = cordinates
Expand All @@ -23,7 +23,7 @@ def initialize(cordinates = [0,0], direction = :north)
def turn_left
@direction = ANTI_CLOCKWISE[@direction]
end

def turn_right
@direction = CLOCKWISE[@direction]
end
Expand All @@ -36,37 +36,37 @@ def move
def move_delta
case @direction
when :north
delta = [0,-1]
[0,-1]
when:south
delta = [0,1]
[0,1]
when :west
delta = [-1,0]
[-1,0]
else
delta = [1,0]
[1,0]
end
end

def coordinate_add(a)
[a[0] + @position[0], a[1] + @position[1]]
end
end

attr_reader :ant

def initialize(ant = Ant.new)
@ant = ant
@color = {}
@ant = ant
@color = {}
@default_color = :black
end
end

def []=(x,y,color)
@color["#{x},#{y}"] = color
end
end

def [](x, y)
@color["#{x},#{y}"] || @default_color
end

def poll
self.[]=(@ant.position[0], @ant.position[1], flip(ant_color))
if ant_color == :black
Expand All @@ -85,5 +85,5 @@ def ant_color
def flip(color)
color == :black ? :white : :black
end

end

0 comments on commit 2672aeb

Please sign in to comment.