示例#1
0
 public Cell(int r, int c, string color, CellStateEnum cellStateEnum)
 {
     Row        = r;
     Column     = c;
     Background = color;
     BlockState = cellStateEnum;
 }
示例#2
0
        public CellStateEnum Evolve()
        {
            var aliveNeighbourCount = GetAliveNeighboursCount();

            // cell rule : under-population if alive then dies
            if (_state == CellStateEnum.Alive && aliveNeighbourCount < 2)
            {
                _state = CellStateEnum.Dead;
            }

            if (_state == CellStateEnum.Alive && (aliveNeighbourCount == 3 || aliveNeighbourCount == 2))
            {
                _state = CellStateEnum.Alive;
            }

            // cell rule : over-population if alive then dies
            if (_state == CellStateEnum.Alive && aliveNeighbourCount > 3)
            {
                _state = CellStateEnum.Dead;
            }

            if (_state == CellStateEnum.Dead && aliveNeighbourCount == 3)
            {
                _state = CellStateEnum.Alive;
            }



            return(_state);
        }
示例#3
0
        /// <summary>
        /// Checks the position down of the given position to set the new cell state
        /// </summary>
        /// <param name="newCellsState"></param>
        /// <param name="row"></param>
        /// <param name="col"></param>
        private void CheckDown(Cell[,] newCellsState, CellStateEnum currentCellState, int row, int col)
        {
            if (row + 1 >= Rows || IsWall(col, row + 1))
            { // reached edge or wall, bounce
                // if there is room to bounce back
                if (row - 1 >= 0 && !IsBlocked(col, row - 1))
                {
                    SetState(newCellsState[col, row - 1], CellStateEnum.Up);
                }
                else
                {
                    SetState(newCellsState[col, row], CellStateEnum.Up);
                }
            }
            else
            {
                if (Cells[col, row + 1].State == CellStateEnum.RotateClockWise)
                {
                    if (col - 1 >= 0 && !IsBlocked(col - 1, row))
                    {
                        SetState(newCellsState[col - 1, row], RotateClockWise(currentCellState));
                    }
                    else
                    {
                        SetState(newCellsState[col, row], RotateClockWise(currentCellState));
                    }
                }
                else if (Cells[col, row + 1].State == CellStateEnum.RotateCounterClockWise)
                {
                    if (col + 1 < Cols && !IsBlocked(col + 1, row))
                    {
                        SetState(newCellsState[col + 1, row], RotateCounterClockWise(currentCellState));
                    }
                    else
                    {
                        SetState(newCellsState[col, row], RotateCounterClockWise(currentCellState));
                    }
                }
                else
                {
                    // free to go to the up side
                    SetState(newCellsState[col, row + 1], CellStateEnum.Down);
                }
            }


            //if (row + 1 < Rows && Cells[col, row + 1].State != CellStateEnum.Wall && Cells[col, row + 1].State != CellStateEnum.SoundWall)
            //    SetState(newCellsState[col, row + 1], CellStateEnum.Down);
            //else // out bounds, let it bounce to the other side
            //{
            //    // if there is room to bounce back
            //    if (row - 1 >= 0 && Cells[col, row - 1].State != CellStateEnum.Wall && Cells[col, row - 1].State != CellStateEnum.SoundWall)
            //        SetState(newCellsState[col, row - 1], CellStateEnum.Up);
            //    else // otherwise stay put and change cell
            //        SetState(newCellsState[col, row], CellStateEnum.Up);
            //}
        }
示例#4
0
        public void When_dead_with_exactly_three_live_neighbours_becomes_alive(CellStateEnum expected, string jsonContext)
        {
            // Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
            var  context = JsonConvert.DeserializeObject <CellStateEnum[]>(jsonContext);
            Cell cell    = new Cell(context);
            var  actual  = cell.Evolve();

            Assert.AreEqual(expected, actual);
        }
示例#5
0
        private CellStateEnum[,] evolveGridCells(int nrows, int ncols)
        {
            var newData = new CellStateEnum[nrows, ncols];

            for (int row = 0; row < nrows; row++)
            {
                evolveRowCells(row, newData);
            }

            return(newData);
        }
