Sha256: 7615fbe0766a9fdb7ac58daa7caa5207f8ce97ac29b8f8d6f49b96a3e7d3e8f2

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

# frozen-string-literal: true
# 駒柱判定用

module Bioshogi
  module Analysis
    module BoardPillerMethods
      attr_accessor :piece_piller_by_latest_piece

      def place_on(soldier, options = {})
        super

        c = piller_counts[soldier.place.column.value] + 1
        if c > Dimension::Row.dimension_size
          raise MustNotHappen, "#{soldier.place.column.hankaku_number}の列に#{c}個目の駒を配置しようとしています。棋譜を二重に読ませようとしていませんか?"
        end
        piller_counts[soldier.place.column.value] = c
        self.piece_piller_by_latest_piece = (c == Dimension::Row.dimension_size) # 最後の駒が反映される
      end

      # 現在の状態は駒柱がある状態か?
      def piece_piller_by_latest_piece?
        piller_counts.each_value.any? { |c| c >= Dimension::Row.dimension_size } # O(n) になるので使いたくない
      end

      def all_clear
        super

        piller_counts.clear
        self.piece_piller_by_latest_piece = false
      end

      def safe_delete_on(*)
        super.tap do |soldier|
          if soldier
            c = piller_counts[soldier.place.column.value]
            c -= 1
            if c.negative?
              raise "must not happen"
            end
            piller_counts[soldier.place.column.value] = c
            self.piece_piller_by_latest_piece = (c == Dimension::Row.dimension_size)
          end
        end
      end

      private

      def piller_counts
        @piller_counts ||= Hash.new(0)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bioshogi-0.0.15 lib/bioshogi/analysis/board_piller_methods.rb