swttt-gem 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/minimax_computer.rb +26 -11
- data/spec/minimax_computer_spec.rb +1 -0
- data/swttt-gem.gemspec +1 -1
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.7.0
|
data/lib/minimax_computer.rb
CHANGED
@@ -12,8 +12,6 @@ class MinimaxComputer
|
|
12
12
|
def move(iteration = 0)
|
13
13
|
if first_move?
|
14
14
|
make_first_move
|
15
|
-
elsif @observer.game_over?
|
16
|
-
return path_score_result if iteration > 0
|
17
15
|
else
|
18
16
|
perform_mini_max(iteration)
|
19
17
|
end
|
@@ -39,14 +37,10 @@ private
|
|
39
37
|
end
|
40
38
|
end
|
41
39
|
|
42
|
-
def path_score_result
|
43
|
-
return -@game_board.player_value if @observer.has_winner?
|
44
|
-
return 0
|
45
|
-
end
|
46
|
-
|
47
40
|
def perform_mini_max(iteration)
|
41
|
+
return path_score if @observer.game_over?
|
48
42
|
best_moves = BestMove.new(@game_board, @my_player_value)
|
49
|
-
for_each_cell { |row, column| best_moves.add_better_move(best_moves.value,
|
43
|
+
for_each_cell { |row, column| best_moves.add_better_move(best_moves.value, calculate_path_score(row, column,iteration),
|
50
44
|
Move.new(row, column)) if @game_board.is_empty_at?(row, column) }
|
51
45
|
return best_moves.value if !iteration.zero?
|
52
46
|
move = best_moves.random
|
@@ -59,13 +53,34 @@ private
|
|
59
53
|
end
|
60
54
|
end
|
61
55
|
|
62
|
-
def path_score
|
56
|
+
def path_score
|
57
|
+
return -@game_board.player_value if @observer.has_winner?
|
58
|
+
return 0
|
59
|
+
end
|
60
|
+
|
61
|
+
def calculate_path_score(row, column, iteration)
|
63
62
|
@game_board.move(row, column)
|
64
|
-
score =
|
63
|
+
score = calculate_score(iteration)
|
65
64
|
@game_board.undo_move
|
66
65
|
return score
|
67
66
|
end
|
68
|
-
|
67
|
+
|
68
|
+
def calculate_score(iteration)
|
69
|
+
if immediate_win?(iteration)
|
70
|
+
return infinity
|
71
|
+
else
|
72
|
+
return perform_mini_max(iteration + 1)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def immediate_win?(iteration)
|
77
|
+
iteration.zero? && @observer.has_winner?
|
78
|
+
end
|
79
|
+
|
80
|
+
def infinity
|
81
|
+
-@game_board.player_value * 1.0/0.0
|
82
|
+
end
|
83
|
+
end
|
69
84
|
|
70
85
|
class BestMove
|
71
86
|
attr_accessor :moves, :value
|
data/swttt-gem.gemspec
CHANGED
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: 3
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 7
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.7.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Stephen Walker
|