* RUBY *极限初学者卡住了

时间:2013-01-05 15:55:12

标签: ruby

我无法弄清楚这个基于文本的RPG的指导。我希望玩家的输入在四个类中的一个之间进行选择,然后保存该类并为其分配统计数据。目前,它仅在我选择"Warrior"时才有效。我做错了什么?

stats = Hash.new
stats["Strength"] = 10
stats["Dexterity"] = 10
stats["Charisma"] = 10
stats["Stamina"] = 10
puts "Hello, brave adventurer.  What is your name?"
player_name = gets.chomp.capitalize

puts "Well, #{player_name}, you are certainly brave!  Choose your profession.  (Choose         from Warrior, Wizard, Archer, or Thief)."

player_class = gets.chomp.downcase

while player_class != ("warrior" || "thief" || "archer" || "wizard")
  puts "I do not recognize #{player_class} as a valid class.  Please choose between     Warrior, Wizard, Archer, or Thief."
  player_class = gets.chomp.downcase
end

if player_class == "warrior"
  puts "Yay a warrior!"
  stats["Strength"] = 20
elsif player_class == "thief"
  puts "yay a thief!"
  stats["Dexterity"] = 20
elsif player_class == "archer"
  puts "yay an archer!"
elsif player_class == "wizard"
  puts "Sweet a wizard!"
end

3 个答案:

答案 0 :(得分:3)

这很简单。

1.9.3p194 :001 > ("warrior" || "thief" || "archer" || "wizard")
 => "warrior" 

几个字符串的逻辑OR评估为第一个字符串。

您可以用以下内容替换该行:

while player_class != "warrior" and player_class != "thief" and player_class !=  "archer" and player_class != "wizard"

答案 1 :(得分:2)

尝试将类设置为数组...

player_classes = ["warrior", "thief", "archer", "wizard"]

然后当你想检查玩家是否进入了有效的班级时......

while ! player_classes.include? player_class 

代替。

您可以使用偶数更好成语来表示单词......

%w(warrior thief archer wizard)

生成

["warrior", "thief", "archer", "wizard"]

前进

通过将播放器类放入哈希值,您可以将此方法向前推进一步。

例如:

player_classes = {
  'warrior' => {:message => "Yay a warrior!", :stats => {:strength => 20} },
  'thief'   => {:message => "Ooh a thief!", :stats => {:dexterity => 20} },
  'archer'  => {:message => "Cool! an archer" },
  'wizard'  => {:message => "Sweet! a wizard" }
}

然后你可以做这样的事情:

while ! player_classes.key? player_class

一旦你得到一个匹配,你就可以从哈希值中拉出这些值,如下所示:

selected_class = player_classes[player_class]
stats.merge selected_class[:stats] if selected_class[:stats] 

如果该玩家类的散列中没有任何统计信息,则不会发生任何事情,如果存在,则会合并。

e.g。测试这个......

selected_class = player_classes['warrior']
stats.merge selected_class[:stats] if selected_class[:stats] 

# stats is now {:strength=>20, :dexterity=>10, :charisma=>10, :stamina=>10}

selected_class = player_classes['wizard']
stats.merge selected_class[:stats] if selected_class[:stats] 

# stats is now {:strength=>10, :dexterity=>10, :charisma=>10, :stamina=>10}

然后我们可以用以下信息显示消息:

puts player_classes[player_class][:message]

这会将你的逻辑降低到捕获玩家类输入然后处理散列。

取消原始代码

使用哈希作为简单的数据模型。

你最终会得到这样的代码:

#!/usr/bin/env ruby

stats = { :strength => 10, :dexterity => 10, :charisma => 10, :stamina => 10 }

player_classes = {
  'warrior' => {:message => "Yay a warrior!", :stats => {:strength => 20} },
  'thief'   => {:message => "Ooh a thief!", :stats => {:dexterity => 20} },
  'archer'  => {:message => "Cool! an archer" },
  'wizard'  => {:message => "Sweet! a wizard" }
}

puts "Welcome brave adventurer, what is your name?"
player_name = gets.chomp.capitalize

puts "Well, #{player_name}, you are certainly brave!  Choose your profession.  (Choose from Warrior, Wizard, Archer, or Thief)."
player_class = gets.chomp.downcase

while ! player_classes.key? player_class
  puts "I do not recognize #{player_class} as a valid class.  Please choose between Warrior, Wizard, Archer, or Thief."
  player_class = gets.chomp.downcase
end

selected_class = player_classes[player_class]
stats.merge selected_class[:stats] if selected_class[:stats] 

puts selected_class[:message]

你也应该发现这个更具可读性,但是,当你扩展你的游戏时,你会发现你不能轻易使用这样的代码。接下来,您应该了解如何使用函数将代码分解为不同的例程。您还可以使用数组,哈希和集合执行更多操作。

此外,您应该尽快开始学习以面向对象的方式编写Ruby,理想情况下应该如何使用它。

Tutorials Point is a pretty decent site for learning more about Ruby

答案 2 :(得分:0)

对于你做错了什么,请看迭戈的答案。

这是您应该使用case语句的典型情况。你可以删除例程来获得这样的玩家类:

public
def get_player_class
  case player_class = gets.chomp.downcase
  when "warrior" then puts "Yay a warrior!"; stats["Strength"] = 20
  when "thief" then puts "yay a thief!"; stats["Dexterity"] = 20
  when "archer" then puts "yay an archer!"; true
  when "wizard" then puts "Sweet a wizard!"; true
  else puts "I do not recognize #{player_class} as a valid class. "\
    "Please choose between Warrior, Wizard, Archer, or Thief."; false
  end
end

stats = {"Strength" => 10, "Dexterity" => 10, "Charisma" => 10, "Stamina" => 10,}
puts "Hello, brave adventurer.  What is your name?"
player_name = gets.chomp.capitalize
puts "Well, #{player_name}, you are certainly brave! "\
  "Choose your profession.  (Choose from Warrior, Wizard, Archer, or Thief)."
nil until get_player_class