示例#6
0
        internal CellStateEnum[] NeighborhoodFor(CellPosition position)
        {
            var context = new CellStateEnum[NEIGHBORHOOD_SIZE];

            for (int pos = 0; pos < NEIGHBORHOOD_SIZE; pos++)
            {
                context[pos] = GetRelativePositionState(position, pos);
            }

            return(context);
        }
示例#7
0
        public void When_alive_dies_due_to_under_population(CellStateEnum expected, string jsonContext)
        {
            // Any live cell with fewer than two live neighbours dies, as if caused by under-population.

            var  context = JsonConvert.DeserializeObject <CellStateEnum[]>(jsonContext);
            Cell cell    = new Cell(context);

            var actual = cell.Evolve();

            Assert.AreEqual(expected, actual);
        }
 static public bool IsOpposite(this CellStateEnum thisState, CellStateEnum state)
 {
     if (thisState == CellStateEnum.Black && state == CellStateEnum.White)
     {
         return(true);
     }
     if (thisState == CellStateEnum.White && state == CellStateEnum.Black)
     {
         return(true);
     }
     return(false);
 }
示例#9
0
		public static String ToString(CellStateEnum state)
		{
			switch (state) {
				case CellStateEnum.Normal:
					return Normal;
				case CellStateEnum.Space:
					return Space;
				case CellStateEnum.Filled:
					return Filled;
				default:
					throw new ArgumentException("Unknown CellStateEnum: " + state.ToString());
			}
		}
示例#10
0
        public Cell(int r, int c, string color, IServerHandlerService serverHandlerService, CellStateEnum cellStateEnum)
        {
            Row        = r;
            Column     = c;
            Background = color;
            BlockState = cellStateEnum;

            Click = new RelayCommand(() =>
            {
                var message = new Message(MessageEnum.Coordinate, Row, Column);

                serverHandlerService.SendData(message);
            });
        }
示例#11
0
 /// <summary>
 /// Sets the state of the given cell. If it already has a state that is not dead,
 /// it will become merged and all the states that are assigned are added in the MergedStates list
 /// </summary>
 /// <param name="c"></param>
 /// <param name="state"></param>
 private void SetState(Cell c, CellStateEnum state)
 {
     if (c.State != CellStateEnum.Dead)
     {
         // already have state
         c.MergedStates.Add(state);
         c.State = CellStateEnum.Merged;
     }
     else
     {
         c.State        = state;
         c.MergedStates = new List <CellStateEnum>()
         {
             state
         };
     }
 }
示例#12
0
        internal GridData Randomize(GridSize gridSize)
        {
            var newData = new CellStateEnum[gridSize.NumberRows, gridSize.NumberCols];

            var numberCells = gridSize.NumberRows * gridSize.NumberCols;

            Random randomizer = new Random();

            for (int cellPos = 0; cellPos < numberCells; cellPos++)
            {
                var rowPos = IncrementEvery(cellPos, gridSize.NumberCols);
                var colPos = cellPos % gridSize.NumberCols;
                newData[rowPos, colPos] = GetRandomState(randomizer);
            }

            return(new GridData(_iodevice, newData));
        }
示例#13
0
 private bool ExtractCellState(string state)
 {
     try
     {                                        // Try to convert the state value to an enum value.
         CellStateEnum eState = (CellStateEnum)Enum.Parse(typeof(CellStateEnum), state);
         if (Common.IsValidStateEnum(eState)) // Conversion valid?
         {
             CellState = eState;              // Yes, save it.
             return(true);                    // Return true.
         }
     }
     catch (Exception)
     {
     }
     CellState = CellStateEnum.Blank;                        // Conversion failed.  Set state to Blank.
     return(false);                                          // Return false.
 }
