robot_on_table 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/robot_game ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/robot_game'
4
+ game = RobotGame.new RobotTable.new 4,4
5
+ puts "Type HELP for how to play"
6
+ while 1 < 2
7
+ cmd = gets.chomp
8
+ result = game.cmd(cmd)
9
+ puts result if result.class == String
10
+ end
@@ -0,0 +1,23 @@
1
+ class CircularList
2
+ def initialize(array)
3
+ @arr = array
4
+ end
5
+
6
+ def size
7
+ @arr.size
8
+ end
9
+
10
+ def fetch_previous(index=0)
11
+ index.nil? ? nil : @arr.unshift(@arr.pop)[index]
12
+ end
13
+
14
+ def fetch_next(index=0)
15
+ index.nil? ? nil : @arr.push(@arr.shift)[index]
16
+ end
17
+
18
+ def rotate_to obj
19
+ hash = Hash[@arr.map.with_index.to_a]
20
+ hash[obj].times {fetch_next} && return if hash[obj]
21
+ nil
22
+ end
23
+ end
data/lib/robot_game.rb ADDED
@@ -0,0 +1,47 @@
1
+ require_relative 'model/robot'
2
+ require_relative 'model/robot_table'
3
+ require_relative 'robot_states/not_on_table'
4
+ require_relative 'robot_states/on_table'
5
+
6
+ class RobotGame
7
+ include RobotNotOnTableState
8
+
9
+ VALID_PLACE_REGEX = /^PLACE\s*([0-4])\s*,\s*([0-4])\s*,\s*([S|N|E|W]{1})$/
10
+
11
+ def initialize robot_table
12
+ @robot_table = robot_table
13
+ end
14
+
15
+ def cmd cmd
16
+ # could have used chain of responsibility but that would be over engineering
17
+ if cmd =~ /^\s*REPORT\s*$/
18
+ report
19
+ elsif match = cmd.match(VALID_PLACE_REGEX)
20
+ @robot = place(match[1].to_i, match[2].to_i, match[3].to_sym)
21
+ elsif cmd =~ /^\s*MOVE\s*$/
22
+ move
23
+ elsif cmd =~ /^\s*LEFT\s*$/
24
+ left
25
+ elsif cmd =~ /^\s*RIGHT\s*$/
26
+ right
27
+ elsif cmd =~ /^\s*HELP\s*$/
28
+ puts """
29
+ Valid commands:
30
+
31
+ PLACE <0-4>,<04>,<N|S|E|W>
32
+ i.e. PLACE 1,1,W
33
+
34
+ MOVE
35
+ LEFT
36
+ RIGHT
37
+ HELP
38
+ REPORT
39
+ EXIT
40
+ """
41
+ elsif cmd =~ /^\s*EXIT\s*$/
42
+ exit 0
43
+ else
44
+ "Invalid command, type HELP for commands"
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,43 @@
1
+ require "rspec"
2
+ require_relative '../lib/circular_list'
3
+
4
+ describe "Circular List" do
5
+
6
+ it "should rotate forward" do
7
+ c = CircularList.new([1,2,3,4])
8
+ c.fetch_next.should == 2
9
+ c.fetch_next.should == 3
10
+ c.fetch_next.should == 4
11
+ c.fetch_next.should == 1
12
+ end
13
+
14
+ it "should rotate backwards" do
15
+ c = CircularList.new([1,2,3,4])
16
+ c.fetch_previous.should == 4
17
+ c.fetch_previous.should == 3
18
+ c.fetch_previous.should == 2
19
+ c.fetch_previous.should == 1
20
+ c.fetch_previous.should == 4
21
+ end
22
+
23
+ it 'should rotate to a specified object (1)' do
24
+ c = CircularList.new([?A,?B,?C,?D])
25
+ c.rotate_to ?C
26
+ c.fetch_next.should == ?D
27
+ end
28
+
29
+ it 'should rotate to a specified object (2)' do
30
+ c = CircularList.new(%w[cat dog horse cow pig sheep])
31
+ c.rotate_to 'horse'
32
+ c.fetch_next.should == 'cow'
33
+ c.rotate_to 'cat'
34
+ c.fetch_next.should == 'dog'
35
+ end
36
+
37
+ it 'should not if specified object not found' do
38
+ c = CircularList.new(%w[cat dog horse cow pig sheep])
39
+ c.rotate_to('penguin').should == nil
40
+ c.fetch_next.should == 'dog'
41
+ end
42
+
43
+ end
@@ -0,0 +1,142 @@
1
+ require 'rspec'
2
+ require_relative '../lib/robot_game'
3
+
4
+ describe 'Game' do
5
+
6
+ before :each do
7
+ @game = RobotGame.new RobotTable.new(4, 4)
8
+ end
9
+
10
+ describe 'Table' do
11
+ it 'should not allow you to fall off the table' do
12
+ @game.cmd('PLACE 0,0,S')
13
+ @game.cmd('MOVE')
14
+ @game.cmd('REPORT').should == '0,0,S'
15
+ end
16
+
17
+ it 'should not allow you to fall off the table' do
18
+ @game.cmd('PLACE 4,4,N')
19
+ @game.cmd('MOVE')
20
+ @game.cmd('REPORT').should == '4,4,N'
21
+ end
22
+ end
23
+
24
+ describe 'Move' do
25
+ it "allow a valid movement NORTH" do
26
+ @game.cmd('PLACE 0,0,N')
27
+ @game.cmd('MOVE')
28
+ @game.cmd('REPORT').should == '0,1,N'
29
+ end
30
+
31
+ it "allow a valid movement EAST" do
32
+ @game.cmd('PLACE 0,0,E')
33
+ @game.cmd('MOVE')
34
+ @game.cmd('REPORT').should == '1,0,E'
35
+ end
36
+
37
+ it "allow a valid movement SOUTH" do
38
+ @game.cmd('PLACE 0,1,S')
39
+ @game.cmd('MOVE')
40
+ @game.cmd('REPORT').should == '0,0,S'
41
+ end
42
+
43
+ it "allow a valid movement WEST" do
44
+ @game.cmd('PLACE 1,0,W')
45
+ @game.cmd('MOVE')
46
+ @game.cmd('REPORT').should == '0,0,W'
47
+ end
48
+ end
49
+
50
+ describe 'Turn' do
51
+ describe 'Right' do
52
+ it "should allow you to turn right facing W" do
53
+ @game.cmd('PLACE 1,0,W')
54
+ @game.cmd('RIGHT')
55
+ @game.cmd('REPORT').should == '1,0,N'
56
+ end
57
+
58
+ it "should allow you to turn right facing S" do
59
+
60
+ @game.cmd('PLACE 1,0,S')
61
+ @game.cmd('RIGHT')
62
+ @game.cmd('REPORT').should == '1,0,W'
63
+ end
64
+
65
+ it "should allow you to turn right facing E" do
66
+
67
+ @game.cmd('PLACE 1,0,E')
68
+ @game.cmd('RIGHT')
69
+ @game.cmd('REPORT').should == '1,0,S'
70
+ end
71
+
72
+ it "should allow you to turn right facing N" do
73
+
74
+ @game.cmd('PLACE 1,0,N')
75
+ @game.cmd('RIGHT')
76
+ @game.cmd('REPORT').should == '1,0,E'
77
+ end
78
+ end
79
+
80
+
81
+ describe 'Left' do
82
+ it "should allow you to turn left facing W" do
83
+
84
+ @game.cmd('PLACE 1,0,W')
85
+ @game.cmd('LEFT')
86
+ @game.cmd('REPORT').should == '1,0,S'
87
+ end
88
+
89
+ it "should allow you to turn left facing S" do
90
+
91
+ @game.cmd('PLACE 1,0,S')
92
+ @game.cmd('LEFT')
93
+ @game.cmd('REPORT').should == '1,0,E'
94
+ end
95
+
96
+ it "should allow you to turn left facing E" do
97
+
98
+ @game.cmd('PLACE 1,0,E')
99
+ @game.cmd('LEFT')
100
+ @game.cmd('REPORT').should == '1,0,N'
101
+ end
102
+
103
+ it "should allow you to turn left facing N" do
104
+
105
+ @game.cmd('PLACE 1,0,N')
106
+ @game.cmd('LEFT')
107
+ @game.cmd('REPORT').should == '1,0,W'
108
+ end
109
+ end
110
+
111
+
112
+ end
113
+
114
+ describe 'Placement' do
115
+ describe 'Valid' do
116
+ [
117
+ ['PLACE 0,0,N', '0,0,N'],
118
+ ['PLACE 3,3,N', '3,3,N'],
119
+ ['PLACE 3,1,S', '3,1,S'],
120
+ ['PLACE 4,4,S', '4,4,S'],
121
+ ['PLACE 4,4,S', '4,4,S']
122
+ ].each do |test_case|
123
+ it "allow a valid placement for #{test_case}" do
124
+
125
+ @game.cmd(test_case[0])
126
+ @game.cmd('REPORT').should == test_case[1]
127
+ end
128
+ end
129
+ end
130
+
131
+ describe 'Invalid' do
132
+ ['PLACE 5,5,N', 'PLACE 4,6,N', 'PLACE 7,1,S', 'PLACE -4,4,S', 'PLACE 4,-4,S', 'PLACE A,4,S', 'PLACE 4,4,SE'
133
+ ].each do |placement|
134
+ it "should not allow an invalid placement for #{placement}" do
135
+
136
+ @game.cmd(placement)
137
+ @game.cmd('REPORT').should == "Not On Grid"
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: robot_on_table
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jarrod Folino
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-15 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Robot On Table
15
+ email: jdfolino@gmail.com
16
+ executables:
17
+ - robot_game
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/circular_list.rb
22
+ - lib/robot_game.rb
23
+ - spec/circular_list_spec.rb
24
+ - spec/robot_spec.rb
25
+ - bin/robot_game
26
+ homepage:
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options:
30
+ - --charset=UTF-8
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.25
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: robot_on_table_0.1
51
+ test_files:
52
+ - spec/circular_list_spec.rb
53
+ - spec/robot_spec.rb