Exemple #1
0
        // Any live cell with fewer than two live neighbours dies, as if caused by under-population.
        // Any live cell with two or three live neighbours lives on to the next generation.
        // Any live cell with more than three live neighbours dies, as if by overcrowding.
        // Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
        internal static bool CurrentlyAliveCellWillStillBeALiveInTheNextGeneration(Grid currentGrid, Coords coords)
        {
            var numLiveNeighbours = GetNumLiveNeighbours(currentGrid, coords);

            // Rules 1, 2 and 3.
            return (numLiveNeighbours == 2 || numLiveNeighbours == 3);
        }
Exemple #2
0
        internal static bool CurrentlyDeadCellWillBecomeALiveInTheNextGeneration(Grid currentGrid, Coords coords)
        {
            var numLiveNeighbours = GetNumLiveNeighbours(currentGrid, coords);

            // Rule 4.
            return (numLiveNeighbours == 3);
        }
Exemple #3
0
 /// <summary>
 /// Change state of grid if required to grow on any side
 /// </summary>
 /// <param name="inputGrid"></param>
 /// <param name="outputGrid"></param>
 public static void ChangeGridState(Grid inputGrid, Grid outputGrid)
 {
     CheckRowGrowth(inputGrid, outputGrid, -1);
     CheckRowGrowth(inputGrid, outputGrid, inputGrid.RowCount);
     CheckColumnGrowth(inputGrid, outputGrid, -1);
     CheckColumnGrowth(inputGrid, outputGrid, inputGrid.ColumnCount);
 }
Exemple #4
0
        public void ProcessGeneration_3x3Grid(bool leftTop, bool top, bool rightTop, bool left, bool center, bool right, bool leftBottom, bool bottom, bool rightBottom,
                                              bool expectedLeftTop, bool expectedTop, bool expectedRightTop,
                                              bool expectedLeft, bool expectedCenter, bool expectedRight,
                                              bool expectedLeftBottom, bool expectedBottom, bool expectedRightBottom)
        {
            var grid = new Grid(new Vector(3, 3));
            grid.Cells[0, 0] = new Cell(0, 0, leftTop);
            grid.Cells[0, 1] = new Cell(0, 1, top);
            grid.Cells[0, 2] = new Cell(0, 2, rightTop);
            grid.Cells[1, 0] = new Cell(1, 0, left);
            grid.Cells[1, 1] = new Cell(1, 1, center);
            grid.Cells[1, 2] = new Cell(1, 2, right);
            grid.Cells[2, 0] = new Cell(2, 0, leftBottom);
            grid.Cells[2, 1] = new Cell(2, 1, bottom);
            grid.Cells[2, 2] = new Cell(2, 2, rightBottom);

            grid.ProcessGeneration();

            Assert.AreEqual(expectedLeftTop, grid.Cells[0, 0].IsAlive);
            Assert.AreEqual(expectedTop, grid.Cells[0, 1].IsAlive);
            Assert.AreEqual(expectedRightTop, grid.Cells[0, 2].IsAlive);
            Assert.AreEqual(expectedLeft, grid.Cells[1, 0].IsAlive);
            Assert.AreEqual(expectedCenter, grid.Cells[1, 1].IsAlive);
            Assert.AreEqual(expectedRight, grid.Cells[1, 2].IsAlive);
            Assert.AreEqual(expectedLeftBottom, grid.Cells[2, 0].IsAlive);
            Assert.AreEqual(expectedBottom, grid.Cells[2, 1].IsAlive);
            Assert.AreEqual(expectedRightBottom, grid.Cells[2, 2].IsAlive);
        }
Exemple #5
0
 public void GridConstructorTest1()
 {
     string pattern = "X -\n- X\n"; // TODO: Initialize to an appropriate value
     Grid target = new Grid(pattern);
     string actual = target.ToString();
     Assert.AreEqual(pattern, actual);
 }
Exemple #6
0
 /// <summary>
 /// Create input and output grids by using rows and column count and initialize reachable cells.
 /// Reachable Cells are cells which can be traversed from inner grid cells or outer grid cells i.e. virtual cells used for expanding grid
 /// </summary>
 /// <param name="rows"></param>
 /// <param name="columns"></param>
 public Game(int rows, int columns)
 {
     if (rows <= 0 || columns <= 0) throw new ArgumentOutOfRangeException("Row and Column size must be greater than zero");
     _inputGrid = new Grid(rows, columns);
     _outputGrid = new Grid(rows, columns);
     ReachableCell.InitializeReachableCells();
 }