示例#14
0
        /// <summary>
        /// Rotate cell state clockwise
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private CellStateEnum RotateClockWise(CellStateEnum s)
        {
            if (s == CellStateEnum.Up)
            {
                return(CellStateEnum.Right);
            }
            else if (s == CellStateEnum.Right)
            {
                return(CellStateEnum.Down);
            }
            else if (s == CellStateEnum.Down)
            {
                return(CellStateEnum.Left);
            }
            else if (s == CellStateEnum.Left)
            {
                return(CellStateEnum.Up);
            }

            return(s);
        }
        static public CellStateEnum GetOppositeState(this CellStateEnum thisState)
        {
            CellStateEnum state;

            if (thisState == CellStateEnum.Empty)
            {
                state = CellStateEnum.White;
            }
            else if (thisState == CellStateEnum.White)
            {
                state = CellStateEnum.Black;
            }
            else if (thisState == CellStateEnum.Black)
            {
                state = CellStateEnum.White;
            }
            else
            {
                state = CellStateEnum.Empty;
            }
            return(state);
        }
示例#16
0
 public Cell(CellStateEnum[] context)
 {
     _context = context;
     _state   = _context[CELL_POSITION];
 }
示例#17
0
 private static void SetCellState(CellClass[,] cells, CellIndex index1, CellIndex index2, CellStateEnum state)
 {
     cells[index1.Column, index1.Row].CellState = state;                 // Set the cell state of the first cell
     cells[index2.Column, index2.Row].CellState = state;                 // Set the cell state of the second cell
 }
示例#18
0
 public Cell(CellStateEnum state)
 {
     State        = state;
     MergedStates = new List <CellStateEnum>();
 }
示例#19
0
		public Cell()
		{
			State = CellStateEnum.Normal;
		}
示例#20
0
        public void When_alive_with_exactly_three_or_two_live_neighbours_remains_alive(CellStateEnum expected, string jsonContext)
        {
            // Any live cell with two or three live neighbours should live on to the next generation.
            var  context = JsonConvert.DeserializeObject <CellStateEnum[]>(jsonContext);
            Cell cell    = new Cell(context);
            var  actual  = cell.Evolve();

            Assert.AreEqual(expected, actual);
        }
示例#21
0
        public void When_alive_with_more_than_three_neighbours_dies_due_over_population(CellStateEnum expected, string jsonContext)
        {
            // Any live cell with more than three live neighbours dies, as if by over-population.

            var  context = JsonConvert.DeserializeObject <CellStateEnum[]>(jsonContext);
            Cell cell    = new Cell(context);
            var  actual  = cell.Evolve();

            Assert.AreEqual(expected, actual);
        }
示例#22
0
        /// <summary>
        /// Generates a board from the given otomata url
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static BoardSettings GetBoardFromOtomataUrl(string url)
        {
            // format of querystring is as follows:
            // <col><row&state><col><row&state>
            // with each <col><row&state> for an active cell
            // if cells are merged, then that pair will be in the querystring multiple times
            // to determine the row and state seperately
            // the rowAndState string below has to be split in chunks of 4 characters
            // the chunk index is the row, the character index in that chunk is the state
            string rowAndState = "qwertyuiopasdfghjklzxcvbnm0123456789";

            Uri uri = new Uri(url);

            string querystring = uri.Query.Replace("?q=", "");

            // create settings that are similar to the otomata settings
            BoardSettings settings = new BoardSettings()
            {
                Instrument   = 0,
                Sample       = "hang.wav",
                UseSamples   = true,
                NoteDuration = 3000,
                Notes        = "D3, A3, A#3, C4, D4, E4, F4, A5, C5",
                Speed        = 250
            };

            // create cell array
            Cell[,] cells = new Cell[9, 9];
            // fill default cells with all dead
            for (int j = 0; j < 9; j++)
            {
                for (int i = 0; i < 9; i++)
                {
                    cells[i, j] = new Cell(CellStateEnum.Dead);
                }
            }

            // foreach pair
            for (int i = 0; i < querystring.Length; i += 2)
            {
                // <col><row&state>
                string cell = querystring.Substring(i, 2);

                // parse col
                int col = int.Parse(cell[0].ToString());

                // parse row & state
                int row   = rowAndState.IndexOf(cell[1].ToString()) / 4;
                int state = rowAndState.IndexOf(cell[1].ToString()) % 4;

                // add the cell to the board, if there is already a cell present, make the cell merged
                // and add it to the merged cells list
                if (cells[col, row] != null && cells[col, row].State != CellStateEnum.Dead)
                {
                    if (cells[col, row].State == CellStateEnum.Merged)
                    {
                        cells[col, row].MergedStates.Add((CellStateEnum)state);
                    }
                    else
                    {
                        CellStateEnum oldState = cells[col, row].State;
                        cells[col, row].State        = CellStateEnum.Merged;
                        cells[col, row].MergedStates = new List <CellStateEnum>();
                        cells[col, row].MergedStates.Add(oldState);
                        cells[col, row].MergedStates.Add((CellStateEnum)state);
                    }
                }
                else
                {
                    cells[col, row] = new Cell((CellStateEnum)state);
                }
            }

            settings.Board = new OtomataBoard(9, 9, cells);

            return(settings);
        }
