Sha256: 5f19e81bddc08332e53a3b01942e5ff5570a03b05f1e7a8183323f0134a88c17

Contents?: true

Size: 1.8 KB

Versions: 8

Compression:

Stored size: 1.8 KB

Contents

module Bioshogi
  module AI
    module Diver
      # コードはわかりやすいが遅い
      class NegaMaxDiver < Base
        def dive(player: params[:current_player], depth: 0, hand_route: [])
          tle_verify

          container = player.container

          if depth == 0
            @eval_counter = 0
          end

          if logger
            log = -> s { logger_info_on_depth(s, player: player, depth: depth, hand_route: hand_route) }
          end

          # 一番深い局面に達したらはじめて評価する
          if depth_max <= depth
            @eval_counter += 1
            score = player.evaluator(params).score
            log["%+d" % score] if log
            return [score, [], []]
          end

          children = collect_children(player, log)

          if children.empty?
            log["#{hand_route.collect(&:to_s).join(' ')} のあとで合法手がない(=詰み)"] if log
            score = -score_max_of(player, depth)
            if f = params[:mate_proc]
              f[player, score, hand_route + [MATE]]
            end
            return [score, [MATE]]
          end

          max = -score_max_of(player, depth)
          best_pv = [MATE]      # 初期値を詰みにしておくことで children が空のときに詰みが返る

          children.each do |hand|
            hand.sandbox_execute(container) do
              log["#{hand}"] if log
              v, pv = dive(player: player.opponent_player, depth: depth + 1, hand_route: hand_route + [hand])
              v = -v # 相手の一番良い手は自分の一番悪い手としたいので符号を反転する
              if v > max
                best_pv = [hand, *pv]
                max = v
              end
            end
          end

          [max, best_pv]
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
bioshogi-0.0.15 lib/bioshogi/ai/diver/nega_max_diver.rb
bioshogi-0.0.14 lib/bioshogi/ai/diver/nega_max_diver.rb
bioshogi-0.0.10 lib/bioshogi/ai/diver/nega_max_diver.rb
bioshogi-0.0.9 lib/bioshogi/ai/diver/nega_max_diver.rb
bioshogi-0.0.8 lib/bioshogi/ai/diver/nega_max_diver.rb
bioshogi-0.0.7 lib/bioshogi/ai/diver/nega_max_diver.rb
bioshogi-0.0.5 lib/bioshogi/ai/diver/nega_max_diver.rb
bioshogi-0.0.4 lib/bioshogi/ai/diver/nega_max_diver.rb