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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
#include <iostream>
#include <cstdlib>
#include <stdlib.h> // You might be able to get away without this one.
using namespace std;
const int size = 24;
typedef bool BoardType[size][size];
// Function prototypes:
void display(BoardType Board, int & iteration);
bool Life(BoardType Board);
void populate(BoardType Board, BoardType Board2);
// A function prototype can't be used with an inline function (see below for function NumLiveNeighbors). Just be careful to use it only after the function is defined.
int main()
{
int Iteration = 0; // needed here to count the use of the display function
int cycle;
BoardType Board2;
BoardType Board =
{
{0,1,0,0,1,1,0,0,1,1,0,0,0,0,1,1,0,0,1,1,0,0,1,0,},
{1,0,1,1,0,1,0,1,0,1,0,1,0,1,1,1,1,0,0,1,0,1,1,1,},
{0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,},
{0,1,1,1,0,0,0,1,0,1,0,1,0,1,1,0,1,0,1,0,1,1,1,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,},
{1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,1,},
{0,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,},
{0,0,1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,0,0,0,0,1,1,1,},
{1,0,0,0,1,1,1,1,1,1,1,0,1,1,1,1,1,0,0,0,0,1,0,1,},
{1,0,1,0,1,0,0,0,0,0,1,0,1,1,1,1,1,1,0,1,0,1,0,1,},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,},
{0,0,0,0,1,1,1,1,0,1,0,1,1,1,1,1,0,0,0,1,0,1,1,1,},
{0,1,0,1,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,0,1,0,},
{1,1,1,1,1,1,0,0,0,0,1,0,1,0,1,0,0,0,0,0,1,1,1,1,},
{1,0,0,0,0,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,1,1,},
{0,0,0,0,1,1,1,1,1,0,1,0,1,0,1,1,1,1,0,1,0,1,0,1,},
{0,1,1,1,1,0,1,0,1,0,1,0,1,0,1,1,1,1,0,1,0,1,0,1,},
{1,1,1,1,1,0,1,0,1,0,1,1,1,1,1,0,0,0,1,0,0,0,0,0,},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,},
{0,1,1,1,0,0,0,0,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,},
{0,1,1,1,1,0,0,0,1,1,1,1,0,0,0,0,0,1,0,1,1,1,1,0,},
{1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,},
{0,1,0,0,1,1,0,0,1,1,0,0,0,0,1,1,0,0,1,1,0,0,1,0,},
};
cout << "Enter the number of generations to run." << endl;
cin >> cycle;
cout << "You will need to press a key between cycles" << endl;
cout << "Press CTRL + C to exit at any time.";
system("PAUSE");
system("cls");
display(Board, Iteration);
cout << endl << endl << endl;
for (int i = 1; i <= cycle; i++)
{
// If system("PAUSE") does not work with your version of Visual Studio
// try the method of prompting the user to press Enter to go on, then
// read that keypress into a char variable. Similarly, if system("cls")
// does not work for you, you might be able to clear the screen by
// outputting the correct number of endl's.
system("PAUSE"); //creates the pause between generations
system("cls"); // Clears the screen
populate(Board, Board2);
display(Board, Iteration);
}
return 0;
}
inline int NumLiveNeighbors(BoardType Board, int Row, int Col)
{
int NumLiveNeighbors = 0;
int top, bottom, right, left;
if (Row==0)
{
top=0;
bottom=1;
}
else if (Row==23)
{
top=22;
bottom=23;
}
else
{
top=Row-1;
bottom= Row+1;
}
if (Col==0)
{
left=0;
right=1;
}
else if (Col==23)
{
left=22;
right=23;
}
else
{
left=Col-1;
right=Col+1;
}
for (int k=top; k<=bottom; k++)
{
for (int c=left; c<=right; c++)
if (Board[k][c]==true)
NumLiveNeighbors++;
}
if (Board[Row][Col]==true)
NumLiveNeighbors--;
return NumLiveNeighbors;
}
bool Life(BoardType Board, int Row, int Col)
{
int live = NumLiveNeighbors(Board, Row, Col);
if ((Board[Row][Col]== true) && (live==2 || live==3)) // if Board[Row][Col] is true then...
{
return true;
// If the number of live neigbors is too large or small (see the rules)
// then return false to say that the cell will die.
// Otherwise, return true to say the the cell will remain alive.
/*1. Any live cell with fewer than two live neighbors dies, as if caused by underpopulation.
2. Any live cell with more than three live neighbors dies, as if by overcrowding.
3. Any live cell with two or three live neighbors lives on to the next generation.
4. Any dead cell with exactly three live neighbours becomes a live cell.
*/
}
else if ((Board[Row][Col] == true) && (live < 2 || live > 3)) // if Board[Row][Col] is false then...
{
return false;
// If the number of live neighbors is just right, return true to say
// that this dead cell will come to life. Otherwise return false to say
// that this dead cell stays dead. (Again, refer to the rules.)
}
else if ((Board[Row][Col]==false) && (live==3))
{
return true;
}
else // ((Board[Row][Col]==false) && (live<3 || live>3))
{
return false;
}
}
void populate(BoardType Board, BoardType Board2)
{
for (int row = 0; row < size; row++)
for (int col = 0; col < size; col++)
Board2[row][col] = Life(Board, row, col);
for (int row = 0; row < size; row++)
for (int col = 0; col < size; col++)
Board[row][col] = Board2[row][col];
}
/* Given: Board The Array to Display
iteration A count of cycles that the function has run.
Task: Display the contents of Board in an easy-to-read format.
Return: Iteration Updated count of number of generations displayed.
*/
void display(BoardType Board, int & iteration)
{
cout << " ";
for (int Col = 0; Col < size; Col++)
{
if (Col < 10)
cout << Col << " " ;
else
cout << Col << " ";
}
cout << endl << endl;
for (int Row = 0; Row < size; Row++)
{
if (Row < 10)
cout << Row << " ";
else
cout << Row << " ";
for (int Col = 0; Col < size; Col++)
if (Board[Row][Col])
cout << "X " ;
else
cout << " ";
cout << endl;
}
cout << " GENERATION: " << iteration << endl;
iteration++;
}
|