Class: BestMove

Inherits:
Object
  • Object
show all
Defined in:
lib/minimax_computer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game_board, my_player_value) ⇒ BestMove

Returns a new instance of BestMove.



87
88
89
90
91
# File 'lib/minimax_computer.rb', line 87

def initialize(game_board, my_player_value)
  @moves = []
  @value = nil
  @game_board, @my_player_value = game_board, my_player_value
end

Instance Attribute Details

#movesObject

Returns the value of attribute moves.



86
87
88
# File 'lib/minimax_computer.rb', line 86

def moves
  @moves
end

#valueObject

Returns the value of attribute value.



86
87
88
# File 'lib/minimax_computer.rb', line 86

def value
  @value
end

Instance Method Details

#add_better_move(path_score, move) ⇒ Object



97
98
99
100
101
102
# File 'lib/minimax_computer.rb', line 97

def add_better_move(path_score, move)
  if better_or_equal_move?(path_score)
    @moves.clear and @value = path_score if path_score !=  @value
    @moves << move
  end
end

#better_or_equal_move?(path_score) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
108
# File 'lib/minimax_computer.rb', line 104

def better_or_equal_move?(path_score)
  (@value.nil?) or
  (path_score >= @value && @game_board.player_value == @my_player_value) or
  (path_score <= @value && @game_board.player_value != @my_player_value)
end

#get_random_moveObject



93
94
95
# File 'lib/minimax_computer.rb', line 93

def get_random_move
  @moves[rand(@moves.size)]
end