I am pretty sure someone of you can help me with this issue. I do not find and do not understand the solution.
if (hero.IsAlive || creature.IsAlive == false)
{
if (hero.IsAlive == true)
{
Console.ForegroundColor = ConsoleColor.Red;
EnemyHitInterface();
Console.ForegroundColor = ConsoleColor.White;
Thread.Sleep(1500);
hero.Exp = 150;
baseSetting.YouWin();
hero.UpdateLvL();
hero.CharacterInterface(hero);
hero.PrintStatsHero();
Console.ReadKey();
CheckForItem();
hero.PrintStatsHero();
Console.ReadKey();
}
else if (creature.IsAlive == true)
{
Console.Clear();
CenterText("You are dead. See you next time...", 15);
Console.ReadKey();
Environment.Exit(0);
}
}
Simple if Operation with asks in my opinion, as soon as the hero dies or the creature it will get triggered. If the creature dies, everything is fine, but if the hero dies it wont be triggered. What is the think i do not understand?
For me it is like:
When Hero or Creature dies, go in If hero is alive do this if creature is alive do this
But like I said, as for the creature it wont work
CodePudding user response:
I suspect you read this line
if (hero.IsAlive || creature.IsAlive == false)
in your head as "If hero or creature are dead". What it actually says is "If hero is alive or creature is dead". And if you follow through the rest of your logic, you'll see why you never hit the else if (creature.IsAlive == true)
part.
What you (I think) wanted in the first if statement was
if (hero.IsAlive == false || creature.IsAlive == false) // if either are dead
or, more usually written
if (!hero.IsAlive || !creature.IsAlive) // if either are dead
CodePudding user response:
This looks a simple logic mistake:
if (hero.IsAlive || creature.IsAlive == false)
Is the same as:
if ((hero.IsAlive == true) || (creature.IsAlive == false))
But then, inside the if block you're checking:
if (hero.IsAlive == true)
{
...
}
else if (creature.IsAlive == true)
{
...
}
}
Which, if you combine them is:
if ((hero.isAlive == true && hero.IsAlive == true) || (creature.IsAlive == false && hero.IsAlive == true))
{
...
}
else if ((hero.IsAlive == true && creature.IsAlive == true) || (creature.IsAlive == false && creature.IsAlive == true))
{
...
}
But (creature.IsAlive == false && creature.IsAlive == true)
is always false, and (hero.isAlive == true && hero.IsAlive == true)
is always true.
So, to rejig your logic:
if (hero.IsAlive != creature.IsAlive ) //Either the hero or creature is dead
{
if (hero.IsAlive)
{
//Hero is alive, creature is dead
}
else
{
//Creature is alive, hero is dead.
}
}
else if (hero.IsAlive)
{
//Hero and creature are both alive
}
else
{
//Hero and creature are both dead
}
Should capture the behaviour you want.