Class: TresBot::Robot
- Inherits:
-
Object
- Object
- TresBot::Robot
- Defined in:
- lib/tres_bot.rb
Instance Attribute Summary collapse
-
#current_position ⇒ Object
Returns the value of attribute current_position.
-
#facing ⇒ Object
Returns the value of attribute facing.
-
#is_on_the_table ⇒ Object
Returns the value of attribute is_on_the_table.
Instance Method Summary collapse
- #compass(dir) ⇒ Object
-
#initialize ⇒ Robot
constructor
A new instance of Robot.
- #left ⇒ Object
- #move ⇒ Object
- #place(position) ⇒ Object
- #report ⇒ Object
- #right ⇒ Object
Constructor Details
#initialize ⇒ Robot
Returns a new instance of Robot.
12 13 14 15 16 |
# File 'lib/tres_bot.rb', line 12 def initialize self.current_position = [0,0] self.facing = 'NORTH' self.is_on_the_table = false end |
Instance Attribute Details
#current_position ⇒ Object
Returns the value of attribute current_position.
8 9 10 |
# File 'lib/tres_bot.rb', line 8 def current_position @current_position end |
#facing ⇒ Object
Returns the value of attribute facing.
9 10 11 |
# File 'lib/tres_bot.rb', line 9 def facing @facing end |
#is_on_the_table ⇒ Object
Returns the value of attribute is_on_the_table.
10 11 12 |
# File 'lib/tres_bot.rb', line 10 def is_on_the_table @is_on_the_table end |
Instance Method Details
#compass(dir) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/tres_bot.rb', line 34 def compass(dir) dir = dir.downcase axis = ['north','south'].include?(dir) ? 'y':'x' operator = ['north','east'].include?(dir) ? '+' : '-' [axis,operator] end |
#left ⇒ Object
29 30 31 32 |
# File 'lib/tres_bot.rb', line 29 def left c = ::CircularList::List.new(['NORTH','EAST','SOUTH','WEST']) self.facing = c.fetch_previous(c.index(self.facing)) end |
#move ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/tres_bot.rb', line 41 def move calibration = self.compass(self.facing) axis = calibration[0] operator = calibration[1] if axis == 'x' unless (self.current_position[0] == 0 && operator == '-') || (self.current_position[0] == 4 && operator == '+') self.current_position[0]=self.current_position[0].to_i.send(operator,1) end else unless (self.current_position[1] == 0 && operator == '-') || (self.current_position[1] == 4 && operator == '+') self.current_position[1]=self.current_position[1].to_i.send(operator,1) end end end |
#place(position) ⇒ Object
18 19 20 21 22 |
# File 'lib/tres_bot.rb', line 18 def place(position) self.is_on_the_table = true self.current_position = [position[0],position[1]] self.facing = position[2].upcase end |
#report ⇒ Object
56 57 58 59 60 |
# File 'lib/tres_bot.rb', line 56 def report rep = "#{self.current_position.join(', ')}, #{self.facing}" puts rep rep end |
#right ⇒ Object
24 25 26 27 |
# File 'lib/tres_bot.rb', line 24 def right c = ::CircularList::List.new(['NORTH','EAST','SOUTH','WEST']) self.facing = c.fetch_next(c.index(self.facing)) end |