C
C#•3mo ago
CarlJohnson

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"); } }
No description
7 Replies
Angius
Angius•3mo ago
I don't see you limit damage to 100 anywhere The debugger will be your friend
CarlJohnson
CarlJohnsonOP•3mo ago
i think the calculation is not good
D4rkwills
D4rkwills•3mo ago
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:
int LIMIT = 100;
var x = 10;

x += 200;

if (x > LIMIT)
x = LIMIT
int LIMIT = 100;
var x = 10;

x += 200;

if (x > LIMIT)
x = LIMIT
x will never transpass 100
CarlJohnson
CarlJohnsonOP•3mo ago
the line attackCalc = attack; should be in the do-while statement right so that each time it runs it updates to 50
D4rkwills
D4rkwills•3mo ago
That's right
CarlJohnson
CarlJohnsonOP•3mo ago
aha now i see it works @D4rkwills thx
D4rkwills
D4rkwills•3mo ago
You're welcome 🤗🤗
Want results from more Discord servers?
Add your server