/// <summary> /// Return the type of Cell acording to the Coordinates within the Grid /// </summary> /// <param name="grid"></param> /// <param name="coor"></param> /// <returns></returns> public static CellType GetCellType(Grid grid, Coordinates coor) { if ((coor.X < 0 || coor.X > grid.GridWidth) || (coor.Y < 0 || coor.Y > grid.GridHeight) || (grid.GridHeight<1 || grid.GridWidth<1)) { throw new ArgumentOutOfRangeException(); } CellType cellType =CellType.BottomLeft; if (coor.X == 0 && coor.Y == 0) cellType = CellType.TopLeft; else if (coor.X == 0 && coor.Y == grid.GridWidth - 1) cellType = CellType.TopRight; else if (coor.X == 0 && (coor.Y > 0 && coor.Y < grid.GridWidth - 1)) cellType = CellType.TopSide; else if (coor.X == grid.GridHeight - 1 && coor.Y == 0) cellType = CellType.BottomLeft; else if (coor.X == grid.GridHeight - 1 && coor.Y == grid.GridWidth - 1) cellType = CellType.BottomRight; else if (coor.X == grid.GridHeight - 1 && (coor.Y > 0 && coor.Y < grid.GridWidth - 1)) cellType = CellType.BottomSide; else if ((coor.X > 0 && coor.X < grid.GridHeight - 1) && coor.Y == 0) cellType = CellType.LeftSide; else if ((coor.X > 0 && coor.X < grid.GridHeight - 1) && coor.Y == grid.GridWidth - 1) cellType = CellType.RightSide; else if ((coor.X > 0 && coor.X < grid.GridHeight - 1) && (coor.Y > 0 && coor.Y < grid.GridWidth - 1)) cellType = CellType.Center; return cellType; }
/// <summary> /// Return true if a cell in a specific coordinate change the status /// </summary> /// <param name="grid"></param> /// <param name="coor"></param> /// <returns></returns> public static bool ChangeStatus(Grid grid, Coordinates coor) { //Calculate number of Alive Neighbour int Neighbours = GridHelper.CountAliveNeighbours(grid,coor); //Rules if (grid[coor.X, coor.Y].IsAlive) { if (Neighbours != 2 && Neighbours != 3) return true; } if (!grid[coor.X, coor.Y].IsAlive) { if (Neighbours == 3) return true; } return false; }
public Cell[] Start(Coordinates[] ListInitialCells, Game game) { inputGrid = new Grid(inputGrid.GridWidth, inputGrid.GridHeight); for (int x = 0; x < inputGrid.GridHeight; x++) { Row newRow = new Row(); for (int y = 0; y < inputGrid.GridWidth; y++) { Cell newCell = new Cell(new Coordinates(x, y), 0, false, game.StartGame_Id); newCell.type = GridHelper.GetCellType(inputGrid, newCell.Coor); newRow.CellsList.Add(newCell); } inputGrid.RowsList.Add(newRow); } foreach (Coordinates coor in ListInitialCells) { //inputGrid[coor.X, coor.Y].IsAlive = true; ListAmendedCell.Add(inputGrid[coor.X, coor.Y]); } return ListAmendedCell.ToArray(); }
public GenerationsModel(int GridWidth, int GridHeight) { if (GridWidth < 1 || GridHeight < 1) throw new ArgumentException("Param must be greater than 0"); inputGrid = new Grid(GridWidth, GridHeight); //ListAmendedCell = new ConcurrentBag<Cell>(); ListAmendedCell = new List<Cell>(); GridHelper.InitializeReachableCells(); }
/// <summary> /// Check if the Neighbour cell is alive /// </summary> /// <param name="grid"></param> /// <param name="cell"></param> /// <param name="neighbour"></param> /// <returns>returns 1 if alive otherwise 0</returns> private static int IsAliveNeighbour(Grid grid, Coordinates cell, Coordinates neighbour) { int live = 0; // set default as 0 int x = cell.X + neighbour.X; // get x axis of neighbour int y = cell.Y + neighbour.Y; // get y axis of neighbour // check the computed bound is within range of grid, if it is not within bounds live is 0 as default if ((x >= 0 && x < grid.GridHeight) && y >= 0 && y < grid.GridWidth) { // if reachable neighbour cell is alive then set live to 1 otherwise 0 live = grid[x, y].IsAlive ? 1 : 0; } return live; }
/// <summary> /// Count live adjacent cells for specified cell co-ordinates /// </summary> /// <param name="grid"></param> /// <param name="coor"></param> /// <returns>returns number of live neighbours</returns> private static int CountAliveNeighbours(Grid grid, Coordinates coor) { int liveNeighbours = 0; // Get the Cell type of current cell CellType celltype = grid[coor.X,coor.Y].type; List<Coordinates> reachableCells = new List<Coordinates>(); // populate reachable cells from current cell for easier traversing GridHelper.ReachableCells.TryGetValue(celltype, out reachableCells); if (reachableCells.Count == 0) throw new ArgumentNullException("Cannot find reachable co-ordinates"); foreach (Coordinates coord in reachableCells) { liveNeighbours += IsAliveNeighbour(grid, coor, coord); } return liveNeighbours; }