Need help with a bug
Hi, im currently making one of my exercises and i am stuck on a bug. Im making a bossfight thing based on random numbers if it is a critical damage or not. The bug im stuck is the one on the picture. it seems that the damage goes to 200 but the max damage is 100 when critical and 50 when normal. does anyone see the mistake i made?
my code:
class Program
{
static void Main()
{
int attack = 50;
double critChance = 0.33;
int bossHP = 1000;
Random damage = new();
int attackCalc = attack;
do
{
double chance = damage.NextDouble();
if (chance < critChance)
{
attackCalc *= 2;
}
bossHP -= attackCalc;
if (bossHP < 0)
{
bossHP = 0;
}
Console.WriteLine($"Boss HP: {bossHP}");
Console.WriteLine($"Damage: {attackCalc}");
}
while (bossHP > 0);
Console.WriteLine("Boss defeated");
}
}
7 Replies
I don't see you limit damage to 100 anywhere
The debugger will be your friend
i think the calculation is not good
You are not setting attackCalc to default value after crit damage
See, you are doing attackCalc *= 2, updating it to 100, and then again updating to 200 (in the loop operations)
You have to set attackCalc = attack before calculate it, because it would grow undefinetely.
It's a good practice also using const flags to limit values, like:
x will never transpass 100
the line attackCalc = attack; should be in the do-while statement right
so that each time it runs it updates to 50
That's right
aha
now i see
it works
@D4rkwills thx
You're welcome 🤗🤗