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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int h = 20;
const int w = 70;
bool show [h] [w], work [h] [w];
int x, y, neighbors, iterations, c, row, col;
//Functions
// Instructions() - describes the game and input
void Instructions()
{
cout << "Welcome to Conway's Game of Life\n\n\n"
<< "The game is played on a 70 x 20 space grid.\n"
<< "Empty spaces are 'dead' and 1s represent 'life'.\n\n"
<< "There are 3 simple rules:\n"
<< " If a space is dead, with exactly 3 live neighbors, "
<< "that space\nwill be 'born'.\n"
<< " If a space is alive, with 2 or 3 live neighbors, "
<< "that space\nwill continue to live.\n"
<< " If a space is alive with 0, 1, or 4 or more dead "
<< "neighbors, that\nspace will die.\n\n"
<< "Enter an X (1-70) and Y (1-20) coordinate where you would "
<< "like to seed\nLife, with 0, 0 the upper left corner.\n"
<< "To remove life, enter the same coordinate a second time.\n"
<< "Type '-1' as an X to indicate you are done entering and wish "
<< "to start\nthe simulation" <<endl;
}
//Clear() - Clears the arrays
void Clear(bool matrix[h][w])
{
for(row=0; row<h; row++)
{
for(col=0; col<w; col++)
matrix[row][col] = 0;
}
}
//Display() - prints an Array
void Display(bool matrix[h][w])
{
for(row=0; row<h; row++)
{
for(col=0; col<w; col++)
{
if (col==0)
cout << endl << matrix[row][col];
else
cout << matrix[row][col];
}
}
cout << endl << endl;
}
//InvalidC() - displays an invalid entry message for columns
void InvalidC()
{
cout << "That is an invalid selection.\n"
<< "Please enter a value between 1 and 70\n"
<< "X ";
cin >> x;
}
//InvalidR() - displays an invalid entry message for rows
void InvalidR()
{
cout << "That is an invalid selection.\n"
<< "Please enter a value between 1 and 20\n"
<< "Y ";
cin >> y;
}
//Input() - receives input and populates an array
void Input(bool matrix [h][w])
{
while (x != -1)
{
cout << "X ";
cin >> x;
if (x == -1)
break;
while (x < 0 || x > w)
InvalidC();
cout << "Y ";
cin >> y;
while (y < 0 || y > h)
InvalidR();
if (show[y-1][x-1] == 0)
show[y-1][x-1] = 1;
else
matrix[y-1][x-1] = 0;
Display (show);
}
cout << endl;
cout << "Enter the number of 'ticks' that you want the\n"
<< "simulation to run" << endl;
cin >> iterations;
}
//Swtich() - moves array Work into Array Show
void Switch(bool matrixa[h][w], bool matrixb[h][w])
{
for(row=0; row<h; row++)
{
for(col=0; col<w; col++)
{
matrixa[row][col] = matrixb[row][col];
}
}
}
//Calculate() - Counts neighbors, and calculates the new array
void Calculate(bool matrixa[h][w], bool matrixb[h][w])
{
for(row=0; row<h; row++)
{
for(col=0; col<w; col++)
{
neighbors=0;
// count neighbors
if (matrixa [row-1][col-1] == 1) neighbors +=1;
if (matrixa [row-1][col] == 1) neighbors +=1;
if (matrixa [row-1][col+1] == 1) neighbors +=1;
if (matrixa [row][col-1] == 1) neighbors +=1;
if (matrixa [row][col+1] == 1) neighbors +=1;
if (matrixa [row+1][col-1] == 1) neighbors +=1;
if (matrixa [row+1][col] == 1) neighbors +=1;
if (matrixa [row+1][col+1] == 1) neighbors +=1;
//calculate life
if (matrixa [row][col] == 1 && neighbors < 2)
matrixb [row][col] = 0;
else if (matrixa [row][col] == 1 && neighbors > 3)
matrixb [row][col] = 0;
else if (matrixa [row][col] == 1 &&
(neighbors == 2 || neighbors == 3))
matrixb [row][col] = 1;
else if (matrixa [row][col] == 0 && neighbors == 3)
matrixb [row][col] = 1;
}
}
}
// declaration of main program
int main()
{
Instructions();
Clear(show);
Clear(work);
Input(show);
Display(show);
for (c=0; c<iterations; c++)
{
cout << "show1";
Display(show);
Clear(work);
cout << "work1";
Display(work);
Calculate(show, work);
cout << "show2";
Display(show);
cout << "work2";
Display(work);
Switch(show, work);
cout << "show3";
Display(show);
cout << "work3";
Display(work);
}
}
|