swttt-gem 0.5.0 → 0.6.0
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/.DS_Store +0 -0
- data/VERSION +1 -1
- data/lib/board.rb +7 -7
- data/lib/game_observer.rb +4 -4
- data/lib/minimax_computer.rb +13 -7
- data/spec/board_spec.rb +14 -14
- data/spec/minimax_computer_spec.rb +10 -0
- data/swttt-gem.gemspec +2 -2
- metadata +4 -4
data/.DS_Store
CHANGED
Binary file
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.6.0
|
data/lib/board.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
class Board
|
2
2
|
attr_reader :game_history, :dimension, :game_board, :player_value, :board,
|
3
|
-
:
|
3
|
+
:row_scores, :column_scores, :left_diagonal_score, :right_diagonal_score
|
4
4
|
|
5
5
|
def initialize(dimension = 3)
|
6
6
|
@dimension = dimension
|
7
7
|
@game_history, @board = [], [[0,0,0],[0,0,0],[0,0,0]]
|
8
|
-
@
|
9
|
-
@
|
8
|
+
@row_scores, @column_scores = [0,0,0], [0,0,0]
|
9
|
+
@left_diagonal_score, @right_diagonal_score = 0, 0
|
10
10
|
end
|
11
11
|
|
12
12
|
def move(row, column, value = nil)
|
@@ -60,10 +60,10 @@ private
|
|
60
60
|
end
|
61
61
|
|
62
62
|
def update_sums(row, column, update_value)
|
63
|
-
@
|
64
|
-
@
|
65
|
-
@
|
66
|
-
@
|
63
|
+
@row_scores[row] += update_value
|
64
|
+
@column_scores[column] += update_value
|
65
|
+
@left_diagonal_score += update_value if row == column
|
66
|
+
@right_diagonal_score += update_value if row == @dimension - column - 1
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
data/lib/game_observer.rb
CHANGED
@@ -20,15 +20,15 @@ private
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def horizontal_win?
|
23
|
-
loop_array { |row| @game_board.
|
23
|
+
loop_array { |row| @game_board.row_scores[row].abs }
|
24
24
|
end
|
25
25
|
|
26
26
|
def vertical_win?
|
27
|
-
loop_array { |column| @game_board.
|
27
|
+
loop_array { |column| @game_board.column_scores[column].abs }
|
28
28
|
end
|
29
29
|
|
30
30
|
def diagonal_win?
|
31
|
-
(@game_board.
|
32
|
-
@game_board.
|
31
|
+
(@game_board.left_diagonal_score.abs == @game_board.dimension ||
|
32
|
+
@game_board.right_diagonal_score.abs == @game_board.dimension)
|
33
33
|
end
|
34
34
|
end
|
data/lib/minimax_computer.rb
CHANGED
@@ -11,11 +11,12 @@ class MinimaxComputer
|
|
11
11
|
|
12
12
|
def move(iteration = 0)
|
13
13
|
if first_move?
|
14
|
-
|
15
|
-
|
14
|
+
make_first_move
|
15
|
+
elsif @observer.game_over?
|
16
|
+
return path_score_result if iteration > 0
|
17
|
+
else
|
18
|
+
perform_mini_max(iteration)
|
16
19
|
end
|
17
|
-
return path_score_result if @observer.game_over?
|
18
|
-
mini_max(iteration)
|
19
20
|
end
|
20
21
|
|
21
22
|
private
|
@@ -24,7 +25,12 @@ private
|
|
24
25
|
@game_board.number_of_moves_made < 2
|
25
26
|
end
|
26
27
|
|
27
|
-
def
|
28
|
+
def make_first_move
|
29
|
+
move = select_first_move
|
30
|
+
@game_board.move(move.row, move.column)
|
31
|
+
end
|
32
|
+
|
33
|
+
def select_first_move
|
28
34
|
if !@first_move.include?(@game_board.game_history.first)
|
29
35
|
move = @first_move[rand(@first_move.size)]
|
30
36
|
else
|
@@ -38,13 +44,13 @@ private
|
|
38
44
|
return 0
|
39
45
|
end
|
40
46
|
|
41
|
-
def
|
47
|
+
def perform_mini_max(iteration)
|
42
48
|
best_moves = BestMove.new(@game_board, @my_player_value)
|
43
49
|
for_each_cell { |row, column| best_moves.add_better_move(best_moves.value, path_score(row, column,iteration),
|
44
50
|
Move.new(row, column)) if @game_board.is_empty_at?(row, column) }
|
45
51
|
return best_moves.value if !iteration.zero?
|
46
52
|
move = best_moves.random
|
47
|
-
@game_board.move(move.row, move.column)
|
53
|
+
@game_board.move(move.row, move.column)
|
48
54
|
end
|
49
55
|
|
50
56
|
def for_each_cell
|
data/spec/board_spec.rb
CHANGED
@@ -97,33 +97,33 @@ describe Board do
|
|
97
97
|
@my_board.move(1,1)
|
98
98
|
end
|
99
99
|
|
100
|
-
it "should update
|
101
|
-
@my_board.
|
100
|
+
it "should update row scores" do
|
101
|
+
@my_board.row_scores[1].should == 1
|
102
102
|
end
|
103
103
|
|
104
|
-
it "should update
|
105
|
-
@my_board.
|
104
|
+
it "should update column scores" do
|
105
|
+
@my_board.column_scores[1].should == 1
|
106
106
|
end
|
107
107
|
|
108
|
-
it "should update the
|
109
|
-
@my_board.
|
110
|
-
@my_board.
|
108
|
+
it "should update the diagonal scores" do
|
109
|
+
@my_board.left_diagonal_score.should == 1
|
110
|
+
@my_board.right_diagonal_score.should == 1
|
111
111
|
end
|
112
112
|
|
113
|
-
it "should undo
|
113
|
+
it "should undo row scores" do
|
114
114
|
@my_board.undo_move
|
115
|
-
@my_board.
|
115
|
+
@my_board.row_scores[1].should == 0
|
116
116
|
end
|
117
117
|
|
118
|
-
it "should undo
|
118
|
+
it "should undo column scores" do
|
119
119
|
@my_board.undo_move
|
120
|
-
@my_board.
|
120
|
+
@my_board.column_scores[1].should == 0
|
121
121
|
end
|
122
122
|
|
123
|
-
it "should undo
|
123
|
+
it "should undo diagonal scores" do
|
124
124
|
@my_board.undo_move
|
125
|
-
@my_board.
|
126
|
-
@my_board.
|
125
|
+
@my_board.left_diagonal_score.should == 0
|
126
|
+
@my_board.right_diagonal_score.should == 0
|
127
127
|
end
|
128
128
|
end
|
129
129
|
end
|
@@ -24,4 +24,14 @@ describe MinimaxComputer do
|
|
24
24
|
@observer.game_over?.should be_true
|
25
25
|
@observer.has_winner?.should be_false
|
26
26
|
end
|
27
|
+
|
28
|
+
it "takes an immediate win" do
|
29
|
+
@my_board.move(1,0)
|
30
|
+
@my_board.move(2,2)
|
31
|
+
@my_board.move(1,1)
|
32
|
+
@my_board.move(1,2)
|
33
|
+
@my_board.move(0,0)
|
34
|
+
@computer.move
|
35
|
+
@observer.has_winner?.should be_true
|
36
|
+
end
|
27
37
|
end
|
data/swttt-gem.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{swttt-gem}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.6.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Stephen Walker"]
|
12
|
-
s.date = %q{2011-05-
|
12
|
+
s.date = %q{2011-05-04}
|
13
13
|
s.description = %q{TTT Gem}
|
14
14
|
s.email = %q{stephenwalker1988@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: swttt-gem
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 7
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 6
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.6.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Stephen Walker
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-05-
|
18
|
+
date: 2011-05-04 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|