Skip to content

Commit 0f842b7

Browse files
committed
Define position. Update robot.position according to user inputs. Add robot and position specs
1 parent b52bc1d commit 0f842b7

File tree

5 files changed

+108
-11
lines changed

5 files changed

+108
-11
lines changed

lib/position.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Position class
2+
class Position
3+
attr_accessor :x, :y
4+
5+
def initialize(x, y)
6+
@x = x
7+
@y = y
8+
end
9+
end

lib/robot.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
1+
require_relative 'position'
2+
13
# Robot Class
24
class Robot
35
attr_reader :position
46

57
def intialize
68
@position = nil
79
end
10+
11+
# Place the robot
12+
def place(command)
13+
place = /^PLACE\s+\d+\s*,\s*\d+\s*$/
14+
15+
if place.match?(command)
16+
command, x, y = command.tr(',', ' ').split
17+
18+
Position.new(x.to_i, y.to_i)
19+
end
20+
end
21+
22+
# Update the robot's position
23+
def update_robot(new_position)
24+
@position = new_position
25+
end
26+
27+
# Check the robot has been placed
28+
def not_in_place?
29+
@position.nil?
30+
end
831
end

spec/position_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'rspec'
2+
require 'spec_helper'
3+
require './lib/position'
4+
5+
describe Position do
6+
describe '#initialize' do
7+
it 'should have an x attribute which is an integer' do
8+
instance = Position.new(0, 0)
9+
10+
expect(instance.x).to be_a Integer
11+
expect(instance.x).to be >= 0
12+
expect(instance.x).to be <= 4
13+
end
14+
15+
it 'should have a y attribute which is an integer' do
16+
instance = Position.new(0, 0)
17+
18+
expect(instance.y).to be_a Integer
19+
expect(instance.y).to be >= 0
20+
expect(instance.y).to be <= 4
21+
end
22+
end
23+
end

spec/robot_spec.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,51 @@
1010
expect(instance.position).to be_nil
1111
end
1212
end
13+
14+
describe '#place' do
15+
it 'should return an instance of Position' do
16+
instance = Robot.new
17+
command = 'PLACE 1,2'
18+
19+
expect(instance.place(command)).to be_a Position
20+
end
21+
22+
it 'should return nil if command is empty' do
23+
instance = Robot.new
24+
command = ''
25+
26+
expect(instance.place(command)).to be_nil
27+
end
28+
29+
it 'should return nil if command does not match the place pattern' do
30+
instance = Robot.new
31+
command = 'get'
32+
33+
expect(instance.place(command)).to be_nil
34+
end
35+
end
36+
37+
describe '#update_robot' do
38+
it 'should return an instance of Position' do
39+
instance = Robot.new
40+
new_position = Position.new(0, 1)
41+
42+
expect(instance.update_robot(new_position)).to be_a Position
43+
end
44+
45+
it 'should return false if new_position is nil' do
46+
instance = Robot.new
47+
new_position = nil
48+
49+
expect(instance.update_robot(new_position)).to be_nil
50+
end
51+
end
52+
53+
describe '#not_in_place?' do
54+
it 'should return true if position attribute is nil' do
55+
instance = Robot.new
56+
57+
expect(instance.not_in_place?).to be true
58+
end
59+
end
1360
end

toy_robot.rb

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
#!/usr/bin/ruby
22

3-
require_relative './lib/table'
43
require_relative './lib/robot'
54

6-
# Initialize Table objects
7-
table = Table.new
85
robot = Robot.new
96

10-
11-
# Is this a valid position on the table
12-
puts table.valid_position?(1, 0)
13-
14-
# Ensure the robot's position is nil
15-
puts 'Robot position is nil' if robot.position.nil?
16-
177
# Keep reading user inputs while the program is running
188
loop do
199
puts 'Select your command'
2010
input = gets.chomp
2111

2212
unless input == 'EXIT'
23-
puts 'Robot has been placed' if input == 'PLACE'
13+
position = robot.place(input)
14+
puts "Position: #{position.x},#{position.y}"
15+
16+
robot.update_robot(position)
17+
18+
puts 'placed' unless robot.not_in_place?
2419
next
2520
end
2621

0 commit comments

Comments
 (0)