0

我正在为角色扮演游戏创建一个点购买方法,玩家可以在其中修改角色的能力得分。if当输入应该正确时,我的条件没有得到满足,我不知道为什么。

$player = {
  abils: {str: 10, con: 10, dex: 10, wis: 10, int: 10, cha: 10}
}


def abil_point_buy(x)
  points = x
  puts "<Add or subtract points from each ability (e.g. +2 STR, -1 CHA, etc.)>"
  loop do
    puts "<You have #{points} points remaining. Current ability scores:>"; print "<"
    $player[:abils].each { |abil, pts| print "#{abil.upcase}: #{pts} "}; puts ">"
    input = gets.chomp.downcase
    abil_check = $player[:abils][input.gsub("^a-z", "").to_sym]
    if abil_check && input.match?(/\d/) #checks if input contains a number and an ability score
      mod = input.gsub(/\D/, "") #determines amount to modify ability score by
      pos_or_neg = !input.include?('-') #determines if mod will be positive or negative
      $player[:abils][abil_check] + mod.to_i * (pos_or_neg ? 1 : -1) #adjusts selected ability score by mod
      pos_or_neg ? points - mod : points + mod
      break if points == 0
    else
      puts "Something is wrong with your input."
    end
  end
end

还将感谢改进我的代码的一般提示。

4

1 回答 1

1

您在gsub. ^a-z被视为“行首”,后跟a破折号和z. 您需要将其替换为否定字符类和downcase字符串:

"2 STR".downcase.gsub(/[^a-z]/, '')
# => "str"
于 2019-11-12T22:03:09.077 回答