View Single Post
Old Feb 6th, 2008, 10:25 PM   #5
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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:

c Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. #define COLS 60
  5. #define ROWS 25
  6.  
  7. using namespace std;
  8.  
  9. char inpGen[ROWS][COLS] = {
  10. {"00000000000000000000000000000000000000000000000000000000000"},
  11. {"00000000000000000000000000000000000000000000000000000000000"},
  12. {"00000000000000000000000000000000000000000000000000000000000"},
  13. {"00000000000000000000000000000000000000000000000000000000000"},
  14. {"00000000000000000000000000000000000000000000000000000000000"},
  15. {"00000000000000000010000000000000000000000000000000000000000"},
  16. {"00000000000011000000000000000000000000000000000000000000000"},
  17. {"00000000000001000111000000000000000000000000000000000000000"},
  18. {"00000000000000000000000000000000000000000000000000000000000"},
  19. {"00000000000000000000000000000000000000000000000000000000000"},
  20. {"00000000000000000000000000000000000000000000000000000000000"},
  21. {"00000000000000000000000000000000000000000000000000000000000"},
  22. {"00000000000000000000000000000000000000000000000000000000000"},
  23. {"00000000000000000000000000000000000000000000000000000000000"},
  24. {"00000000000000000000000000000000000000000000000000000000000"},
  25. {"00000000000000000000000000000000000000000000000000000000000"},
  26. {"00000000000000000000000000000000000000000000000000000000000"},
  27. {"00000000000000000000000000000000000000000000000000000000000"},
  28. {"00000000000000000000000000000000000000000000000000000000000"},
  29. {"00000000000000000000000000000000000000000000000000000000000"},
  30. {"00000000000000000000000000000000000000000000000000000000000"},
  31. {"00000000000000000000000000000000000000000000000000000000000"},
  32. {"00000000000000000000000000000000000000000000000000000000000"},
  33. {"00000000000000000000000000000000000000000000000000000000000"},
  34. {"00000000000000000000000000000000000000000000000000000000000"}
  35. };
  36.  
  37. int curGen[ROWS][COLS];
  38. int nxtGen[ROWS][COLS];
  39. int evalCell(int i, int j);
  40. int getInt(char c){return ((c=='1')?1:0);}
  41. void printArray(){for(int i=0;i<ROWS; i++){for(int j=0;j<COLS; j++){cout<<(curGen[i][j]?"@":".");}cout<<endl;}}
  42. void swapArrays(){for(int i=0;i<ROWS; i++){for(int j=0;j<COLS;j++) curGen[i][j]=nxtGen[i][j];}}
  43. void parseInput(){for(int i=0;i<ROWS; i++){for(int j=0;j<COLS;j++) curGen[i][j]=getInt(inpGen[i][j]);}}
  44.  
  45. int main()
  46. {
  47. int i = 0, j = 0, k = 1;
  48. parseInput();
  49. printArray();
  50. cout<<"\n";
  51.  
  52. while(1)
  53. {
  54. for (i = 1; i < ROWS-1; i++)
  55. for(j = 1; j < COLS-1; j++)
  56. nxtGen[i][j] = evalCell(i,j);
  57. swapArrays();
  58. cout<<"\n\nIteration "<<k++<<endl;
  59. printArray();
  60. for(i = 1; i < 100000000; i++); //artificial delay
  61. }
  62. return 0;
  63. }
  64.  
  65. int evalCell(int i, int j)
  66. {
  67. 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];
  68. return (curGen[i][j]&&((liveNeighbors<2)||(liveNeighbors>3)))?0:(liveNeighbors==3?1:curGen[i][j]);
  69. }
OpenLoop is offline   Reply With Quote