示例#1
0
        private void btnRunSim_Click(object sender, EventArgs e)
        {
            btnOpenMenu.Visible = true;
            openMenu            = new Thread(() =>
            {
                for (int i = 0; i >= -190; i = i - 3)
                {
                    moveMenu(i, 0, pnlMenu);
                    Thread.Sleep(2);
                }
            });
            openMenu.Start();
            if (!simRunning)
            {
                simRunning     = true;
                btnRunSim.Text = "Running...";
                lstActions.Items.Clear();
                #region species
                var cageIds = from loc in locations
                              where !(loc.Cage.Equals(null))
                              select loc;
                List <Location> tempLoc = new List <Location>();
                foreach (Location item in cageIds)
                {
                    tempLoc.Add(item);
                }
                int       counters = 0;
                Species[] spacess  = new Species[tempLoc.Count];
                int       counting = 0;
                foreach (Location item in tempLoc)
                {
                    foreach (Animal item2 in animals)
                    {
                        if (item.ID == item2.LocationID && (!spacess.Contains(item2.Species)))
                        {
                            spacess[counting] = item2.Species;
                            counting++;
                        }
                    }
                    counters++;
                }
                Species[] space = new Species[counting];
                for (int i = 0; i < counting; i++)
                {
                    space[i] = spacess[i];
                }
                double[] slowestAnimalEat = new double[counting];
                for (int i = 0; i < slowestAnimalEat.Length; i++)
                {
                    foreach (Animal item in animals)
                    {
                        if (item.Species.Equals(species[i]) && item.EatingTime > slowestAnimalEat[i])
                        {
                            slowestAnimalEat[i] = item.EatingTime;
                        }
                    }
                }
                #endregion
                Thread runSim = new Thread(() =>
                {
                    addToActionsLst("Running Simulation...");
                    addToActionsLst("All Gates Opened...");
                    #region sort
                    for (int pass = 1; pass <= space.Length - 2; pass++)
                    {
                        for (int i = 0; i <= space.Length - 2; i++)
                        {
                            if (slowestAnimalEat[i] > slowestAnimalEat[i + 1])
                            {
                                double temp             = slowestAnimalEat[i + 1];
                                Species spec            = space[i + 1];
                                slowestAnimalEat[i + 1] = slowestAnimalEat[i];
                                space[i + 1]            = space[i];
                                slowestAnimalEat[i]     = temp;
                                space[i] = spec;
                            }
                        }
                    }
                    #endregion
                    for (int i = 0; i < space.Length; i++)
                    {
                        addToActionsLst("");
                        for (int j = 0; j < space.Length; j++)
                        {
                            if (i == j)
                            {
                                addToActionsLst(space[j].AnimalName + "s are walking to the Feeding Area");
                            }
                            else
                            {
                                addToActionsLst(space[j].AnimalName + "s have been diverted");
                            }
                        }
                        Thread.Sleep((int)((10 - space[i].Speed) * 1000));
                        addToActionsLst("");
                        addToActionsLst(space[i].AnimalName + "s are eating");
                        Thread.Sleep((int)slowestAnimalEat[i] * 2000);
                        addToActionsLst("");
                        addToActionsLst(space[i].AnimalName + "s are walking back");
                        Thread.Sleep((int)((10 - space[i].Speed) * 1000));
                    }
                    addToActionsLst("");
                    addToActionsLst("All animals have eaten and are back in their respective Cages");
                    addToActionsLst("");
                    addToActionsLst("...All gates Closed");

                    simRunning = false;
                    // btnRunSim.Text = "Run Simulation";
                });
                runSim.Start();
            }
        }