diff --git a/direction.rb b/direction.rb index 1daf3b7..f8a83de 100644 --- a/direction.rb +++ b/direction.rb @@ -1,20 +1,32 @@ class Robot - class Direction < String + class Direction ALL_STR = %w(NORTH EAST SOUTH WEST) def self.Direction direction + return direction if direction.is_a? Direction + raise ArgumentError, "#{direction}: unknown direction" \ unless ALL_STR.include? direction Direction.new direction end + attr_reader :name + + def initialize name + @name = name + end + NORTH = Direction 'NORTH' EAST = Direction 'EAST' SOUTH = Direction 'SOUTH' WEST = Direction 'WEST' ALL = [NORTH, EAST, SOUTH, WEST].map! { |d| d.freeze } + def == other + @name == other.name + end + def next case self when NORTH then EAST @@ -25,7 +37,8 @@ def next end def next! - replace self.next + @name = self.next.name + self end def prev @@ -38,7 +51,12 @@ def prev end def prev! - replace self.prev + @name = prev.name + self + end + + def to_s + @name end end end diff --git a/state.rb b/state.rb index 799cc58..870d828 100644 --- a/state.rb +++ b/state.rb @@ -1,5 +1,10 @@ class Robot State = Struct.new(:x, :y, :direction) do + def initialize x, y, direction + self.x, self.y, self.direction = + Integer(x), Integer(y), Direction(direction) + end + def to_s [x, y, direction].join ',' end