Sha256: 3edf022a8fa3fa81bf39ee852c8af71be61699bad052139f076bf33c27457ae6

Contents?: true

Size: 1.03 KB

Versions: 9

Compression:

Stored size: 1.03 KB

Contents

# frozen-string-literal: true
#
# 盤上にプレイヤーの特定の種類の駒が何個あるかを O(n) で調べる
# 大駒コンプリートのためだけにある
#
module Bioshogi
  module Board
    module PieceCountsMethods
      def place_on(soldier, options = {})
        super
        piece_counts[location_with_piece_key_from(soldier)] += 1 # soldier.piece.stronger をキーにすると速くなるかも?
      end

      def safe_delete_on(place)
        super.tap do |soldier|
          if soldier
            piece_counts[location_with_piece_key_from(soldier)] -= 1
          end
        end
      end

      def all_clear
        super
        piece_counts.clear
      end

      def piece_count_of(location_key, piece_key)
        piece_counts[[location_key, piece_key]]
      end

      private

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

      # promoted は見ていない
      def location_with_piece_key_from(soldier)
        [soldier.location.key, soldier.piece.key]
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
bioshogi-0.0.15 lib/bioshogi/board/piece_counts_methods.rb
bioshogi-0.0.14 lib/bioshogi/board/piece_counts_methods.rb
bioshogi-0.0.10 lib/bioshogi/board/piece_counts_methods.rb
bioshogi-0.0.9 lib/bioshogi/board/piece_counts_methods.rb
bioshogi-0.0.8 lib/bioshogi/board/piece_counts_methods.rb
bioshogi-0.0.7 lib/bioshogi/board/piece_counts_methods.rb
bioshogi-0.0.5 lib/bioshogi/board/piece_counts_methods.rb
bioshogi-0.0.4 lib/bioshogi/board/piece_counts_methods.rb
bioshogi-0.0.3 lib/bioshogi/board/piece_counts_methods.rb