Class: Lemmibot::Bot
- Inherits:
-
Object
- Object
- Lemmibot::Bot
- Defined in:
- lib/lemmibot/bot.rb
Overview
A simulated toy robot on a tabletop
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
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/lemmibot/bot.rb', line 26 def move # Move the bot one unit in the direction it is facing # TODO: Find a more elegant solution for this 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
38 39 40 41 42 43 44 45 |
# File 'lib/lemmibot/bot.rb', line 38 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
47 48 49 50 51 52 53 54 55 |
# File 'lib/lemmibot/bot.rb', line 47 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 22 23 24 |
# File 'lib/lemmibot/bot.rb', line 11 def turn(relative_direction) # Rotate the bot 90 degrees to face another direction # TODO: Find a nicer way to find the new direction return false unless @placed change = if relative_direction == :left -1 else 1 end next_direction_index = DIRECTIONS.index(@direction) + change next_direction_index = 0 if next_direction_index > DIRECTIONS.count - 1 new_direction = DIRECTIONS[next_direction_index] set_direction(new_direction) end |