Sha256: 4c570166e4f7026e4051e76c7880f2b420e69b5768a94939a391c9beeb242fdd

Contents?: true

Size: 1.56 KB

Versions: 9

Compression:

Stored size: 1.56 KB

Contents

# frozen-string-literal: true
#
# soldier_box = SoldierBox.new
# soldier_box << Soldier.from_str("△5一玉")
# soldier_box << Soldier.from_str("▲6八銀")
# soldier_box.collect(&:name)                                             # => ["△5一玉", "▲6八銀"]
# soldier_box.sorted_soldiers.collect(&:name)                             # => ["▲6八銀", "△5一玉"]
# soldier_box.place_as_key_table.collect { |p, s| [p.name, s.name] }.to_h # => {"5一"=>"△5一玉", "6八"=>"▲6八銀"}
# soldier_box.location_split.transform_values { |e| e.collect(&:name) }   # => {:black=>["▲6八銀"], :white=>["△5一玉"]}
# soldier_box.location_adjust.transform_values { |e| e.collect(&:name) }  # => {:black=>["▲6八銀", "△5一玉"], :white=>["△4二銀", "▲5九玉"]}
#
module Bioshogi
  class SoldierBox < Array
    def soldiers
      self
    end

    def sorted_soldiers
      @sorted_soldiers ||= soldiers.sort.freeze
    end

    def place_as_key_table
      @place_as_key_table ||= soldiers.inject({}) { |a, e| a.merge(e.place => e) }.freeze
    end

    # ▲または△から見た状態に補正した全体のデータ
    def location_adjust
      @location_adjust ||= Location.inject({}) { |a, location|
        a.merge(location.key => sorted_soldiers.collect { |e| e.public_send(location.normalize_key) })
      }.freeze
    end

    # ▲△に分割
    def location_split
      @location_split ||= Location.inject({}) { |a, e| a.merge(e.key => []) }.merge(sorted_soldiers.group_by { |e| e.location.key }).freeze
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

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