Class: LocoBot::Robot

Inherits:
Object
  • Object
show all
Defined in:
lib/loco_bot/robot.rb,
lib/loco_bot/robot/direction.rb,
lib/loco_bot/robot/direction/west.rb,
lib/loco_bot/robot/direction/east.rb,
lib/loco_bot/robot/direction/north.rb,
lib/loco_bot/robot/direction/south.rb

Overview

Representation of a robot, placeable on tables.

Defined Under Namespace

Modules: Direction

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#directionDirection (readonly)

Returns the Direction the Robot is currently facing on the Table

Returns:

  • (Direction)

    the Direction the Robot is currently facing on the Table


20
21
22
# File 'lib/loco_bot/robot.rb', line 20

def direction
  @direction
end

#tableTable (readonly)

Returns the Table the Robot is currently placed on

Returns:

  • (Table)

    the Table the Robot is currently placed on


8
9
10
# File 'lib/loco_bot/robot.rb', line 8

def table
  @table
end

#xInteger (readonly)

Returns the x-axis coordinate the Robot is currently at on the Table

Returns:

  • (Integer)

    the x-axis coordinate the Robot is currently at on the Table


12
13
14
# File 'lib/loco_bot/robot.rb', line 12

def x
  @x
end

#yInteger (readonly)

Returns the y-axis coordinate the Robot is currently at on the Table

Returns:

  • (Integer)

    the y-axis coordinate the Robot is currently at on the Table


16
17
18
# File 'lib/loco_bot/robot.rb', line 16

def y
  @y
end

Instance Method Details

#hodor!void

This method returns an undefined value.

Outputs a friendly greating.


85
86
87
# File 'lib/loco_bot/robot.rb', line 85

def hodor!
  puts 'HODOR HODOR !'
end

#moveBoolean

Moves the robot one unit forward in the direction it is currently facing.

Returns:

  • (Boolean)

    true if moving was successful, false otherwise.


43
44
45
46
47
48
49
50
# File 'lib/loco_bot/robot.rb', line 43

def move
  return false unless next_position_valid?

  @x = next_position[:x]
  @y = next_position[:y]

  true
end

#next_positionHash

Returns a Hash containing the next x and y coordinates of the Robot if it moves facing its current direction. The Hash will be empty if the Robot has not been placed.

Examples:

robot.place(1, 2, Direction::West)
robot.next_position # => {x: 0, y: 2}

Returns:

  • (Hash)

    the next x and y coordinates of the Robot.


95
96
97
98
99
# File 'lib/loco_bot/robot.rb', line 95

def next_position
  return {} if table.nil?

  direction.vector(x, y)
end

#next_position_valid?Boolean

Determines if the next position position of the Robot is valid according to its Table.

Returns:

  • (Boolean)

    true if the position is valid, false otherwise.


103
104
105
106
107
# File 'lib/loco_bot/robot.rb', line 103

def next_position_valid?
  return false if table.nil?

  table.position_valid?(next_position[:x], next_position[:y])
end

#place(table, x, y, direction) ⇒ Boolean

Places the Robot on the given Table at the given coordinates, facing the given Direction. If the Robot was previously on another Table, it is removed from it before being placed on the given.

Parameters:

  • table (Table)

    the Table to place the Robot at

  • x (Integer)

    the x-axis coordinate

  • y (Integer)

    the y-axis coordinate

  • direction (Direction)

    the facing Direction

Returns:

  • (Boolean)

    true if placing was successful, false otherwise.


29
30
31
# File 'lib/loco_bot/robot.rb', line 29

def place(table, x, y, direction)
  table.place_robot(self, x, y, direction)
end

#removeBoolean

Removes the Robot from its current Table. Position attributes of the Robot are set to nil.

Returns:

  • (Boolean)

    true if removing was successful, false otherwise.


35
36
37
38
39
# File 'lib/loco_bot/robot.rb', line 35

def remove
  return false if table.nil?

  table.remove_robot(self)
end

#reportHash

Returns a Hash containing the x, y and direction of the robot.

Examples:

robot.place(1, 2, Direction::West)
robot.report # => {x: 1, y: 2, direction: LocoBot::Robot::Direction::West}

Returns:

  • (Hash)

    the x, y and direction of the Robot.


77
78
79
80
81
# File 'lib/loco_bot/robot.rb', line 77

def report
  return {} if table.nil?

  { x: x, y: y, direction: direction }
end

#turn_leftBoolean

Rotates the robot 90 degrees to the left without changing its position.

Returns:

  • (Boolean)

    true if turning was successful, false otherwise.


54
55
56
57
58
59
60
# File 'lib/loco_bot/robot.rb', line 54

def turn_left
  return false if table.nil?

  @direction = direction.left

  true
end

#turn_rightBoolean

Rotates the robot 90 degrees to the right without changing its position.

Returns:

  • (Boolean)

    true if turning was successful, false otherwise.


64
65
66
67
68
69
70
# File 'lib/loco_bot/robot.rb', line 64

def turn_right
  return false if table.nil?

  @direction = direction.right

  true
end