C
C#8mo ago
iparalyze

❔ Looping problem

Hey, I can't figure out what is not working in the logic behind my TurnsManager method in the GameEngine class. For some reason the game carries on even after either all heroes have been killed or after all Monsters have been eliminated. As you can see I have three lists, a HeroesParty list, a MonsterParty list and a TurnList. The property IsDead gets updated in all of them, so I really don't understand where it clogs... https://github.com/aleviti2/FInal-Battle GitHub
GitHub
GitHub - aleviti2/FInal-Battle
Contribute to aleviti2/FInal-Battle development by creating an account on GitHub.
13 Replies
SinFluxx
SinFluxx8mo ago
Struggling to follow it completely, I think you've still got some general issues, e.g.: Using the party being passed into GameEngine, rather than updating the Party property of GameEngine, on the three lines where you're adding monsters:
public GameEngine(Party party)
{
Party = party;
TurnList = new List<ICharacter>();

party.AddCharacter(skeleton1 = new Skeleton(1, 0, "Gomer"));
party.AddCharacter(skeleton2 = new Skeleton(1, 0, "Nefasto"));
party.AddCharacter(uncodedOne = new TheUncodedOne(1, 0));
public GameEngine(Party party)
{
Party = party;
TurnList = new List<ICharacter>();

party.AddCharacter(skeleton1 = new Skeleton(1, 0, "Gomer"));
party.AddCharacter(skeleton2 = new Skeleton(1, 0, "Nefasto"));
party.AddCharacter(uncodedOne = new TheUncodedOne(1, 0));
You're returning an int from SetHeroesNumber but not assigning it to anything (wouldn't think you'd need to return anything here, so not going to be causing a major issue):
public int SetHeroesNumber()
...
gameEngine.SetHeroesNumber();
public int SetHeroesNumber()
...
gameEngine.SetHeroesNumber();
In your ChooseHeroes method, you should be able to trim it down/make it a bit easier to read by knowing that you can cast an int to an enum value, e.g.:
Hero hero = new Hero(1, 0, (CharacterType)1);
// hero is now a Hero with 1hp, 0 battles won, and a CharacterType of Tog
Hero hero = new Hero(1, 0, (CharacterType)1);
// hero is now a Hero with 1hp, 0 battles won, and a CharacterType of Tog
For why it's looping indefinitely, presumably you would want it to stop looping when all heroes OR all monsters are dead? Is that what the condition in your while loop is currently checking for?
iparalyze
iparalyze8mo ago
Thank you for pointing out the issues, I'll definitely sort them out later. I'm not sure they're impacting the current problem I have. I don't understand why the turn manager doesn't stop when all monsters are dead. this is how far I've gotten but it's still giving me the same issue. I gave up on IsDead and now I'm removing characters from the lists and check if (Party.HeroesParty.Count ==0 || Party.MonstersParty.Count ==0) but still nothing... https://github.com/aleviti2/FInal-Battle
GitHub
GitHub - aleviti2/FInal-Battle
Contribute to aleviti2/FInal-Battle development by creating an account on GitHub.
SinFluxx
SinFluxx8mo ago
Well yes as I was trying to hint before it would have been looping because you were waiting for all characters in both parties to be dead Have you tried debugging, and inspecting your list of characters on each loop to be sure they've actually got the IsDead values you're expecting? Similarly, if you're waiting for the entire list of characters (from both parties) to be 0, then that sounds like you're doing it wrong - only one party needs to have no characters left alive for the game to be over
iparalyze
iparalyze8mo ago
if (Party.HeroesParty.Count ==0/All(hero => hero.IsDead)/ || Party.MonstersParty.Count ==0) but this should exit the while loop right?
SinFluxx
SinFluxx8mo ago
why isn't that the condition of your while loop then? that's also going to mess with your output at the end where you're trying to output the text on who won/names of characters etc as they'll no longer be in the party lists
iparalyze
iparalyze8mo ago
that was my first option but it didn't work
SinFluxx
SinFluxx8mo ago
well it won't work there either then Checking IsDead makes more sense - you just need to debug it while it's running to see that what you're expecting to happen is in fact happening
iparalyze
iparalyze8mo ago
I did, I've been running around in circles, the IsDead gets updated correctly, there must be something in the logic of TurnsManager() that is wrong... I wish I could figure out what that is...
SinFluxx
SinFluxx8mo ago
and it was definitely the IsDead property on a character within your TurnList collection, not a copy of a character somewhere?
iparalyze
iparalyze8mo ago
I placed a debugger inside the AreYouDead list. It shows that everything is getting updated correctly. Well now the characters get eliminated from the lists but if you remove
`if (cAttacked.CharacterCategory == Category.Hero)
Party.HeroesParty.Remove(cAttacked);
else Party.MonstersParty.Remove(cAttacked);
`if (cAttacked.CharacterCategory == Category.Hero)
Party.HeroesParty.Remove(cAttacked);
else Party.MonstersParty.Remove(cAttacked);
you will see it works
SinFluxx
SinFluxx8mo ago
So there you're directly referencing/updating Party in your old code I think you'll find you were updating copies of the characters In my opinion you should go back to checking IsDead, what if you later on wanted to introduce a revival spell or something but the character no longer exists in the party?, and then double check that code/variables
iparalyze
iparalyze8mo ago
The problem was that foreach inside a while loop. If you want to have a look at it it's now working. I found a way to breakaway from both the for and the while loop using a bool variable. Well chatgpt suggested it, and for once I have to admit it's working... You can check out the latest commit on github. Thanks a lot for taking a look at my code
Accord
Accord8mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.