robot_on_table 0.5.2 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/model/robot.rb +32 -0
- data/lib/model/robot_table.rb +6 -0
- data/lib/robot_states/not_on_table.rb +22 -0
- data/lib/robot_states/on_table.rb +28 -0
- metadata +6 -4
data/lib/model/robot.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative '../circular_list'
|
2
|
+
class Robot
|
3
|
+
|
4
|
+
attr_accessor :x_coord, :y_coord, :current_direction
|
5
|
+
|
6
|
+
def initialize x_coord, y_coord, starting_direction
|
7
|
+
@directions = CircularList.new([:N, :E, :S, :W])
|
8
|
+
@directions.rotate_to starting_direction
|
9
|
+
@x_coord, @y_coord, @current_direction = x_coord, y_coord, starting_direction
|
10
|
+
end
|
11
|
+
|
12
|
+
def left
|
13
|
+
@current_direction = @directions.fetch_previous
|
14
|
+
end
|
15
|
+
|
16
|
+
def right
|
17
|
+
@current_direction = @directions.fetch_next
|
18
|
+
end
|
19
|
+
|
20
|
+
def move
|
21
|
+
case @current_direction
|
22
|
+
when :N
|
23
|
+
@y_coord = @y_coord + 1
|
24
|
+
when :E
|
25
|
+
@x_coord = @x_coord + 1
|
26
|
+
when :S
|
27
|
+
@y_coord = @y_coord - 1
|
28
|
+
when :W
|
29
|
+
@x_coord = @x_coord - 1
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module RobotNotOnTableState
|
2
|
+
def place x, y, d
|
3
|
+
extend RobotOnTableState
|
4
|
+
Robot.new x, y, d
|
5
|
+
end
|
6
|
+
|
7
|
+
def left
|
8
|
+
"Not On Grid, cannot call left command"
|
9
|
+
end
|
10
|
+
|
11
|
+
def right
|
12
|
+
"Not On Grid, cannot call right command"
|
13
|
+
end
|
14
|
+
|
15
|
+
def move
|
16
|
+
"Not On Grid, cannot call move command"
|
17
|
+
end
|
18
|
+
|
19
|
+
def report
|
20
|
+
"Not On Grid"
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module RobotOnTableState
|
2
|
+
def place x, y, d
|
3
|
+
"Cannot call place once robot is on table"
|
4
|
+
end
|
5
|
+
|
6
|
+
def left
|
7
|
+
@robot.left
|
8
|
+
end
|
9
|
+
|
10
|
+
def right
|
11
|
+
@robot.right
|
12
|
+
end
|
13
|
+
|
14
|
+
def move
|
15
|
+
@robot.move if valid_move?
|
16
|
+
end
|
17
|
+
|
18
|
+
def report
|
19
|
+
"#{@robot.x_coord},#{@robot.y_coord},#{@robot.current_direction.to_s}"
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def valid_move?
|
24
|
+
tmp_robot = @robot.dup
|
25
|
+
tmp_robot.move
|
26
|
+
tmp_robot.x_coord >=0 && tmp_robot.y_coord >=0 && tmp_robot.x_coord <=@robot_table.x_coord && tmp_robot.y_coord <=@robot_table.y_coord
|
27
|
+
end
|
28
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: robot_on_table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -20,6 +20,10 @@ extra_rdoc_files: []
|
|
20
20
|
files:
|
21
21
|
- lib/circular_list.rb
|
22
22
|
- lib/robot_game.rb
|
23
|
+
- lib/model/robot.rb
|
24
|
+
- lib/model/robot_table.rb
|
25
|
+
- lib/robot_states/not_on_table.rb
|
26
|
+
- lib/robot_states/on_table.rb
|
23
27
|
- spec/circular_list_spec.rb
|
24
28
|
- spec/robot_spec.rb
|
25
29
|
- bin/robot_game
|
@@ -30,8 +34,6 @@ rdoc_options:
|
|
30
34
|
- --charset=UTF-8
|
31
35
|
require_paths:
|
32
36
|
- - lib
|
33
|
-
- lib/model
|
34
|
-
- lib/robot_states
|
35
37
|
required_ruby_version: !ruby/object:Gem::Requirement
|
36
38
|
none: false
|
37
39
|
requirements:
|
@@ -49,7 +51,7 @@ rubyforge_project:
|
|
49
51
|
rubygems_version: 1.8.25
|
50
52
|
signing_key:
|
51
53
|
specification_version: 3
|
52
|
-
summary: robot_on_table_0.
|
54
|
+
summary: robot_on_table_0.6.2
|
53
55
|
test_files:
|
54
56
|
- spec/circular_list_spec.rb
|
55
57
|
- spec/robot_spec.rb
|