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.
--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.
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.
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.
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.
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