I have problem to work out in which 2 hypothetical oppenents are dueling RPG style. I am asked to give them HP and each round of the loop HP is lost respectiviely to what their oppenent has hit them for. I've got the basic understanding for it but my numbers keep on repeating. Working on Visual Studios 15. Thanks in advance!
/*Fighter 1 is Charlie The Unicorn
*Fighter 2 is Nyan Cat
*Fighter 1 has 100 HP
*Fighter 2 has 150 HP
*Fighter 1 can hit as hard as 15HP each turn
*Fighter 2 can hit as hard as 10HP each turn
*/
//Who wins the fight
int fighterOneHealth = 100;
int fighterTwoHealth = 150;
Random randomizer = new Random();
while (fighterOneHealth > 0 || fighterTwoHealth > 0)
{
int fOneRandomHit;
int fTwoRandomHit;
fOneRandomHit = randomizer.Next(0, 15);
fTwoRandomHit = randomizer.Next(0, 10);
int fightOneHpLoss = fighterOneHealth - fTwoRandomHit;
int fightTwoHpLoss = fighterTwoHealth - fOneRandomHit;
Console.WriteLine("After attack: Charlie the Unicorn HP: {0}, Nyan Cat HP: {1}", fightOneHpLoss, fightTwoHpLoss);
}
You're declaring new variables inside the loop:
int fightOneHpLoss = fighterOneHealth - fTwoRandomHit;
int fightTwoHpLoss = fighterTwoHealth - fOneRandomHit;
... but you're never modifying the variables which are meant to represent the players' current health. You don't need those extra variables at all - you just need to reduce the health:
fighterOneHealth -= fTwoRandomHit;
fighterTwoHealth -= fOneRandomHit;
Console.WriteLine("After attack: Charlie the Unicorn HP: {0}, Nyan Cat HP: {1}",
fighterOneHealth, fighterTwoHealth);
See more on this question at Stackoverflow