Exemple #7
0
        public MainWindow()
        {
            InitializeComponent();

            this.Text = "Game of Life";

            // Setter opp tabell
            Console.WriteLine ("Setter opp tabell..");
            table = new Table ();

            // Teiknar forma på nytt dersom tabellen blir forandra
            table.TableChangedEvent += new Table.TableChangedHandler (this.OnTableChanged);

            // Setter opp grid
            Console.WriteLine ("Setter opp grid..");
            grid = new Grid (this);

            // Teikn enkeltcelle dersom dei blir forandra i tabellen
            table.TableCellChangedEvent += new Table.TableCellChangedHandler (grid.DrawCell);

            // Teikn gridden når forma blir teikna
            this.Paint += new PaintEventHandler (grid.Draw);

            clock = new Timer();
            clock.Interval = 1000;
            clock.Start();

            // Tooltips
            ToolTip toolTip1 = new ToolTip();
            toolTip1.SetToolTip(this.btnRun, "Starter og stopper");

            // Setter opp eit nytt spel av typen 'MaxCells'
            game = new MaxCells (this);
            // game = new Sandbox (this);
        }
Exemple #8
0
 public void Then_the_cell_is_dead()
 {
     var cellAlive = false;
     var startingGrid = new Grid(new LiveCell(1, 0), new LiveCell(0, 1), new LiveCell(2, 1), new LiveCell(1, 2), new LiveCell(0, 0), new LiveCell(2, 0), new LiveCell(0, 2));
     GridIterator.NextEvolution(startingGrid).GetCellState(new Coordinate(1, 1), () => cellAlive = true, () => cellAlive = false);
     Assert.IsFalse(cellAlive);
 }
        public GenerationController(Vector gridSize)
        {
            this.GridSize = gridSize;

            _grid = new Grid(this.GridSize);
            _timmer = new Stopwatch();
        }
 /// <summary>
 /// Get the co-ordinates with respectt to grid and return the Cell type enum
 /// </summary>
 /// <param name="grid"></param>
 /// <param name="coOrdinates"></param>
 /// <returns>returns CellTypeEnum</returns>
 public static CellTypeEnum GetCellType(Grid grid, CoOrdinates coOrdinates)
 {
     if ((coOrdinates.X < -1 || coOrdinates.X > grid.RowCount) || (coOrdinates.Y < -1 || coOrdinates.Y > grid.ColumnCount))
     {
         throw new ArgumentOutOfRangeException("Invalid Index value: must be greater than or equal to minus one and less than or equal to Row count");
     }
     CellTypeEnum enumCellType = CellTypeEnum.None;
     if (coOrdinates.X == 0 && coOrdinates.Y == 0)
         enumCellType = CellTypeEnum.TopLeftCorner;
     else if (coOrdinates.X == 0 && coOrdinates.Y == grid.ColumnCount - 1)
         enumCellType = CellTypeEnum.TopRightCorner;
     else if (coOrdinates.X == grid.RowCount - 1 && coOrdinates.Y == 0)
         enumCellType = CellTypeEnum.BottomLeftCorner;
     else if (coOrdinates.X == grid.RowCount - 1 && coOrdinates.Y == grid.ColumnCount - 1)
         enumCellType = CellTypeEnum.BottomRightCorner;
     else if (coOrdinates.X == 0 && (coOrdinates.Y > 0 && coOrdinates.Y < grid.ColumnCount - 1))
         enumCellType = CellTypeEnum.TopSide;
     else if (coOrdinates.X == grid.RowCount - 1 && (coOrdinates.Y > 0 && coOrdinates.Y < grid.ColumnCount - 1))
         enumCellType = CellTypeEnum.BottomSide;
     else if ((coOrdinates.X > 0 && coOrdinates.X < grid.RowCount - 1) && coOrdinates.Y == 0)
         enumCellType = CellTypeEnum.LeftSide;
     else if ((coOrdinates.X > 0 && coOrdinates.X < grid.RowCount - 1) && coOrdinates.Y == grid.ColumnCount - 1)
         enumCellType = CellTypeEnum.RightSide;
     else if ((coOrdinates.X > 0 && coOrdinates.X < grid.RowCount - 1) && (coOrdinates.Y > 0 && coOrdinates.Y < grid.ColumnCount - 1))
         enumCellType = CellTypeEnum.Center;
     else if (coOrdinates.X == -1 && (coOrdinates.Y > 0 && coOrdinates.Y < grid.ColumnCount - 1))
         enumCellType = CellTypeEnum.OuterTopSide;
     else if ((coOrdinates.X > 0 && coOrdinates.X < grid.RowCount - 1) && coOrdinates.Y == grid.ColumnCount)
         enumCellType = CellTypeEnum.OuterRightSide;
     else if (coOrdinates.X == grid.RowCount && (coOrdinates.Y > 0 && coOrdinates.Y < grid.ColumnCount - 1))
         enumCellType = CellTypeEnum.OuterBottomSide;
     else if ((coOrdinates.X > 0 && coOrdinates.X < grid.RowCount - 1) && coOrdinates.Y == -1)
         enumCellType = CellTypeEnum.OuterLeftSide;
     return enumCellType;
 }
 /// <summary>
 /// Create input and output grids by using rows and column count and initialize reachable cells.
 /// Reachable Cells are cells which can be traversed from inner grid cells or outer grid cells i.e. virtual cells used for expanding grid
 /// </summary>
 /// <param name="rows"></param>
 /// <param name="columns"></param>
 public Game(int rows, int columns)
 {
     if (rows <= 0 || columns <= 0) throw new ArgumentOutOfRangeException("Size of Row and Column for the Grid must be greater than zero");
     _inputGrid = new Grid(rows, columns);
     _outputGrid = new Grid(rows, columns);
     HelperFactory.InitializeReachableCells();
 }
