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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ec291290496aafcdceadf74c6dd16c7518c35bf8
4
- data.tar.gz: 240389d6f6bd16d4d1faddb13f81947b06d9ebb9
3
+ metadata.gz: ecc15f23d0502fa36714f6f15cba2363fa60547d
4
+ data.tar.gz: 7d78c8b75ad2d46f1d3f76cd7c4c3d4af5c25e06
5
5
  SHA512:
6
- metadata.gz: 9e6d081fbb7ccfac04952cfce6b2eb5b803653a03140545189625594dda0285167db364c1170468917d643379f2362515d049472139baaea82e66e6ae102482b
7
- data.tar.gz: fdf42b81348d96b4ec91575469e839c6037cf9ef55f539dbfdbd0122b0e1bae1aaeb2bf487c370f433b087dde0d2069a8346c48b09290335b834110b23d127eb
6
+ metadata.gz: 46ce2afc86d779ec6f930152f91cd1a254196a16b3d5cd82b58fbe222dc0add6b64a64ce68a90a3ddd21432ca18b2d01cfce4e7d57bea48bbd7a7f0fd878bb5c
7
+ data.tar.gz: c4973199f9ae95da935b75ffbd91ee275523d78cb09ad8ccb87de16426c274e4ee777faa61665b36d28827dff895129870d8da4e39310dad944c0ae729ff4172
@@ -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
@@ -1,3 +1,3 @@
1
1
  module MarsRover
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mars_rover
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo Diniz