Skip to content

Commit

Permalink
Rename Rover Position class to Position Class
Browse files Browse the repository at this point in the history
A position in itself can have x & y coordinates(in terms of being applicable to a two dimensional grid system) along with an orientation(i.e., one of the 4 cardinal compass points) and the ability for it to be applied to a given terrain, hence a generic Position class feels more appropriate here than a specific RoverPosition class.

Also it makes sense that a Rover is composed of a Position and it needn't only have to be restricted to a Rover is composed of a RoverPosition and also in retrospect, the former sounds better.
  • Loading branch information
boddhisattva committed Jun 24, 2024
1 parent 39c7573 commit 3b670b4
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 32 deletions.
6 changes: 3 additions & 3 deletions bin/deploy_mars_rover.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require_relative '../lib/rover'
require_relative '../lib/rover_position'
require_relative '../lib/position'
require_relative '../lib/path'
require_relative '../lib/rectangular_plateau'
require 'pry' # Handy for debugging purposes
Expand All @@ -11,8 +11,8 @@
path1 = Path.new('LMLMLMLMM')
path2 = Path.new('MMRMMRMRRM')

rover1 = Rover.new(RoverPosition.new(1, 2, 'N', terrain))
rover2 = Rover.new(RoverPosition.new(3, 3, 'E', terrain))
rover1 = Rover.new(Position.new(1, 2, 'N', terrain))
rover2 = Rover.new(Position.new(3, 3, 'E', terrain))

rover1.traverse(path1)
puts rover1.current_position.location
Expand Down
2 changes: 1 addition & 1 deletion lib/rover_position.rb → lib/position.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class OutsideBoundaryLimitsError < StandardError; end

class RoverPosition
class Position
def initialize(x_coordinate, y_coordinate, orientation, terrain)
@x_coordinate = x_coordinate
@y_coordinate = y_coordinate
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/east_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
describe '#move' do
context 'given an entity points in the East direction' do
it 'moves a step forward horizontally along that path(i.e., along the x-axis)' do
current_position = RoverPosition.new(1, 3, 'E', RectangularPlateau.new(0, 0, 5, 5))
current_position = Position.new(1, 3, 'E', RectangularPlateau.new(0, 0, 5, 5))

east.move(current_position)

Expand Down
2 changes: 1 addition & 1 deletion spec/lib/north_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
describe '#move' do
context 'given an entity points in the North direction' do
it 'moves a step upward vertically along that path(i.e., along the y-axis)' do
current_position = RoverPosition.new(1, 3, 'N', RectangularPlateau.new(0, 0, 5, 5))
current_position = Position.new(1, 3, 'N', RectangularPlateau.new(0, 0, 5, 5))

north.move(current_position)

Expand Down
24 changes: 12 additions & 12 deletions spec/lib/rover_position_spec.rb → spec/lib/position_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# frozen_string_literal: true

describe RoverPosition do
describe Position do
let(:terrain) { RectangularPlateau.new(0, 0, 5, 5) }

describe '#move_vertically_upward' do
context 'movement attempted within terrain boundary limits' do
it 'allows one to go further up the Y axis(imagine a 2D Grid system) by one unit' do
current_position = RoverPosition.new(1, 3, 'N', terrain)
current_position = Position.new(1, 3, 'N', terrain)

current_position.move_vertically_upward

Expand All @@ -17,7 +17,7 @@

context 'movement attempted outside terrain boundary limits' do
it 'returns an outside boundary limits error with related message' do
current_position = RoverPosition.new(1, 5, 'N', terrain)
current_position = Position.new(1, 5, 'N', terrain)

expect do
current_position.move_vertically_upward
Expand All @@ -29,7 +29,7 @@
describe '#move_vertically_downward' do
context 'movement attempted within terrain boundary limits' do
it 'allows one to go further down the Y axis(imagine a 2D Grid system) by one unit' do
current_position = RoverPosition.new(1, 3, 'S', terrain)
current_position = Position.new(1, 3, 'S', terrain)

