コード例 #1
0
ファイル: Game.cs プロジェクト: tremaines/cab201_gameoflife
        /// <summary>
        /// Set the initial board based on the random factor
        /// </summary>
        private void InitialiseFromRandom()
        {
            double chance = settings.Random;
            Random random = new Random();

            for (int r = 0; r < settings.Rows; r++)
            {
                for (int c = 0; c < settings.Columns; c++)
                {
                    if (chance > random.NextDouble())
                    {
                        grid.UpdateCell(r, c, CellState.Full);
                        statusArray[r, c] = DeadOrAlive.alive;
                    }
                }
            }
        }
コード例 #2
0
 public static void InitialiseFirstGen(Grid grid, int[,,] currentCells)
 {
     // Double for loop that simply checks whether or not a particular cell is alive or dead in the first
     // generation and then changes it's state to the corresponding full or blank CellState.
     for (int row = 0; row < currentValues.Rows; row++)
     {
         for (int col = 0; col < currentValues.Columns; col++)
         {
             if (currentCells[row, col, 0] == 1)
             {
                 grid.UpdateCell(row, col, CellState.Full);
             }
             else
             {
                 grid.UpdateCell(row, col, CellState.Blank);
             }
         }
     }
 }
コード例 #3
0
 private static void UpdateGrid(Grid grid, int[,] universe)
 {
     for (int i = 0; i < universe.GetLength(0); i++)
     {
         for (int j = 0; j < universe.GetLength(1); j++)
         {
             grid.UpdateCell(i, j, (CellState)universe[i, j]);
         }
     }
 }
コード例 #4
0
        /// <summary >
        /// Starts the simulation by running the main loop of the program
        /// This method calls other methods to collect the information required to use the Display API.
        /// </ summary >
        /// <param name =" grid " > Used to display the Cell matrix in the console </ param >
        public int start(Grid grid)
        {
            Stopwatch watch = new Stopwatch();                                              //initialise time

            CellState[,] state = new CellState[getRows, getColumns];                        //set state for each cell
            CellState[] ghostState = { CellState.Dark, CellState.Medium, CellState.Light }; //ghost states

            for (int g = 0; g <= getGenerations; g++)                                       //loop per generation
            {
                watch.Restart();

                for (int r = 0; r < getRows; r++) // For each of the cells...
                {
                    for (int c = 0; c < getColumns; c++)
                    {
                        state[r, c] = CellState.Blank;                                                    //clear cell state

                        int ghosts = (previousGenerations.Count > 2) ? 2 : previousGenerations.Count - 1; //ghost count

                        ghosts = (ghostMode) ? ghosts : -1;                                               //turn on : off ghost mode

                        for (int gh = ghosts; gh >= 0; gh--)
                        {
                            state[r, c] = (previousGenerations[gh][r, c] == alive) ? ghostState[gh] : state[r, c]; //set ghosts
                        }

                        state[r, c] = (cells[r, c] == alive) ? CellState.Full : state[r, c]; //get new generation states

                        grid.UpdateCell(r, c, state[r, c]);                                  // Update grid
                    }
                }

                while (watch.ElapsedMilliseconds < (1 / getMaxUpdate * 1000))
                {
                    ;                                 //Timer
                }
                grid.SetFootnote("Generation: " + g); //prints current generation

                grid.Render();                        // Render updates to the console window...

                period = steadyState(cells);          //get period

                if (period > -1)                      //steady state? End game if true
                {
                    if (getOutputPath != String.Empty)
                    {
                        outputSeed(cells);                               //output seed
                    }
                    return(period);
                }

                cells = this.mutation(cells); //MUTATION, following the Conways game of life...

                if (stepMode)
                {
                    space();           //Step Mode
                }
            }

            if (getOutputPath != String.Empty)
            {
                outputSeed(cells);                                //output seed
            }
            return(period);
        }
