Esempio n. 1
0
        // Advances board.
        public override Life Next()
        {
            long size = Size();

            bool[,] newgrid = new bool[size, size];
            for (int i = 0; i < size; i++)
            {
                long im = (i - 1 + size) % size;
                long ip = (i + 1) % size;
                for (int j = 0; j < size; j++)
                {
                    long jm  = (j - 1 + size) % size;
                    long jp  = (j + 1) % size;
                    int  sum = (grid[im, jm] ? 1 : 0) +
                               (grid[im, j] ? 1 : 0) +
                               (grid[im, jp] ? 1 : 0) +
                               (grid[i, jm] ? 1 : 0) +
                               (grid[i, jp] ? 1 : 0) +
                               (grid[ip, jm] ? 1 : 0) +
                               (grid[ip, j] ? 1 : 0) +
                               (grid[ip, jp] ? 1 : 0);
                    newgrid[i, j] = sum == 3 || (grid[i, j] && sum == 2);
                }
            }

            return(GridLife.Create(newgrid));
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            // Create grid.
            const int N = 32;

            bool[,] grid = new bool[N, N];

            // Fill it up with something.
            //GridUtils.AddGlider(grid, N / 2, N / 2);
            //GridUtils.AddPentoR(grid, N / 2, N / 2);
            GridUtils.FillRandom(grid, 0.2, 11235);

            // Create candidates.
            Life gridLife    = GridLife.Create(grid);
            Life nodeCropped = NodeLife.Create(grid, NodeLife.Mode.Cropped);
            Life nodeTorus   = NodeLife.Create(grid, NodeLife.Mode.Torus);
            Life nodeOpen    = NodeLife.Create(grid, NodeLife.Mode.Open);
            Life nodeWarp    = NodeLife.Create(grid, NodeLife.Mode.Warp);
            Life nodeWarp16  = NodeLife.Create(grid, NodeLife.Mode.Warp, 16);
            Life nodeWarp32  = NodeLife.Create(grid, NodeLife.Mode.Warp, 32);
            Life nodeWarp60  = NodeLife.Create(grid, NodeLife.Mode.Warp, 60);

            // Animate one?
            //Life.Animate(gridLife, false /*pause*/);

            // Some validation.
            Life.Validate(gridLife, nodeTorus, 5);
            Life.Validate(nodeOpen, nodeWarp16, 5);
            //Life.Validate(nodeWarp, nodeWarp16, 5); // should fail when warp grid is cropped.
            //Life.Validate(gridLife, nodeOpen, 5);   // should fail when gridLife wraps around

            // Performance.
            Life[] candidates = new Life[] {
                gridLife,
                nodeTorus,
                nodeOpen,
                nodeWarp,
                nodeWarp16,
                nodeWarp32,
                nodeWarp60
            };

            while (true)
            {
                Console.WriteLine();
                for (int i = 0; i < candidates.Count(); ++i)
                {
                    Life.Measure(ref candidates[i], 5);
                }
            }
        }