回答編集履歴

1 partial file

hello-world

hello-world score 1281

2015/08/07 15:08  投稿

こんな感じでどうですか?
```lang-ruby
module Game
 TABLE = {}.tap { |hash|
   hash[[Priest, Wizard]] = { cleverness: 0.75 }
   hash[[Wizard, Fighter]] = { strength: 0.85 }
   hash[[Fighter, Priest]] = { strength: 0.95, cleverness: 0.9 }
 }
 module_function
 def battle(a, b)
   a_power = calc_power(a, TABLE[[b.class, a.class]])
   b_power = calc_power(b, TABLE[[a.class, b.class]])
   case
   when a_power > b_power
     "勝利"
   when a_power < b_power
     "敗北"
   else
     "未決着"
   end
 end
 def calc_power(person, bonus)
   str = person.strength
   clv = person.cleverness
   if bonus != nil
     if bonus[:strength]
       str *= bonus[:strength]
     end
     if bonus[:cleverness]
       clv *= bonus[:cleverness]
     end
   end
   str + clv
 end
end
```  
```lang-ruby
f = Fighter.new(100, 100)
w = Wizard.new(100, 100)
p = Priest.new(100, 100)
 
puts "--- 従来の戦闘計算"  
puts f.battle(w)
puts w.battle(p)
puts p.battle(f)
puts "==================="
puts "--- 新しい戦闘計算"
puts Game.battle(f, w)
puts Game.battle(w, p)
puts Game.battle(p, f)
```

思考するエンジニアのためのQ&Aサイト「teratail」について詳しく知る