public Cell(int r, int c, string color, CellStateEnum cellStateEnum) { Row = r; Column = c; Background = color; BlockState = cellStateEnum; }
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); }
/// <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); //} }
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); }
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); }
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); }
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); }
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()); } }
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); }); }
/// <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 }; } }
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)); }
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. }
/// <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); }
public Cell(CellStateEnum[] context) { _context = context; _state = _context[CELL_POSITION]; }
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 }
public Cell(CellStateEnum state) { State = state; MergedStates = new List <CellStateEnum>(); }
public Cell() { State = CellStateEnum.Normal; }
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); }
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); }
/// <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); }
/// <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)); }
/// <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); //} }