【问题标题】:Refactoring Pokemon type weekness重构口袋妖怪类型的弱点
【发布时间】:2020-06-10 19:37:27
【问题描述】:

我用 ruby​​ 制作了 pokemon weekness finder。这需要很多话。 你能建议编写这段代码有点漂亮和复杂吗?

菜单如下。

puts "Which type of Pokemon do you want to know weaknesses? 

Menu

  1 Normal
  2 Fire
  3 Water
  4 Electric
  5 Grass
  6 Ice
  7 Fighting
  8 Poison
  9 Ground
  10 Flying
  11 Psychic
  12 Bug
  13 Rock
  14 Ghost
  15 Dragon
  16 Dark
  17 Steel
  18 Fairy

然后我获取用户的响应并打印弱点供他们选择。

  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

【问题讨论】:

  • 一个好的起点是创建一个如下所示的哈希:h = { 1 =>{ prompt: 'Normal', weakness: 'Fighting' }; 2 =>{ prompt: 'Fire', weakness: 'Ground, Rock, Water' }; ..., 18=>{ prompt: 'Fairy' weakness: 'Poison, Steel' }}。然后你可以用h.each { |nbr,g| print "#{nbr} #{prompt}" }显示菜单,如果用户输入s,那么n = s.to_i; if h.key?(n); puts h[i][:weakness]; else <deal with invalid entry>
  • @CarySwoveland 好收获 ;)

标签: ruby pokemon-go


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    相关资源
    最近更新 更多