Permalink
Browse files

Tidy and reorganise code to make more sense. Update specs.

  • Loading branch information...
SelenaSmall committed Aug 27, 2017
1 parent ceed3ff commit 5047899aa3bff4c6ad856e2122e7cf5299d69b59

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -1,9 +1,4 @@
require_relative './actions/place'
require_relative './actions/move'
require_relative './actions/turn'
require_relative './actions/report'
# HandleInput class
# HandleInput
class HandleInput
attr_accessor :robot, :table
@@ -21,18 +16,25 @@ def initialize(robot, table)
# Interpret user Command
def interpret(command)
return Place.new(robot, table).perform(command) if PLACE.match?(command)
return exec(robot.place(command)) if PLACE.match?(command)
return if robot.not_in_place?
return Move.new(robot, table).perform if MOVE.match?(command)
return exec(robot.move(robot.position)) if MOVE.match?(command)
return Turn.new(robot).left if LEFT.match?(command)
return exec(robot.left(robot.position)) if LEFT.match?(command)
return Turn.new(robot).right if RIGHT.match?(command)
return exec(robot.right(robot.position)) if RIGHT.match?(command)
return Report.new(robot).perform if REPORT.match?(command)
return robot.report(robot.position) if REPORT.match?(command)
# TODO: Exception handler
end
# Update robot position if it's valid
def exec(position)
return unless table.valid_position?(position)
robot.update_robot(position)
end
end
@@ -1,4 +1,4 @@
# Position class
# Position
class Position
attr_accessor :x, :y, :f
@@ -1,20 +1,76 @@
require_relative 'position'
# Robot Class
# Robot
class Robot
attr_reader :position
def intialize
@position = nil
end
# Update the robot's position
def place(command)
_command, x, y, f = command.tr(',', ' ').split
Position.new(x.to_i, y.to_i, f)
end
def move(position)
y = position.y
x = position.x
f = position.f
y += 1 if f.match?('NORTH')
x += 1 if f.match?('EAST')
y -= 1 if f.match?('SOUTH')
x -= 1 if f.match?('WEST')
Position.new(x, y, f)
end
def left(position)
Position.new(position.x, position.y, prev_option(position.f))
end
def right(position)
Position.new(position.x, position.y, next_option(position.f))
end
def report(position)
message = "Output: #{position.x},#{position.y},#{position.f} \n"
$stdout.print message
end
def update_robot(new_position)
@position = new_position
end
# Check the robot has been placed
def not_in_place?
@position.nil?
end
private
OPTIONS = %w[WEST NORTH EAST SOUTH].freeze
def prev_option(direction)
return unless OPTIONS.include?(direction)
return OPTIONS.last if direction == OPTIONS.first
i = OPTIONS.index { |e| e == direction }
i -= 1
OPTIONS.fetch(i)
end
def next_option(direction)
return unless OPTIONS.include?(direction)
return OPTIONS.first if direction == OPTIONS.last
i = OPTIONS.index { |e| e == direction }
i += 1
OPTIONS.fetch(i)
end
end
@@ -1,4 +1,4 @@
# Table Class
# Table
class Table
attr_reader :x, :y
@@ -7,8 +7,7 @@ def initialize(x = 5, y = 5)
@y = (0..(y - 1))
end
# Validate x & y axis are within 5x5 area
def valid_position?(x, y)
@x.cover?(x) && @y.cover?(y)
def valid_position?(position)
@x.cover?(position.x) && @y.cover?(position.y)
end
end
@@ -18,18 +18,74 @@
end
describe '#interpret' do
it 'should return an instance of Position command matches place pattern' do
it 'should return an instance of Position command matches PLACE pattern' do
instance = HandleInput.new(Robot.new, Table.new)
command = 'PLACE 1,2,NORTH'
expect(instance.interpret(command)).to be_a Position
end
it 'should return nil if command does not match a valid pattern' do
it 'should return nil if robot is not in place' do
instance = HandleInput.new(Robot.new, Table.new)
command = 'hello'
command = ''
expect(instance.interpret(command)).to be_nil
end
it 'should return an instance of Position if command matches MOVE pattern' do
instance = HandleInput.new(Robot.new, Table.new)
command1 = 'PLACE 1,2,NORTH'
command2 = 'MOVE'
instance.interpret(command1)
expect(instance.interpret(command2)).to be_a Position
end
it 'should return an instance of Position if command matches LEFT pattern' do
instance = HandleInput.new(Robot.new, Table.new)
command1 = 'PLACE 1,2,NORTH'
command2 = 'LEFT'
instance.interpret(command1)
expect(instance.interpret(command2)).to be_a Position
end
it 'should return an instance of Position if command matches RIGHT pattern' do
instance = HandleInput.new(Robot.new, Table.new)
command1 = 'PLACE 1,2,NORTH'
command2 = 'RIGHT'
instance.interpret(command1)
expect(instance.interpret(command2)).to be_a Position
end
it 'should return nil if command matches REPORT pattern' do
instance = HandleInput.new(Robot.new, Table.new)
command1 = 'PLACE 1,2,NORTH'
command2 = 'REPORT'
instance.interpret(command1)
expect(instance.interpret(command2)).to be_nil
end
end
describe '#exec' do
it 'should return an instance of Position if position is a valid table position' do
instance = HandleInput.new(Robot.new, Table.new)
position = Position.new(0, 0, 'NORTH')
expect(instance.exec(position)).to be_a Position
end
it 'should return nil if position is not a valid table position' do
instance = HandleInput.new(Robot.new, Table.new)
position = Position.new(0, 6, 'NORTH')
expect(instance.exec(position)).to be_nil
end
end
end

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 5047899

Please sign in to comment.