结对编程第二阶段

一、实验目标

1)体验敏捷开发中的两人合作。

2)进一步提高个人编程技巧与实践。

二 、实验内容

1)根据以下问题描述,练习结对编程(pair programming)实践;

2)要求学生两人一组,自由组合。每组使用一台计算机,二人共同编码,完成实验要求。

3)要求在结对编程工作期间,两人的角色至少切换 4 次;

4)编程语言不限,版本不限。建议使用 Python 或 JAVA 进行编程。

三、实验记录

1.组队情况

丁磊https://www.cnblogs.com/ dinglei918/

王旭https://www.cnblogs.com/qw123456/

github仓库地址https://github.com/runman33/my-travel-plans/new/master

2.课题分析

生命游戏其实是一个零玩家游戏,它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死了的细胞。一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死了的细胞的数量。如果相邻方格活着的细胞数量过多,这个细胞会因为资源匮乏而在下一个时刻死去;相反,如果周围活细胞过少,这个细胞会因太孤单而死去。实际中,你可以设定周围活细胞的数目怎样时才适宜该细胞的生存。如果这个数目设定过低,世界中的大部分细胞会因为找不到太多的活的邻居而死去,直到整个世界都没有生命;如果这个数目设定过高,世界中又会被生命充满而没有什么变化。实际中,这个数目一般选取2或者3;这样整个生命世界才不至于太过荒凉或拥挤,而是一种动态的平衡。这样的话,游戏的规则就是:当一个方格周围有2或3个活细胞时,方格中的活细胞在下一个时刻继续存活;即使这个时刻方格中没有活细胞,在下一个时刻也会“诞生”活细胞。在这个游戏中,还可以设定一些更加复杂的规则,例如当前方格的状况不仅由父一代决定,而且还考虑祖父一代的情况。你还可以作为这个世界的上帝,随意设定某个方格细胞的死活,以观察对世界的影响。在游戏的进行中,杂乱无序的细胞会逐渐演化出各种精致、有形的结构;这些结构往往有很好的对称性,而且每一代都在变化形状。一些形状已经锁定,不会逐代变化。有时,一些已经成形的结构会因为一些无序细胞的“入侵”而被破坏。但是形状和秩序经常能从杂乱中产生出来。

3.开发工具

应为有一名组员没带电脑手机,手机上C加加较为方便,选用了C加加作为此次的语言。

四、实验过程

1.代码规范

1) 空行

空行起着分隔程序段落的作用。空行得体将使程序的布局更加清晰。空行不会浪费内存,虽然打印含有空行的程序会多消耗一些纸张,但是值得。

规则一:定义变量后要空行。尽可能在定义变量的同时初始化该变量,即遵循就近原则。如果变量的引用和定义相隔比较远,那么变量的初始化就很容易被忘记。若引用了未被初始化的变量,就会导致程序出错。

规则二:每个函数定义结束之后都要加空行。

总规则三:两个相对独立的程序块、变量说明之后必须要加空行。比如上面几行代码完成的是一个功能,下面几行代码完成的是另一个功能,那么它们中间就要加空行。这样看起来更清晰。

2) 空格

规则一:关键字之后要留空格。像 const、case 等关键字之后至少要留一个空格,否则无法辨析关键字。像 if、for、while 等关键字之后应留一个空格再跟左括号(,以突出关键字。

规则二:函数名之后不要留空格,应紧跟左括号(,以与关键字区别。

规则三:(向后紧跟;)、,、;这三个向前紧跟;紧跟处不留空格。

规则四:,之后要留空格。如果;不是一行的结束符号,其后要留空格。

规则五:赋值运算符、关系运算符、算术运算符、逻辑运算符、位运算符,如 =、==、!=、+=、-=、*=、/=、%=、>>=、<<=、&=、^=、|=、>、<=、>、>=、+、-、*、/、%、&、|、&&、||、<<、>>、^ 等双目运算符的前后应当加空格。

注意,运算符“%”是求余运算符,与 printf 中 %d 的“%”不同,所以 %d 中的“%”前后不用加空格。

规则六:单目运算符 !、~、++、--、-、*、& 等前后不加空格。

规则七:像数组符号[]、结构体成员运算符.、指向结构体成员运算符->,这类操作符前后不加空格。

规则八:对于表达式比较长的 for 语句和 if 语句,为了紧凑起见,可以适当地去掉一些空格。但 for 和 if 后面紧跟的空格不可以删,其后面的语句可以根据语句的长度适当地去掉一些空格。

3) 成对书写

成对的符号一定要成对书写,如 ()、{}。不要写完左括号然后写内容最后再补右括号,这样很容易漏掉右括号,尤其是写嵌套程序的时候。

4) 缩进

缩进是通过键盘上的 Tab 键实现的,缩进可以使程序更有层次感。原则是:如果地位相等,则不需要缩进;如果属于某一个代码的内部代码就需要缩进。

