Permalink
Browse files

Define position. Update robot.position according to user inputs. Add …

…robot and position specs
  • Loading branch information...
SelenaSmall committed Aug 26, 2017
1 parent b52bc1d commit 0f842b78c4f83ba5d779937653bd6b972e70b0c4
Showing with 108 additions and 11 deletions.
  1. +9 −0 lib/position.rb
  2. +23 −0 lib/robot.rb
  3. +23 −0 spec/position_spec.rb
  4. +47 −0 spec/robot_spec.rb
  5. +6 −11 toy_robot.rb
@@ -0,0 +1,9 @@
# Position class
class Position
attr_accessor :x, :y
def initialize(x, y)
@x = x
@y = y
end
end
@@ -1,8 +1,31 @@
require_relative 'position'
# Robot Class
class Robot
attr_reader :position
def intialize
@position = nil
end
# Place the robot
def place(command)
place = /^PLACE\s+\d+\s*,\s*\d+\s*$/
if place.match?(command)
command, x, y = command.tr(',', ' ').split
Position.new(x.to_i, y.to_i)
end
end
# Update the robot's position
def update_robot(new_position)
@position = new_position
end
# Check the robot has been placed
def not_in_place?
@position.nil?
end
end
@@ -0,0 +1,23 @@
require 'rspec'
require 'spec_helper'
require './lib/position'
describe Position do
describe '#initialize' do
it 'should have an x attribute which is an integer' do
instance = Position.new(0, 0)
expect(instance.x).to be_a Integer
expect(instance.x).to be >= 0
expect(instance.x).to be <= 4
end
it 'should have a y attribute which is an integer' do
instance = Position.new(0, 0)
expect(instance.y).to be_a Integer
expect(instance.y).to be >= 0
expect(instance.y).to be <= 4
end
end
end
@@ -10,4 +10,51 @@
expect(instance.position).to be_nil
end
end
describe '#place' do
it 'should return an instance of Position' do
instance = Robot.new
command = 'PLACE 1,2'
expect(instance.place(command)).to be_a Position
end
it 'should return nil if command is empty' do
instance = Robot.new
command = ''
expect(instance.place(command)).to be_nil
end
it 'should return nil if command does not match the place pattern' do
instance = Robot.new
command = 'get'
expect(instance.place(command)).to be_nil
end
end
describe '#update_robot' do
it 'should return an instance of Position' do
instance = Robot.new
new_position = Position.new(0, 1)
expect(instance.update_robot(new_position)).to be_a Position
end
it 'should return false if new_position is nil' do
instance = Robot.new
new_position = nil
expect(instance.update_robot(new_position)).to be_nil
end
end
describe '#not_in_place?' do
it 'should return true if position attribute is nil' do
instance = Robot.new
expect(instance.not_in_place?).to be true
end
end
end
@@ -1,26 +1,21 @@
#!/usr/bin/ruby
require_relative './lib/table'
require_relative './lib/robot'
# Initialize Table objects
table = Table.new
robot = Robot.new
# Is this a valid position on the table
puts table.valid_position?(1, 0)
# Ensure the robot's position is nil
puts 'Robot position is nil' if robot.position.nil?
# Keep reading user inputs while the program is running
loop do
puts 'Select your command'
input = gets.chomp
unless input == 'EXIT'
puts 'Robot has been placed' if input == 'PLACE'
position = robot.place(input)
puts "Position: #{position.x},#{position.y}"
robot.update_robot(position)
puts 'placed' unless robot.not_in_place?
next
end

0 comments on commit 0f842b7

Please sign in to comment.