Exemplo n.º 1
0
        /// <summary>
        /// The execute start simulation.
        /// </summary>
        private void ExecuteStartSimulation()
        {
            this.isSimulationRunning = true;

            try
            {
                if (!Directory.Exists(this.PictureSaveFolder))
                {
                    Directory.CreateDirectory(this.PictureSaveFolder);
                }
            }
            catch
            {
                this.PictureSaveFolder = @".\";
            }

            this.watorSimulationObj =
                new WatorSimulation(
                    new WatorSettings()
            {
                FishBreedTime          = this.FishBreedTime,
                InitialFishPopulation  = this.FishPopulation,
                InitialSharkPopulation = this.SharkPopulation,
                SaveFolder             = this.PictureSaveFolder,
                SharkBreedTime         = this.SharkBreedTime,
                SharkStarveTime        = this.SharkStarveTime,
                WorldHeight            = this.WorldHeight,
                WorldWidth             = this.WorldWidth,
            });

            this.watorSimulationObj.ImageFinished += this.ImageCreatorJobFinished;
            this.watorSimulationObj.StepDone      += this.WatorSimulationObjStepDone;
            this.watorSimulationObj.StartSimulation();
        }
Exemplo n.º 2
0
        ///// <summary>
        ///// Gets the color of the draw.
        ///// </summary>
        ///// <value>
        ///// The color of the draw.
        ///// </value>
        // public override Color DrawColor
        // {
        // get
        // {
        // return this.Settings.FishColor;
        // }
        // }

        /// <summary>
        /// Steps of fish.
        /// </summary>
        public override void Step()
        {
            // increase lifetime
            this.Lifetime++;

            if (this.BreedMoveStep())
            {
                WatorSimulation.ChangeFishPopulation(true);
            }

            // set step down - animal moved
            this.IsMoved = true;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Steps of shark.
        /// </summary>
        public override void Step()
        {
            bool lockTaken = false;

            // increase lifetime
            this.Lifetime++;

            // find fish around
            var preyFieldDirection = this.GetRandomFishDirectionAround();
            var preyField          = this.GetFieldFromDirection(preyFieldDirection);

            // shark eats a fish if found
            if (preyField != null)
            {
                try
                {
                    if (this.CheckLockRequired(preyFieldDirection, preyField))
                    {
                        Monitor.Enter(preyField, ref lockTaken);
                    }

                    // check again fish is on field - could be changed in meantime
                    if (preyField.Animal != null)
                    {
                        // clear own old animal space
                        this.Field.Animal = null;

                        // fish dies (clear animal.field and field.animal)
                        preyField.Animal.Die();
                        WatorSimulation.ChangeFishPopulation(false);

                        // fish is dead place shark on field
                        preyField.Animal = this;

                        // set fíeld as new place for shark
                        this.Field = preyField;
                    }
                }
                finally
                {
                    if (lockTaken)
                    {
                        Monitor.Exit(preyField);
                    }
                }
            }
            else
            {
                // if no fish found - increase starve
                this.Starve++;

                if (this.Starve > this.Settings.SharkStarveTime)
                {
                    // shark dies (animal.field null and field.animal.null)
                    this.Die();
                    WatorSimulation.ChangeSharkPopulation(false);

                    return;
                }
            }

            // execute breed and move
            if (this.BreedMoveStep())
            {
                WatorSimulation.ChangeSharkPopulation(true);
            }

            // set step down - animal moved
            this.IsMoved = true;
        }