Sha256: 4f5c1d8d1570d23579d5cb77ebcd90656146e3abadf38459a10340cd6bd8942f

Contents?: true

Size: 1.62 KB

Versions: 7

Compression:

Stored size: 1.62 KB

Contents

# -*- compile-command: "ruby -I../.. -rbioshogi -e Bioshogi::Explain::TacticHitTurnTableGenerator.new.generate" -*-

module Bioshogi
  module Explain
    class TacticHitTurnTableGenerator
      def generate
        body = body_format(turns_hash.inspect)
        output_file.write(template % { body: body })
        puts "write: #{output_file}"
      end

      private

      def turns_hash
        hash = {}
        Explain::TacticInfo.all_elements.each do |e|
          print "."
          info = e.sample_kif_info
          found = info.formatter.container.hand_logs.each.with_index do |hand_log, i|
            if hand_log.skill_set.flat_map { |e| e.flat_map(&:key) }.include?(e.key)
              hash[e.key.to_s] = i.next
              break true
            end
          end
          unless found
            raise e.key
          end
        end
        puts
        hash
      end

      def output_file
        Pathname("#{__dir__}/tactic_hit_turn_table.rb").expand_path
      end

      def template
        o = []
        o << "# -*- frozen_string_literal: true -*-"
        o << "# #{__FILE__} から生成しているので編集するべからず"
        o << "module Bioshogi"
        o << "  module Explain"
        o << "    TacticHitTurnTable = {"
        o << "%{body}"
        o << "    }"
        o << "  end"
        o << "end"
        o.join("\n") + "\n"
      end

      def body_format(str)
        str = str.gsub(/{/, "")
        str = str.gsub(/}/, ",")
        str = str.gsub(/\s*,\s*/, ",\n")
        str = str.gsub(/^(?=")/, "  " * 3)
        str = str.gsub("=>", " => ")
        str = str.rstrip
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

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