Ejemplo n.º 1
0
 public void KillCurrentAnimal()
 {
     if (_animal != null)
     {
         if (_animal is fish)
         {
             FishBowl._Instance._nCurrentFish--;
         }
         else
         {
             FishBowl._Instance._nCurrentSharks--;
         }
         _animal = null;
     }
 }
Ejemplo n.º 2
0
        public animal _animal; // null is empty, else fish or shark
        public Cell(int nRow, int nCol)
        {
            _nRow = nRow;
            _nCol = nCol;
            var nRand = FishBowl._Instance._random.Next(100);

            if (nRand < FishBowl._Instance._FishInitPct)
            {
                _animal = new fish();
            }
            else
            {
                nRand -= FishBowl._Instance._FishInitPct;
                if (nRand < FishBowl._Instance._SharkInitPct)
                {
                    _animal = new shark();
                }
                else
                {
                    _animal = null; // no animal
                }
            }
        }
Ejemplo n.º 3
0
 public void CalcGeneration()
 {// see what happends to this cell
     if (_MouseButton.HasValue)
     {
         var button = _MouseButton;
         _MouseButton = null;
         {
             var baby = Activator.CreateInstance(
                 button == MouseButton.Left ? typeof(fish) : typeof(shark)
                 );
             KillCurrentAnimal();
             _animal = (animal)baby;
             _animal._nDateOfLastChildBirth = FishBowl._Instance._nYearCurrent;
             _animal._nDateOfLastAction     = FishBowl._Instance._nYearCurrent;
             _priorBrush = IntPtr.Zero; // recalc color for cur cell
             return;
         }
     }
     // is it a shark? does it die of starvation?
     if (_animal != null && !_animal.Survives())
     {
         KillCurrentAnimal();
     }
     if (_animal != null &&
         (FishBowl._Instance._OneActionPerYear ?
          !_animal.DidSomethingInCurrentYear : // moved in current yr
          true)
         )
     { // if still alive
         var neighborCells = FishBowl._Instance.GetRandomNeighbors(this);
         // sharks eat 1 adjacent fish
         bool fDidEat = false;
         if (_animal is shark)
         {
             // find first fish in neighbors
             var foodCell = neighborCells.Where(
                 c => c._animal is fish &&
                 c._animal.Age > 0 // don't eat babies
                 ).FirstOrDefault();
             if (foodCell != null)
             {// eat the fish! and move shark to where food was
                 _animal._nDateOfLastMeal =
                     FishBowl._Instance._nYearCurrent;
                 foodCell._animal           = _animal;
                 _animal._nDateOfLastAction = FishBowl._Instance._nYearCurrent;
                 _priorBrush = IntPtr.Zero; // recalc color for cur cell
                 FishBowl._Instance._nCurrentFish--;
                 fDidEat = true;
                 if (_animal.CanBreed) // replace current cell with baby cuz mama moved
                 {
                     _animal._nDateOfLastAction = FishBowl._Instance._nYearCurrent;
                     var baby = Activator.CreateInstance(_animal.GetType());
                     foodCell._animal._nDateOfLastChildBirth = FishBowl._Instance._nYearCurrent;
                     _animal = (animal)baby;
                 }
                 else
                 {
                     _animal = null; // shark moved, so current cell empty
                 }
             }
         }
         if (!fDidEat) // if cell still occupied by original animal (didn't move)
         {
             var emptyNeighborCell = neighborCells.Where(
                 c => c._animal == null
                 ).FirstOrDefault();
             if (emptyNeighborCell != null)
             {
                 // Breeding
                 if (_animal.CanBreed)
                 {
                     // old enough to breed: have a baby
                     // 100% fertility rate
                     var baby = Activator.CreateInstance(_animal.GetType());
                     emptyNeighborCell._animal      = (animal)baby;
                     _animal._nDateOfLastChildBirth = FishBowl._Instance._nYearCurrent;
                     _animal._nDateOfLastAction     = FishBowl._Instance._nYearCurrent;
                     _priorBrush = IntPtr.Zero; // recalc color for cur cell
                 }
                 else
                 {// can't breed AND move
                     var nMoves = _animal.NumMovesPerGeneration;
                     for (int i = 0; i < nMoves; i++)
                     {
                         // moving to neighbor cell
                         _animal._nDateOfLastAction = FishBowl._Instance._nYearCurrent;
                         emptyNeighborCell._animal  = _animal;
                         _animal     = null;
                         _priorBrush = IntPtr.Zero; // recalc color for cur cell
                         if (i + 1 < nMoves)
                         {                          // could move back to original cell
                             neighborCells     = FishBowl._Instance.GetRandomNeighbors(this);
                             emptyNeighborCell = neighborCells.Where(
                                 c => c._animal == null
                                 ).FirstOrDefault();
                             if (emptyNeighborCell == null)
                             {
                                 break;
                             }
                         }
                     }
                 }
             }
         }
     }
 }