Beispiel #1
0
    public Round(int unitCount, int maxTeam, int maxGroup) : base()
    {
        List <int> CommonFactor(int number)
        {
            List <int> cf = new List <int>();

            for (int i = 2; i <= maxTeam; i++)
            {
                if (number % i == 0)
                {
                    cf.Add(i);
                }
            }
            return(cf);
        }

        List <int> unitsCF    = CommonFactor(unitCount);
        int        groupCount = 1;

        if (unitCount >= 4)
        {
            int minGroup = unitsCF.Count > 1 && unitCount <= maxTeam ? 1 : 2;
            groupCount = Random.Range(minGroup, maxGroup + 1);
        }
        int  groupRemain = unitCount;
        bool done        = false;
        int  repeatCount = 0;

        while (!done && repeatCount++ < 200)
        {
            int        groupUnits   = Random.Range(2, groupRemain + 1);
            List <int> groupUnitsCF = CommonFactor(groupUnits);
            if (groupUnitsCF.Any())
            {
                int   teamCount = ListRandom.In(groupUnitsCF);
                Group group     = new Group();
                for (int i = 0; i < teamCount; i++)
                {
                    group.Add(new Team());
                }
                group.eachTeamMember = groupUnits / teamCount;
                Add(group);
                groupRemain -= groupUnits;
            }
            if (groupRemain == 1 || Count > groupCount)
            {
                Clear();
                groupRemain = unitCount;
            }
            done = Count <= groupCount && groupRemain == 0;
        }
        if (repeatCount > 200)
        {
            Debug.Log("fail");
        }
    }
Beispiel #2
0
    public void RandomAssign(List <UnitInfo> units)
    {
        List <int> Ids = Enumerable.Range(0, units.Count).ToList();

        foreach (Group group in this)
        {
            foreach (Team team in group)
            {
                for (int i = 0; i < group.eachTeamMember; i++)
                {
                    int id = ListRandom.In(Ids);
                    Ids.Remove(id);
                    UnitInfo unit = units[id];
                    unit.team = team;
                    team.unitInfos.Add(unit);
                }
            }
        }
    }