C
C#7mo 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
SinFluxx7mo ago
Are you sure your party doesn't end up with >2 Heroes because you're re-using the party?
iparalyze
iparalyze7mo 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
SinFluxx7mo 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
iparalyze7mo 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...
WEIRD FLEX
WEIRD FLEX7mo 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
iparalyze7mo 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;
}
WEIRD FLEX
WEIRD FLEX7mo 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
More Posts
Xamarin.Forms UWP Publish issuewhile i publish a existing xamarin.forms uwp app i get the list of errors shown down below. The PublBlazor JS Interop - Bootstrap 5 TooltipHey. I want to use the Bootstrap 5 Tooltip on divs. i have a site.js that has this func ```JS functNeed help structuring a class library for internal toolsHi there, I'm working on creating an internal tool for exposing some commonly used functionality thaXML Serialization: How to serialize a property with a null value without xsi:nill attribute?I have a property the following code: ` [Serializable()] public class MyClass { UserControl issue - UC2 doesn't appear after closing UC1Hello, I started learning about almost a week ago and the past day or two this problem has been buggCompile for i mac osHow do i compile my avalonia app for mac os. the docs are really confusing for me to understand. I aIm trying to open another program but my code isn't workingI made a login system. If login info is correct another program will open if its not it will tell wrCorrect environments handling (appSettings.json)We are using azure DevOps and I wondered what's the best way to handle environments. Option 1: keep Can someone explain how I use MQTTnet and how MQTT in general works?MQTTnet: https://github.com/dotnet/MQTTnet Help would be really appreciated, thank you :)Can't call C++ function in Unity through mono_add_internal_call, with a MissingMethodExceptionthis is my question detail on stackoverflow: https://stackoverflow.com/questions/77478866/how-to-use