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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <memory.h>
#include <time.h>
using namespace std;
void Ask(int, int);
void F_Gen(int, int);
void Cell(int, int);
void Start_Again(int);
const int M_Row (60);
const int M_Col (60);
int Gen = 0;
bool I_Cell [M_Row + 2] [M_Col + 2];
bool L_Cell [M_Row + 2] [M_Col + 2];
int main()
{
int Row = 0;
int Col = 0;
// int Start = 0;
time_t StopTime;
do
{
do
{
Ask(Row, Col);
F_Gen(Row, Col);
/* Start_Again(Start);*/
I_Cell [Row][Col] = L_Cell [Row][Col];
}
while (1);
memcpy (I_Cell, L_Cell, (M_Row + 2) * (M_Col + 2) * sizeof (bool));
StopTime = time (0) + 3;
while (time (0) < StopTime);
} while (!_kbhit ());
cout << "Bye" << endl;
}
void Ask (int X, int Y)
{
cout << " Game Of Life " << endl;
cout << "Enter the Number of Row (1 to 60): ";
cin >> X;
if ((X < 0) || (X > 60))
{
cout << "Invalid Row" << endl;
exit (0);
}
else if ((Y >=0 ) || (Y <= 60))
{
cout << "Enter the Number of Column (1 to 60): ";
cin >> Y;
}
else if ((Y < 0) || (Y > 60))
{
cout << "Invalid Column" << endl;
exit (0);
}
else if ((X < 0) || (X > 60) || (Y < 0) || (Y > 60))
{
cout << "Invalid Number" << endl;
exit (0);
}
else;
}
void F_Gen(int XY, int YZ)
{
int SN;
memset (I_Cell, false, (M_Row + 2) * (M_Col + 2) * sizeof (bool));
memset (L_Cell, false, (M_Row + 2) * (M_Col + 2) * sizeof (bool));
do
{
for (XY = 1; XY <= M_Row; XY++)
{
for (YZ = 1; YZ <= M_Col; YZ++)
cout << (I_Cell [XY] [YZ] ? '*' : ' ') << endl;
}
++Gen;
for (XY = 1; XY <= M_Row; XY++)
for (YZ = 1; YZ <= M_Col; YZ++)
{
Cell(XY, YZ);
}
cout << "Would You like to Start the next generation (Y/N): ";
cin >> SN;
}
while (SN == 'Y' && SN == 'y');
}
void Cell(int W, int Z)
{
if (I_Cell [W][Z] == 1)
{
if ((I_Cell [W][Z - 1] == 1) && (I_Cell [W][Z + 1] == 1) && (I_Cell [W - 1][Z] == 1) && (I_Cell [W + 1][Z] == 1))
I_Cell [W][Z] == 0;
else if ((I_Cell [W][Z - 1] <= 1) && (I_Cell [W][Z + 1] <= 1) && (I_Cell [W - 1][Z] <= 1) && (I_Cell [W + 1][Z] <= 1))
I_Cell [W][Z] == 0;
else if (((I_Cell [W][Z - 1] == 1) && (I_Cell [W][Z + 1] == 1) && (I_Cell [W - 1][Z] == 1) && (I_Cell [W + 1][Z] == 0)) || ((I_Cell [W][Z - 1] == 1) && (I_Cell [W][Z + 1] == 1) && (I_Cell [W - 1][Z] == 0) &&
(I_Cell [W + 1][Z] == 1)) || ((I_Cell [W][Z - 1] == 0) && (I_Cell [W][Z + 1] == 1) && (I_Cell [W - 1][Z] == 1) && (I_Cell [W + 1][Z] == 1)) || ((I_Cell [W][Z - 1] == 1) && (I_Cell [W][Z + 1] == 0) &&
(I_Cell [W - 1][Z] == 1) && (I_Cell [W + 1][Z] == 1)))
I_Cell [W][Z] == 1;
else
I_Cell [W][Z] == 0;
}
}
//void Start_Again(int SA)
//{
// cout << "Would you like to Start the Program Again (Y/N): ";
// cin >> SA;
//
// if ((SA == 'Y') && (SA == 'y'))
// system ("CLS");
//
// else;
//}
|