【发布时间】:2017-09-09 23:54:44
【问题描述】:
我想创建一个计数器,如果发生某些事情,它会重置为 0。它看起来像这样:
计数器 = 0
如果发生了什么事
counter += 1
elsif 发生了其他事情
*counter is set back to 0*
结束
非常感谢!
编辑
def attack(cookie)
cookie.changeDefense()
@offense = Random.new.rand(0..100)
win_count = 0
puts "#{cookie.name}'s health is #{cookie.health}"
puts "The foe's health is #{@health}"
puts win_count
puts "Do you want to fight?"
response = gets.chomp
if response == "yes"
if @offense > cookie.defense
cookie.health -= 10
puts "The foe has defeated you!"
win_count += 1
else
puts "You win! The foe has lost health!"
@health -= 10
win_count = 0
number = Random.new.rand(0..100)
if number.between?(15, 50)
puts "The enemy has dropped a health item!"
cookie.health += 5
elsif number.between?(60, 95)
puts "The enemy has dropped a fighting upgrade! You
have a hightened chance of winning!"
cookie.lowDefense += 10
else
end
end
这是代码,它来自基于文本的游戏,所以那里不仅仅是计数器。
另一个编辑
我已通过在此处未显示的部分代码中使用全局变量来解决此问题,该问题无法删除但无需更多帮助。谢谢!
【问题讨论】:
-
普通作业
c = 0应该没问题。
标签: ruby-on-rails ruby variables