Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Define position. Update robot.position according to user inputs. Add …
…robot and position specs
- Loading branch information
1 parent
b52bc1d
commit 0f842b7
Showing
5 changed files
with
108 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Position class | ||
class Position | ||
attr_accessor :x, :y | ||
|
||
def initialize(x, y) | ||
@x = x | ||
@y = y | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters