예제 #1
0
        private void StartSimulation()
        {
            startStopButton.Text = "Stop";
            Refresh();

            int width            = int.Parse(gridWidthTextBox.Text);
            int height           = int.Parse(gridHeightTextBox.Text);
            int numFish          = int.Parse(numFishTextBox.Text);
            int fishBreedingTime = int.Parse(fishBreedingTimeTextBox.Text);
            int fishEnergyValue  = int.Parse(fishEnergyValueTextBox.Text);

            int numSharks        = int.Parse(numSharksTextBox.Text);
            int sharkEnergyLoss  = int.Parse(sharkEnergyLossTextBox.Text);
            int sharkSplitEnergy = int.Parse(sharkSplitEnergyTextBox.Text);

            Ocean = new Ocean(width, height,
                              numFish, fishBreedingTime, fishEnergyValue,
                              numSharks, sharkEnergyLoss, sharkSplitEnergy);
            oceanPictureBox.Image = Ocean.Bitmap;
            this.ClientSize       =
                new Size(ClientSize.Width, oceanPictureBox.Bottom + 12);

            Turn                         = 0;
            turnTextbox.Text             = Turn.ToString();
            currentNumFishTextBox.Text   = Ocean.Fishes.Count.ToString();
            currentNumSharksTextBox.Text = Ocean.Sharks.Count.ToString();

            turnTimer.Interval = int.Parse(msPerTurnTextBox.Text);
            turnTimer.Enabled  = true;
        }
예제 #2
0
 // Position this animal.
 public Animal(Ocean ocean, Color color, int x, int y)
 {
     Ocean            = ocean;
     X                = x;
     Y                = y;
     Ocean.Grid[X, Y] = this;
     Ocean.Bitmap.SetPixel(X, Y, color);
 }
예제 #3
0
        public override void Move()
        {
            // Lose energy.
            Energy -= Ocean.SharkEnergyLoss;
            if (Energy == 0)
            {
                // Die.
                Ocean.Bitmap.SetPixel(X, Y, Color.Black);
                Ocean.Grid[X, Y] = null;
                Ocean.Sharks.Remove(this);
                return;
            }

            // Look for a free spot.
            List <Point> spots = Ocean.FreeSpots(X, Y, true);

            if (spots.Count == 0)
            {
                return;
            }

            // Move.
            Ocean.Bitmap.SetPixel(X, Y, Color.Black);
            Ocean.Grid[X, Y] = null;

            // See if the new spot contains food.
            Point moveTo = spots.Random();

            if (Ocean.Grid[moveTo.X, moveTo.Y] is Fish)
            {
                // Eat it.
                Energy += Ocean.FishEnergyValue;
                Fish fish = Ocean.Grid[moveTo.X, moveTo.Y] as Fish;
                Ocean.Fishes.Remove(fish);
            }

            // Move to the new spot.
            X = moveTo.X;
            Y = moveTo.Y;
            Ocean.Bitmap.SetPixel(X, Y, Fish.Color);
            Ocean.Grid[X, Y] = this;

            // Breed.
            Split();
        }
예제 #4
0
        public override void Move()
        {
            // Look for a free spot.
            List <Point> spots = Ocean.FreeSpots(X, Y, true);

            if (spots.Count > 0)
            {
                // Move.
                Ocean.Bitmap.SetPixel(X, Y, Color.Black);
                Ocean.Grid[X, Y] = null;

                Point moveTo = spots.Random();
                X = moveTo.X;
                Y = moveTo.Y;
                Ocean.Bitmap.SetPixel(X, Y, Fish.Color);
                Ocean.Grid[X, Y] = this;
            }

            // Breed.
            Breed();
        }
예제 #5
0
        // See if we should breed.
        private void Split()
        {
            if (Energy < Ocean.SharkSplitEnergy)
            {
                return;
            }

            // Split.
            Energy /= 2;

            // Position a child.
            List <Point> spots = Ocean.FreeSpots(X, Y, false);

            if (spots.Count == 0)
            {
                return;
            }

            Point childSpot = spots.Random();

            Ocean.Sharks.Add(new Shark(Ocean, Energy, childSpot.X, childSpot.Y));
        }
예제 #6
0
        // See if we should breed.
        private void Breed()
        {
            if (--TimeUntilBreeding > 0)
            {
                return;
            }

            // Breed.
            TimeUntilBreeding = Ocean.FishBreedingTime;

            // Position a child.
            List <Point> spots = Ocean.FreeSpots(X, Y, false);

            if (spots.Count == 0)
            {
                return;
            }

            Point childSpot = spots.Random();

            Ocean.Fishes.Add(new Fish(Ocean, Ocean.FishBreedingTime, childSpot.X, childSpot.Y));
        }
예제 #7
0
 // Initialize the location randomly.
 public Animal(Ocean ocean, Color color)
     : this(ocean, color, Rand.Next(ocean.Width), Rand.Next(ocean.Height))
 {
 }
예제 #8
0
 public Shark(Ocean ocean, int energy)
     : this(ocean, energy,
            Rand.Next(ocean.Width), Rand.Next(ocean.Height))
 {
 }
예제 #9
0
 public Shark(Ocean ocean, int energy, int x, int y)
     : base(ocean, Color, x, y)
 {
     Energy = energy;
 }
예제 #10
0
 public Fish(Ocean ocean, int breedingTime)
     : this(ocean, breedingTime,
            Rand.Next(ocean.Width), Rand.Next(ocean.Height))
 {
 }
예제 #11
0
 public Fish(Ocean ocean, int breedingTime, int x, int y)
     : base(ocean, Color, x, y)
 {
     TimeUntilBreeding = 1 + Rand.Next(Ocean.FishBreedingTime);
 }