バトエンのシミュレータ

ドラゴンクエスト バトエンG のシミュレータっぽいものを書いた。
えんぴつのデータはYAML使って読み込むよっ( `・ω・´)
まだまだ雛形といった感じだけど、
発展させていくと面白いものができるかもねぎ。
今のところは、CUIで画面をただ見つめるのみ。
出力はこんな感じ。

$ ruby -Ks batoen.rb
---- 参加者 ----
名前: バトルアックス
マーク: ●

名前: ワルぼう
マーク: ★

----------------

バトルアックスのターン
	★に60のダメージ
	ワルぼうは60のダメージをうけた
	ワルぼうの残りHPは40
ワルぼうのターン
	全員に30のダメージ
	バトルアックスは30のダメージをうけた
	バトルアックスの残りHPは70
バトルアックスのターン
	ミス
ワルぼうのターン
	★に30のダメージ
バトルアックスのターン
	●に60のダメージ
ワルぼうのターン
	全員に30のダメージ
	バトルアックスは30のダメージをうけた
	バトルアックスの残りHPは40
バトルアックスのターン
	★に60のダメージ
	ワルぼうは60のダメージをうけた
	ワルぼうの残りHPは-20
バトルアックスの勝利!!


以下はソース

batoen.rb

require 'yaml'
require 'pp'

class Batoen
  def initialize(ply_num)
    ply_num = ply_num
    yaml_file = 'batoen.yml'
    @yaml_data = nil
    @pencils = Array.new
    
    read_yaml_data(yaml_file)
    create_pencils(ply_num)
  end

  def read_yaml_data(yaml_file)
    content = ''
    open(yaml_file) do |file|
      content = file.read
    end
    
    @yaml_data = YAML.load(content)
    #pp @yaml_data
  end

  def create_pencils(num)
    puts '---- 参加者 ----'
    
    num.times do
      r = rand(@yaml_data.size)
      pencil = @yaml_data[r].dup
      @pencils << pencil
      puts "名前: #{pencil['name']}"
      case pencil['mark']
      when 'maru'
        puts "マーク: ●"
      when 'hosi'
        puts "マーク: ★"
      end
      puts
    end
    
    puts '----------------'
    puts

    #pp @pencils
  end
  
  def fight
    #debug
    count = 0
    
    loop do
      #debug
      count += 1
      exit if count == 5
      
      @pencils.each_with_index do |pencil, idx|
        r = rand(pencil['spots'].size)
        spot = pencil['spots'][r]
        puts "#{pencil['name']}のターン"
        puts "\t#{spot['str']}"
        
        case spot['type']
        when 'attack'
          case spot['mark']
          when 'maru'
            @pencils.each_with_index do |p, i|
              next if idx == i
              
              if p['mark'] == 'maru'
                p['hp'] -= spot['point']
                puts "\t#{p['name']}#{spot['point']}のダメージをうけた"
                puts "\t#{p['name']}の残りHPは#{p['hp']}"
              end
            end
          when 'hosi'
            @pencils.each_with_index do |p, i|
              next if idx == i

              if p['mark'] == 'hosi'
                p['hp'] -= spot['point']
                puts "\t#{p['name']}#{spot['point']}のダメージをうけた"
                puts "\t#{p['name']}の残りHPは#{p['hp']}"
              end
            end
          when 'all'
            @pencils.each_with_index do |p, i|
              next if idx == i
              
              p['hp'] -= spot['point']
              puts "\t#{p['name']}#{spot['point']}のダメージをうけた"
              puts "\t#{p['name']}の残りHPは#{p['hp']}"
            end
          end
        when 'miss'
        end

        # 生存者が1人のときはそいつの勝利でゲーム終了
        if winner = alivers_check
          puts "#{winner}の勝利!!"
          exit
        end
      end
    end
  end

  def alivers_check
    # 生存者の名前の配列
    alivers = Array.new
    @pencils.each do |pencil|
      #puts pencil['hp']
      if pencil['hp'] > 0
        alivers << pencil['name']
      end
    end
    
    #puts "alivers.size = #{alivers.size}"    
    # 生存者が1人のとき
    if alivers.size == 1
      return alivers.shift
    else
      return nil
    end
  end
end

# 使ってNEEEEEE
class Pencil
  def initialize(name)
    @name = name
    @mark = ""
    @spots = Array.new(6, Hash.new)
  end
end


ply_num = ARGV[0] || 2
batoen = Batoen.new(ply_num.to_i)
batoen.fight

batoen.yaml

- name: バトルアックス
  mark: maru
  hp: 100
  spots: 
    - str: ●に60のダメージ
      type: attack
      point: 60
      mark: maru
    - str: ミス
      type: miss      
    - str: ★に60のダメージ
      type: attack
      point: 60
      mark: hosi
    - str: ●に60のダメージ
      type: attack
      point: 60
      mark: maru
    - str: ミス
      type: miss
    - str: ★に60のダメージ
      type: attack
      point: 60
      mark: hosi
- name: ワルぼう
  mark: hosi
  hp: 100
  spots:
    - str: ★に30のダメージ
      type: attack
      point: 30
      mark: hosi
    - str: ヒャド  ●に40のダメージ
      type: attack
      point: 40
      mark: maru
    - str: 全員に30のダメージ
      type: attack
      point: 30
      mark: all
    - str: ギラ  ★に40のダメージ
      type: attack
      point: 40
      mark: hosi
    - str: ●に30のダメージ
      type: attack
      point: 30
      mark: maru
    - str: 全員に30のダメージ
      type: attack
      point: 30
      mark: all