Class: AiPlayer

Inherits:
Object
  • Object
show all
Includes:
Player
Defined in:
lib/ai_player.rb

Instance Method Summary collapse

Methods included from Player

#game_symbol

Constructor Details

#initialize(symbol) ⇒ AiPlayer

Returns a new instance of AiPlayer.



7
8
9
# File 'lib/ai_player.rb', line 7

def initialize(symbol)
  super(symbol)
end

Instance Method Details

#choose_move(board) ⇒ Object



11
12
13
# File 'lib/ai_player.rb', line 11

def choose_move(board)
  minimax(board, true, board.vacant_indices.size, ALPHA, BETA).first[1]
end

#minimax(board, is_max_player, depth, alpha, beta) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ai_player.rb', line 19

def minimax(board, is_max_player, depth, alpha, beta)
  best_score_so_far = initial_score(is_max_player)

  if round_is_over(board, depth)
    return score(board, depth)
  end

  board.vacant_indices.each do |i|
    new_board = board.make_move(i, current_players_symbol(is_max_player))
    result = minimax(new_board, !is_max_player, depth - 1, alpha, beta)
    best_score_so_far = update_score(is_max_player, i, best_score_so_far, score_from(result))

    alpha = update_alpha(is_max_player, best_score_so_far, alpha)
    beta = update_beta(is_max_player, best_score_so_far, beta)

    if alpha > beta
      break
    end
  end

  best_score_so_far
end

#ready?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/ai_player.rb', line 15

def ready?
  true
end