Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <ctime>
- int main()
- {
- int myarray [25][45];
- int arrayCopy [25][45];
- char repeat;
- int choice;
- //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
- }
- 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(myarray[x][y] == 1)
- std::cout << '*';
- else
- std::cout << ' ';
- }
- std::cout << std::endl;
- }
- std::cout << "Would you like to run another simulation?" << std::endl;
- std::cin >> repeat;
- }while(repeat == 'y' || repeat == 'Y' && repeat != 'n');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement