Class: Lemmibot::Bot
- Inherits:
-
Object
- Object
- Lemmibot::Bot
- Defined in:
- lib/lemmibot/bot.rb
Overview
A simulated toy robot, which can be placed on a tabletop and given commands
Instance Method Summary collapse
-
#initialize ⇒ Bot
constructor
A new instance of Bot.
- #move ⇒ Object
- #place(x, y, direction) ⇒ Object
- #report ⇒ Object
- #turn(relative_direction) ⇒ Object
Constructor Details
#initialize ⇒ Bot
Returns a new instance of Bot.
4 5 6 7 8 9 |
# File 'lib/lemmibot/bot.rb', line 4 def initialize @pos_x = 0 @pos_y = 0 @direction = :north @placed = false end |
Instance Method Details
#move ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/lemmibot/bot.rb', line 23 def move # Move the bot one unit in the direction it is facing return false unless @placed case @direction when :north then return change_position(:y, 1) when :south then return change_position(:y, -1) when :east then return change_position(:x, 1) when :west then return change_position(:x, -1) end end |
#place(x, y, direction) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/lemmibot/bot.rb', line 34 def place(x, y, direction) # Place the bot at a specified position, facing specified direction return false unless set_position(:x, x) && set_position(:y, y) && set_direction(direction) @placed = true true end |
#report ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/lemmibot/bot.rb', line 43 def report # Return a hash of the bot's current location and direction return false unless @placed { x: @pos_x, y: @pos_y, dir: @direction } end |
#turn(relative_direction) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/lemmibot/bot.rb', line 11 def turn(relative_direction) # Rotate the bot 90 degrees to face another direction return false unless @placed case relative_direction when :left then change = -1 when :right then change = 1 end current_direction_index = DIRECTIONS.index(@direction) new_direction = DIRECTIONS.rotate(current_direction_index + change).first set_direction(new_direction) end |