class ToyRoboSimulator::Robo

Robo is the character of this program. It serves to simulate the movement of a robot on a table. The available actions are place, move, left, right, and report. Each action is validated before execution and will respond with the result.

Attributes

orientation[RW]
x[RW]
y[RW]

Public Class Methods

new() click to toggle source

Initializes a Robo instance with a table sizes 5x5.

# File lib/toy_robo_simulator/robo.rb, line 11
def initialize
  @table  = Table.new(5, 5)
  @errors = []
end

Public Instance Methods

left() click to toggle source

Turns left. It changes the Robo's current orientation. This method is noly valid after the Robo is placed.

“` robo = ToyRoboSimulator::Robo.new robo.left # => 'The Robo is not placed yet. Use PLACE command first.'

robo.place(3, 3, :east) # => 'It is placed.' robo.orientation # => :east robo.left # => 'It turns left' robo.orientation # => :north “`

# File lib/toy_robo_simulator/robo.rb, line 76
def left
  return unless placed?
  turn(:left)
  puts 'It turns left.'
end
move() click to toggle source

Moves forward one space. This method is noly valid after the Robo is placed. Facing north will move the Robo one unit toward north. However, no further move is allowed if the Robo is at the edge of a table, which means, for example, its x coordinate is equal to the max value of a Table's width.

“` robo = ToyRoboSimulator::Robo.new robo.move # => 'The Robo is not placed yet. Use PLACE command first.'

robo.place(4, 2, :east) # => 'It is placed.' robo.x # => 4 robo.move # => 'It moves forward.' robo.x # => 5 robo.move # => 'The Robo is at edge. No further move is allowed.' robo.x # => 5 “`

# File lib/toy_robo_simulator/robo.rb, line 58
def move
  return unless placed? && moveable?
  move_forward(1)
  puts 'It moves forward.'
end
place(x, y, orientation) click to toggle source

Places the robo on the table. The x and y arguments indicates its position. The positions shoule be within the range of the table. The orientation is limited to either north, south, west, and east. Any invalid argument will not set the Robo's attributes.

“` robo = ToyRoboSimulator::Robo.new robo.place(1, 2, :north) # => 'It is placed.' robo.x # => 1 robo.y # => 2 robo.orientation # => :north

robo.place(“foo”, “bar”, :south) # => 'X must be a number' # => 'Y must be a number' robo.x # => nil robo.y # => nil “`

# File lib/toy_robo_simulator/robo.rb, line 34
def place(x, y, orientation)
  @x, @y = x, y
  @orientation = orientation.downcase.to_sym
  return unless valid_placement?
  puts 'It is placed.'
end
report() click to toggle source

Reports current postion and orientation. This method is noly valid after the Robo is placed.

“` robo = ToyRoboSimulator::Robo.new robo.report # => 'The Robo is not placed yet. Use PLACE command first.'

robo.place(3, 3, :east) # => 'It is placed.' robo.report # => 'Robo is now at (3,3) facing EAST' “`

# File lib/toy_robo_simulator/robo.rb, line 99
def report
  return unless placed?
  puts "Robo is now at (#{@x},#{@y}) facing #{@orientation.to_s.upcase}"
end
right() click to toggle source

Turns right. See `#left`.

# File lib/toy_robo_simulator/robo.rb, line 83
def right
  return unless placed?
  turn(:right)
  puts 'It turns right'
end

Private Instance Methods

move_forward(unit) click to toggle source
# File lib/toy_robo_simulator/robo.rb, line 117
def move_forward(unit)
  case @orientation
  when :north then @y += unit
  when :east  then @x += unit
  when :south then @y -= unit
  when :west  then @x -= unit
  end
end
turn(direction) click to toggle source
# File lib/toy_robo_simulator/robo.rb, line 111
def turn(direction)
  i            = direction == :left ? 1 : -1
  index        = ORIENTATIONS.index(@orientation) + i
  @orientation = ORIENTATIONS[index] || :north
end
warning() click to toggle source
# File lib/toy_robo_simulator/robo.rb, line 106
def warning
  @errors.each { |message| puts message }
  @errors = []
end