Addition of variables in iterative processing in a while statement

I'm writing a Ruby program and I'm stuck, so I'll post a question here.

Currently, I am working on creating a Dragon Quest program that often appears as an example of a ruby program on the net.

The specifications you want to implement are as follows.

Assumed specifications

--Attack until slime (enemy) appears and slime's hp (hit points) are exhausted --Slime's hp (hit points) is 6. ――The attack is turn-based, and you are always ahead of yourself. --Your (warrior) hp (hit points) is 10. --Damage is always 1 due to slime (enemy) attack --You can select attack (naguru) and recovery spell (hoimi) every turn --Your attack randomly removes slime (enemy) hp from 1.2.3. --The recovery spell (Hoimi) restores hp to 10. --However, recovery spells sometimes fail (conditioned by your hp at the time the spell is used) --If you select anything other than attack (naguru) and recovery spell (hoimi), the turn is forcibly ended and you move to the slime turn.

It's roughly like this.

Question matters

I can't write a process that accumulates the numbers of my (warrior) attacks while repeating the process in the while statement.

In particular, [Turn 1] Preceding: Warrior attack. 2 damage to slime. High school: Slime attack. 1 damage to the warrior. [Turn 2] Preceding: Warrior attack. 2 damage to slime. High school: Slime attack. 1 damage to the warrior. [Turn 3] Preceding: Warrior attack. 2 damage to slime. High school: Slime attack. 1 damage to the warrior.

In the above case, you should be able to defeat slime with 2x3 = 6. However, the numbers do not seem to be piled up and will be repeated forever.

The actual code is below.

puts "Slime appeared"

hp = 6
damage = 0
ownhp = 10
while damage < hp do
  puts ""
  puts "Warrior turn"
  puts "Please choose an attack. 1: Naguru 2: Hoimi"
  attack = gets.to_i
  damage=[1,2,3].sample
    if attack == 1
      puts "Warrior attack."
      puts "To slime#{damage}Damage"
      damage += damage
    elsif attack == 2
      case ownhp
        when 10
        puts ""
        puts "The warrior chanted Hoimi. But the warrior is full of energy! Nothing happened."
        when 1..4,6..8
        puts ""
        puts "The warrior chanted Hoimi. The warrior is full of energy."
        else
        puts ""
        puts "The spell failed."
      end
    else
      puts ""
      puts "This command is not an option. End the turn."
    end
  if damage < 6
  puts ""
  puts "Slime turn" 
  puts "Slime attack. "1" damage to the warrior" 
  ownhp -=1
  end
end

puts "The warrior defeated slime. I got 10 experience points."

It is above.

In fact, you may defeat slime. That is when you (warrior) attack and you get a 3.

As a test,

① If 1 or 2 appears several times and it does not end even if it reaches 6 in total ② When 3 comes out and ends

I will write the code for each of the above irbs.

① If 2 does not end even after several times

ec2-user:~/environment/ruby-book $ ruby lib/dorakue.rb
Slime appeared

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
1
Warrior attack.
"2" damage to slime

Slime turn
Slime attack. "1" damage to the warrior

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
1
Warrior attack.
"1" damage to slime

Slime turn
Slime attack. "1" damage to the warrior

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
1
Warrior attack.
"1" damage to slime

Slime turn
Slime attack. "1" damage to the warrior

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
1
Warrior attack.
"1" damage to slime

Slime turn
Slime attack. "1" damage to the warrior

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
1
Warrior attack.
"1" damage to slime

Slime turn
Slime attack. "1" damage to the warrior

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
1
Warrior attack.
"2" damage to slime

Slime turn
Slime attack. "1" damage to the warrior

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
1
Warrior attack.
"2" damage to slime

Slime turn
Slime attack. "1" damage to the warrior

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
1
Warrior attack.
"2" damage to slime

Slime turn
Slime attack. "1" damage to the warrior

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
1
Warrior attack.
"1" damage to slime

Slime turn
Slime attack. "1" damage to the warrior

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
1
Warrior attack.
"2" damage to slime

Slime turn
Slime attack. "1" damage to the warrior

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
1
Warrior attack.
"2" damage to slime

Slime turn
Slime attack. "1" damage to the warrior

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
1
Warrior attack.
"2" damage to slime

Slime turn
Slime attack. "1" damage to the warrior

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
1
Warrior attack.
"1" damage to slime

Slime turn
Slime attack. "1" damage to the warrior

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
1
Warrior attack.
"2" damage to slime

Slime turn
Slime attack. "1" damage to the warrior

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
1
Warrior attack.
"2" damage to slime

Slime turn
Slime attack. "1" damage to the warrior

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
1
Warrior attack.
"1" damage to slime

Slime turn
Slime attack. "1" damage to the warrior

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
1
Warrior attack.
"2" damage to slime

Slime turn
Slime attack. "1" damage to the warrior

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
^CTraceback (most recent call last):
        2: from lib/dorakue.rb:10:in `<main>'
        1: from lib/dorakue.rb:10:in `gets'
lib/dorakue.rb:10:in `gets': Interrupt

I forced it to end.

② When 3 comes out and ends

ec2-user:~/environment/ruby-book $ ruby lib/dorakue.rb
Slime appeared

Warrior turn
Please choose an attack. 1: Naguru 2: Hoimi
1
Warrior attack.
"3" damage to slime
The warrior defeated slime. I got 10 experience points.

It looks like this.

Possible factors

damage += damage

I think the above part is wrong in the code I wrote to accumulate the damage, but I don't know how to fix it. If you get a 3, I think you can beat it with 3 + 3 = 6. In other words, it is a form that is added only in the lap rather than repeated processing.

I would be grateful if you could teach me.

Thank you very much.

Recommended Posts

Addition of variables in iterative processing in a while statement
Display all in Iterator by iterative processing by while in ArrayList array
[Swift] "for-in statement" about iterative processing
Implementation of asynchronous processing in Tomcat
Order of processing in the program
[Ruby basics] About the role of true and break in the while statement
How to insert processing with any number of elements in iterative processing in Ruby
Implementation of multi-tenant asynchronous processing in Tomcat
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
Iterative processing
Do you use the for statement after all? Do you use a while statement? Proper use of for statement and while statement
How to start a subscript from an arbitrary number in Ruby iterative processing
Escape processing when creating a URL in Ruby
[Ruby] Behavior of evaluation of conditional expression in while
Stuck in an enum in front of a blacksmith
Create a simple batch processing framework in Eclipse.
Measure the size of a folder in Java
Draw a too beloved Mandelbrot set in Processing
A quick review of Java learned in class
Create a native extension of Ruby in Rust
Count the frequency of occurrence of words in a sentence by stream processing (Apache Apex)