Created
January 17, 2017 19:32
-
-
Save gudnm/29f88bd270d398226b86adfb42c0592b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main() { | |
int count = 0, foo = 0, i = 0, j = 0, m = 0, n = 0; | |
const int LEN = 10; | |
int a[LEN][LEN]; | |
int b[LEN][LEN]; | |
for (i = 0; i < LEN; i++) { | |
for (j = 0; j < LEN; j++) { | |
a[i][j] = b[i][j] = 0; | |
} | |
} | |
// glider | |
a[0][1] = 1; | |
a[1][2] = 1; | |
a[2][2] = 1; | |
a[2][1] = 1; | |
a[2][0] = 1; | |
while (true) { | |
// output | |
for (i = 0; i < LEN; i++) { | |
for (j = 0; j < LEN; j++) { | |
cout << a[i][j] << ' '; | |
} | |
cout << endl; | |
} | |
cin.ignore(); // allows to press 'enter' to see next step | |
system("cls"); | |
for (i = 0; i < LEN; i++) { | |
for (j = 0; j < LEN; j++) { | |
// count live neighbors | |
count = 0; | |
for (m = i - 1; m <= i + 1; m++) { | |
for (n = j - 1; n <= j + 1; n++) { | |
// make sure to only check within legal range | |
if (m >= 0 && n >= 0 && m < LEN | |
&& n < LEN && a[m][n] == 1) { | |
count++; | |
} | |
} | |
} | |
// apply the rules and put result in b | |
b[i][j] = 0; | |
if (a[i][j] == 1) { | |
count -= 1; | |
if ((count == 2) || (count == 3)) { | |
b[i][j] = 1; | |
} | |
} else if (count == 3) { | |
b[i][j] = 1; | |
} | |
} | |
} | |
// replace a with b, and fill b with zeroes | |
for (i = 0; i < LEN; i++) { | |
for (j = 0; j < LEN; j++) { | |
a[i][j] = b[i][j]; | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment