Class: ToyRobotCli::Robot

Inherits:
Object
  • Object
show all
Defined in:
lib/toy_robot_cli/robot.rb

Overview

Ruby class to handle Robot moves on square table top

Constant Summary collapse

VALID_DIRECTIONS =

Valid directions what robot can be faced

%w[NORTH EAST SOUTH WEST].freeze

Instance Method Summary collapse

Constructor Details

#initializeRobot

Initialize a new Robot instance.



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

def initialize
  @position = nil
  @direction = nil
end

Instance Method Details

#current_directionObject

Get the current direction of the robot. Example:

>> robot = ToyRobotCli::Robot.new
=> #<ToyRobotCli::Robot:0x000000... @direction=nil, @position=nil>
>> robot.place(0, 0, "SOUTH")
=> 2
>> robot.current_direction
=> "SOUTH"

Returns:

(String) - An array containing the X and Y coordinates of the robot's current position.


63
64
65
66
67
# File 'lib/toy_robot_cli/robot.rb', line 63

def current_direction
  return nil unless placed?

  VALID_DIRECTIONS[@direction]
end

#current_positionObject

Get the current position (X, Y) of the robot. Example:

>> robot = ToyRobotCli::Robot.new
=> #<ToyRobotCli::Robot:0x000000... @direction=nil, @position=nil>
>> robot.place(0, 0, "SOUTH")
=> 2
>> robot.current_position
=> [0, 0]

Returns:

(Array) - An array containing the X and Y coordinates of the robot's current position.


46
47
48
49
50
# File 'lib/toy_robot_cli/robot.rb', line 46

def current_position
  return nil unless placed?

  @position.to_a.flatten
end

#leftObject

Rotate the robot 90 degrees to the left.



78
79
80
81
82
# File 'lib/toy_robot_cli/robot.rb', line 78

def left
  return unless placed?

  @direction = (@direction - 1) % VALID_DIRECTIONS.length
end

#moveObject

Move the robot one unit forward in the current direction.



70
71
72
73
74
75
# File 'lib/toy_robot_cli/robot.rb', line 70

def move
  return unless placed?

  new_position = @position + direction_vector
  @position = new_position if valid_position?(new_position[0, 0], new_position[1, 0])
end

#place(coordinate_x, coordinate_y, facing) ⇒ Object

Place the robot on the table at the specified coordinates and facing direction. Example:

>> robot = ToyRobotCli::Robot.new
=> #<ToyRobotCli::Robot:0x000000... @direction=nil, @position=nil>
>> robot.place(0, 0, "SOUTH")
=> 2

Arguments:

coordinate_x: (Integer) - The X-coordinate of the robot's position.
coordinate_y: (Integer) - The Y-coordinate of the robot's position.
facing: (String) - The direction the robot is facing (NORTH, EAST, SOUTH, WEST).


28
29
30
31
32
33
# File 'lib/toy_robot_cli/robot.rb', line 28

def place(coordinate_x, coordinate_y, facing)
  return unless valid_position?(coordinate_x, coordinate_y) && VALID_DIRECTIONS.include?(facing)

  @position = Matrix[[coordinate_x], [coordinate_y]]
  @direction = VALID_DIRECTIONS.index(facing)
end

#reportObject

Report the current position and direction of the robot. Example:

>> robot = ToyRobotCli::Robot.new
=> #<ToyRobotCli::Robot:0x000000... @direction=nil, @position=nil>
>> robot.place(0, 0, "SOUTH")
=> 2
>> robot.report
=> "0,0,SOUTH"

Returns:

(String) - The current position and direction of the robot in the format "X,Y,FACING".


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

def report
  return unless placed?

  coordinate_x, coordinate_y = @position.to_a.flatten
  "#{coordinate_x},#{coordinate_y},#{VALID_DIRECTIONS[@direction]}"
end

#rightObject

Rotate the robot 90 degrees to the right.



85
86
87
88
89
# File 'lib/toy_robot_cli/robot.rb', line 85

def right
  return unless placed?

  @direction = (@direction + 1) % VALID_DIRECTIONS.length
end