liz
liz

Reputation: 21

Reset Variable Value to Original Value? (Ruby) (CLOSED)

I want to make a counter that will reset to 0 if something happens. It would look vaguely like this:

counter = 0

if something happens

counter += 1

elsif something else happens

*counter is set back to 0*

end

Thanks so much!

EDIT

 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

This is the code, it is from a text based game so there is more than just the counter there.

ANOTHER EDIT

I have solved it by using global variables in parts of the code not shown here, the question cannot be deleted but no more assistance is needed. Thank you!

Upvotes: 1

Views: 257

Answers (2)

wille771
wille771

Reputation: 27

make the win_count = 0 in else puts "You win! The foe has lost health!" @health -= 10 win_count = 0 Part to win_count -= 1

Upvotes: 0

wille771
wille771

Reputation: 27

just put `elseif ("something else happens"){ counter = 0 }

this should make counter equal 0 again

Upvotes: 1

Related Questions