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 CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.7.0
@@ -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, path_score(row, column,iteration),
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(row, column, iteration)
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 = move(iteration + 1)
63
+ score = calculate_score(iteration)
65
64
  @game_board.undo_move
66
65
  return score
67
66
  end
68
- end
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
@@ -33,5 +33,6 @@ describe MinimaxComputer do
33
33
  @my_board.move(0,0)
34
34
  @computer.move
35
35
  @observer.has_winner?.should be_true
36
+ @my_board.number_of_moves_made.should == 6
36
37
  end
37
38
  end
data/swttt-gem.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{swttt-gem}
8
- s.version = "0.6.0"
8
+ s.version = "0.7.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"]
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: 7
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 6
8
+ - 7
9
9
  - 0
10
- version: 0.6.0
10
+ version: 0.7.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Stephen Walker