1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#include<iostream>
#include<iomanip>
#include<ctime>
using namespace std;
#define ROWS 12 // Definted amount of rows
#define COLS 12 // Defined amount of columns
int generation(int world[ROWS][COLS], int& rows, int& cols, int& n_count);
void display(int world[ROWS][COLS], int world_2[ROWS][COLS], int& rows, int& cols);
void scanArray(int world[ROWS][COLS], int& rows, int& cols, int& n_count);
void generateArray(int world[ROWS][COLS], int& rows, int& cols);
int main()
{
int world[ROWS][COLS]={0}; // Original World/ Also helps print border of zeroes
int world_2[ROWS][COLS]={0}; //New world generated
int rows = 12;
int cols = 12;
int n_count = 0;
cout << "Welcome to The Game of Life!" << endl;
generateArray(world, rows, cols);
display(world, world_2, rows, cols);
cout << "This is the original world generated with 0s as no life and 1s as a single life";
scanArray(world, rows, cols, n_count);
world_2[ROWS][COLS] = world[ROWS][COLS];
display(world, world_2, rows, cols);
system("pause");
return 0;
}
void generateArray(int world[ROWS][COLS], int& rows, int& cols)
{
for(int row = 1; row < rows-1; row++) //Minus one and set equal to one so that broder is not included.
{
for(int col = 1; col < cols-1; col++)
{
world[row][col] = rand()%2;
}
}
}
void display(int world[ROWS][COLS], int world_2[ROWS][COLS], int& rows, int& cols)
{
for(int row = 0; row < rows; row++)
{
for(int col = 0; col < cols; col++)
{
cout << setw(5) << world[row][col];
}
cout << endl;
}
cout << endl;
}
void scanArray(int world[ROWS][COLS], int& rows, int& cols, int& n_count)
{
int neighbor_count = 0;
for(int row = 1; row < rows-1; row++)
{
for(int col = 1; col < cols-1; col++)
{
neighbor_count = generation(world, rows, cols, n_count);
if(world[row][col] == 1){
if(neighbor_count == 1 || neighbor_count == 0)
world[col][row] = 0;
if(neighbor_count >= 4)
world[col][row] = 0;
if(neighbor_count == 2 || neighbor_count == 3)
world[col][row] = 1;
}
if(world[row][col] == 0){
if(neighbor_count == 3)
world[row][col] = 1;
}
}
}
}
int generation(int world[ROWS][COLS], int& rows, int& cols, int& n_count)
{
if(world[cols][rows-1] == 1) // The cell above
n_count++;
if(world[cols-1][rows] == 1) // The cell to the left
n_count++;
if(world[cols+1][rows] == 1) // The cell to the right
n_count++;
if(world[cols][rows+1] == 1) // The cell below
n_count++;
if(world[cols-1][rows-1] == 1) // The cell diagonally upper left
n_count++;
if(world[cols-1][rows+1] == 1) // The cell diagonally lower left
n_count++;
if(world[cols+1][rows-1] == 1) // The cell diagonally upper right
n_count++;
if(world[cols+1][rows+1] == 1) // The cell diagonally lower right
n_count++;
return n_count++;
}
|