创建一个 RPG 积分购买系统。为什么不满足我的 if 条件?

Creating an RPG point buy system. Why is my if condition not being met?

我正在为角色扮演游戏创建点数购买方法,玩家可以在其中修改角色的能力得分。当输入应该正确时,我的 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

也非常感谢改进我的代码的一般提示。

您在 gsub 中使用的模式无效。 ^a-z 被视为 "beginning of the line" 后跟 a 破折号和 z。您需要将其替换为否定字符 class 和 downcase 字符串:

"2 STR".downcase.gsub(/[^a-z]/, '')
# => "str"