Example #1
0
        public void ChangeStateTest()
        {
            uint size = 25;
            GameOfLife gof = new GameOfLife(size);

            int? oldState = gof[0, 0];

            CoordState stateChange;
            if (oldState == null)
            {
                stateChange = new CoordState(0, 0, 1);
            }
            else
            {
                stateChange = new CoordState(0, 0, null);
            }

            gof.ChangeState(stateChange);

            int? newState = gof[0, 0];

            Assert.AreNotEqual(oldState, newState);
        }
Example #2
0
        /// <summary>
        /// Makes a day go by, updating the fields
        /// corresponding to the set of rules.
        /// </summary>
        public void NextDay()
        {
            List<CoordState> updates = new List<CoordState>();
            //Makes a copy of the world in order to have a reference
            //which will not be changed when updating the original.
            int?[,] worldCopy = GetWorldCopy();
            //Loop through the cells of the world
            for (int x = 0; x < Size; x++)
            {
                for (int y = 0; y < Size; y++)
                {
                    //Amount of neighboring species
                    int zombies = 0;
                    int alive = 0;
                    //Loop through the 8 neighboring tiles
                    for (int i = -1; i <= 1; i++)
                    {
                        for (int j = -1; j <= 1; j++)
                        {
                            //If we're on the initial field we're
                            //testing from, we do nothing
                            if (i == 0 && j == 0) { }
                            else if(IsValidPoint(x+i, y+j))
                            {
                                //Get the state of the cell and
                                //increment the corresponding counter
                                int? state = worldCopy[x + i, y + j];
                                if (state == null) { zombies++; }
                                else if (state == 1) { alive++; }
                            }
                        }
                    }
                    //If the cell is either dead or alive, process it
                    CoordState self = new CoordState(x, y, worldCopy[x, y]);
                    if (self.State == 1)
                    {
                        CoordState newState = ProcessLiveCell(self, zombies, alive);
                        if (!newState.Equals(self))
                        {
                            updates.Add(newState);
                        }

                    }
                    else if (self.State == 0)
                    {
                        CoordState newState = ProcessDeadCell(self, alive);
                        if(!newState.Equals(self))
                        {
                            updates.Add(newState);
                        }
                    }
                }
            }
            //Change the state of all the updates
            ChangeState(updates.ToArray());
        }
Example #3
0
 /// <summary>
 /// Processes the cell according to the rules given to living cells
 /// </summary>
 /// <param name="curState">The current state of the cell</param>
 /// <param name="zombies">Number of neighboring zombies</param>
 /// <param name="alive">Number of neighboring living cells</param>
 /// <returns>The new state of the cell</returns>
 private CoordState ProcessLiveCell(CoordState curState, int zombies, int alive)
 {
     CoordState newState = curState;
     if (alive <= 1) { newState = new CoordState(curState.X, curState.Y, 0); }
     else if (alive == 2 && alive == 3) { newState = new CoordState(curState.X, curState.Y, 1); }
     else if (alive >= 4) { newState = new CoordState(curState.X, curState.Y, 0); }
     else if (zombies >= 1)
     {
         int? state;
         Random rnd = new Random();
         double num = rnd.NextDouble();
         if (num < 0.5) { state = 1; }
         else { state = null; }
         newState = new CoordState(curState.X, curState.Y, state);
     }
     return newState;
     //ChangeState(newState);
 }
Example #4
0
 /// <summary>
 /// Processes the cell according to the rules given to dead cells
 /// </summary>
 /// <param name="curState">The current state of the cell</param>
 /// <param name="alive">Number of neighboring living cells</param>
 /// <returns>The new state of the cell</returns>
 private CoordState ProcessDeadCell(CoordState curState, int alive)
 {
     CoordState newState = curState;
     if (alive == 3) { newState = new CoordState(curState.X, curState.Y, 1); }
     return newState;
     //ChangeState(newState);
 }