current_position.move_vertically_downward

Expand All @@ -40,7 +40,7 @@

context 'movement attempted outside terrain boundary limits' do
it 'returns an outside boundary limits error with related message' do
current_position = RoverPosition.new(1, 0, 'S', terrain)
current_position = Position.new(1, 0, 'S', terrain)

expect do
current_position.move_vertically_downward
Expand All @@ -52,7 +52,7 @@
describe '#move_horizontally_forward' do
context 'movement attempted within terrain boundary limits' do
it 'allows one to go a step further horizontally on the X axis(imagine a 2D Grid system) by one unit' do
current_position = RoverPosition.new(1, 3, 'E', terrain)
current_position = Position.new(1, 3, 'E', terrain)

current_position.move_horizontally_forward

Expand All @@ -63,7 +63,7 @@

context 'movement attempted outside terrain boundary limits' do
it 'returns an outside boundary limits error with related message' do
current_position = RoverPosition.new(5, 3, 'E', terrain)
current_position = Position.new(5, 3, 'E', terrain)

expect do
current_position.move_horizontally_forward
Expand All @@ -75,7 +75,7 @@
describe '#move_horizontally_backward' do
context 'movement attempted within terrain boundary limits' do
it 'allows one to go a step backward horizontally on the X axis(imagine a 2D Grid system) by one unit' do
current_position = RoverPosition.new(1, 3, 'W', terrain)
current_position = Position.new(1, 3, 'W', terrain)

current_position.move_horizontally_backward

Expand All @@ -86,7 +86,7 @@

context 'movement attempted outside terrain boundary limits' do
it 'returns an outside boundary limits error with related message' do
current_position = RoverPosition.new(0, 5, 'W', terrain)
current_position = Position.new(0, 5, 'W', terrain)

expect do
current_position.move_horizontally_backward
Expand All @@ -97,15 +97,15 @@

describe '#get_orientation' do
it 'returns the current compass based position of a given entity' do
current_position = RoverPosition.new(1, 3, 'W', terrain)
current_position = Position.new(1, 3, 'W', terrain)

expect(current_position.get_orientation).to eq('W')
end
end

describe '#set_orientation' do
it 'sets a compass based position for a given entity' do
current_position = RoverPosition.new(1, 3, 'W', terrain)
current_position = Position.new(1, 3, 'W', terrain)

current_position.set_orientation('N')

Expand All @@ -115,7 +115,7 @@

describe '#location' do
it 'returns the current location of a given entity' do
current_position = RoverPosition.new(1, 3, 'W', terrain)
current_position = Position.new(1, 3, 'W', terrain)

expect(current_position.location).to eq('1 3 W')
end
Expand Down
16 changes: 8 additions & 8 deletions spec/lib/rectangular_plateau_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
context 'when an entity wants to move vertically upward along the Y-axis' do
context 'movement is within terrain boundary limits' do
it 'returns true' do
current_position = RoverPosition.new(1, 3, 'N', terrain)
current_position = Position.new(1, 3, 'N', terrain)

expect(terrain.upward_vertical_movement_permissible?(current_position.y_coordinate)).to be true
end
end

context 'movement is outside terrain boundary limits' do
it 'returns false' do
current_position = RoverPosition.new(1, 5, 'N', terrain)
current_position = Position.new(1, 5, 'N', terrain)

expect(terrain.upward_vertical_movement_permissible?(current_position.y_coordinate)).to be false
end
Expand All @@ -27,15 +27,15 @@
context 'when an entity wants to move vertically downward along the Y-axis' do
context 'movement is within terrain boundary limits' do
it 'returns true' do
current_position = RoverPosition.new(1, 3, 'S', terrain)
current_position = Position.new(1, 3, 'S', terrain)

expect(terrain.downward_vertical_movement_permissible?(current_position.y_coordinate)).to be true
end
end