示例#23
0
 /// <summary>
 /// Determine if the cell is merged and its merged states contain the specified state
 /// </summary>
 /// <param name="c"></param>
 /// <param name="state"></param>
 /// <returns></returns>
 private bool CellIsMergedAndContainsState(Cell c, CellStateEnum state)
 {
     return(c.State == CellStateEnum.Merged && c.MergedStates.Contains(state));
 }
示例#24
0
        /// <summary>
        /// Checks the position left of the given position to set the new cell state
        /// </summary>
        /// <param name="newCellsState"></param>
        /// <param name="row"></param>
        /// <param name="col"></param>
        private void CheckLeft(Cell[,] newCellsState, CellStateEnum currentCellState, int row, int col)
        {
            if (col - 1 < 0 || IsWall(col - 1, row))
            { // reached edge or wall, bounce
                // if there is room to bounce back
                if (col + 1 < Cols && !IsBlocked(col + 1, row))
                {
                    SetState(newCellsState[col + 1, row], CellStateEnum.Right);
                }
                else
                {
                    SetState(newCellsState[col, row], CellStateEnum.Right);
                }
            }
            else
            {
                if (Cells[col - 1, row].State == CellStateEnum.RotateClockWise)
                {
                    if (row - 1 >= 0 && !IsBlocked(col, row - 1))
                    {
                        SetState(newCellsState[col, row - 1], RotateClockWise(currentCellState));
                    }
                    else
                    {
                        SetState(newCellsState[col, row], RotateClockWise(currentCellState));
                    }
                }
                else if (Cells[col - 1, row].State == CellStateEnum.RotateCounterClockWise)
                {
                    if (row + 1 < Rows && !IsBlocked(col, row + 1))
                    {
                        SetState(newCellsState[col, row + 1], RotateCounterClockWise(currentCellState));
                    }
                    else
                    {
                        SetState(newCellsState[col, row], RotateCounterClockWise(currentCellState));
                    }
                }
                else
                {
                    // free to go to the left side
                    SetState(newCellsState[col - 1, row], CellStateEnum.Left);
                }
            }


            //if (col - 1 >= 0 &&
            //    Cells[col - 1, row].State != CellStateEnum.Wall && Cells[col - 1, row].State != CellStateEnum.SoundWall &&
            //    Cells[col - 1, row].State != CellStateEnum.RotateClockWise && Cells[col - 1, row].State != CellStateEnum.RotateCounterClockWise)
            //    // free to go to the left side
            //    SetState(newCellsState[col - 1, row], CellStateEnum.Left);
            //else
            //{
            //    // let it bounce to the other side

            //    // if there is room to bounce back
            //    if (col + 1 < Cols && Cells[col + 1, row].State != CellStateEnum.Wall && Cells[col + 1, row].State != CellStateEnum.SoundWall)
            //        SetState(newCellsState[col + 1, row], CellStateEnum.Right);
            //    else
            //        SetState(newCellsState[col, row], CellStateEnum.Right);
            //}
        }