Class: ToyRobotCli::CLI

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

Constant Summary collapse

VALID_DIRECTIONS =
%w[NORTH EAST SOUTH WEST].freeze

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



9
10
11
12
# File 'lib/toy_robot_cli/cli.rb', line 9

def initialize(*args)
  super
  @robot = Robot.new
end

Instance Method Details

#current_directionObject



50
51
52
53
54
55
56
57
# File 'lib/toy_robot_cli/cli.rb', line 50

def current_direction
  direction = @robot.current_direction
  if direction
    puts "Current Direction: #{direction}"
  else
    puts "Robot is not placed on the table."
  end
end

#current_positionObject



40
41
42
43
44
45
46
47
# File 'lib/toy_robot_cli/cli.rb', line 40

def current_position
  position = @robot.current_position
  if position
    puts "Current Position: #{position[0]}, #{position[1]}"
  else
    puts "Robot is not placed on the table."
  end
end

#interactiveObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/toy_robot_cli/cli.rb', line 60

def interactive
  loop do
    print "> "
    input = $stdin.gets.chomp.downcase

    command, *args = input.split(" ")

    case command
    when "place"
      place(*args)
    when "move"
      move
    when "left"
      left
    when "right"
      right
    when "report"
      report
    when "current_position"
      current_position
    when "current_direction"
      current_direction
    when "exit"
      break
    else
      puts "Invalid command. Please try again."
    end
  end
end

#leftObject



25
26
27
# File 'lib/toy_robot_cli/cli.rb', line 25

def left
  @robot.left
end

#moveObject



20
21
22
# File 'lib/toy_robot_cli/cli.rb', line 20

def move
  @robot.move
end

#place(x, y, facing) ⇒ Object



15
16
17
# File 'lib/toy_robot_cli/cli.rb', line 15

def place(x, y, facing)
  @robot.place(x.to_i, y.to_i, facing.upcase)
end

#reportObject



35
36
37
# File 'lib/toy_robot_cli/cli.rb', line 35

def report
  puts @robot.report
end

#rightObject



30
31
32
# File 'lib/toy_robot_cli/cli.rb', line 30

def right
  @robot.right
end