生命游戏的代码很优美,大家看我的注释像不像现代诗
...
Grid map; // square array holding cells 二维数组的,四周有一圈篱笆的世界
Gridcount numNeighbors; // square array holding neighbor counts 二维数组的邻居数量
List newlive, // cells that have just been vivified 新出生的队列
newdie, // cells that have just died 刚死掉的队列
maylive, // candidates to vivify in next generation 可能即将出生的队列
maydie; // candidates to kill in next generation 可能即将死掉的队列
...
int main(int argc, char* argv[])
{
...
Initialize(map, numNeighbors, &newlive, &newdie, &maylive, &maydie, fName, &gens, &c1, &c2);
while (gencount < gens) {//如果没有结束
gencount++;
TraverseList(&maylive, Vivify);// 就让可能即将出生的队列中确实应该出生的出生
TraverseList(&maydie, Kill); // 让可能即将死掉的队列中确实应该死掉的死掉
ClearList(&maylive); // 清除即将出生的队列
ClearList(&maydie); // 清除即将死掉的队列
TraverseList(&newlive, AddNeighbors); // 根据新生的队列调整邻居的数量,调整可能即将生的队列和可能即将死的队列
TraverseList(&newdie, SubtractNeighbors); // 根据刚死的队列调整邻居的数量,调整可能即将生的队列和可能即将死的队列
ClearList(&newlive); // 清除新出生的队列
ClearList(&newdie); // 清除刚死掉的队列
}
WriteMap(map, gencount, c1, c2); // 输出要输出的
return 0;
}
void AddNeighbors(Cell cell)
{// Check neighbors around cell that has recently come to life
// Update neighbor counts of cells around cell of interest; add to appropriate list
int nbrrow, // loop index for row of neighbor loops
nbrcol; // column loop index
Cell neighbor; // structure form of a neighbor
for (nbrrow = cell.row-1; nbrrow <=cell.row+1; nbrrow++)//3次循环
for (nbrcol = cell.col-1; nbrcol <=cell.col+1; nbrcol++)//3次循环
if (nbrrow != cell.row || nbrcol != cell.col) { //skip cell itself跳过自己,只处理周围8个
numNeighbors[nbrrow][nbrcol]++;// 刚出生的我是周围8个单元的新邻居
switch(numNeighbors[nbrrow][nbrcol]) {
case 0:
Error("Impossible case in AddNeighbors.");//刚加过1,怎么可能
break;
case 3: //如果有3个邻居
if (map[nbrrow][nbrcol] == DEAD) {//而且原来是死的
neighbor.row = nbrrow;
neighbor.col = nbrcol;
AddList(neighbor, &maylive);//下一次就可能生了
}
break;
case 4://如果有4个邻居
if (map[nbrrow][nbrcol] == ALIVE) {//而且原来是活的
neighbor.row = nbrrow;
neighbor.col = nbrcol;
AddList(neighbor, &maydie);//下一次就可能死了
}
break;
} // switch
}
}
void Vivify(Cell cell)
{// Determine if conditions are right for cell to be brought to life
if (map[cell.row][cell.col] == DEAD &&// 如果原来是死的而且
numNeighbors[cell.row][cell.col] == 3)// 有3个邻居
if (cell.row >= 1 && cell.row <= MAXROW &&
cell.col >= 1 && cell.col <= MAXCOL) {// 如果在篱笆里面
map[cell.row][cell.col] = ALIVE;// 让他生
AddList(cell, &newlive);// 加入新生的队列
}
}
void SubtractNeighbors(Cell cell)
{// Check number of neighbors around cell that has recently died
// Update neighbor counts of cells around cell of interest; add to appropriate list
int nbrrow, // loop index for row of neighbor loops
nbrcol; // column loop index
Cell neighbor; // structure form of a neighbor
for (nbrrow = cell.row-1; nbrrow <=cell.row+1; nbrrow++)//3行
for (nbrcol = cell.col-1; nbrcol <=cell.col+1; nbrcol++)//3列
if (nbrrow != cell.row || nbrcol != cell.col) { //skip cell itself去掉自己
numNeighbors[nbrrow][nbrcol]--;// 自己死掉了,周围的8个单元少了一个邻居
switch(numNeighbors[nbrrow][nbrcol]) {
case 8:
Error("Impossible case in SubtractNeighbors.");//刚减掉一个,怎么可能
break;
case 3: //如果有3个邻居
if (map[nbrrow][nbrcol] == DEAD) {//而且原来是死的
neighbor.row = nbrrow;
neighbor.col = nbrcol;
AddList(neighbor, &maylive);//下次可能生了
}
break;
case 1://如果有1个邻居
if (map[nbrrow][nbrcol] == ALIVE) {//而且原来是活的
neighbor.row = nbrrow;
neighbor.col = nbrcol;
AddList(neighbor, &maydie);//下次可能死了
}
break;
} // switch
}
}
void Kill(Cell cell)
{// Determine if conditions are right for cell to be killed
if (map[cell.row][cell.col] == ALIVE &&// 如果原来是生的
(numNeighbors[cell.row][cell.col] > 3 ||//如果邻居多于3个
numNeighbors[cell.row][cell.col] < 2))//或者少于2个
if (cell.row >= 1 && cell.row <= MAXROW &&
cell.col >= 1 && cell.col <= MAXCOL) {// 如果在篱笆里面
map[cell.row][cell.col] = DEAD;//让他死
AddList(cell, &newdie);// 加入刚死的队列
}
}
void Initialize(Grid map, Gridcount numNeighbors,
List *newlive, List *newdie,
List *maylive, List *maydie,
char *fName, int *gens, Cell *c1, Cell *c2)
{
int row, col;
CreateList(newlive); // 创建4个空队列
CreateList(newdie);
CreateList(maylive);
CreateList(maydie);
// Obtain the initial configuration
ReadMap(newlive, map, fName, gens, c1, c2);// 读输入文件,建立了刚出生队列
// Set all entries in numNeighbors to 0
for (row=0; row <= MAXROW+1; row++)
for (col=0; col <= MAXCOL+1; col++)
numNeighbors[row][col] = 0;// 清空邻居数数组
// Put the candidates to live into maylive
TraverseList(newlive, AddNeighbors);// 根据新生的队列调整邻居数,调整可能即将生的队列和可能即将死的队列
// Check all living cells to see which may die
CopyList(maydie, newlive);// 将新生的队列复制到可能即将死的队列
ClearList(newlive);//清除新生的队列
}
...
void ReadMap(List *newlive, Grid map, char *fName, int *gens, Cell *c1, Cell *c2)
{//Read in the initial configuration of cells, number of generations to simulate,
// and the region to be printed after the final generation
...
for (row=0; row <= MAXROW+1; row++)
for (col=0; col <= MAXCOL+1; col++)
map[row][col] = DEAD; // Set all cells empty, including the hedge连四周的篱笆都设成空
if ((fp = fopen(fName, "r")) == NULL) Error("Input file not found");
printf("Reading input data from %s...",fName);
fscanf(fp,"%d %d",&row, &col);// 读一个单元的行列值
//Read row,col pairs until 0,0 marker read
while (row != 0 || col != 0) { // 直到行列值都是0
if (row >= 1 && row <= MAXROW && col >= 1 && col <= MAXCOL) {
newcell.row = row;
newcell.col = col;
if (map[row][col] == DEAD) {
AddList(newcell, newlive); // no dups夹到刚出生的队列,他们是第一代出生的
good++;// 累计有效的
}
else {
dup++; // 累计重复的
}
map[row][col] = ALIVE;// 让他生
}
else {
bad++;// 累计错误的
}
fscanf(fp,"%d %d",&row, &col);// 读下一个
}
// Get number of generations to be simulated
fscanf(fp,"%d", gens);// 0 0后面是要繁衍的代数
// Get region bounds for final print
fscanf(fp,"%d %d %d %d",&(c1->row), &(c1->col), &(c2->row), &(c2->col));// 再后面是输出的范围
printf("Done.\n %d Cells created",good);// 打印读到的东西
...
}