5) 对齐

对齐主要是针对大括号{}说的:

规则一:{和}分别都要独占一行。互为一对的{和}要位于同一列,并且与引用它们的语句左对齐。

规则二:{}之内的代码要向内缩进一个 Tab,且同一地位的要左对齐,地位不同的继续缩进。

2.程序的总体设计

从题目的意思结合自己的想法总体进行设计了一下。大致分为四个阶段。

商最决定设计地图,设计地图为48+ 4的指阵。二维数组存储黑色方块,进行输出。当没有细胞时屏幕为黑色,色方块代表细

胞。根据生命游戏规则,来设计。

 

 3.程序结对编程过程

我们用QQ电话和屏幕分享完成本次实验,并用Github进行了记录。

第一步由王旭将他设计的规则代码上传到仓库中。

 

我fork王旭的代码,到本地,然后开始设计主函数代码部分。一个小时后,发消息通知他,我已经设计好代码并pullrequest了,王旭就Merge请求。

 

 

 

 

 

 

comment同意合并王旭对程序进行检验。发现程序有错,将这个合并删除,退回到上个版本。我从新修改上传。程序设计完成,

终程序没问题了,下载然后运行。

 

 

4.功能实现

代码界面:7.cpp运行。修改了几遍这是成功的第七代

运行结果:

完成实验要求,运行成功

 

 截取其中两代进行记录,进过自己的计算也是正常运行。

 代码界面:

 

 

五、实验小结

通过这次实验加强了我自己的编码能力更重要的是与同组交流两个人合作达成一加一大于二的效果,然我学到了结对编程的经验,同时把平时所学与实验结合更好的提升学习效率。

源代码附录:

#include<iostream>

#include<conio.h>

#include<windows.h>

#include<stdlib.h>

#include<time.h> 

using namespace std;

#define N 48

#define GEN 999

int a[N][N][GEN];

void gotoxy(int x, int y)

{

       HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

       COORD pos;

       pos.X = x;

       pos.Y = y;

       SetConsoleCursorPosition(handle, pos);

}

void show(int g)

{

       gotoxy(0, 0);

       int i, j;

       cout << "演化" << g<< "代之后的生命细胞状态为" << endl << "--------------------------------------------------------------------" << endl;

       for (i = 0;i < N;i++)

       {

              for (j = 0;j < N;j++)

              {

                     if (a[i][j][g] == 1)

                            cout << "■";

                     else if (a[i][j][g] == 0)

                            cout << "  ";

              }

              cout << endl;

       }

       Sleep(50);

}

void update(int k)