context 'movement is outside terrain boundary limits' do
it 'returns false' do
current_position = RoverPosition.new(1, 0, 'S', terrain)
current_position = Position.new(1, 0, 'S', terrain)

expect(terrain.downward_vertical_movement_permissible?(current_position.y_coordinate)).to be false
end
Expand All @@ -47,15 +47,15 @@
context 'when an entity wants to move horizontally forward along the X-axis' do
context 'movement is within terrain boundary limits' do
it 'returns true' do
current_position = RoverPosition.new(1, 3, 'E', terrain)
current_position = Position.new(1, 3, 'E', terrain)

expect(terrain.forward_horizontal_movement_permissible?(current_position.x_coordinate)).to be true
end
end

context 'movement is outside terrain boundary limits' do
it 'returns false' do
current_position = RoverPosition.new(5, 3, 'E', terrain)
current_position = Position.new(5, 3, 'E', terrain)

expect(terrain.forward_horizontal_movement_permissible?(current_position.x_coordinate)).to be false
end
Expand All @@ -67,15 +67,15 @@
context 'when an entity wants to move horizontally backward along the X-axis' do
context 'movement is within terrain boundary limits' do
it 'returns true' do
current_position = RoverPosition.new(1, 3, 'W', terrain)
current_position = Position.new(1, 3, 'W', terrain)

expect(terrain.backward_horizontal_movement_permissible?(current_position.x_coordinate)).to be true
end
end

context 'movement is outside terrain boundary limits' do
it 'returns false' do
current_position = RoverPosition.new(0, 5, 'W', terrain)
current_position = Position.new(0, 5, 'W', terrain)

expect(terrain.backward_horizontal_movement_permissible?(current_position.x_coordinate)).to be false
end
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/rover_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
path1 = Path.new('LMLMLMLMM')
path2 = Path.new('MMRMMRMRRML')

rover1 = Rover.new(RoverPosition.new(1, 2, 'N', terrain))
rover1 = Rover.new(Position.new(1, 2, 'N', terrain))
rover1.traverse(path1)

rover2 = Rover.new(RoverPosition.new(1, 2, 'E', terrain))
rover2 = Rover.new(Position.new(1, 2, 'E', terrain))
rover2.traverse(path2)

expect(rover1.current_position.location).to eq('1 3 N')
Expand All @@ -24,15 +24,15 @@
it 'returns an appropriate error message related to an invalid step' do
path1 = Path.new('LMLMLSMLMM')

rover1 = Rover.new(RoverPosition.new(1, 2, 'N', terrain))
rover1 = Rover.new(Position.new(1, 2, 'N', terrain))
expect { rover1.traverse(path1) }.to raise_error('Invalid step in path')
end
end

context 'when rover movement is attempted outside terrain boundary limits' do
it 'returns an outside boundary limits error with related message' do
path1 = Path.new('LMLM')
rover1 = Rover.new(RoverPosition.new(5, 0, 'S', terrain))
rover1 = Rover.new(Position.new(5, 0, 'S', terrain))

expect do
rover1.traverse(path1)
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/south_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
describe '#move' do
context 'given an entity points in the South direction' do
it 'moves a step downward vertically along that path(i.e., along the y-axis)' do
current_position = RoverPosition.new(1, 3, 'S', RectangularPlateau.new(0, 0, 5, 5))
current_position = Position.new(1, 3, 'S', RectangularPlateau.new(0, 0, 5, 5))

south.move(current_position)

Expand Down
2 changes: 1 addition & 1 deletion spec/lib/west_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
describe '#move' do
context 'given an entity points in the West direction' do
it 'moves a step backward horizontally along that path(i.e., along the x-axis)' do
current_position = RoverPosition.new(1, 3, 'W', RectangularPlateau.new(0, 0, 5, 5))
current_position = Position.new(1, 3, 'W', RectangularPlateau.new(0, 0, 5, 5))

west.move(current_position)

Expand Down

0 comments on commit 3b670b4

Please sign in to comment.