从网上的java代码改的
  1 /*
  2 1,孤单死亡:如果细胞的邻居小于一个,则该细胞在下一个状态死亡。
  3 
  4 2,拥挤死亡:如果细胞的邻居在四个以上,则该细胞在下一个状态死亡。
  5 
  6 3,稳定:如果细胞的邻居为两个或三个,则该细胞在下一个状态稳定。
  7 
  8 4,复活:如果某位置原无细胞存活,而该位置的邻居为三个,则该位置将复活一个细胞。 
  9 */
 10 #include<iostream>
 11 using namespace std;
 12 class Conway_life
 13 {
 14       public:
 15              Conway_life();
 16              
 17              void set_cell(int x,int y)
 18              {
 19                              map[x][y]=true;        
 20              }
 21              
 22              void next();
 23              
 24              void copyMap();
 25              void outputMap();      
 26       private:
 27              int neighbors(int row, int col);
 28              bool map[30][40];
 29              bool newmap[30][40];
 30       
 31 };
 32 
 33 Conway_life::Conway_life()
 34 {
 35             int row,col;
 36             
 37             
 38             for(row=0;row<30;++row)
 39             {
 40                                    for(col=0;col<40;++col)
 41                                    {
 42                                                    map[row][col]=false;   
 43                                                    newmap[row][col]=false;                           
 44                                    }                       
 45             }                                        
 46 }
 47 
 48 void Conway_life::next()
 49 {
 50         int row,col;
 51         for( row = 0; row < 30; row++)
 52         {  
 53                 for( col = 0; col < 40; col++)
 54                 {  
 55                         switch (neighbors(row, col))
 56                          {  
 57                                case 0:   
 58                                case 1:   
 59                                case 4:   
 60                                case 5:   
 61                                case 6:   
 62                                case 7:   
 63                                case 8:   
 64                                     newmap[row][col] = false;   
 65                                    break;   
 66                                case 2:   
 67                                     newmap[row][col] = map[row][col];   
 68                                    break;   
 69                                case 3:   
 70                                     newmap[row][col] = true;   
 71                                    break;   
 72                          }   
 73                }  
 74         }  
 75   
 76         copyMap();  
 77 }
 78 
 79 void Conway_life::copyMap()
 80 {
 81      for(int row = 0; row < 30; row++)   
 82         for(int col = 0; col < 40; col++)   
 83             map[row][col] = newmap[row][col];        
 84 }
 85 
 86 void Conway_life::outputMap()
 87 {
 88      cout<<"\n\n Game of life cell status";
 89      int row,col;
 90      for(row=0;row<30;++row)
 91      {
 92           cout<<"\n";
 93           for(col=0;col<40;++col)
 94           {
 95                 if(map[row][col])
 96                                  cout<<"#";
 97                 else
 98                                  cout<<"-";                 
 99           }
100      }
101            
102 }
103 
104 int Conway_life::neighbors(int row,int col)
105 {
106      int count = 0;  
107      int r,c;
108      
109      for(int r = row-1; r <= row+1; r++)   
110              for(int c = col-1; c <= col+1; c++) {   
111                      if(r < 0 || r >= 30 ||  c < 0 || c >= 40)   
112                           continue;   
113                        if(map[r][c] == true)   
114                           count++;   
115              }   
116 
117     if(map[row][col] == true)   
118         count--;   
119             
120     return count;   
121 }
122 
123 
124 int main()
125 {
126     
127     Conway_life game;
128   
129     cout<<"Game of life Program"<<endl;   
130     cout<<"Enter row, col where rol, col is living cell"<<endl;  
131     cout<<"0 <= rol < 30, 0 <= col < 40"<<endl;   
132     cout<<"Terminate with row, col = -1, -1"<<endl;
133     
134     int row,col;
135     
136     cin>>row>>col;
137     char c='y';
138     char d;
139    
140     while(true) {  
141         
142             if(0 <= row && row < 30 && 0 <= col && col < 40)   
143                  game.set_cell(row, col);  
144             else if(row == -1 || col == -1) {  
145                  break;  
146             }  
147             else {   
148                  cout<<"(x, y) exceeds map ranage!"<<endl;  
149             }  
150             cin>>row>>col;
151     } 
152      
153      while(true) {  
154            game.outputMap();  
155            game.next();  
156  
157            cout<<"\nContinue next Generation ? "<<endl;  
158            
159            cin>>d;
160            if(d!=c)  
161                break;
162              
163           
164      }  
165     system("pause");
166     return 0;    
167 }
168