|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 
|
Re: Conway's Game Of Life - Programming Challenge
I've never heard of Conway's game of life before you mentionned. The output is rather amuzing. Here's my code with a Diehard starting pattern which will generate 129 different paterns before dying on the 130th. It's not build for speed or readability, just for fun:
#include <iostream> #include <string> #define COLS 60 #define ROWS 25 using namespace std; char inpGen[ROWS][COLS] = { {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000010000000000000000000000000000000000000000"}, {"00000000000011000000000000000000000000000000000000000000000"}, {"00000000000001000111000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"}, {"00000000000000000000000000000000000000000000000000000000000"} }; int curGen[ROWS][COLS]; int nxtGen[ROWS][COLS]; int evalCell(int i, int j); int getInt(char c){return ((c=='1')?1:0);} void printArray(){for(int i=0;i<ROWS; i++){for(int j=0;j<COLS; j++){cout<<(curGen[i][j]?"@":".");}cout<<endl;}} void swapArrays(){for(int i=0;i<ROWS; i++){for(int j=0;j<COLS;j++) curGen[i][j]=nxtGen[i][j];}} void parseInput(){for(int i=0;i<ROWS; i++){for(int j=0;j<COLS;j++) curGen[i][j]=getInt(inpGen[i][j]);}} int main() { int i = 0, j = 0, k = 1; parseInput(); printArray(); cout<<"\n"; while(1) { for (i = 1; i < ROWS-1; i++) for(j = 1; j < COLS-1; j++) nxtGen[i][j] = evalCell(i,j); swapArrays(); cout<<"\n\nIteration "<<k++<<endl; printArray(); for(i = 1; i < 100000000; i++); //artificial delay } return 0; } int evalCell(int i, int j) { int liveNeighbors=curGen[i-1][j-1]+curGen[i-1][j]+curGen[i-1][j+1]+curGen[i][j-1]+curGen[i][j+1]+curGen[i+1][j-1]+curGen[i+1][j]+curGen[i+1][j+1]; return (curGen[i][j]&&((liveNeighbors<2)||(liveNeighbors>3)))?0:(liveNeighbors==3?1:curGen[i][j]); }
|