public int SimToVictory(bool abortIfAnElfDies = false) { var round = 0; while (true) { if (DoRound(abortIfAnElfDies)) { if (abortIfAnElfDies && Elves.Any(e => e.IsDead)) { return(-1); } if (Goblins.All(g => g.IsDead)) { var hpLeft = Elves.Where(e => !e.IsDead).Sum(e => e.HP); return(round * hpLeft); } if (Elves.All(e => e.IsDead)) { var hpLeft = Goblins.Where(e => !e.IsDead).Sum(e => e.HP); return(round * hpLeft); } } round++; } }
public bool DoRound(bool abortIfAnElfDies = false) { var allFighters = Goblins.Cast <Fighter>() .Concat(Elves.Cast <Fighter>()) .Where(f => !f.IsDead) .OrderBy(f => f.Y) .ThenBy(f => f.X).ToList(); foreach (var fighter in allFighters) { if (Goblins.All(g => g.IsDead) || Elves.All(e => e.IsDead)) { return(true); } if (abortIfAnElfDies && Elves.Any(e => e.IsDead)) { return(true); } if (fighter.IsDead) { continue; } if (fighter is Elf) { var asElf = (Elf)fighter; if (!TryToFight(asElf)) { MoveToClosestEnemy(asElf); TryToFight(asElf); } } else { var asGoblin = (Goblin)fighter; if (!TryToFight(asGoblin)) { MoveToClosestEnemy(asGoblin); TryToFight(asGoblin); } } } return(false); }