Ruby 重构口袋妖怪类型的weekness

Ruby 重构口袋妖怪类型的weekness,ruby,pokemon-go,Ruby,Pokemon Go,我用ruby制作了口袋妖怪weekness finder。这需要很多话。 你能建议写这段代码时要有点精致和复杂吗 菜单如下 put“你想知道哪种口袋妖怪的弱点? 菜单 1正常 2火灾 3水 4电气 5草 6冰 7战斗 8毒药 9地 10飞行 11灵媒 12小虫 13岩石 14鬼 15龙 16暗 17钢 18仙女 然后,我获取用户的响应,并打印弱点以供选择 type = gets.to_i case type when 1 #Normal puts "Fighting"

我用ruby制作了口袋妖怪weekness finder。这需要很多话。 你能建议写这段代码时要有点精致和复杂吗

菜单如下

put“你想知道哪种口袋妖怪的弱点?
菜单
1正常
2火灾
3水
4电气
5草
6冰
7战斗
8毒药
9地
10飞行
11灵媒
12小虫
13岩石
14鬼
15龙
16暗
17钢
18仙女
然后,我获取用户的响应,并打印弱点以供选择

  type = gets.to_i

  case type 
  when 1 #Normal
    puts "Fighting"
  when 2  #Fire
    puts "Ground, Rock, Water"
  when 3 #water
    puts "Fire, Ground, Rock"
  when 4 #Electric
    puts "Ground"
  when 5 #Grass
    puts "Bug, Fire, Flying, Ice, Poison"
  when 6 #Ice
    puts "Fighting, Fire, Rock, Steel"
  when 7 #Fighting
    puts "Fairy, Flying, Psychic"
  when 8 #Poison
    puts "Ground, Psychic"
  when 9 #Ground
    puts "Grass, Ice, Water"
  when 10 #Flying
    puts "Electric, Ice, Rock"
  when 11 #Psychic
    puts "Bug, Dark, Ghost"
  when 12 #Bug
    puts "Fire, Flying, Rock"
  when 13 #Rock
    puts "Fighting, Grass, Ground, Steel, Water"
  when 14 #Ghost
    puts "Dark, Ghost"
  when 15 #Dragon
    puts "Dragon, Fairy, Ice"
  when 16 #Dark
    puts "Bug, Fairy, Fighting"
  when 17 #Steel
    puts "Fighting, Fire, Ground"
  when 18 #Fairy
    puts "Poison, Steel"
  else
    puts "Error"
  end

我认为您可以从游戏数据的结构开始:

game_data = {
  1 => ["Normal", "Fighting" ],
  2 => ["Fire", "Ground, Rock, Water"],
  3 => ["Water", "Fire, Ground, Rock"],
  4 => ["Electric", "Ground"],
  5 => ["Grass", "Bug, Fire, Flying, Ice, Poison"],
  6 => ["Ice", "Fighting, Fire, Rock, Steel"],
  7 => ["Fighting", "Fairy, Flying, Psychic"],
  8 => ["Poison", "Ground, Psychic"],
  9 => ["Ground", "Grass, Ice, Water"],
  10 => ["Flying", "Electric, Ice, Rock"],
  11 => ["Psychic", "Bug, Dark, Ghost"],
  12 => ["Bug", "Fire, Flying, Rock"],
  13 => ["Rock", "Fighting, Grass, Ground, Steel, Water"],
  14 => ["Ghost", "Dark, Ghost"],
  15 => ["Dragon", "Dragon, Fairy, Ice" ],
  16 => ["Dark", "Bug, Fairy, Fighting"],
  17 => ["Steel", "Fighting, Fire, Ground"],
  18 => ["Fairy", "Poison, Steel" ]
}
现在您可以使用它了:

puts "Which type of Pokemon do you want to know weaknesses?\nMenu"
game_data.each { |point, data| puts "#{point} #{data.first}" }
type = gets.to_i
return "Error" unless game_data.has_key?(type)
puts game_data[type].last

对于未来的建议,如果案例陈述变得太大,最好考虑使用
散列

我认为您可以从游戏数据的结构开始:

game_data = {
  1 => ["Normal", "Fighting" ],
  2 => ["Fire", "Ground, Rock, Water"],
  3 => ["Water", "Fire, Ground, Rock"],
  4 => ["Electric", "Ground"],
  5 => ["Grass", "Bug, Fire, Flying, Ice, Poison"],
  6 => ["Ice", "Fighting, Fire, Rock, Steel"],
  7 => ["Fighting", "Fairy, Flying, Psychic"],
  8 => ["Poison", "Ground, Psychic"],
  9 => ["Ground", "Grass, Ice, Water"],
  10 => ["Flying", "Electric, Ice, Rock"],
  11 => ["Psychic", "Bug, Dark, Ghost"],
  12 => ["Bug", "Fire, Flying, Rock"],
  13 => ["Rock", "Fighting, Grass, Ground, Steel, Water"],
  14 => ["Ghost", "Dark, Ghost"],
  15 => ["Dragon", "Dragon, Fairy, Ice" ],
  16 => ["Dark", "Bug, Fairy, Fighting"],
  17 => ["Steel", "Fighting, Fire, Ground"],
  18 => ["Fairy", "Poison, Steel" ]
}
现在您可以使用它了:

puts "Which type of Pokemon do you want to know weaknesses?\nMenu"
game_data.each { |point, data| puts "#{point} #{data.first}" }
type = gets.to_i
return "Error" unless game_data.has_key?(type)
puts game_data[type].last

对于未来的建议,如果case语句变得太大,考虑使用
Hash

是一个很好的起点,可以创建如下哈希:
h={1=>{prompt:'Normal',弱点:'Fighting'};2=>{prompt:'Fire',弱点:'Ground Rock,Water'};…,18=>{prompt:'Fairy'弱点:'Poison,Steel'}}
。然后你可以用
h显示菜单。每个{nbr,g | print“{nbr}{prompt}}
如果用户输入
s
,则
n=s.to\u i;如果h.key?(n);将h[i][:弱点];else
@CarySwoveland good catch;)一个很好的起点是创建如下哈希:
h={1=>{prompt:'Normal',弱点:'Fighting'};2=>{prompt:'Fire',弱点:'Ground,Rock,Water'};…,18=>{prompt:'Fairy'弱点:'毒物Steel'}
。然后你可以用
h显示菜单{nbr,g|print“{nbr}{prompt}}
如果用户输入
s
,则
n=s.to_i;如果h.key?(n);放入h[i][:弱点];否则
@CarySwoveland good catch;)