Sha256: 8f73030555e0d21b5ad8a8a7a6c9586108a02342774ab73bec58c49ebbea6c1e

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 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 "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 "exit"
          break
        else
          puts "Invalid command. Please try again."
        end
      end
    end

    default_task :interactive
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
toy_robot_cli-0.1.0 lib/toy_robot_cli/cli.rb