Sha256: 322ceec58bee1dba556b576868d44902e9ca7d77a537193c5a64b44b15022f8b

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

require "thor"

module ToyRobotCli
  class CLI < Thor
    VALID_DIRECTIONS = %w[NORTH EAST SOUTH WEST].freeze

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

    desc "place X Y FACING", "Place the robot on the table"
    def place(x, y, facing)
      @robot.place(x.to_i, y.to_i, facing.upcase)
    end

    desc "move", "Move the robot one unit forward in the current direction"
    def move
      @robot.move
    end

    desc "left", "Rotate the robot 90 degrees to the left"
    def left
      @robot.left
    end

    desc "right", "Rotate the robot 90 degrees to the right"
    def right
      @robot.right
    end

    desc "report", "Report the current position and direction of the robot"
    def report
      puts @robot.report
    end

    desc "current_position", "Return the current position of the robot"
    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

    desc "interactive", "Interactive mode to input commands"
    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 "exit"
          break
        else
          puts "Invalid command. Please try again."
        end
      end
    end

    default_task :interactive
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
toy_robot_cli-0.1.3 lib/toy_robot_cli/cli.rb
toy_robot_cli-0.1.2 lib/toy_robot_cli/cli.rb
toy_robot_cli-0.1.1 lib/toy_robot_cli/cli.rb