class Tictactoe::Ai::PerfectIntelligence

Constants

SCORE_FOR_UNKNOWN_FUTURE

Public Instance Methods

desired_moves(state, player) click to toggle source
# File lib/tictactoe/ai/perfect_intelligence.rb, line 9
def desired_moves(state, player)
  find_best_moves(state, player)
end

Private Instance Methods

dynamic_depth_for(state) click to toggle source
# File lib/tictactoe/ai/perfect_intelligence.rb, line 21
def dynamic_depth_for(state)
  is_4_by_4 = state.board.locations.length == 16
  if is_4_by_4
    initial_depth_to_stay_out_of_trouble = 0
    minimum_depth_to_avoid_lethal_moves = 7
  else
    initial_depth_to_stay_out_of_trouble = 4
    minimum_depth_to_avoid_lethal_moves = 5
  end

  [minimum_depth_to_avoid_lethal_moves, state.played_moves + initial_depth_to_stay_out_of_trouble].min
end
find_best_moves(state, player) click to toggle source
# File lib/tictactoe/ai/perfect_intelligence.rb, line 14
def find_best_moves(state, player)
  depth = dynamic_depth_for(state)
  root = Tree.new(state, player)
  ai = ABMinimax.new(-1000, SCORE_FOR_UNKNOWN_FUTURE, depth)
  ai.best_nodes(root).map(&:move)
end