mars_rover 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mars_rover/position.rb +19 -0
- data/lib/mars_rover/version.rb +1 -1
- data/test/rover_test.rb +29 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ecc15f23d0502fa36714f6f15cba2363fa60547d
|
4
|
+
data.tar.gz: 7d78c8b75ad2d46f1d3f76cd7c4c3d4af5c25e06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46ce2afc86d779ec6f930152f91cd1a254196a16b3d5cd82b58fbe222dc0add6b64a64ce68a90a3ddd21432ca18b2d01cfce4e7d57bea48bbd7a7f0fd878bb5c
|
7
|
+
data.tar.gz: c4973199f9ae95da935b75ffbd91ee275523d78cb09ad8ccb87de16426c274e4ee777faa61665b36d28827dff895129870d8da4e39310dad944c0ae729ff4172
|
data/lib/mars_rover/position.rb
CHANGED
@@ -1,11 +1,30 @@
|
|
1
1
|
module MarsRover
|
2
2
|
class Position
|
3
|
+
|
3
4
|
def self.move(rover)
|
5
|
+
return unless can_move?(rover)
|
4
6
|
direction = rover.direction
|
5
7
|
rover.x += 1 if direction == 'E'
|
6
8
|
rover.y += 1 if direction == 'N'
|
7
9
|
rover.x -= 1 if direction == 'W'
|
8
10
|
rover.y -= 1 if direction == 'S'
|
9
11
|
end
|
12
|
+
|
13
|
+
def self.can_move?(rover)
|
14
|
+
plateau = rover.plateau
|
15
|
+
direction = rover.direction
|
16
|
+
|
17
|
+
case direction
|
18
|
+
when 'N'
|
19
|
+
rover.y < plateau.y_upper_limit
|
20
|
+
when 'S'
|
21
|
+
rover.y > plateau.y_lower_limit
|
22
|
+
when 'W'
|
23
|
+
rover.x > plateau.x_lower_limit
|
24
|
+
when 'E'
|
25
|
+
rover.x < plateau.x_upper_limit
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
10
29
|
end
|
11
30
|
end
|
data/lib/mars_rover/version.rb
CHANGED
data/test/rover_test.rb
CHANGED
@@ -120,4 +120,33 @@ class RoverTest < ActiveSupport::TestCase
|
|
120
120
|
assert_equal 14, rover.y
|
121
121
|
end
|
122
122
|
|
123
|
+
test 'cannot go beyond the plateau' do
|
124
|
+
plateau = MarsRover::Plateau.new(50, 50)
|
125
|
+
rover = MarsRover::Rover.new(50, 50, 'N', plateau)
|
126
|
+
rover.move
|
127
|
+
|
128
|
+
assert_equal 50, rover.x
|
129
|
+
assert_equal 50, rover.y
|
130
|
+
assert_equal 'N', rover.direction
|
131
|
+
|
132
|
+
rover = MarsRover::Rover.new(50, 0, 'S', plateau)
|
133
|
+
rover.move
|
134
|
+
assert_equal 50, rover.x
|
135
|
+
assert_equal 0, rover.y
|
136
|
+
assert_equal 'S', rover.direction
|
137
|
+
|
138
|
+
|
139
|
+
rover = MarsRover::Rover.new(0, 30, 'W', plateau)
|
140
|
+
rover.move
|
141
|
+
assert_equal 0, rover.x
|
142
|
+
assert_equal 30, rover.y
|
143
|
+
assert_equal 'W', rover.direction
|
144
|
+
|
145
|
+
rover = MarsRover::Rover.new(50, 30, 'E', plateau)
|
146
|
+
rover.move
|
147
|
+
assert_equal 50, rover.x
|
148
|
+
assert_equal 30, rover.y
|
149
|
+
assert_equal 'E', rover.direction
|
150
|
+
end
|
151
|
+
|
123
152
|
end
|