Class: BestMove
- Inherits:
-
Object
- Object
- BestMove
- Defined in:
- lib/minimax_computer.rb
Instance Attribute Summary collapse
-
#moves ⇒ Object
Returns the value of attribute moves.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #add_better_move(path_score, move) ⇒ Object
- #better_or_equal_move?(path_score) ⇒ Boolean
- #get_random_move ⇒ Object
-
#initialize(game_board, my_player_value) ⇒ BestMove
constructor
A new instance of BestMove.
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
#moves ⇒ Object
Returns the value of attribute moves.
86 87 88 |
# File 'lib/minimax_computer.rb', line 86 def moves @moves end |
#value ⇒ Object
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
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_move ⇒ Object
93 94 95 |
# File 'lib/minimax_computer.rb', line 93 def get_random_move @moves[rand(@moves.size)] end |