Exemple #12
0
 public World(params int[] initialCoords)
 {
     _grid = new Grid();
     for (var i = 0; i < initialCoords.Length; i += 2)
     {
         _grid.AddPoint(initialCoords[i], initialCoords[i + 1]);
     }
 }
Exemple #13
0
        public void GetCell_Position_ReturnCell(int gridWidth, int gridHeigth, int cellPositionX, int cellPositionY, int expectedCellPositionX, int expectedCellPositionY)
        {
            var grid = new Grid(new Vector(gridWidth, gridHeigth));
            var selectedCell = grid.Cells[cellPositionX, cellPositionY];

            Assert.AreEqual(expectedCellPositionX, selectedCell.Position.X);
            Assert.AreEqual(expectedCellPositionY, selectedCell.Position.Y);
        }
Exemple #14
0
 public void GridConstructorPositiveTest()
 {
     int rows = 2;
     int columns = 2;
     Grid target = new Grid(rows, columns);
     Assert.AreEqual(target.RowCount, 2);
     Assert.AreEqual(target.ColumnCount, 2);
 }
Exemple #15
0
        public void AnyLiveCellWithMoreThanThreeLiveNeighboursDiesAsIfByOvercrowding()
        {
            var grid = new Grid(new Cell(0, 0), new Cell(1, 0), new Cell(2, 0), new Cell(0, 1), new Cell(1, 1));

            grid = grid.CreateNextGeneration();

            Assert.False(grid.IsAlive(new Cell(1, 1)));
        }
Exemple #16
0
        public void AnyLiveCellWithFewerThanTwoLiveNeighboursDiesAsIfCausedByUnderPopulation()
        {
            var grid = new Grid(new Cell(1, 1), new Cell(0, 1));

            grid = grid.CreateNextGeneration();

            Assert.False(grid.IsAlive(new Cell(1, 1)));
        }
Exemple #17
0
        public void AnyLiveCellWithTwoLiveNeighboursLivesOnToTheNextGeneration()
        {
            var grid = new Grid(new Cell(1, 1), new Cell(0, 1), new Cell(2, 1));

            grid.CreateNextGeneration();

            Assert.True(grid.IsAlive(new Cell(1, 1)));
        }
Exemple #18
0
        public void AnyDeadCellWithExactlyThreeLiveNeighboursBecomesALiveCellAsIfByReproduction()
        {
            var grid = new Grid(new Cell(0, 0), new Cell(1, 0), new Cell(2, 0));

            grid = grid.CreateNextGeneration();

            Assert.True(grid.IsAlive(new Cell(1, 1)));
        }
        DispatcherTimer timer; //  Generation timer

        #endregion Fields

        #region Constructors

        public MainWindow()
        {
            InitializeComponent();
            mainGrid = new Grid(MainCanvas);

            timer = new DispatcherTimer();
            timer.Tick += OnTimer;
            timer.Interval = TimeSpan.FromMilliseconds(200);
        }
Exemple #20
0
        public IGrid Generate()
        {
            var nextGen = new Grid(Width, Height);
            for (int x = 0; x < Width; ++x)
                for (int y = 0; y < Height; ++y)
                    nextGen[x, y].SetState(GetCellNeighbours(x, y));

            return nextGen;
        }
