📄 functions.h
字号:
/*Initialize:initalize grid map.
Pre:None.
Post:All the cell in the grid map have been set to initial configuration of living cells.*/
void Initialize(Grid map)
{
int row,col; /*coordinates of a cell*/
printf("This program is a simulation of the game of life.\n "
"The grid has a size of %d rows and %d columns.\n",MAXROW,MAXCOL);
for(row=0;row<=MAXROW+1;row++)
for(col=0;col<=MAXCOL+1;col++)
map[row][col]=DEAD; /*Set all cells empty ,includeing the hedge.*/
printf("On each line give a pair of coordinates for a living cell.\n"
"Terminate the list with special pair 00.\n");
scanf("%d%d",&row,&col);
while(row!=0||col!=0) /*if you want to stop,input "0 0" */
{
if(row>=1&&row<=MAXROW&&col>=1&&col<=MAXCOL)
map[row][col]=ALIVE;
else
printf("values are not within range!\n");
scanf("%d%d",&row,&col);
}
while(getchar()!='\n')
;
}
/*Writemap:diplay grid map.
Pre: The retangular array map contains the current life configuration.
Post: The current life configuration is written for the user.*/
void Writemap(Grid map)
{
int row,col;
putchar('\n');
putchar('\n');
for(row=1;row<=MAXROW;row++)
{
for(col=1;col<=MAXCOL;col++)
{
if(col==MAXCOL+1)
printf("\n");
if(map[row][col]==ALIVE)
putchar('*');
else putchar('-');
}
putchar('\n');
}
}
/*Neighborcount:count neighbors of row,col.
Pre: The pair row ,col is a valid cell in a life configuration.
Post: The function returns the noumber of living neighbors of theliving cell. */
int NeighborCount(Grid map,int row,int col)
{
int i; /*row of neighbor of the cell(row,col)*/
int j; /*column of a neighbor of the cell(row,col)*/
int count=0; /*counter of living neighbors*/
for(i=row-1;i<=row+1;i++)
for(j=col-1;j<=col+1;j++)
if(map[i][j]==ALIVE)
count++;
if(map[row][col]==ALIVE)
count--;
return count;
}
/*Copymap: copy newmap into map.
Pre: The grid newmap has the current life configuration.
Post: The grid map has a copy. */
void Copymap(Grid map,Grid newmap)
{
int row,col;
for(row=0;row<=MAXROW+1;row++)
for(col=0;col<=MAXCOL+1;col++)
map[row][col]=newmap[row][col];
}
/*UserSaysYes: TRUE if the user wants to continue execution.
Pre: NONE.
Post: Returns TRUE if the user's answer begins with either y or Y ,FALSE if the user
respond with any response beginning with n or N.*/
Boolean UserSaysYes(void)
{
int c;
printf("y,n?");
do{
while((c=getchar())=='\n')
;
if(c=='y'||c=='Y'||c=='n'||c=='N')
return ((enum Boolean)(c=='y'||c=='Y'));
printf("Please respond by typing one of the letters 'y' or 'Y'\n");
}while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -