C
C#15mo ago
Theos

❔ Adjust lists

Hey, so I have
enum AIType { heor, courier, scout }
enum AIType { heor, courier, scout }
class Unit
{
public AIType aiType;
}


class Player
{
List<Unit> units;
}
class Unit
{
public AIType aiType;
}


class Player
{
List<Unit> units;
}
class Settings
{
List<AIType> neededUnits;
}
class Settings
{
List<AIType> neededUnits;
}
2 Replies
Theos
TheosOP15mo ago
What I need to do is lets say in neededUnits i have hero, hero, scout, courier and player has units with types scout, courier, courier, hero i need to change player units types so he has neededUnits hero, hero, scout, courier I can't find a good way to do this and my code looks ugly ;p will something like this work?
Dictionary<AIUnitType, int> availableUnits = player.Units
.GroupBy(unit => unit.AIType)
.ToDictionary(group => group.Key, group => group.Count());

foreach (AIUnitType neededUnit in _recruitSettings.Units)
{
if (availableUnits.ContainsKey(neededUnit) && availableUnits[neededUnit] > 0)
{
availableUnits[neededUnit]--;
}
else
{
AIUnitType replacementUnit = availableUnits.FirstOrDefault(pair => pair.Value > 0).Key;
player.Units.First(unit => unit.AIType == replacementUnit).AIType = neededUnit;
availableUnits[replacementUnit]--;
}
}
Dictionary<AIUnitType, int> availableUnits = player.Units
.GroupBy(unit => unit.AIType)
.ToDictionary(group => group.Key, group => group.Count());

foreach (AIUnitType neededUnit in _recruitSettings.Units)
{
if (availableUnits.ContainsKey(neededUnit) && availableUnits[neededUnit] > 0)
{
availableUnits[neededUnit]--;
}
else
{
AIUnitType replacementUnit = availableUnits.FirstOrDefault(pair => pair.Value > 0).Key;
player.Units.First(unit => unit.AIType == replacementUnit).AIType = neededUnit;
availableUnits[replacementUnit]--;
}
}
bump
Accord
Accord15mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server