Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************************************************************
- ** Program Filename: Assignment1
- ** Author: Jared Hughes
- ** Date: 4/7/2015
- ** Description: Program demostrates oscillation, glider, and gun patterns
- ** in Conway's Game of Life.
- ** Input: User imputs type of pattern desired and beginning location
- ** on the grid. They also input repeat commands.
- ** Output: Display outputs three different patterns on the screen.
- ** until 100 generations have passed or user quits.
- *********************************************************************/
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <ctime>
- #include <stdlib.h>
- #include <time.h>
- #include <unistd.h>
- void array_copy(int array_copy[30][50], int array_paste [30][50]);
- void oscillation(int array_osc[30][50]);
- void glider(int array_glider[30][50]);
- void gun(int array_gun[30][50]);
- void life_rules(int array_rules[30][50]);
- void display_grid(int output_grid[30][50]);
- bool array_compare(int current_array[30][50], int previous_array[30][50]);
- void array_wipe(int array_blank[30][50]);
- int main()
- {
- int my_array [30][50]; //array that will be displayed to user
- int previous [30][50]; //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
- {
- //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"
- array_wipe(my_array);
- array_wipe(previous);
- generation = 0;
- //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 == 1)
- {
- oscillation(my_array);
- }
- if(choice == 2)
- {
- glider(my_array);
- }
- if(choice == 3)
- {
- gun(my_array);
- }
- if(choice == 4)
- {
- return 0;
- }
- }while(choice < 1 || choice > 4);
- do{
- array_copy(my_array, previous);
- display_grid(my_array); //displays current grid
- life_rules(my_array); //runs the cell life/death rules on current grid
- usleep(200000); //allows for the system slow down the generation of new cells
- generation++; //increments the generation for tracking purposes.
- if(generation > 100)
- {
- break;
- }
- same = array_compare(my_array, 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)
- std::cout << std::endl;
- }while(same == false);
- std::cout << "Would you like to run another simulation? (Y/N)" << std::endl;
- std::cin >> repeat;
- }while(repeat == 'y' || repeat == 'Y' && (repeat != 'n' || repeat != 'N'));
- return 0;
- }
- /*******************************************************************************
- ** Function: void oscillation
- ** Description: inputs the simple oscillation pattern onto the grid
- ** Parameters: the first array is needed to run the function
- ** Pre-Conditions: User selects oscillation from the menu
- ** Post-Conditions: Grid is updated with location of 1x3 occpied cells on grid
- ********************************************************************************/
- void oscillation(int array_osc[30][50])
- {
- int coord_x;
- int coord_y;
- 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 >> coord_x;
- }while(coord_x < 2 || coord_x > 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 >> coord_y;
- }while(coord_y < 2 || coord_y > 19);
- array_osc[coord_y+4][coord_x+4] = 1;
- array_osc[coord_y+4][coord_x+3] = 1;
- array_osc[coord_y+4][coord_x+5] = 1;
- }
- /*******************************************************************************
- ** Function: void glider
- ** Description: inputs the glider pattern onto the active grid
- ** Parameters: the first array is needed to run the function
- ** Pre-Conditions: User selects glider from the menu
- ** Post-Conditions: Grid is updated with location of a glider that will move
- ** after each cycle of generations until outside the active grid.
- ********************************************************************************/
- void glider(int array_glider[30][50])
- {
- int coord_x;
- int coord_y;
- 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 >> coord_x;
- }while(coord_x < 2 || coord_x > 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 >> coord_y;
- }while(coord_y < 2 || coord_y > 19);
- array_glider[coord_y+4][coord_x+4] = 1;
- array_glider[coord_y+4][coord_x+5] = 1;
- array_glider[coord_y+3][coord_x+3] = 1;
- array_glider[coord_y+5][coord_x+4] = 1;
- array_glider[coord_y+5][coord_x+3] = 1;
- }
- void gun(int array_gun[30][50])
- {
- int coord_x;
- int coord_y;
- std::cout << "Please select the location you would like to put the left square of the gun pattern within the 40x20 grid."
- << std:: endl;
- do
- {
- std::cout << "Please enter an X-axis coordinate between 1 and 4." << std::endl;
- std::cout << "This will make sure the pattern will start on the displayed grid." << std::endl;
- std::cin >> coord_x;
- }while(coord_x < 1 || coord_x > 4);
- do
- {
- std::cout << "Please enter an Y-axis coordinate between 5 and 16." << std::endl;
- std::cout << "This will make sure the pattern will start on the displayed grid." << std::endl;
- std::cin >> coord_y;
- }while(coord_y < 5 || coord_y > 16);
- //hard codes location of left square
- array_gun[coord_y+4][coord_x+4] = 1;
- array_gun[coord_y+4][coord_x+5] = 1;
- array_gun[coord_y+5][coord_x+4] = 1;
- array_gun[coord_y+5][coord_x+5] = 1;
- /*********************************
- * Hard codes shape
- * ##
- * #
- * #
- * # #
- * #
- * #
- * ##
- **********************************/
- array_gun[coord_y+4][coord_x+14] = 1;
- array_gun[coord_y+5][coord_x+14] = 1;
- array_gun[coord_y+6][coord_x+14] = 1;
- array_gun[coord_y+3][coord_x+15] = 1;
- array_gun[coord_y+7][coord_x+15] = 1;
- array_gun[coord_y+2][coord_x+16] = 1;
- array_gun[coord_y+8][coord_x+16] = 1;
- array_gun[coord_y+2][coord_x+17] = 1;
- array_gun[coord_y+8][coord_x+17] = 1;
- array_gun[coord_y+5][coord_x+18] = 1;
- /***********************************
- * #
- * #
- * ##
- * #
- * #
- ***********************************/
- array_gun[coord_y+3][coord_x+19] = 1;
- array_gun[coord_y+7][coord_x+19] = 1;
- array_gun[coord_y+4][coord_x+20] = 1;
- array_gun[coord_y+5][coord_x+20] = 1;
- array_gun[coord_y+6][coord_x+20] = 1;
- array_gun[coord_y+5][coord_x+21] = 1;
- /***********************************
- * Hard codes shape
- * #
- * #
- * #
- * # #
- * # #
- * # #
- * #
- * #
- * #
- ***********************************/
- array_gun[coord_y+4][coord_x+24] = 1;
- array_gun[coord_y+3][coord_x+24] = 1;
- array_gun[coord_y+2][coord_x+24] = 1;
- array_gun[coord_y+4][coord_x+25] = 1;
- array_gun[coord_y+3][coord_x+25] = 1;
- array_gun[coord_y+2][coord_x+25] = 1;
- array_gun[coord_y+1][coord_x+26] = 1;
- array_gun[coord_y+5][coord_x+26] = 1;
- array_gun[coord_y+5][coord_x+28] = 1;
- array_gun[coord_y+1][coord_x+28] = 1;
- array_gun[coord_y][coord_x+28] = 1;
- array_gun[coord_y+6][coord_x+28] = 1;
- //hard codes location of right square
- array_gun[coord_y+2][coord_x+38] = 1;
- array_gun[coord_y+3][coord_x+38] = 1;
- array_gun[coord_y+2][coord_x+39] = 1;
- array_gun[coord_y+3][coord_x+39] = 1;
- }
- /*********************************************************************
- ** Function: array_copy
- ** Description: copys the active array to a temporary one not seen by
- ** the user.
- ** Parameters: requires 2 arrays to be passed, one to copy one to paste
- ** Pre-Conditions: 2 arrays.
- ** Post-Conditions: First array is copied to second array.
- *********************************************************************/
- void array_copy(int array_copy[30][50], int array_paste [30][50])
- {
- for(int j = 0; j < 30; j++)
- {
- for(int i = 0; i < 50; i++)
- array_paste[j][i] = array_copy[j][i];
- }
- }
- /*********************************************************************
- ** Function: life_rules
- ** Description: Runs the game of life rules. Each cell has each of 8
- ** adjacent cells checked for occupancy.
- ** Parameters: active array (grid) with pattern input
- ** Pre-Conditions: Grid needs to have something on it to act on
- ** Post-Conditions: Grid may or may not change depending on rules
- *********************************************************************/
- void life_rules(int array_rules[30][50])
- {
- int temp_array [30][50];
- array_copy(array_rules, temp_array); //copies the passed array to a temp array to hold the values.
- for(int j = 1; j < 29; j++)
- {
- for(int i = 1; i < 49; i++)
- {
- //checks the surrounding cells for other alive or dead cells(1 alive, 0 dead)
- int count = 0;
- count = array_rules[j-1][i] + array_rules[j-1][i-1] + array_rules[j][i-1] + array_rules[j+1][i-1] +
- array_rules[j+1][i] + array_rules[j+1][i+1] + array_rules[j][i+1] + array_rules[j-1][i+1];
- //If the cell has exactly 2 neighbors, nothing happens to it.
- if(count == 2)
- temp_array[j][i] = array_rules[j][i];
- //if the cell has 3 neighbors it is born, or if it is already alive it stays the same.
- if(count == 3)
- temp_array[j][i] = 1;
- //if the cell has 1 or less neighbors, or more than 3 neighbors he dies
- if(count < 2 || count > 3)
- temp_array[j][i] = 0;
- }
- }
- //copies the temporary grid back to the display grid to show the user.
- array_copy(temp_array, array_rules);
- }
- /*********************************************************************
- ** Function: display_grid
- ** Description: Displays the grid to the user.
- ** Parameters: active array with pattern
- ** Pre-Conditions: None
- ** Post-Conditions: Pattern is displayed on screen
- *********************************************************************/
- void display_grid(int output_grid[30][50])
- {
- for(int x=5; x<24; x++) //This loops on the rows.
- {
- for(int y=5; y<44; y++) //This loops on the columns
- {
- if(output_grid[x][y] == 1)
- std::cout << '*';
- else
- std::cout << ' ';
- }
- std::cout << std::endl;
- }
- }
- /*****************************************************************************
- ** Function: array_compare
- ** Description: checks to see if the array has a stable pattern.
- ** stable pattern means no change through a generation to
- ** the next.
- ** Parameters: active array with pattern and previous generations grid
- ** Pre-Conditions: None
- ** Post-Conditions: If the same returns a true value which will stop program.
- *****************************************************************************/
- bool array_compare(int current_array[30][50], int previous_array[30][50])
- {
- int tally = 0;
- for(int j = 0; j < 30; j++)
- {
- for(int i = 0; i < 50; i++)
- {
- if(current_array[j][i] == previous_array[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 == 30*50)
- return true;
- else
- return false;
- }
- /*****************************************************************************
- ** Function: array_wipe
- ** Description: Wipes the passed array to a blank slate of only 0's
- ** Parameters: desired array to erase
- ** Pre-Conditions: None
- ** Post-Conditions: Array will have 0 for each element
- *****************************************************************************/
- void array_wipe(int array_blank[30][50])
- {
- for(int x=0; x<30; x++)
- for(int y=0; y<50; y++)
- {
- array_blank[x][y] = 0; //assigns a blank spot to all spaces to start
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement