示例#1
0
 public void CellEnsureCellIsAlive()
 {
     // object initializer syntax
     // use public setters when creating new instance
     Cell my_cell = new Cell { IsAlive = true };
     Assert.IsTrue(my_cell.IsAlive);
 }
        public CellBoard(bool[,] start)
        {
            Cell current;
            size = start.GetLength(1);
            for (int i = 0; i < start.GetLength(0); i++)
            {
                for (int j = 0; j < start.GetLength(1); j++)
                {
                    current = new Cell(start[i,j]);
                    currentBoard.Add(current);
                    if (j == 0)
                    {
                        current.Left = null;
                        current.TopLeft = null;

                        try { currentBoard[currentBoard.Count - 2].Right = null; }
                        catch (ArgumentOutOfRangeException) { }

                        try { currentBoard[currentBoard.Count - start.GetLength(1) - 2].BottomRight = null; }
                        catch (ArgumentOutOfRangeException) { }
                    }
                    else
                    {
                        try { current.Left = currentBoard[currentBoard.Count - 2]; }
                        catch (Exception) { }

                        try { current.TopLeft = currentBoard[currentBoard.Count - start.GetLength(1) - 2]; }
                        catch (ArgumentOutOfRangeException) { current.TopLeft = null; }

                        try { currentBoard[currentBoard.Count - 2].Right = current; }
                        catch (ArgumentOutOfRangeException) { }

                        try { currentBoard[currentBoard.Count - start.GetLength(1) - 2].BottomRight = current; }
                        catch (ArgumentOutOfRangeException) { }
                    }

                    if (j == start.GetLength(1) - 1)
                    {
                        current.TopRight = null;

                        try { currentBoard[currentBoard.Count - start.GetLength(1)].BottomLeft = null; }
                        catch (ArgumentOutOfRangeException) { }
                    }
                    else
                    {
                        try { current.TopRight = currentBoard[currentBoard.Count - start.GetLength(1)]; }
                        catch (ArgumentOutOfRangeException) { }

                        try { currentBoard[currentBoard.Count - start.GetLength(1)].BottomLeft = current; }
                        catch (ArgumentOutOfRangeException) { }
                    }

                    try { current.Top = currentBoard[currentBoard.Count - start.GetLength(1) - 1]; }
                    catch (ArgumentOutOfRangeException) { }

                    try { currentBoard[currentBoard.Count - start.GetLength(1) - 1].Bottom = current; }
                    catch (ArgumentOutOfRangeException) { }
                }
            }
        }
示例#3
0
 public void CellEnsureCellIsDead()
 {
     // object initializer syntax
     // use public setters when creating new instance
     Cell my_cell = new Cell();
     Assert.IsFalse(my_cell.IsAlive);
 }
 // Mark a cell to be removed if live neighborsCount < 2 || > 3
 public void ApplyKillRules(Cell cell, int numLiveNeighbors)
 {
     if(numLiveNeighbors > 3 || numLiveNeighbors < 2)
     {
         cell.MarkForChange = true;
     }
 }
示例#5
0
        public void CellConstructorTest()
        {
            Cell cell = new Cell(1, 2);

            Assert.AreEqual(cell.LocationX, 1);
            Assert.AreEqual(cell.LocationY, 2);
            Assert.AreEqual(cell.NeighborCount, 0);
        }
示例#6
0
 public void CellEnsureCellIsAlive()
 {
     // Object Initializer Syntax
     // Use public setters when creating new instance
     // Use {} instead of ()
     Cell cell = new Cell { IsAlive = true };
     Assert.IsTrue(cell.IsAlive);
 }
示例#7
0
 public void CellEnsureWeCanProvidePosition()
 {
     int x = 1;
     int y = 2;
     Cell my_cell = new Cell {X = x, Y = y};
     Assert.AreEqual(x, my_cell.X);
     Assert.AreEqual(y, my_cell.Y);
 }
 public bool Equals(Cell comparison)
 {
     // If parameter is null return false:
     if ((object)comparison == null)
     {
         return false;
     }
     // Return true if the fields match:
     return (Row == comparison.Row) && (Col == comparison.Row);
 }
 public void CanApplyKillRules()
 {
     List<Cell> alive = new List<Cell>();
     alive.Add(new Cell ( 1, 2 ));
     alive.Add(new Cell ( 2, 2 ));
     alive.Add(new Cell ( 2, 3 ));
     alive.Add(new Cell ( 4, 3 ));
     GameBoard board = new GameBoard(alive);
     Cell cellToKill = new Cell(1, 2);
     board.ApplyKillRules(cellToKill, 4);
     Assert.IsTrue(cellToKill.MarkForChange);
 }
示例#10
0
        public bool checkCellCollectionForCell(List<Cell> cellCollection, Cell cellInQuestion)
        {
            foreach (Cell cell in cellCollection)
            {
                if (cellInQuestion.LocationX == cell.LocationX && cellInQuestion.LocationY == cell.LocationY)
                {
                    return true;
                }
            }

            return false;
        }
 public GameOfLife(int i, int j)
 {
     cells = new Cell[i, j];
     for (int h = 0; h < i; h++)
     {
         for (int k = 0; k < j; k++)
         {
             cells[h, k] = new Cell(h, k);
         }
     }
     this.RemoveOffGridNeighbors();
 }
示例#12
0
 public Cell(Cell oldCell)
 {
     Grid.SetColumn(this, oldCell.col);
     Grid.SetRow(this, oldCell.row);
     row = oldCell.row;
     col = oldCell.col;
     Width = oldCell.Width;
     Height = oldCell.Height;
     _geometry = oldCell._geometry;
     isLiving = oldCell.isLiving;
     this.Fill = oldCell.Fill;
 }
示例#13
0
 public Form1()
 {
     InitializeComponent();
     cells = new Cell[cellborder,cellborder];
     for (int x = 0; x < cellborder; x++)
     {
         for (int y = 0; y < cellborder; y++)
         {
             cells[x, y] = new Cell();
         }
     }
 }
示例#14
0
 private bool IsAlive(Cell c)
 {
     bool result = false;
     foreach (var cell in cells)
     {
         if (c.X == cell.X && c.Y == cell.Y)
         {
             result = true;
             break;
         }
     }
     return result;
 }
示例#15
0
 public BoundedWorld(int width, int height)
 {
     cells = new Cell[height, width];
     Width = width;
     Height = height;
     for (int row = 0; row < Height; row++)
     {
         for (int col = 0; col < Width; col++)
         {
             cells[row, col] = new Cell();
         }
     }
 }
示例#16
0
        public void GetLiveNeighborsTest()
        {
            Cell newCell = new Cell { X = 0, Y = 0 };
            Cell anotherCell = new Cell { X = 1, Y = 0 };
            World world = new World();
            world.AddCell(0, 0);
            world.AddCell(0,1);
            world.AddCell(2,3);
            world.AddCell(-1, -1);
            List<Cell>actual = world.GetLiveNeighbors(world.GetCell(0,0).X, world.GetCell(0, 0).Y);

            Assert.AreEqual(2,actual.Count);
        }
 public void CellEnsureWeCanProvidePosition()
 {
     int x = 1;
     int y = 2;
     Cell my_cell = new Cell { X = x, Y = y};
     /* The same as:
     Cell my_cell = new Cell();
     my_cell.X = x;
     my_cell.Y = y;
     */
     Assert.AreEqual(x, my_cell.X);
     Assert.AreEqual(y, my_cell.Y);
 }
示例#18
0
        public List<Cell> AliveNeighbors(int x, int y)
        {
            List<Cell> neighbors = new List<Cell>();
            Cell above = null;
            Cell below = null;

            below = new Cell { X = x, Y = y - 1 };
            above = new Cell { X = x, Y = y + 1 };

            if (cells.Exists(c => c.X==above.X && c.Y == above.Y))
            {
                neighbors.Add(above);
            }

            if (cells.Exists(c => c.X == below.X && c.Y == below.Y))
            {
                neighbors.Add(below);
            }

            return neighbors;
        }
 // Constructor with just height, width & algorithm rules creates random board
 public GameOfLife(int height, int width, string rules)
 {
     RulesParser(rules);
     Random rand = new Random();
     this.height = height;
     this.width = width;
     gameBoard = new Cell[height, width];
     for (int y = 0; y < height; y++)
     {
         ObservableCollection<bool> row = new ObservableCollection<bool> { };
         for (int x = 0; x < width; x++)
         {
             double check = rand.NextDouble();
             bool cellAlive = check >= 0.5;
             gameBoard[y, x] = new Cell(cellAlive) { Y = y, X = x };
             if (cellAlive) activeCells.Add(gameBoard[y, x]);
             row.Add(cellAlive);
         }
         cellList.Add(row);
     }
 }
示例#20
0
 public void CalculateAppropriateNeighborCoordinatesForCellAt0x0()
 {
     Cell ATestCell = new Cell(0, 0);
     List<int[]> TestNeighbors = new List<int[]>();
     TestNeighbors.Add(new int[] { -1, -1 });
     TestNeighbors.Add(new int[] { -1, 0 });
     TestNeighbors.Add(new int[] { -1, 1 });
     TestNeighbors.Add(new int[] { 0, -1 });
     TestNeighbors.Add(new int[] { 0, 1 });
     TestNeighbors.Add(new int[] { 1, -1 });
     TestNeighbors.Add(new int[] { 1, 0 });
     TestNeighbors.Add(new int[] { 1, 1 });
     List<int[]> ActualNeighbors = ATestCell.Neighbors;
     CollectionAssert.AreEquivalent(TestNeighbors[0], ActualNeighbors[0]);
     CollectionAssert.AreEquivalent(TestNeighbors[1], ActualNeighbors[1]);
     CollectionAssert.AreEquivalent(TestNeighbors[2], ActualNeighbors[2]);
     CollectionAssert.AreEquivalent(TestNeighbors[3], ActualNeighbors[3]);
     CollectionAssert.AreEquivalent(TestNeighbors[4], ActualNeighbors[4]);
     CollectionAssert.AreEquivalent(TestNeighbors[5], ActualNeighbors[5]);
     CollectionAssert.AreEquivalent(TestNeighbors[6], ActualNeighbors[6]);
     CollectionAssert.AreEquivalent(TestNeighbors[7], ActualNeighbors[7]);
 }
示例#21
0
 public void CalculateAppropriateNeighborCoordinatesForCellAt2x2()
 {
     Cell ATestCell = new Cell(2, 2);
     PrivateObject ATestCellObj = new PrivateObject(ATestCell);
     List<int[]> TestNeighbors = new List<int[]>();
     TestNeighbors.Add(new int[] { 1, 1 });
     TestNeighbors.Add(new int[] { 1, 2 });
     TestNeighbors.Add(new int[] { 1, 3 });
     TestNeighbors.Add(new int[] { 2, 1 });
     TestNeighbors.Add(new int[] { 2, 3 });
     TestNeighbors.Add(new int[] { 3, 1 });
     TestNeighbors.Add(new int[] { 3, 2 });
     TestNeighbors.Add(new int[] { 3, 3 });
     List<int[]> ActualNeighbors = ATestCell.Neighbors;
     CollectionAssert.AreEquivalent(TestNeighbors[0], ActualNeighbors[0]);
     CollectionAssert.AreEquivalent(TestNeighbors[1], ActualNeighbors[1]);
     CollectionAssert.AreEquivalent(TestNeighbors[2], ActualNeighbors[2]);
     CollectionAssert.AreEquivalent(TestNeighbors[3], ActualNeighbors[3]);
     CollectionAssert.AreEquivalent(TestNeighbors[4], ActualNeighbors[4]);
     CollectionAssert.AreEquivalent(TestNeighbors[5], ActualNeighbors[5]);
     CollectionAssert.AreEquivalent(TestNeighbors[6], ActualNeighbors[6]);
     CollectionAssert.AreEquivalent(TestNeighbors[7], ActualNeighbors[7]);
 }
 public void CellHasColProperty()
 {
     Cell cell = new Cell(false, 1, 0,10);
     Assert.AreEqual(0, cell.Col);
 }
        // Return the Count of alive neighbors
        public int NeighborsAliveCount(Cell cell)
        {
            int aliveCount = 0;
            List<Cell> neighbors = GenerateNeighborsList(cell);

            foreach(var neighbor in neighbors)
            {
                if (AliveCells.Contains(neighbor))
                {
                    aliveCount++;
                } else
                {
                    ressurectionCellCandidates.Add(neighbor);
                }
            }
            return aliveCount;
        }
 // Returns lists of lists of all neighbor coordinates from the input list
 public List<Cell> GenerateNeighborsList(Cell cell)
 {
     List<Cell> neighbors = new List<Cell>();
     neighbors.Add(new Cell (cell.Row - 1, cell.Col - 1 ));
     neighbors.Add(new Cell (cell.Row - 1, cell.Col));
     neighbors.Add(new Cell (cell.Row - 1, cell.Col + 1));
     neighbors.Add(new Cell (cell.Row, cell.Col - 1 ));
     neighbors.Add(new Cell (cell.Row, cell.Col + 1 ));
     neighbors.Add(new Cell (cell.Row + 1, cell.Col - 1 ));
     neighbors.Add(new Cell (cell.Row + 1, cell.Col ));
     neighbors.Add(new Cell (cell.Row + 1, cell.Col + 1 ));
     return neighbors;
 }
 public void CanCreateCell()
 {
     Cell cell = new Cell(false,0,0,10);
     Assert.IsNotNull(cell);
 }
示例#26
0
 public void CellEnsureCellHasAPosition()
 {
     Cell my_cell = new Cell();
     Assert.AreEqual(0, my_cell.X);
     Assert.AreEqual(0, my_cell.Y);
 }
 public void CellHasAliveProperty()
 {
     Cell cell = new Cell(false, 1, 0,10);
     Assert.AreEqual(false, cell.Alive);
 }
 public void CellHasRowProperty()
 {
     Cell cell = new Cell(false, 5, 0,10);
     Assert.AreEqual(5, cell.Row);
 }
 public void CellHasNeighbors()
 {
     Cell cell = new Cell(false, 0, 0,10);
     Assert.IsNotNull(cell.Neighbors);
 }
 public void CellHasCorrectNumberOfNeighbors()
 {
     Cell cell = new Cell(false, 4, 4,10);
     Assert.AreEqual(8, cell.Neighbors.Count);
 }