bare_gato 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/bare_gato/move.rb +11 -9
- data/lib/bare_gato/player.rb +9 -7
- data/lib/bare_gato/version.rb +1 -1
- data/spec/bare_gato/game_spec.rb +173 -171
- data/spec/bare_gato/move_spec.rb +14 -12
- data/spec/bare_gato/player_spec.rb +13 -11
- metadata +3 -3
data/lib/bare_gato/move.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module BareGato
|
2
|
+
class Move
|
3
|
+
attr_reader :x, :y, :player
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
def initialize player, space
|
6
|
+
@player = player
|
7
|
+
@x = space[:x]
|
8
|
+
@y = space[:y]
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
11
|
+
def valid?
|
12
|
+
x > -1 && y > -1
|
13
|
+
end
|
12
14
|
end
|
13
15
|
end
|
data/lib/bare_gato/player.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
|
1
|
+
module BareGato
|
2
|
+
class Player
|
2
3
|
|
3
|
-
|
4
|
+
attr_reader :display
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
def initialize display
|
7
|
+
@display = display
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
def == other
|
11
|
+
other.display == display
|
12
|
+
end
|
11
13
|
end
|
12
14
|
end
|
data/lib/bare_gato/version.rb
CHANGED
data/spec/bare_gato/game_spec.rb
CHANGED
@@ -1,198 +1,200 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
it 'should tell if a player belongs to a game' do
|
24
|
-
expect(subject.includes_player? x_player).to be_true
|
25
|
-
expect(subject.includes_player? o_player).to be_true
|
26
|
-
expect(subject.includes_player? Player.new 'pepino').to be_false
|
27
|
-
end
|
3
|
+
module BareGato
|
4
|
+
describe Game do
|
5
|
+
|
6
|
+
let(:x_player) { Player.new 'x' }
|
7
|
+
|
8
|
+
let(:o_player) { Player.new 'o' }
|
9
|
+
|
10
|
+
describe 'a 3x3 game' do
|
11
|
+
subject do
|
12
|
+
bare_gato_game = BareGato::Game.new players: [
|
13
|
+
x_player, o_player
|
14
|
+
]
|
15
|
+
bare_gato_game.strategies = [
|
16
|
+
BareGato::WinningStrategies::Row,
|
17
|
+
BareGato::WinningStrategies::Column,
|
18
|
+
BareGato::WinningStrategies::Diagonal,
|
19
|
+
BareGato::WinningStrategies::InverseDiagonal
|
20
|
+
]
|
21
|
+
bare_gato_game
|
22
|
+
end
|
28
23
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
24
|
+
it 'should tell if a player belongs to a game' do
|
25
|
+
expect(subject.includes_player? x_player).to be_true
|
26
|
+
expect(subject.includes_player? o_player).to be_true
|
27
|
+
expect(subject.includes_player? Player.new 'pepino').to be_false
|
28
|
+
end
|
34
29
|
|
35
|
-
|
36
|
-
|
37
|
-
|
30
|
+
it 'should start with the last added player' do
|
31
|
+
expect(subject.next? o_player).to be_true
|
32
|
+
move = Move.new o_player, x: 0, y: 0
|
33
|
+
subject.play move
|
34
|
+
end
|
38
35
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
expect(subject.next? x_player).to be_true
|
36
|
+
it 'should return the next move type' do
|
37
|
+
subject.next.should eq(o_player)
|
38
|
+
end
|
43
39
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
40
|
+
it 'should keep movements sequence' do
|
41
|
+
subject.play Move.new(o_player, x: 0, y: 0)
|
42
|
+
expect(subject.next? o_player).to be_false
|
43
|
+
expect(subject.next? x_player).to be_true
|
48
44
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
[nil, nil, nil]],
|
54
|
-
next: o_player
|
55
|
-
})
|
56
|
-
end
|
45
|
+
subject.play Move.new(x_player, x: 1, y: 0)
|
46
|
+
expect(subject.next? x_player).to be_false
|
47
|
+
expect(subject.next? o_player).to be_true
|
48
|
+
end
|
57
49
|
|
58
|
-
|
50
|
+
it 'should return the current game status' do
|
51
|
+
subject.status.should eq({
|
52
|
+
game: [[nil, nil, nil],
|
53
|
+
[nil, nil, nil],
|
54
|
+
[nil, nil, nil]],
|
55
|
+
next: o_player
|
56
|
+
})
|
57
|
+
end
|
59
58
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
59
|
+
describe 'playing a few moves' do
|
60
|
+
|
61
|
+
context 'providing correct moves order' do
|
62
|
+
before do
|
63
|
+
subject.play Move.new(o_player, x: 0, y: 0)
|
64
|
+
subject.play Move.new(x_player, x: 1, y: 0)
|
65
|
+
subject.play Move.new(o_player, x: 0, y: 2)
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'should return the current game status' do
|
69
|
+
subject.status.should eq({
|
70
|
+
game: [[o_player, x_player, nil],
|
71
|
+
[nil, nil, nil],
|
72
|
+
[o_player, nil, nil]],
|
73
|
+
next: x_player
|
74
|
+
})
|
75
|
+
end
|
65
76
|
end
|
66
77
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
78
|
+
context 'providing incorrect moves order' do
|
79
|
+
it 'should raise error' do
|
80
|
+
expect { subject.play Move.new(x_player, x: 0, y: 0) }.to raise_error
|
81
|
+
subject.play Move.new(o_player, x: 0, y: 0)
|
82
|
+
subject.status.should eq({
|
83
|
+
game: [[o_player, nil, nil],
|
84
|
+
[nil, nil, nil],
|
85
|
+
[nil, nil, nil]],
|
86
|
+
next: x_player
|
87
|
+
})
|
88
|
+
end
|
74
89
|
end
|
75
90
|
end
|
76
91
|
|
77
|
-
context '
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
92
|
+
context 'a full game' do
|
93
|
+
context 'when winning by column' do
|
94
|
+
it 'should determine a winner' do
|
95
|
+
subject.play Move.new(o_player, x: 0, y: 0)
|
96
|
+
subject.got_a_winner?.should be_false
|
97
|
+
|
98
|
+
subject.play Move.new(x_player, x: 1, y: 0)
|
99
|
+
subject.got_a_winner?.should be_false
|
100
|
+
|
101
|
+
subject.play Move.new(o_player, x: 2, y: 0)
|
102
|
+
subject.got_a_winner?.should be_false
|
103
|
+
|
104
|
+
subject.play Move.new(x_player, x: 1, y: 1)
|
105
|
+
subject.got_a_winner?.should be_false
|
106
|
+
|
107
|
+
subject.play Move.new(o_player, x: 2, y: 1)
|
108
|
+
subject.got_a_winner?.should be_false
|
109
|
+
|
110
|
+
subject.play Move.new(x_player, x: 1, y: 2)
|
111
|
+
subject.status.should eq({
|
112
|
+
game: [[o_player, x_player, o_player],
|
113
|
+
[nil, x_player, o_player],
|
114
|
+
[nil, x_player, nil]],
|
115
|
+
next: o_player
|
116
|
+
})
|
117
|
+
subject.got_a_winner?.should be_true
|
118
|
+
end
|
87
119
|
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
context 'a full game' do
|
92
|
-
context 'when winning by column' do
|
93
|
-
it 'should determine a winner' do
|
94
|
-
subject.play Move.new(o_player, x: 0, y: 0)
|
95
|
-
subject.got_a_winner?.should be_false
|
96
|
-
|
97
|
-
subject.play Move.new(x_player, x: 1, y: 0)
|
98
|
-
subject.got_a_winner?.should be_false
|
99
|
-
|
100
|
-
subject.play Move.new(o_player, x: 2, y: 0)
|
101
|
-
subject.got_a_winner?.should be_false
|
102
|
-
|
103
|
-
subject.play Move.new(x_player, x: 1, y: 1)
|
104
|
-
subject.got_a_winner?.should be_false
|
105
120
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
121
|
+
context 'when winning by row' do
|
122
|
+
it 'should determine a winner' do
|
123
|
+
subject.play Move.new(o_player, x: 0, y: 0)
|
124
|
+
subject.got_a_winner?.should be_false
|
125
|
+
|
126
|
+
subject.play Move.new(x_player, x: 0, y: 1)
|
127
|
+
subject.got_a_winner?.should be_false
|
128
|
+
|
129
|
+
subject.play Move.new(o_player, x: 1, y: 0)
|
130
|
+
subject.got_a_winner?.should be_false
|
131
|
+
|
132
|
+
subject.play Move.new(x_player, x: 1, y: 1)
|
133
|
+
subject.got_a_winner?.should be_false
|
134
|
+
|
135
|
+
subject.play Move.new(o_player, x: 2, y: 0)
|
136
|
+
subject.status.should eq({
|
137
|
+
game: [[o_player, o_player, o_player],
|
138
|
+
[x_player, x_player, nil],
|
139
|
+
[nil, nil, nil]],
|
140
|
+
next: x_player
|
141
|
+
})
|
142
|
+
subject.got_a_winner?.should be_true
|
143
|
+
end
|
117
144
|
end
|
118
|
-
end
|
119
|
-
|
120
|
-
context 'when winning by row' do
|
121
|
-
it 'should determine a winner' do
|
122
|
-
subject.play Move.new(o_player, x: 0, y: 0)
|
123
|
-
subject.got_a_winner?.should be_false
|
124
|
-
|
125
|
-
subject.play Move.new(x_player, x: 0, y: 1)
|
126
|
-
subject.got_a_winner?.should be_false
|
127
|
-
|
128
|
-
subject.play Move.new(o_player, x: 1, y: 0)
|
129
|
-
subject.got_a_winner?.should be_false
|
130
145
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
146
|
+
context 'when winning by diagonal' do
|
147
|
+
it 'should determine a winner' do
|
148
|
+
subject.play Move.new(o_player, x: 0, y: 0)
|
149
|
+
subject.got_a_winner?.should be_false
|
150
|
+
|
151
|
+
subject.play Move.new(x_player, x: 0, y: 1)
|
152
|
+
subject.got_a_winner?.should be_false
|
153
|
+
|
154
|
+
subject.play Move.new(o_player, x: 1, y: 1)
|
155
|
+
subject.got_a_winner?.should be_false
|
156
|
+
|
157
|
+
subject.play Move.new(x_player, x: 1, y: 0)
|
158
|
+
subject.got_a_winner?.should be_false
|
159
|
+
|
160
|
+
subject.play Move.new(o_player, x: 2, y: 2)
|
161
|
+
subject.status.should eq({
|
162
|
+
game: [[o_player, x_player, nil],
|
163
|
+
[x_player, o_player, nil],
|
164
|
+
[nil, nil, o_player]],
|
165
|
+
next: x_player
|
166
|
+
})
|
167
|
+
subject.got_a_winner?.should be_true
|
168
|
+
end
|
142
169
|
end
|
143
|
-
end
|
144
|
-
|
145
|
-
context 'when winning by diagonal' do
|
146
|
-
it 'should determine a winner' do
|
147
|
-
subject.play Move.new(o_player, x: 0, y: 0)
|
148
|
-
subject.got_a_winner?.should be_false
|
149
|
-
|
150
|
-
subject.play Move.new(x_player, x: 0, y: 1)
|
151
|
-
subject.got_a_winner?.should be_false
|
152
170
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
171
|
+
context 'when winning by inverse diagonal' do
|
172
|
+
it 'should determine a winner' do
|
173
|
+
subject.play Move.new(o_player, x: 2, y: 0)
|
174
|
+
subject.got_a_winner?.should be_false
|
175
|
+
|
176
|
+
subject.play Move.new(x_player, x: 0, y: 1)
|
177
|
+
subject.got_a_winner?.should be_false
|
178
|
+
|
179
|
+
subject.play Move.new(o_player, x: 1, y: 1)
|
180
|
+
subject.got_a_winner?.should be_false
|
181
|
+
|
182
|
+
subject.play Move.new(x_player, x: 1, y: 0)
|
183
|
+
subject.got_a_winner?.should be_false
|
184
|
+
|
185
|
+
subject.play Move.new(o_player, x: 0, y: 2)
|
186
|
+
subject.status.should eq({
|
187
|
+
game: [[nil, x_player, o_player],
|
188
|
+
[x_player, o_player, nil],
|
189
|
+
[o_player, nil, nil]],
|
190
|
+
next: x_player
|
191
|
+
})
|
192
|
+
subject.got_a_winner?.should be_true
|
193
|
+
end
|
167
194
|
end
|
168
|
-
end
|
169
|
-
|
170
|
-
context 'when winning by inverse diagonal' do
|
171
|
-
it 'should determine a winner' do
|
172
|
-
subject.play Move.new(o_player, x: 2, y: 0)
|
173
|
-
subject.got_a_winner?.should be_false
|
174
|
-
|
175
|
-
subject.play Move.new(x_player, x: 0, y: 1)
|
176
|
-
subject.got_a_winner?.should be_false
|
177
195
|
|
178
|
-
subject.play Move.new(o_player, x: 1, y: 1)
|
179
|
-
subject.got_a_winner?.should be_false
|
180
|
-
|
181
|
-
subject.play Move.new(x_player, x: 1, y: 0)
|
182
|
-
subject.got_a_winner?.should be_false
|
183
|
-
|
184
|
-
subject.play Move.new(o_player, x: 0, y: 2)
|
185
|
-
subject.status.should eq({
|
186
|
-
game: [[nil, x_player, o_player],
|
187
|
-
[x_player, o_player, nil],
|
188
|
-
[o_player, nil, nil]],
|
189
|
-
next: x_player
|
190
|
-
})
|
191
|
-
subject.got_a_winner?.should be_true
|
192
|
-
end
|
193
196
|
end
|
194
197
|
|
195
198
|
end
|
196
|
-
|
197
199
|
end
|
198
200
|
end
|
data/spec/bare_gato/move_spec.rb
CHANGED
@@ -1,20 +1,22 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
module BareGato
|
4
|
+
describe Move do
|
5
|
+
it "should accept valid movements" do
|
6
|
+
player = Player.new 'x'
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
move = Move.new player, x: 0, y: 0
|
9
|
+
move.valid?.should be_true
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
12
|
+
it "should not accept invalid indexes" do
|
13
|
+
player = Player.new 'x'
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
move = Move.new player, x: -1, y: 0
|
16
|
+
move.valid?.should_not be_true
|
16
17
|
|
17
|
-
|
18
|
-
|
18
|
+
move = Move.new player, x: 0, y: -1
|
19
|
+
move.valid?.should_not be_true
|
20
|
+
end
|
19
21
|
end
|
20
22
|
end
|
@@ -1,17 +1,19 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module BareGato
|
4
|
+
describe Player do
|
5
|
+
subject do
|
6
|
+
Player.new('X')
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
it "should be string friendly" do
|
10
|
+
subject.display.should == "X"
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
it 'should identify players' do
|
14
|
+
expect(Player.new 'X' ).to eq Player.new('X')
|
15
|
+
expect(Player.new 'X' ).to_not eq Player.new('R')
|
16
|
+
end
|
16
17
|
|
18
|
+
end
|
17
19
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bare_gato
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -106,7 +106,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
106
106
|
version: '0'
|
107
107
|
segments:
|
108
108
|
- 0
|
109
|
-
hash:
|
109
|
+
hash: -162106955877737498
|
110
110
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
111
|
none: false
|
112
112
|
requirements:
|
@@ -115,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
115
115
|
version: '0'
|
116
116
|
segments:
|
117
117
|
- 0
|
118
|
-
hash:
|
118
|
+
hash: -162106955877737498
|
119
119
|
requirements: []
|
120
120
|
rubyforge_project:
|
121
121
|
rubygems_version: 1.8.23
|