I'm creating a text-based game, and in the game when an enemy hits you, the amount of damage done is randomly generated, however, in a while loop it repeats the generated number.
Expected Outcome
Okay, lets fight him!
The monster has a health of 25! What do you wish to do?
[Press 0 to attack.]
0
The monster's health went down by 2. Their health is now 23
The monster attacks! Your health went down by 9. Your health is now 41
The monster has a health of 23! What do you wish to do?
[Press 0 to attack.]
0
The monster's health went down by 2. Their health is now 21
The monster attacks! Your health went down by 10. Your health is now 31
The monster has a health of 21! What do you wish to do?
[Press 0 to attack.]
Output
Okay, lets fight him!
The monster has a health of 25! What do you wish to do?
[Press 0 to attack.]
0
The monster's health went down by 2. Their health is now 23
The monster attacks! Your health went down by 3. Your health is now 47
The monster has a health of 23! What do you wish to do?
[Press 0 to attack.]
0
The monster's health went down by 2. Their health is now 21
The monster attacks! Your health went down by 3. Your health is now 44
The monster has a health of 21! What do you wish to do?
[Press 0 to attack.]
You can see that in the output the amount of damage the enemy does is repeated. Why is it doing this and how do I fix it?
Code:
using System;
using Game;
public class Zero
{
public static void G1()
{
while (Player.HP > 0||Enemy.h > 0)
{
Console.WriteLine($"The monster has a health of {Enemy.h}! What do you wish to do?");
Console.WriteLine("[Press 0 to attack.]");
string read_action = Console.ReadLine();
if (read_action == "0")
{
Enemy.h = Enemy.h - Player.At;
Player.HP = Player.HP - Enemy.at;
Console.WriteLine($"The monster's health went down by {Player.At}. Their health is now {Enemy.h}");
Console.WriteLine($"The monster attacks! Your health went down by {Enemy.at}. Your health is now {Player.HP}");
}
}
}
}
Variables that were in another file:
public static Random numGen = new Random();
public static int at = numGen.Next(1, 11); //this is the enemy's attack function
Aucun commentaire:
Enregistrer un commentaire