Class: ABComputer
- Inherits:
-
Object
- Object
- ABComputer
- Defined in:
- lib/abcomputer.rb
Instance Method Summary collapse
- #get_max(depth, alpha, beta) ⇒ Object
- #get_min(depth, alpha, beta) ⇒ Object
-
#initialize ⇒ ABComputer
constructor
A new instance of ABComputer.
- #make_move ⇒ Object
Constructor Details
#initialize ⇒ ABComputer
Returns a new instance of ABComputer.
2 3 4 |
# File 'lib/abcomputer.rb', line 2 def initialize end |
Instance Method Details
#get_max(depth, alpha, beta) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/abcomputer.rb', line 22 def get_max(depth, alpha, beta) iterate_board do |row, column| if alpha > beta return alpha else if @board.is_empty_at(row, column) @board.move(row, column) if @observer.game_over? return -@board.player_value if @observer.has_winner? return 0 else value = get_min(depth+1, alpha, beta) alpha = value if alpha < value end end end end end |
#get_min(depth, alpha, beta) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/abcomputer.rb', line 41 def get_min(depth, alpha, beta) iterate_board do |row, column| if alpha < beta return beta else if @board.is_empty_at(row, column) @board.move(row, column) if @observer.game_over? return -@board.player_value if @observer.has_winner? return 0 else value = get_max(depth+1, alpha, beta) beta = value if beta > value end end end end end |
#make_move ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/abcomputer.rb', line 6 def make_move alpha = (-1.0)/(0.0) beta = (1.0)/(0.0) best_score = nil iterate_board do |row, column| @board.make_move(row, column) path_score = get_max(1, alpha, beta) @board.undo_move if best_score.nil? || path_score > best_score best_row = row best_column = column best_score = path_score end end end |