Sha256: 0cd6f45e737e5ceb8f5ff0416cbeb26413a5a4c376668ecb092277fc6b4dee00

Contents?: true

Size: 1.52 KB

Versions: 7

Compression:

Stored size: 1.52 KB

Contents

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

module Bioshogi
  module Explain
    module BoardPillerMethods
      attr_accessor :piece_piller_by_latest_piece

      def place_on(soldier, options = {})
        super

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

      # 現在の状態は駒柱がある状態か?
      def piece_piller_by_latest_piece?
        piller_counts.each_value.any? { |c| c >= Dimension::PlaceY.dimension } # 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.x.value]
            c -= 1
            if c.negative?
              raise "must not happen"
            end
            piller_counts[soldier.place.x.value] = c
            self.piece_piller_by_latest_piece = (c == Dimension::PlaceY.dimension)
          end
        end
      end

      private

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
bioshogi-0.0.10 lib/bioshogi/explain/board_piller_methods.rb
bioshogi-0.0.9 lib/bioshogi/explain/board_piller_methods.rb
bioshogi-0.0.8 lib/bioshogi/explain/board_piller_methods.rb
bioshogi-0.0.7 lib/bioshogi/explain/board_piller_methods.rb
bioshogi-0.0.5 lib/bioshogi/explain/board_piller_methods.rb
bioshogi-0.0.4 lib/bioshogi/explain/board_piller_methods.rb
bioshogi-0.0.3 lib/bioshogi/explain/board_piller_methods.rb