C
C#13mo ago
iparalyze

Issue with events

I've been trying to solve this puzzle for days now, but I can't seem to be able to solve it. I'm working on a very simple game with two heroes and two monsters. I've created a system where a TurnsManager method checks if all monsters are dead and if there's at least one character left, it will proceed to the second battle. Everything works well until I reach the second battle. If both heroes die the game continues until it ends itself. Any help would be highly appreciated https://github.com/aleviti2/FInal-Battle
GitHub
GitHub - aleviti2/FInal-Battle
Contribute to aleviti2/FInal-Battle development by creating an account on GitHub.
7 Replies
SinFluxx
SinFluxx13mo ago
Are you sure your party doesn't end up with >2 Heroes because you're re-using the party?
iparalyze
iparalyzeOP13mo ago
Mm the logic inside the TurnsManager method should check for the property hero.IsDead, not if the character is present... Characters are not eliminated from the list when they die, only their property goes from IsDead = false to true
SinFluxx
SinFluxx13mo ago
Ah ok, sorry I didn't really look much, just that you mentioned it specifically being 2 of each character then saw you were reusing the party from the last fight, so wasn't sure if you had some hardcoded logic elsewhere specifically around 2 characters being dead
iparalyze
iparalyzeOP13mo ago
I really don't know what to do... I've just noticed that if I move the BattleSeries.CurrentBattleNumber++; in the second constructor, the game stops when the two heroes win the first battle...
Omnissiah
Omnissiah13mo ago
you could really simplify the code by using variables instead of calculating stuff multiple times, it would make it more readable also did you debug the ending step by step to look at the Party content and how the ending conditions behave?
iparalyze
iparalyzeOP13mo ago
I did, and I'm thinking there must be something in the third constructor, or in the event manager in BattleSeries Looking at the GameEngine constructor, could it be a case of circular dependency?
public GameEngine(Party party) //First battle
{
Party = party;
TurnList = new List<ICharacter>();
//this.battleSeries = battleSeries;

party.AddCharacter(/*skeleton1 = */new Skeleton(1, 0, "Gomer"));
party.AddCharacter(/*skeleton2 = */new Skeleton(1, 0, "Nefasto"));
//party.AddCharacter(uncodedOne = new TheUncodedOne(1, 0));
BattleSeries = new BattleSeries(this);
BattleEnded += BattleSeries.OnBattleManager;
}
public GameEngine(Party party) //First battle
{
Party = party;
TurnList = new List<ICharacter>();
//this.battleSeries = battleSeries;

party.AddCharacter(/*skeleton1 = */new Skeleton(1, 0, "Gomer"));
party.AddCharacter(/*skeleton2 = */new Skeleton(1, 0, "Nefasto"));
//party.AddCharacter(uncodedOne = new TheUncodedOne(1, 0));
BattleSeries = new BattleSeries(this);
BattleEnded += BattleSeries.OnBattleManager;
}
Omnissiah
Omnissiah13mo ago
you did? because it could look like this, for example, and i don't see it there yet
var aliveHeroesCount = Party.HeroesParty.Count(hero => hero.IsDead == false);
var areMonstersDead = Party.MonstersParty.All(monster => monster.IsDead);

if (aliveHeroesCount > 0 && areMonstersDead)
{
var survivingHeroes = string.Join("and", Party.HeroesParty.Where(hero => hero.IsDead == false).Select(h => h.Name));
Console.WriteLine(partyAliveHeroes == 1
? $"Congratulations! The Heroes prevailed. The winner is {survivingHeroes}."
: $"Congratulations! The Heroes prevailed. The winners are {survivingHeroes}."
);
}
else if (aliveHeroesCount == 0 && areMonstersDead == false)
Console.WriteLine("The Monsters have prevailed!");
var aliveHeroesCount = Party.HeroesParty.Count(hero => hero.IsDead == false);
var areMonstersDead = Party.MonstersParty.All(monster => monster.IsDead);

if (aliveHeroesCount > 0 && areMonstersDead)
{
var survivingHeroes = string.Join("and", Party.HeroesParty.Where(hero => hero.IsDead == false).Select(h => h.Name));
Console.WriteLine(partyAliveHeroes == 1
? $"Congratulations! The Heroes prevailed. The winner is {survivingHeroes}."
: $"Congratulations! The Heroes prevailed. The winners are {survivingHeroes}."
);
}
else if (aliveHeroesCount == 0 && areMonstersDead == false)
Console.WriteLine("The Monsters have prevailed!");
also are you sure it can't happen that both monsters and heroes are dead? sometimes in these type of games you have to account for weird stuff
Want results from more Discord servers?
Add your server