class Checkers::AI::Engine::Alphabeta

Public Instance Methods

next_board(board) click to toggle source
Calls superclass method
# File lib/checkers/ai/engine/alphabeta.rb, line 7
def next_board(board)
  super(board) { |root, tree_depth| alphabeta(root, tree_depth, Float::MIN, Float::MAX, true) }
end

Private Instance Methods

alphabeta(node, tree_depth, a, b, maxplayer) click to toggle source
# File lib/checkers/ai/engine/alphabeta.rb, line 13
def alphabeta(node, tree_depth, a, b, maxplayer)
  return node.score if tree_depth.zero? || node.children_size.zero?

  if maxplayer
    node.children.each do |child|
      a = max(a, alphabeta(child, tree_depth - 1, a, b, !maxplayer))
      break if a >= b
    end

    node.score = a
  else
    node.children.each do |child|
      b = min(b, alphabeta(child, tree_depth - 1, a, b, !maxplayer))
      break if a >= b
    end

    node.score = b
  end
end