public Cell AddCell(int x, int y) { var cell = new Cell(); Cells[x, y] = cell; cell.Reposition(x, y); return cell; }
public Board() { // size of the 2d array playing board Size = new Point(Game1.cellnoX, Game1.cellnoY); lastKState = Keyboard.GetState(); // setting it up like cell[400,200] and cellState[bool[400],bool[200]]; cell = new Cell[Size.X, Size.Y]; cellState = new bool[Size.X, Size.Y]; //filling up the playing board with cell in place //initializing every cell state to be false by default for (i = 0; i < Size.X; i++) { for (j = 0; j < Size.Y; j++) { cell[i, j] = new Cell(new Point(i, j)); cellState[i, j] = false; } } //setting clock to 0 timer = TimeSpan.Zero; }
/// <summary> /// Implementation of the rules of Conway's Game of Life. /// Any live cell with fewer than two live neighbors dies, as if caused by under-population. /// Any live cell with two or three live neighbors lives on to the next generation. /// Any live cell with more than three live neighbors dies, as if by over-population. /// Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction. /// </summary> /// <param name="cell">Cell in current move</param> /// <param name="isAlive">State of cell</param> /// <returns>Whether will be given cell alive in next move.</returns> private bool WillBeCellAliveInNextTurn(Cell cell,bool isAlive) { int countOfNeighbors = cell.GetCountOfNeighbors(_cells); if (isAlive) return countOfNeighbors == 2 || countOfNeighbors == 3; return countOfNeighbors == 3; }
public RandomBoard() { // size of the 2d array playing board Size = new Point(Game1.cellnoX, Game1.cellnoY); lastKState = Keyboard.GetState(); // setting it up like cell[400,200] and cellState[bool[400],bool[200]]; cell = new Cell[Size.X, Size.Y]; cellState = new bool[Size.X, Size.Y]; //filling up the playing board with cell in place //initializing every cell state to be either true or false according to the random variable for (i = 0; i < Size.X; i++) { for (j = 0; j < Size.Y; j++) { cell[i, j] = new Cell(new Point(i, j)); int flagbool = random.Next(2); if (flagbool == 1) { cell[i, j].Alive = false; cellState[i, j] = false; } else if (flagbool == 0) { cell[i, j].Alive = true; cellState[i, j] = true; } } } //setting clock to 0 timer = TimeSpan.Zero; }
public void save(string filePath, ref Cell[,] area) { var rank = area.Rank; if (rank != 2) throw new Exception("Not two dimensional"); int height = area.GetUpperBound(1); int width = area.GetLowerBound(2); try { using (StreamWriter sw = new StreamWriter(filePath)) { sw.WriteLine("%d", width); sw.WriteLine("%d", height); for (var i = 0; i < height; ++height) { for (var j = 0; j < width; ++width) { sw.WriteLine("%d", area[i, j].status); } } } } catch (Exception e) { throw e; } }
public Cell(int x, int y, bool isAlive, Cell[,] cells) { IsAlive = isAlive; _point = new Point(x, y); _cells = cells; _log = ""; }
public Grid(bool[,] grid) { cells = new ICell[grid.GetLength(0), grid.GetLength(1)]; for (int x = 0; x < grid.GetLength(0); ++x) for (int y = 0; y < grid.GetLength(1); ++y) cells[x, y] = new Cell(this) { IsAlive = grid[x,y] }; }
/// <summary> /// Creates an empty row with dead cells for the number of columns passed as argument /// </summary> /// <param name="cellsCount">Number of columns</param> public Row(int cellsCount) : this() { for (int i = 0; i < cellsCount; i++) { Cell cell = new Cell("-"); cells.Add(cell); } }
/// <summary> /// Set the status of a cell (Alive or Dead) according to the following rules: /// 1.- Any live cell with fewer than two live neighbours dies, as if caused by under-population. /// 2.- Any live cell with two or three live neighbours lives on to the next generation. /// 3.- Any live cell with more than three live neighbours dies, as if by over-population. /// 4.- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. /// </summary> /// <param name="neighbors"></param> public void SetAliveStatus(Cell[] neighbors) { int neighborsAlive = neighbors.Where(x => x.IsAlive).Count(); if (IsAlive && (neighborsAlive == 3 || neighborsAlive == 2))//Should live IsAlive = true; if (IsAlive && neighborsAlive > 3 || neighborsAlive < 2) IsAlive = false; if (!IsAlive && neighborsAlive == 3)//Born IsAlive = true; }
private void Initialize() { for (int i = 0; i < Cells.GetLength(0); i++) { for (int j = 0; j < Cells.GetLength(1); j++) { Cells[i, j] = new Cell(new Position(i, j)); } } }
private void CreateLife() { for (int x = 0; x < Size; x++) { for (int y = 0; y < Size; y++) { Cell @newCell = new Cell(x, y, false); Life[x, y] = @newCell; } } }
public virtual CellState ApplyRulesTo(Cell cell) { var cellState = cell.State; foreach (var rule in ruleSet) { if(cellState == cell.State) cellState = rule.NextStateFor(cell); } return cellState; }
private int getAliveNeighbours(Cell allCell) { return allCells .Where(c => c.isAlive == true) .Where(c => c.X >= allCell.X - 1 && c.X <= allCell.X + 1 && c.Y >= allCell.Y - 1 && c.Y <= allCell.Y + 1) .Where(c => c != allCell) .Count(); }
public Grid(Vector gridSize) { Cells = new Cell[gridSize.X, gridSize.Y]; this.InitializeCells(); _cellsMarkedToBirth = new List<Cell>(); _cellsMarkedToDeath = new List<Cell>(); _cellsWidth = this.Cells.GetLength(0); _cellsHeight = this.Cells.GetLength(1); }
/// <summary> /// Constructor that initializes the pattern for the row /// </summary> /// <param name="rowPattern">Pattern for the row</param> public Row(String rowPattern) : this() { rowPattern = rowPattern.Trim(); char[] splitPattern = " ".ToCharArray(); String[] columns = rowPattern.Split(splitPattern); for (int i = 0; i < columns.Length; i++) { Cell cell = new Cell(columns[i]); cells.Add(cell); } }
public void load(string filePath, out Cell[,] area) { try { using (StreamReader sr = new StreamReader(filePath)) { String widthString, heightString; if ((widthString = sr.ReadLine()) == null || (heightString = sr.ReadLine()) == null) { throw new Exception("Invalid file format"); } UInt32 width = Convert.ToUInt32(widthString); UInt32 height = Convert.ToUInt32(heightString); Cell[,] newArea = new Cell[width, height]; UInt32 numberOfCells = width * height; UInt32 cellCounter = 0; String cellStatusString; while ((cellStatusString = sr.ReadLine()) != null) { ++cellCounter; UInt32 x = cellCounter % width; UInt32 y = cellCounter/width; Cell.Status status; if (cellStatusString == "0") status = Cell.Status.Empty; else if (cellStatusString == "1") status = Cell.Status.Alive; else if (cellStatusString == "2") status = Cell.Status.Dead; else throw new Exception("Invalid file format: Invalid cell status"); newArea[x, y] = new Cell(status, 0); } if (cellCounter != numberOfCells) throw new Exception("Invalid file format: Not enough cells"); area = newArea; } } catch (Exception e) { area = null; throw e; } }
public GameGrid(int w, int h,Cell [,] grid, GraphicsDevice graphics, SpriteBatch spriteBatch, MacGame game) { m_spriteBatch = spriteBatch; m_graphics = graphics; m_Width = w; m_Height = h; m_Grid = grid; texture = game.Content.Load<Texture2D>("alive"); dead = game.Content.Load<Texture2D>("dead"); }
public Grid(int width, int height) { if (width <= 0) throw new ArgumentOutOfRangeException("width"); if (height <= 0) throw new ArgumentOutOfRangeException("height"); cells = new ICell[width, height]; for(int x = 0; x < width; ++x) for(int y = 0; y < height; ++y) cells[x,y] = new Cell(this); }
private IEnumerable<Cell> GetNeighborAddress(Cell address) { for (int row = address.Row - 1; row <= address.Row + 1; row++) { for (int collum = address.Collumn - 1; collum <= address.Collumn + 1; collum++) { if (row != address.Row || collum != address.Collumn) { yield return new Cell(row, collum); } } } }
/// <summary> /// Update the status of a cell according to the rules /// </summary> /// <param name="cell">The cell that will be updated according to the rules</param> private void UpdateCellStatus(Cell cell) { Cell[] neighbors = new Cell[8]; neighbors[0] = GetNeighbor(cell.PosX - 1, cell.PosY - 1);//Top left neighbors[1] = GetNeighbor(cell.PosX, cell.PosY - 1);//Top neighbors[2] = GetNeighbor(cell.PosX + 1, cell.PosY - 1);//Top right neighbors[3] = GetNeighbor(cell.PosX - 1, cell.PosY);//left neighbors[4] = GetNeighbor(cell.PosX + 1, cell.PosY);//right neighbors[5] = GetNeighbor(cell.PosX - 1, cell.PosY + 1);//bottom left neighbors[6] = GetNeighbor(cell.PosX, cell.PosY + 1);//bottom neighbors[7] = GetNeighbor(cell.PosX + 1, cell.PosY + 1);//bottom right cell.SetAliveStatus(neighbors); }
public void Tick() { NextLife = new Cell[Size, Size]; for (int x = 0; x < Size; x++) { for (int y = 0; y < Size; y++) { Cell cell = Life[x, y]; NextLife[x, y] = cell.EvolveFrom(Life); } } Life = NextLife; }
public void AddCell(Cell cell) { if (cell.Alive) { cell.LiveNeighbors = 0; _liveCells.Add(cell); if (_potentiallySignificantCells.Contains(cell)) { _potentiallySignificantCells.Remove(cell); } AddDeadCells(cell); } }
private Boolean DetermineNextGenerationLifeStatusFor(Cell cell, GameCriteria criteria) { var iterator = criteria.CellIterator; var aliveNeighbors = 0; iterator.Initialize(Cells); iterator.SetHomeCell(cell); for (iterator.First(); !iterator.IsDone(); iterator.Next()) { if (iterator.CurrentItem() != null && iterator.CurrentItem().IsAlive) aliveNeighbors++; } return criteria.GameRules.IsLifeGrantedFor(cell.IsAlive, aliveNeighbors); }
public void DrawBoard() { var sb = new StringBuilder(); for (int y = 0; y < ViewportY; y++) { for (int x = 0; x < ViewportX; x++) { var c = new Cell { X = x, Y = y }; sb.Append(Cells.Contains(c) ? "X " : " "); } sb.AppendLine(); } System.Console.Clear(); System.Console.Write(sb); }
public World(int width, int height) { this.width = width; this.height = height; grid = new Cell[width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { grid[x, y] = new Cell(); } } }
public DaWorld(int ExistentialDilemma) { if (ExistentialDilemma >= 9) { Cell initCell = new Cell(true); Cell initCell2 = new Cell(true); Cell initCell3 = new Cell(true); Cell initCell4 = new Cell(true); this.Barren = false; } else { this.Barren = true; } }
public Map(Coordinate x, Coordinate y, IEnumerable<Cell> aliveCells) { X = x.Value(); Y = y.Value(); Factory = new StateFactory(); Cells = new List<Cell>((X+1) * (Y+1)); for (var i = 0; i < X+1; i++) for (var j = 0; j < Y+1; j++) { var coordinateX = new Coordinate(i); var coordinateY = new Coordinate(j); var cell = new Cell(coordinateX, coordinateY); cell.State = (aliveCells.Contains(cell)) ? (State) new Alive(cell) : new Dead(cell); Cells.Add(cell); } }
private Cell[][] BuildLocalWorld(int xCell, int yCell) { Cell[][] localWorld = new Cell[3][]; for (int i = 0; i < 3; i++) { localWorld[i] = new Cell[3]; } for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { Cell c = GetCellRelativeTo(xCell, yCell, x, y); localWorld[x + 1][y + 1] = c; } } return localWorld; }
private void SetALiveNeighbours(Cell cell) { var a = cell.Position.X ; var b = cell.Position.Y ; cell.LiveNeighbours = 0; for (var x = a -1 ; x <= a+1; x++ ) for (var y = b - 1; y <= b+1; y++) { if (x==a && y == b) continue; if (!AreCoordinatesOnTheMap(x, y)) continue; var x1 = x; var y1 = y; var c1 = (Cells.Find(c => c.Position.X == x1 && c.Position.Y == y1)); cell.LiveNeighbours += ((c1.State.GetType() == typeof(Alive)) ? 1 : 0); } }
public SimpleMap(int sideSize, GameRules rules) { this.SideSize = sideSize; this.Rules = rules; int arraySize = SideSize * SideSize; CellMap = new Cell[arraySize]; CellBuffer = new Cell[arraySize]; Point point; for (int x = 0; x < arraySize; x++) { //Translate x/y coordinates into 1Dimensional array index point.X = x % SideSize; point.Y = x / SideSize; CellMap[x] = new Cell(point, false); CellBuffer[x] = new Cell(point, false); } }
public void Generate() // This method Create the world each turn. { /// The method generate the world, it has two parts: /// 1. When the world is empty (else part). /// 2. When the world has been created (if part) the method will call other methods to make the necessary changes. if (cells != null) // [2] { ChangeLadybirds(); ChangeGreenflies(); graphValues.Add(timeStep, new int[] { greenflies.Count, ladybirds.Count }); continu &= (ladybirds.Count != 0 && greenflies.Count != 0); // Checking if the number of greenflies or ladybirds has reached 0. } else //[1] { timeStep = 0; graphValues.Add(timeStep, new int[] { startGreenflyNums, startLadybirdNums }); cells = new Cell[length, width]; for (int i = 0; i < cells.GetLength(0); i++) { for (int j = 0; j < cells.GetLength(1); j++) { cells[i, j] = new Cell(i, j); } } // GreenFlies and Ladybirds are created randomly throughout the grid. Random rand = new Random(); for (int L = 0; L < startLadybirdNums; L++) // Creating Ladybirds. { int randX = rand.Next(0, length); int randY = rand.Next(0, width); if (CellIsEmpty(randX, randY)) { LadyBird ladyBird = new LadyBird(randX, randY); ladybirds.Add(ladyBird); cells[randX, randY].content = ladyBird.shape; } else { while (!CellIsEmpty(randX, randY)) // keep looping until en empty cell is found. { randX = rand.Next(0, length); randY = rand.Next(0, width); } LadyBird ladyBird = new LadyBird(randX, randY); ladybirds.Add(ladyBird); cells[randX, randY].content = ladyBird.shape; } } for (int G = 0; G < startGreenflyNums; G++) //Creating Greenflies. { int randX = rand.Next(0, length); int randY = rand.Next(0, width); if (CellIsEmpty(randX, randY)) { GreenFly greenfly = new GreenFly(randX, randY); greenflies.Add(greenfly); cells[randX, randY].content = greenfly.shape; } else { while (!CellIsEmpty(randX, randY)) { randX = rand.Next(0, length); randY = rand.Next(0, width); } GreenFly greenfly = new GreenFly(randX, randY); greenflies.Add(greenfly); cells[randX, randY].content = greenfly.shape; } } } Information(); // Update the information. }