Exemple #21
0
 /// <summary>
 /// Assign Source grid cell values to target grid
 /// </summary>
 /// <param name="sourceGrid"></param>
 /// <param name="targetGrid"></param>
 private static void AssignCellValues(Grid sourceGrid, Grid targetGrid)
 {
     for (int i = 0; i < sourceGrid.RowCount; i++)
     {
         for (int j = 0; j < sourceGrid.ColumnCount; j++)
         {
             targetGrid[i, j].IsAlive = sourceGrid[i, j].IsAlive;
         }
     }
 }
Exemple #22
0
 static void Main(string[] args)
 {
     string pattern = "- - - X X - - -\n- X - - - - X -\nX - - - - - - X\n- X - - - - X -\n- - - X X - - -\n";
     Grid grid = new Grid(pattern);
     Console.WriteLine("Grid As given");
     Console.WriteLine(grid.ToString());
     grid.AdvanceToNextGeneration();
     Console.WriteLine(grid.ToString());
     Console.ReadLine();
 }
Exemple #23
0
 public static void Tick()
 {
     Grid grid = new Grid("- - - -\n- X X X\nX X X -\n- - - -");
     Console.WriteLine ("Grid As given");
     Console.WriteLine(grid.ToString());
     grid.AdvanceGridToNextGeneration();
     Console.WriteLine ("Grid Output");
     Console.WriteLine(grid.ToString());
     Console.Read();
 }
Exemple #24
0
 /// <summary>
 /// Display the grid
 /// </summary>
 public static void Display(Grid grid)
 {
     foreach (Row row in grid.GridObj)
     {
         foreach (Cell cell in row.Cells)
         {
             Console.Write(cell.ToString());
         }
         Console.WriteLine();
     }
 }
Exemple #25
0
        public void IsLiveCellAt_GivenCoordsWhereThereIsNoLiveCell_ReturnsFalse()
        {
            // Arrange
            var grid = new Grid();
            var coords = Coords.Create(12, 13);

            // Act
            var actual = grid.IsLiveCellAt(coords);

            // Assert
            Assert.That(actual, Is.False);
        }
        protected override void Initialize()
        {
            base.Initialize();
            if (Program.grid == null)
            {
                grid = new Grid();
                Program.grid = grid;
            }
            else grid = Program.grid;

            keyboardState = lastKeyboardState = Keyboard.GetState();
        }
Exemple #27
0
        public void AnyLiveCellWithNoLiveNeighboursDies()
        {
            // Arrange
            var grid = new Grid();
            var cellCoords = Coords.Create(1, 1);
            grid.MarkLiveCellAt(cellCoords);

            // Act
            var actual = Rules.CurrentlyAliveCellWillStillBeALiveInTheNextGeneration(grid, cellCoords);

            // Assert
            Assert.That(actual, Is.False);
        }
Exemple #28
0
        public void IterateLiveCells_GivenGridWithOneLiveCell_InvokesActionOnce()
        {
            // Arrange
            var grid = new Grid();
            grid.MarkLiveCellAt(Coords.Create(12, 13));
            var numInvocations = 0;

            // Act
            grid.IterateLiveCells((_, __) => numInvocations++);

            // Assert
            Assert.That(numInvocations, Is.EqualTo(1));
        }
Exemple #29
0
        /// <summary>
        /// Change Cell state of specified co-ordinate using Ruls
        /// </summary>
        /// <param name="inputGrid"></param>
        /// <param name="outputGrid"></param>
        /// <param name="coOrdinates"></param>
        public static void ChangeCellsState(Grid inputGrid, Grid outputGrid, CoOrdinates coOrdinates)
        {
            int liveNeighbourCount = CountAliveNeighbours(inputGrid, coOrdinates);
            lock (outputGrid)
            {
                if (IsAliveInNextState(inputGrid[coOrdinates.X, coOrdinates.Y], liveNeighbourCount))
                {

                    //set output grid's cell to live only if it is in alive status in next generation
                    outputGrid[coOrdinates.X, coOrdinates.Y].IsAlive = true;
                }

            }
        }
Exemple #30
0
        public void AnyDeadCellWithExactlyTwoLiveNeighboursStaysDead()
        {
            // Arrange
            var grid = new Grid();
            var cellCoords = Coords.Create(1, 1);
            grid.MarkLiveCellAt(cellCoords.Below());
            grid.MarkLiveCellAt(cellCoords.BelowLeft());

            // Act
            var actual = Rules.CurrentlyDeadCellWillBecomeALiveInTheNextGeneration(grid, cellCoords);

            // Assert
            Assert.That(actual, Is.False);
        }