Game_Of_Life

Posted jovesun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Game_Of_Life相关的知识,希望对你有一定的参考价值。

Rules:

1,Any live cell with fewer than two live neighbors dies, as if caused by under-population.
2,Any live cell with two or three live neighbors lives on to the next generation.
3,Any live cell with more than three live neighbors dies, as if by over-population..
4,Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
My Code As Follows:

  1 #include<iostream>
  2 #include<vector>
  3 #include<fstream>
  4 #define ALIVE 1        //存活
  5 #define DEAD 0        //死亡
  6 #define MAXROW 5    //最大行
  7 #define MAXCOL 5    //最大列
  8 using namespace std;
  9 using Mat = vector<vector<int>>;
 10 void init_self(Mat & mat);    //用户手动输入数据
 11 bool init_file(Mat & mat);    //从文件读取
 12 void show(const Mat & mat);    //输出,用空格和"X"表示存活状态
 13 void update(Mat & pre_mat, Mat & update_mat);    //跟新状态
 14 void init_date(Mat & mat);    //初始化pre和update的数据
 15 int main() 
 16     Mat pre_mat(MAXROW, vector<int>(MAXCOL));
 17     Mat update_mat(MAXROW, vector<int>(MAXCOL));
 18     init_date(pre_mat);        //先将pre_mat和update_mat置为0
 19     init_date(update_mat);
 20 
 21     cout << "Welcome to Game_Of_Life:" << endl
 22         << endl
 23         << "按下键盘数字 1 ,选择手动输入" << endl
 24         << endl
 25         << "按下键盘数字 2 ,将从文件(D:\\mat.txt)读入数据" << endl
 26         << "其中数据0表示死亡,数据1表示存活" << endl
 27         << "请输入20行*20列规模的数据,要求数据仅为0或1" << endl
 28         << endl
 29         << "请按 1 或 2 " << endl;
 30     int choice;
 31     bool SWITCH_ERROR = true;    //未发生switch异常,标记为true;
 32     bool Read_File;
 33     while (SWITCH_ERROR) 
 34         cin >> choice;
 35         switch (choice)
 36         
 37         case 1:
 38             init_self(pre_mat);
 39             SWITCH_ERROR = false;
 40             break;
 41         case 2:
 42             Read_File = init_file(pre_mat);
 43             if (!Read_File) 
 44                 SWITCH_ERROR = false;
 45                 return 0;
 46             
 47             else
 48                 SWITCH_ERROR = false;
 49             break;
 50         default:
 51             cout << "您的输入有误,请重新输入数字1 或 数字2" << endl
 52                 << "按下键盘数字 1 ,选择手动输入" << endl
 53                 << "按下键盘数字 2 ,将从文件(D:\\mat.txt)读入" << endl
 54                 << "请按 1 或 2 " << endl;
 55             break;
 56         
 57     
 58     int order = 0;
 59     cout << "" << order++ << "代生存图如下:" << endl;
 60     show(pre_mat);
 61     int Go_On = 1;
 62     while (Go_On) 
 63         cout << "" << order++ << "代生存图如下:" << endl;
 64         update(pre_mat, update_mat);
 65         show(pre_mat);
 66         cout << "继续输出下一代生存状况,请按1" << "退出请按0" << endl;
 67         cin >> Go_On;
 68         if (Go_On != 1 || !Go_On) 
 69             cout << "继续输出下一代生存状况,请按1" << "退出请按0" << endl;
 70             cin >> Go_On;
 71         
 72     
 73     return 0;
 74 
 75 
 76 void init_date(Mat & mat) 
 77     for (int i = 0; i < MAXROW; i++) 
 78         for (int j = 0; j < MAXCOL; j++)
 79             mat[i][j] = 0;
 80     
 81 
 82 
 83 void init_self(Mat & mat) 
 84     int x, y;
 85     cout << "请依次输入存活细胞的行坐标x,列坐标y (注意其中1<=x<=" << MAXROW
 86         << ",1<=y<=" << MAXCOL << ",当x=0且y=0时,输入结束" << endl;
 87     while (true) 
 88         cin >> x >> y;
 89         if (0 == x && 0 == y) 
 90             cout << "您已完成输入!" << endl;
 91             break;
 92         
 93         else if (x > 0 && y > 0 && x<MAXROW && x<MAXCOL) 
 94             mat[MAXCOL - y][x - 1] = ALIVE;
 95             cout << "您已令(" << x << "," << y << ")存活" << endl;
 96         
 97         else 
 98             cout << "您的输入有误,请重新输入!" << endl;
 99             cout << "请依次输入存活细胞的行坐标x,列坐标y (注意其中1<=x<=" << MAXROW
100                 << ",1<=y<=" << MAXCOL << ",当x=0且y=0时,输入结束" << endl;
101         
102     
103 
104 
105 bool init_file(Mat & mat) 
106     ifstream fin("D:\\mat.txt");
107     int x;
108     for (int i = 0; i < MAXROW; i++) 
109         for (int j = 0; j < MAXCOL; j++) 
110             fin >> x;
111             if (!fin) 
112                 cout << "读取文件(D:\\mat.txt)失败,程序退出!" << endl;
113                 return 0;
114             
115             if (1 == x || !x) 
116                 mat[i][j] = x;
117             
118             else 
119                 cout << "文件内容错误,请确保文件数据仅为0或1,数据之间用空格分开!"
120                     << "格式为 " << MAXROW << "行*" << MAXCOL << "" << endl
121                     << "程序退出,请检查(D:\\mat.txt)数据正确性!" << endl;
122                 return 0;
123             
124         
125     
126     return 1;
127 
128 void update(Mat & pre_mat, Mat & update_mat)     
129     for (int i = 1; i < MAXROW - 1; i++) 
130         for (int j = 1; j < MAXCOL - 1; j++) 
131             int sum = 0;
132             if (pre_mat[i][j - 1] == ALIVE) sum++;
133             if (pre_mat[i][j + 1] == ALIVE) sum++;
134             if (pre_mat[i - 1][j] == ALIVE) sum++;
135             if (pre_mat[i + 1][j] == ALIVE) sum++;
136             if (pre_mat[i - 1][j - 1] == ALIVE) sum++;
137             if (pre_mat[i - 1][j + 1] == ALIVE) sum++;
138             if (pre_mat[i + 1][j - 1] == ALIVE) sum++;
139             if (pre_mat[i + 1][j + 1] == ALIVE) sum++;
140 
141             if (pre_mat[i][j] == ALIVE) 
142                 if (sum < 2)
143                     update_mat[i][j] = DEAD;
144                 else if (2 == sum || 3 == sum)
145                     update_mat[i][j] = ALIVE;
146                 else
147                     update_mat[i][j] = DEAD;
148             
149             else 
150                 if (sum == 3)
151                     update_mat[i][j] = ALIVE;
152             
153         
154     
155     pre_mat = update_mat;
156     init_date(update_mat);
157 
158 
159 void show(const Mat & mat) 
160     for (auto &c : mat) 
161         for (auto & cc : c) 
162             if (cc == DEAD)
163                 cout << "D";
164             else
165                 cout << "X";
166         
167         cout << endl;
168     
169 

 

以上是关于Game_Of_Life的主要内容,如果未能解决你的问题,请参考以下文章