/// <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; }
public Cell(Coordinates coor, int Generation, bool IsAlive, DateTime StartGame) { if (coor == null) throw new NullReferenceException(); this.Generation = Generation; this.IsAlive = IsAlive; X = coor.X; Y = coor.Y; this.StartGame_Id = StartGame; }
/// <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; }
/// <summary> /// Calculate next generation and store the changes in the database. /// </summary> /// <returns></returns> public JsonResult NextGeneration() { Cell[] ChangedStatusCells; //Get array with the cells which will change the status in the next generation ChangedStatusCells = Generationsgame.NextGenerationGrid(); //Update the Cell State Generationsgame.UpdateGrid(ChangedStatusCells); Coordinates[] ChangedCoor = new Coordinates[ChangedStatusCells.Length]; int index = 0; //Set the cells to be store in the dabase and create the cell coordinates array // foreach (Cell cell in ChangedStatusCells) { cell.StartGame_Id = CurrentGame.StartGame_Id; ChangedCoor[index] = cell.Coor; index++; } //Save the amended Cells MainController.repository.SaveCell(ChangedStatusCells); return Json(ChangedCoor, JsonRequestBehavior.AllowGet); }
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(); }
/// <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; }