Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <ctime>
- void arrayCopy(int arrayCopy[25][45], int arrayPaste [25][45]);
- void oscillation(int arrayOsc[25][45]);
- void lifeRules(int arrayRules[25][45]);
- void displayGrid(int outputGrid[25][45]);
- bool arrayCompare(int currentArray[25][45], int previousArray[25][45]);
- int main()
- {
- int myarray [25][45]; //array that will be displayed to user
- int previous [25][45]; //array to compare current configuration to previous
- char repeat; //allows user to repeat simulation
- int choice; //menu choice
- int generation; //keeps track of how many generations have passed.
- bool same; //value to stop simulation if pattern is stable
- //Explains the rules of the game to the user.
- std::cout
- << std::endl << "This is a simulation of the \"Game of Life\"." << std::endl
- << std::endl << "These are the rules of the \"Game of Life\":" << std::endl
- << std::endl << "1. If an occupied cell has zero or one neighbor, it dies of loneliness."
- << std::endl << "2. If an occupied cell has more than three neighbors, it dies of overcrowding."
- << std::endl << "3. If an empty cell has exactly three occupied neighbor cells, there is a birth of a new cell to replace the empty cell."
- << std::endl << "4. Births and deaths are instantaneous and occur at the changes of generation."
- << std::endl << std::endl;
- std::cout << "There are three choices to the starting pattern of your cells:" << std::endl;
- std::cout << "1. Simple oscillation." << std::endl;
- std::cout << "2. Glider pattern." << std::endl;
- std::cout << "3. Gun pattern." << std::endl;
- std::cout << "4. Quit program" << std::endl;
- std::cout << "If the pattern becomes \"stable\" the program will end." << std::endl;
- //loop to allow the user to run a new simulation
- do
- {
- //gets input choice from the user, if 4 is entered program terminates.
- do
- {
- std::cout << "Which pattern would you like to simulate?" << std::endl;
- std::cout << "Or press 4 to quit." << std::endl;
- std::cin >> choice;
- if(choice == 4)
- {
- return 0;
- }
- }while(choice < 1 || choice > 4);
- //loop assigns all spaces a blank position to start the simulation
- //the actual grid is larger than the displayed grid to account for "infinite grid"
- for(int x=0; x<20; x++)
- for(int y=0; y<40; y++)
- {
- myarray[x][y] = 0; //assigns a blank spot to all spaces to start
- }
- if(choice == 1)
- {
- oscillation(myarray);
- }
- do{
- arrayCopy(myarray, previous);
- displayGrid(myarray); //displays current grid
- lifeRules(myarray); //runes the cell life/death rules on current grid
- system("sleep .2"); //allows for the system slow down the generation of new cells
- generation++;
- if(generation > 100)
- {
- break;
- }
- same = arrayCompare(myarray, previous); //compares current grid to previous generation grid
- if(same == true) //if they are the same, simulation ends
- std::cout << std::endl;
- if(same == false)
- system("clear");
- }while(same == false);
- std::cout << "Would you like to run another simulation?" << std::endl;
- std::cin >> repeat;
- }while(repeat == 'y' || repeat == 'Y' && repeat != 'n');
- return 0;
- }
- //runs the oscillation pattern
- void oscillation(int arrayOsc[25][45])
- {
- int coordX;
- int coordY;
- //int tempArray [25][45];
- //arrayCopy(arrayOsc, tempArray); //copies the passed array to a temp array to hold the values.
- std::cout << "This demonstration of a simple oscillation will be "
- << "a 1x3 grouping of cells to start." << std::endl;
- std::cout << "Please select the location you would like to center the cells within the 40x20 grid."
- << std:: endl;
- do
- {
- std::cout << "Please enter an X-axis coordinate between 2 and 39." << std::endl;
- std::cout << "This will make sure the pattern will start on the displayed grid." << std::endl;
- std::cin >> coordX;
- }while(coordX < 2 || coordX > 39);
- do
- {
- std::cout << "Please enter an Y-axis coordinate between 2 and 19." << std::endl;
- std::cout << "This will make sure the pattern will start on the displayed grid." << std::endl;
- std::cin >> coordY;
- }while(coordY < 2 || coordY > 19);
- arrayOsc[coordY][coordX] = 1;
- arrayOsc[coordY][coordX-1] = 1;
- arrayOsc[coordY][coordX+1] = 1;
- }
- //copies the array to a temporary array for puposes of grid calculation (in real time)
- void arrayCopy(int arrayCopy[25][45], int arrayPaste [25][45])
- {
- for(int j = 0; j < 25; j++)
- {
- for(int i = 0; i < 45; i++)
- arrayPaste[j][i] = arrayCopy[j][i];
- }
- }
- void lifeRules(int arrayRules[25][45])
- {
- int tempArray [25][45];
- arrayCopy(arrayRules, tempArray); //copies the passed array to a temp array to hold the values.
- for(int j = 0; j < 25; j++)
- {
- for(int i = 0; i < 45; i++)
- {
- //checks the surrounding cells for other alive or dead cells(1 alive, 0 dead)
- int count = 0;
- count = arrayRules[j-1][i] + arrayRules[j-1][i-1] + arrayRules[j][i-1] + arrayRules[j+1][i-1] +
- arrayRules[j+1][i] + arrayRules[j+1][i+1] + arrayRules[j][i+1] + arrayRules[j-1][i+1];
- //If the cell has exactly 2 neighbors, nothing happens to it.
- if(count == 2)
- tempArray[j][i] = arrayRules[j][i];
- //if the cell has 3 neighbors it is born, or if it is already alive it stays the same.
- if(count == 3)
- tempArray[j][i] = 1;
- //if the cell has 1 or less neighbors, or more than 3 neighbors he dies
- if(count < 2 || count > 3)
- tempArray[j][i] = 0;
- }
- }
- //copies the temporary grid back to the display grid to show the user.
- arrayCopy(arrayRules, tempArray);
- }
- void displayGrid(int outputGrid[25][45])
- {
- for(int x=0; x<20; x++) //This loops on the rows.
- {
- for(int y=0; y<40; y++) //This loops on the columns
- {
- if(outputGrid[x][y] == 1)
- std::cout << '*';
- else
- std::cout << ' ';
- }
- std::cout << std::endl;
- }
- }
- bool arrayCompare(int currentArray[25][45], int previousArray[25][45])
- {
- int tally = 0;
- for(int j = 0; j < 25; j++)
- {
- for(int i = 0; i < 45; i++)
- {
- if(currentArray[j][i] == previousArray[j][i])
- tally++;
- }
- }
- //whenever the locations are matching, the tally goes up. If tally equals the total number of possible
- //cells, then the grids match perfectly and the pattern is stable.
- if(tally == 25*45)
- return true;
- else
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement