Skip to content

Commit

Permalink
Tidy and reorganise code to make more sense. Update specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
SelenaSmall committed Aug 27, 2017
1 parent ceed3ff commit 5047899
Show file tree
Hide file tree
Showing 16 changed files with 226 additions and 240 deletions.
39 changes: 0 additions & 39 deletions lib/actions/move.rb

This file was deleted.

16 changes: 0 additions & 16 deletions lib/actions/place.rb

This file was deleted.

12 changes: 0 additions & 12 deletions lib/actions/report.rb

This file was deleted.

45 changes: 0 additions & 45 deletions lib/actions/turn.rb

This file was deleted.

24 changes: 13 additions & 11 deletions lib/handle_input.rb
@@ -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

Expand All @@ -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
2 changes: 1 addition & 1 deletion lib/position.rb
@@ -1,4 +1,4 @@
# Position class
# Position
class Position
attr_accessor :x, :y, :f

Expand Down
62 changes: 59 additions & 3 deletions lib/robot.rb
@@ -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
7 changes: 3 additions & 4 deletions lib/table.rb
@@ -1,4 +1,4 @@
# Table Class
# Table
class Table
attr_reader :x, :y

Expand All @@ -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
62 changes: 59 additions & 3 deletions spec/handle_input_spec.rb
Expand Up @@ -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
19 changes: 0 additions & 19 deletions spec/move_spec.rb

This file was deleted.

19 changes: 0 additions & 19 deletions spec/place_spec.rb

This file was deleted.

13 changes: 0 additions & 13 deletions spec/report_spec.rb

This file was deleted.

0 comments on commit 5047899

Please sign in to comment.