From 3b670b41ba14ad6046b40a3342d8ff7e7cbe12d9 Mon Sep 17 00:00:00 2001 From: Mohnish G J Date: Mon, 24 Jun 2024 13:52:59 +0200 Subject: [PATCH] Rename Rover Position class to Position Class 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. --- bin/deploy_mars_rover.rb | 6 ++--- lib/{rover_position.rb => position.rb} | 2 +- spec/lib/east_spec.rb | 2 +- spec/lib/north_spec.rb | 2 +- ...over_position_spec.rb => position_spec.rb} | 24 +++++++++---------- spec/lib/rectangular_plateau_spec.rb | 16 ++++++------- spec/lib/rover_spec.rb | 8 +++---- spec/lib/south_spec.rb | 2 +- spec/lib/west_spec.rb | 2 +- 9 files changed, 32 insertions(+), 32 deletions(-) rename lib/{rover_position.rb => position.rb} (98%) rename spec/lib/{rover_position_spec.rb => position_spec.rb} (83%) diff --git a/bin/deploy_mars_rover.rb b/bin/deploy_mars_rover.rb index e11130c..dfc899c 100644 --- a/bin/deploy_mars_rover.rb +++ b/bin/deploy_mars_rover.rb @@ -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 @@ -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 diff --git a/lib/rover_position.rb b/lib/position.rb similarity index 98% rename from lib/rover_position.rb rename to lib/position.rb index 125d990..12a6eb8 100644 --- a/lib/rover_position.rb +++ b/lib/position.rb @@ -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 diff --git a/spec/lib/east_spec.rb b/spec/lib/east_spec.rb index 9aeefee..6afac7f 100644 --- a/spec/lib/east_spec.rb +++ b/spec/lib/east_spec.rb @@ -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) diff --git a/spec/lib/north_spec.rb b/spec/lib/north_spec.rb index bc767ed..ee4fb71 100644 --- a/spec/lib/north_spec.rb +++ b/spec/lib/north_spec.rb @@ -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) diff --git a/spec/lib/rover_position_spec.rb b/spec/lib/position_spec.rb similarity index 83% rename from spec/lib/rover_position_spec.rb rename to spec/lib/position_spec.rb index 8b977c3..f99d60b 100644 --- a/spec/lib/rover_position_spec.rb +++ b/spec/lib/position_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -97,7 +97,7 @@ 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 @@ -105,7 +105,7 @@ 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') @@ -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 diff --git a/spec/lib/rectangular_plateau_spec.rb b/spec/lib/rectangular_plateau_spec.rb index 299bfcd..f284988 100644 --- a/spec/lib/rectangular_plateau_spec.rb +++ b/spec/lib/rectangular_plateau_spec.rb @@ -7,7 +7,7 @@ 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 @@ -15,7 +15,7 @@ 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 @@ -27,7 +27,7 @@ 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 @@ -35,7 +35,7 @@ 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 @@ -47,7 +47,7 @@ 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 @@ -55,7 +55,7 @@ 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 @@ -67,7 +67,7 @@ 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 @@ -75,7 +75,7 @@ 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 diff --git a/spec/lib/rover_spec.rb b/spec/lib/rover_spec.rb index 5e51f84..009fedb 100644 --- a/spec/lib/rover_spec.rb +++ b/spec/lib/rover_spec.rb @@ -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') @@ -24,7 +24,7 @@ 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 @@ -32,7 +32,7 @@ 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) diff --git a/spec/lib/south_spec.rb b/spec/lib/south_spec.rb index 09ddb76..51b11f9 100644 --- a/spec/lib/south_spec.rb +++ b/spec/lib/south_spec.rb @@ -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) diff --git a/spec/lib/west_spec.rb b/spec/lib/west_spec.rb index b4d1ed9..e017b20 100644 --- a/spec/lib/west_spec.rb +++ b/spec/lib/west_spec.rb @@ -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)