{

       int i,j, count = 0;

              for (i = 0;i < N;i++)

                     for (j = 0;j < N;j++)

                     {

                            count = 0;

                            if (i == 0 && j == 0)

                            {

                                   if (a[i][j + 1][k] == 1)

                                          count++;

                                   else if (a[i + 1][j][k] == 1)

                                          count++;

                                   else if (a[i + 1][j + 1][k] == 1)

                                          count++;

                                   if (count <= 1 || count >= 4)

                                          a[i][j][k + 1] = 0;

                                   else if (count == 2)

                                          a[i][j][k + 1] = a[i][j][k];

                                   else if (count == 3)

                                          a[i][j][k + 1] = 1;

                            }

                            if (i == 0 && j != 0 && j != N - 1)

                            {

                                   if (a[i][j - 1][k] == 1)

                                          count++;

                                   if (a[i][j + 1][k] == 1)

                                          count++;

                                   if (a[i + 1][j - 1][k] == 1)

                                          count++;

                                   if (a[i + 1][j][k] == 1)

                                          count++;

                                   if (a[i + 1][j + 1][k] == 1)

                                          count++;

                                   if (count <= 1 || count >= 4)

                                          a[i][j][k + 1] = 0;

                                   if (count == 2)

                                          a[i][j][k + 1] = a[i][j][k];

                                   if (count == 3)

                                          a[i][j][k + 1] = 1;

                            }

                            if (i == 0 && j == N - 1)

                            {

                                   if (a[i][j - 1][k] == 1)

                                          count++;

                                   if (a[i + 1][j - 1][k] == 1)

                                          count++;

                                   if (a[i + 1][j][k] == 1)

                                          count++;

                                   if (count <= 1 || count >= 4)

                                          a[i][j][k + 1] = 0;

                                   if (count == 2)

                                          a[i][j][k + 1] = a[i][j][k];

                                   if (count == 3)

                                          a[i][j][k + 1] = 1;

                            }

                            if (i != 0 && i != N - 1 && j == 0)

                            {

                                   count = 0;

                                   if (a[i - 1][j][k] == 1)

                                          count++;

                                   if (a[i - 1][j + 1][k] == 1)

                                          count++;

                                   if (a[i][j + 1][k] == 1)

                                          count++;

                                   if (a[i + 1][j][k] == 1)

                                          count++;

                                   if (a[i + 1][j + 1][k] == 1)

                                          count++;

                                   if (count <= 1 || count >= 4)

                                          a[i][j][k + 1] = 0;

                                   if (count == 2)

                                          a[i][j][k + 1] = a[i][j][k];

                                   if (count == 3)

                                          a[i][j][k + 1] = 1;

                            }

                            if (i == N - 1 && j == 0)

                            {

                                   count = 0;

                                   if (a[i - 1][j][k] == 1)

                                          count++;

                                   if (a[i - 1][j + 1][k] == 1)

                                          count++;

                                   if (a[i][j + 1][k] == 1)

                                          count++;

                                   if (count <= 1 || count >= 4)

                                          a[i][j][k + 1] = 0;

                                   if (count == 2)

                                          a[i][j][k + 1] = a[i][j][k];

                                   if (count == 3)

                                          a[i][j][k + 1] = 1;

                            }

                            if (i == N - 1 && j != 0 && j != N - 1)

                            {

                                   count = 0;

                                   if (a[i - 1][j - 1][k] == 1)

                                          count++;

                                   if (a[i - 1][j][k] == 1)

                                          count++;

                                   if (a[i - 1][j + 1][k] == 1)

                                          count++;

                                   if (a[i][j - 1][k] == 1)

                                          count++;

                                   if (a[i][j + 1][k] == 1)

                                          count++;

                                   if (count <= 1 || count >= 4)

                                          a[i][j][k + 1] = 0;

                                   if (count == 2)

                                          a[i][j][k + 1] = a[i][j][k];

                                   if (count == 3)

                                          a[i][j][k + 1] = 1;

                            }

                            if (j == N - 1 && i != 0 && i != N - 1)

                            {

                                   count = 0;

                                   if (a[i - 1][j - 1][k] == 1)

                                          count++;

                                   if (a[i - 1][j][k] == 1)

                                          count++;

                                   if (a[i][j - 1][k] == 1)

                                          count++;

                                   if (a[i + 1][j - 1][k] == 1)

                                          count++;

                                   if (a[i + 1][j][k] == 1)

                                          count++;

                                   if (count <= 1 || count >= 4)

                                          a[i][j][k + 1] = 0;

                                   if (count == 2)

                                          a[i][j][k + 1] = a[i][j][k];

                                   if (count == 3)

                                          a[i][j][k + 1] = 1;

                            }

                            if (i == N - 1 && j == N - 1)

                            {

                                   count = 0;

                                   if (a[i - 1][j - 1][k] == 1)

                                          count++;

                                   if (a[i - 1][j][k] == 1)

                                          count++;

                                   if (a[i][j - 1][k] == 1)

                                          count++;

                                   if (count <= 1 || count >= 4)

                                          a[i][j][k + 1] = 0;

                                   if (count == 2)

                                          a[i][j][k + 1] = a[i][j][k];

                                   if (count == 3)

                                          a[i][j][k + 1] = 1;

                            }

                            else if (i != 0 && j != 0 && i != N - 1 && j != N - 1)

                            {

                                   count = 0;

                                   if (a[i - 1][j - 1][k] == 1)

                                          count++;

                                   if (a[i - 1][j][k] == 1)

                                          count++;

                                   if (a[i - 1][j + 1][k] == 1)

                                          count++;

                                   if (a[i][j - 1][k] == 1)

                                          count++;

                                   if (a[i][j + 1][k] == 1)

                                          count++;

                                   if (a[i + 1][j - 1][k] == 1)

                                          count++;

                                   if (a[i + 1][j][k] == 1)

                                          count++;

                                   if (a[i + 1][j + 1][k] == 1)

                                          count++;

                                   if (count <= 1 || count >= 4)

                                          a[i][j][k + 1] = 0;

                                   if (count == 2)

                                          a[i][j][k + 1] = a[i][j][k];

                                   if (count == 3)

                                          a[i][j][k + 1] = 1;

                            }

                     }

}

int main()

{

       int i, j, k, count;

       srand(time(NULL));

       for (i = 0;i <  N ;i++)

              for (j = 0 ;j <  N ;j++)

                     a[i][j][0] = rand() %2;

       /*cout << "随机初始生命细胞状态为" << endl << "--------------------------------------------------------------------" << endl;

       for (i = 0;i < N;i++)

       {

              for (j = 0;j < N;j++)

              {

                     if (a[i][j][0] == 0)

                            cout << "⊙";

                     else

                            a[i][j][0] = 1;

                            cout << "  ";

              }

              cout << endl;

       }*/

       for (i = 0;i < GEN;i++)

       {

              show(i);

              update(i);

       }

       system("pause");

       return 0;

}

 

posted @ 2020-04-05 15:39  dinglei918  阅读(166)  评论(1编辑  收藏  举报