Sha256: 06a3ea4adfe008f2ba58169cd99e9299722dd9852e7f13c02e743a5780ed678c

Contents?: true

Size: 842 Bytes

Versions: 1

Compression:

Stored size: 842 Bytes

Contents

# frozen_string_literal: true

module Checkers
  module AI
    module Engine
      class Minmax < Base
        def next_board(board)
          super(board) { |root, tree_depth| minmax(root, tree_depth, true) }
        end

        private

        def minmax(node, tree_depth, maxplayer)
          return node.score if tree_depth.zero? || node.children_size.zero?

          value = nil

          if maxplayer
            value = Float::MIN

            node.children.each do |child|
              value = max(value, minmax(child, tree_depth - 1, !maxplayer))
            end
          else
            value = Float::MAX

            node.children.each do |child|
              value = min(value, minmax(child, tree_depth - 1, !maxplayer))
            end
          end

          node.score = value
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
checkers-game-0.1.0 lib/checkers/ai/engine/minmax.rb