Ejemplo n.º 1
0
    private void ProcessStack(Stack <Crate> stack, List <Crate> checkList, List <Crate> updateList, Group newGroup)
    {
        // INGRESS
        // get crate from stack - get nbrs - add nbrs to stack and list
        while (stack.Count > 0)
        {
            Crate   nextCrate = stack.Peek();           // get crate from stack - PEEK ONLY
            Crate[] nbrs      = nextCrate.Neighbours(); // get nbrs
            foreach (Crate nbr in nbrs)                 // get deeper if there are unchecked nbrs
            {
                if (nbr != null && !checkList.Contains(nbr))
                {
                    stack.Push(nbr);
                    checkList.Add(nbr);
                    ProcessStack(stack, checkList, updateList, newGroup);
                }
            }

            // EGRESS
            // pull a crate from the stack - reassign group
            if (stack.Count > 0)
            {
                Crate crateToUpdate = stack.Pop();
                if (!updateList.Contains(crateToUpdate))                       // was the crate already updated?
                {
                    GetGroupByCrate(crateToUpdate).RemoveCrate(crateToUpdate); // remove crate from old group
                    master.orderMaster.RemoveGroupMatches(GetGroupByCrate(crateToUpdate));
                    newGroup.AddCrate(crateToUpdate);                          // add crate to new group
                    crateToUpdate.SetGroup(newGroup.Index);                    // update group index in crate
                    updateList.Add(crateToUpdate);                             // mark crate as updated
                }
            }
        }
    }
Ejemplo n.º 2
0
    public void AssignCrateToNewGroup(Crate crate) // handle group management when a new crate is added
    {
        // get crate nbrs
        Crate[] crateNbrs = crate.Neighbours();
        // check number of nbrs
        int nbrsCount = 0;

        for (int count = 0; count < 4; count++)
        {
            if (crateNbrs[count] != null)
            {
                nbrsCount++;
            }
        }

        // CASES
        Group newGroup = null;

        switch (nbrsCount)
        {
        case 0:     // no nbrs
            NewGroupNumber();
            crate.SetGroup(lastCreatedGroup);
            newGroup = new Group(crate, lastCreatedGroup);
            groupList.Add(newGroup);
            int newColor = 0;
            if (!master.crateMaster.startingColorUsed)
            {
                newColor = master.crateMaster.randomStartingColorIndex;
                master.crateMaster.startingColorUsed = true;
            }
            else
            {
                newColor = master.crateMaster.GetNewColor();
            }
            groupToColorMap.Add(newGroup, newColor);
            break;

        case 1:     // 1 nbr
            // find nbr and assign its group to crate
            for (int count = 0; count < 4; count++)
            {
                if (crateNbrs[count] != null)
                {
                    // set new group to crate
                    crate.SetGroup(crateNbrs[count].Group);
                    // find group OBJ and add crate to it
                    newGroup = GetGroupByIndex(crateNbrs[count].Group);
                    newGroup.AddCrate(crate);
                    break;
                }
            }
            break;

        default:     // more than 1 nbr

            // if all nbrs are of the same group

            if (NbrCount(crateNbrs) == 1)
            {
                for (int count = 0; count < 4; count++)
                {
                    if (crateNbrs[count] != null)
                    {
                        // set up the group for the crate
                        crate.SetGroup(crateNbrs[count].Group);
                        // find group OBJ and add crate to it
                        newGroup = GetGroupByIndex(crateNbrs[count].Group);
                        newGroup.AddCrate(crate);
                        break;
                    }
                }
                break;
            }
            else     // more than 1 group in the nbrs - find the largest one and assign all relevant crates to it
            {
                // if there are 2 or more nbrs and they are of more than 1 group
                // find the largest adjacent group
                int        largestGroupIndex = 0;
                int        largestGroupSize  = 0;
                List <int> obsoleteGroups    = new List <int>();;
                for (int count = 0; count < 4; count++)
                {
                    if ((crateNbrs[count] != null))                      // if this is a nbr
                    {
                        obsoleteGroups.Add(crateNbrs[count].Group);      // add nbr.Group to obsolete groups
                        Group group = GetGroupByCrate(crateNbrs[count]); // find the group of that nbr
                        if (group.CrateList.Count > largestGroupSize)    // if this group is largest so far, note that in the 'largest...' variables
                        {
                            largestGroupSize  = group.CrateList.Count;
                            largestGroupIndex = group.Index;
                        }
                    }
                }

                // set up the largest group for the crate
                crate.SetGroup(largestGroupIndex);
                // find group OBJ and add the crate to it
                newGroup = GetGroupByIndex(largestGroupIndex);
                newGroup.AddCrate(crate);
                // remove obsolete groups
                obsoleteGroups.Remove(largestGroupIndex);                  // make sure the largest group is NOT obsolete
                Group largestGroup = newGroup;
                foreach (Crate checkCrate in master.crateMaster.crateList) // the new crate is not in the crateList YET!
                {
                    if (obsoleteGroups.Contains(checkCrate.Group))         // if a group is in the obolete list..

                    {
                        // remove the crate from the current group
                        GetGroupByCrate(checkCrate).RemoveCrate(checkCrate);
                        master.orderMaster.RemoveGroupMatches(GetGroupByCrate(checkCrate));
                        // .. assign the crate the new group,
                        checkCrate.SetGroup(largestGroupIndex);
                        // .. then add it to the group
                        largestGroup.AddCrate(checkCrate);
                    }
                }
                foreach (int obsoleteGroupIndex in obsoleteGroups)     // remove each group with an obsolete index (crates already assigned to the largest group)
                {
                    Group obsoleteGroup = GetGroupByIndex(obsoleteGroupIndex);
                    RemoveGroup(obsoleteGroup);
                    // groupToColorMap.Remove(obsoleteGroup);
                    // groupList.Remove(obsoleteGroup);
                }
            }
            break;
        }
    }