コード例 #5
0
        /// <summary>
        /// Main Function which contains all code that wasn't put into methods
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            // Declaring the variables
            int    Rows               = 16;    // Default: 16. Change as required for testing.
            int    Columns            = 16;    // Default: 16. Change as required for testing.
            bool   PeriodicBehaviour  = false; // Default: false. Change as required for testing
            double RandFactor         = 0.5;   // Default: 0.5. Change as required for testing
            string InputFile          = "";    // Default: NO input. Change as required for testing
            int    Generations        = 50;    // Default: 50. Change as required fo testing
            double MaxUpdateRate      = 5;     // Default: 5. Change as required for testing
            bool   StepMode           = false; // Default: NOT step mode. Change as required for testing
            bool   CheckAllConditions = true;  //Checking all the parameters

            //To count the number of generations
            int GenCount = 0;

            Stopwatch watch = new Stopwatch();


            // Checking the user input
            for (int count = 0; count < args.Length; count++)
            {
                //Checking the dimensions of the game
                CheckDimensions(ref args, ref count, ref Rows, ref Columns, ref CheckAllConditions);

                //Checking if the game will have periodic behaviour
                CheckPeriodic(ref args, ref count, ref PeriodicBehaviour);

                //Checking Random factor
                CheckRandomFactor(ref args, ref count, ref RandFactor, ref CheckAllConditions);

                //Checking the input file
                CheckSeedfile(ref args, ref count, ref InputFile, ref CheckAllConditions);

                //Checking the generations
                GenerationMethod(ref args, ref count, ref Generations, ref CheckAllConditions);

                //Checking the max update rate
                MaxUpdaterate(ref args, ref count, ref MaxUpdateRate, ref CheckAllConditions);

                //Checking step mode
                StepModeMethod(ref args, ref count, ref StepMode);
            }

            //Game array which stores the cell coordinates
            int[,] GameArray = new int[Rows, Columns];

            //Temporary Array to store the neighbours
            int[,] TempArray = new int[Rows, Columns];

            // Construct grid...
            Grid grid = new Grid(Rows, Columns);


            //Checking randomness
            RandomnessMethod(ref InputFile, ref Columns, ref Rows, ref GameArray, ref RandFactor);

            //Displaying user uput
            DispUserInput(ref CheckAllConditions, ref InputFile, ref Generations, ref MaxUpdateRate, ref PeriodicBehaviour,
                          ref Rows, ref Columns, ref StepMode, ref RandFactor);

            // Wait for user to press a key...
            Console.WriteLine("Press any key to start...");
            Console.ReadKey();


            // Game
            // Initialize the grid window (this will resize the window and buffer
            grid.InitializeWindow();

            // Set the footnote (appears in the bottom left of the screen).
            grid.SetFootnote("Life " + GenCount);

            // Declaring variable to see if neighbouring cells are active for next generation
            int ThreeCount = 0;

            // For each of the cells...
            for (int i = 0; i < GameArray.GetLength(0); i++)
            {
                for (int j = 0; j < GameArray.GetLength(1); j++)
                {
                    if (GameArray[i, j] == 1)
                    {
                        // Update grid with a new cell...
                        grid.UpdateCell(i, j, CellState.Full);
                    }
                    else
                    {
                        grid.UpdateCell(i, j, CellState.Blank);
                    }
                }
            }

            // Timer for Life game
            watch.Restart();
            while (watch.ElapsedMilliseconds < (1000 / MaxUpdateRate))
            {
                ;
            }

            grid.Render();

            //Increasing the generations
            while (Generations >= GenCount)
            {
                //Checking the neighbours of the cells
                ActualGame(ref Rows, ref Columns, ref GameArray, ref ThreeCount, ref PeriodicBehaviour, ref TempArray);

                //Step function holding answer till space is pressed
                if (StepMode == true)
                {
                    while (Console.ReadKey().Key != ConsoleKey.Spacebar)
                    {
                        ;
                    }
                }
                grid.SetFootnote("Life " + GenCount);

                // For each of the cells...
                for (int i = 0; i < GameArray.GetLength(0); i++)
                {
                    for (int j = 0; j < GameArray.GetLength(1); j++)
                    {
                        if (TempArray[i, j] == 1)
                        {
                            // Update grid with a new cell...
                            grid.UpdateCell(i, j, CellState.Full);
                            GameArray[i, j] = TempArray[i, j];
                        }
                        else
                        {
                            grid.UpdateCell(i, j, CellState.Blank);
                            GameArray[i, j] = 0;
                        }
                        TempArray[i, j] = 0;
                    }
                }

                // Timer for Life Game
                watch.Restart();
                while (watch.ElapsedMilliseconds < (1000 / MaxUpdateRate))
                {
                    ;
                }
                // Render updates to the console window...
                grid.Render();
                GenCount++;
            }

            // Set complete marker as true
            grid.IsComplete = true;

            grid.Render();

            Console.ReadKey();

            grid.RevertWindow();
        }