生命游戏
来源:互联网 发布:淘宝拍卖流程图 编辑:程序博客网 时间:2024/06/08 13:05
/*Name: The Game Of Life
Time:2014/10/4
Autor:Ashely
***
Tips: Run the Life program with the initial configurations shown in Figure 1.4 (7 instances).
For each instance, output the configuration for the 100 generations.
(the initial configuration corresponds to the 0-th generation)*/
#include<iostream>
using namespace std;
const int maxrow=30,maxcol=30;
//使用游戏说明
void instruction()
{
cout<<" ****************************************** "<<endl;
cout<<" ☆ "<<" Welcome to The Game Of Life!"<<" ☆"<<endl;
cout<<" ****************************************** "<<endl;
cout<<"Wish you a good time!"<<endl;
cout<<"This game uses a gird of size "<<maxrow<<" by "<<maxcol<<" in which"<<endl;
}
class Life
{
public:
void initialization();
void print();
void update();
private:
int grid[maxrow+2][maxcol+2];
int neighbor_count(int row,int col);
};
//设置初始配置
void Life::initialization()
{
int row,col;
for(row=0;row<maxrow+1;row++)
for(col=0;col<maxcol+1;col++)
grid[row][col]=0;
cout<<"Now,list the coordinates for living cells."<<endl;
cout<<"Tirminate the list with the special pair (-1,-1)"<<endl;
cin>>row>>col;
while(row!=-1||col!=-1)
{
if(row>=1&&row<maxrow&&col>=1&&col<maxcol)
grid[row][col]=1;
else
cout<<"Out of range!"<<endl;
cin>>row>>col;
}
}
//输出
void Life::print()
{
int row,col;
for(row=1;row<maxrow;row++)
{
cout<<"▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁";
cout<<"▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁"<<endl;
for(col=1;col<maxcol;col++)
{
cout<<"▏";
if(grid[row][col]==1)
cout<<"卐";
else cout<<" ";
}
cout<<endl;
}
cout<<endl;
}
int Life::neighbor_count(int row,int col)
{
int i,j;
int count=0;
for(i=row-1;i<=row+1;i++)
for(j=col-1;j<=col+1;col++)
count+=grid[i][j];
count-=grid[row][col];
return count;
}
void Life::update()
/*
Pre: The Life object contains a configuration.
Post: The Life object contains the next generation of configuration.
*/
{
int row, col;
int new_grid[maxrow + 2][maxcol + 2];
for (row = 1; row <= maxrow; row++)
for (col = 1; col <= maxcol; col++)
switch (neighbor_count(row, col)) {
case 2:
new_grid[row][col] = grid[row][col]; // Status stays the same.
break;
case 3:
new_grid[row][col] = 1; // Cell is now alive.
break;
default:
new_grid[row][col] = 0; // Cell is now dead.
}
for (row = 1; row <= maxrow; row++)
for (col = 1; col <= maxcol; col++)
grid[row][col] = new_grid[row][col];
}
/*void Life::update()
{
// for(int i=0;i<100;i++){//第一百代
int row,col;
int new_grid[maxrow+2][maxcol+2];
for(row=1;row<=maxrow;row++)
for(col=1;col<=maxcol;col++)
switch(neighbor_count(row,col))
{
case 2:
new_grid[row][col]=grid[row][col];//细胞保持原样
break;
case 3:
new_grid[row][col]=1;//细胞现在是活的
break;
default:
new_grid[row][col]=0;//其他情况细胞都是死的
}
for(row=1;row<=maxrow;row++)
for(col=1;col<=maxcol;col++)
grid[row][col]=new_grid[row][col];
// }
}*/
//_____________________________________________________________________________________//
//Life 游戏的主程序
int main()
{
instruction();//调用用户说明
for(int i=0;i<7;i++)
{
Life configuration;//创建life的一个对象
configuration.initialization();
configuration.print();
cout<<"\n"<<endl;
configuration.update();//死在这里面了
configuration.print();
cout<<"---------------------------------------------------------------"<<endl;
cout<<"---------------------------------------------------------------"<<endl;
cout<<endl;
}
return 0;
}
Time:2014/10/4
Autor:Ashely
***
Tips: Run the Life program with the initial configurations shown in Figure 1.4 (7 instances).
For each instance, output the configuration for the 100 generations.
(the initial configuration corresponds to the 0-th generation)*/
#include<iostream>
using namespace std;
const int maxrow=30,maxcol=30;
//使用游戏说明
void instruction()
{
cout<<" ****************************************** "<<endl;
cout<<" ☆ "<<" Welcome to The Game Of Life!"<<" ☆"<<endl;
cout<<" ****************************************** "<<endl;
cout<<"Wish you a good time!"<<endl;
cout<<"This game uses a gird of size "<<maxrow<<" by "<<maxcol<<" in which"<<endl;
}
class Life
{
public:
void initialization();
void print();
void update();
private:
int grid[maxrow+2][maxcol+2];
int neighbor_count(int row,int col);
};
//设置初始配置
void Life::initialization()
{
int row,col;
for(row=0;row<maxrow+1;row++)
for(col=0;col<maxcol+1;col++)
grid[row][col]=0;
cout<<"Now,list the coordinates for living cells."<<endl;
cout<<"Tirminate the list with the special pair (-1,-1)"<<endl;
cin>>row>>col;
while(row!=-1||col!=-1)
{
if(row>=1&&row<maxrow&&col>=1&&col<maxcol)
grid[row][col]=1;
else
cout<<"Out of range!"<<endl;
cin>>row>>col;
}
}
//输出
void Life::print()
{
int row,col;
for(row=1;row<maxrow;row++)
{
cout<<"▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁";
cout<<"▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁"<<endl;
for(col=1;col<maxcol;col++)
{
cout<<"▏";
if(grid[row][col]==1)
cout<<"卐";
else cout<<" ";
}
cout<<endl;
}
cout<<endl;
}
int Life::neighbor_count(int row,int col)
{
int i,j;
int count=0;
for(i=row-1;i<=row+1;i++)
for(j=col-1;j<=col+1;col++)
count+=grid[i][j];
count-=grid[row][col];
return count;
}
void Life::update()
/*
Pre: The Life object contains a configuration.
Post: The Life object contains the next generation of configuration.
*/
{
int row, col;
int new_grid[maxrow + 2][maxcol + 2];
for (row = 1; row <= maxrow; row++)
for (col = 1; col <= maxcol; col++)
switch (neighbor_count(row, col)) {
case 2:
new_grid[row][col] = grid[row][col]; // Status stays the same.
break;
case 3:
new_grid[row][col] = 1; // Cell is now alive.
break;
default:
new_grid[row][col] = 0; // Cell is now dead.
}
for (row = 1; row <= maxrow; row++)
for (col = 1; col <= maxcol; col++)
grid[row][col] = new_grid[row][col];
}
/*void Life::update()
{
// for(int i=0;i<100;i++){//第一百代
int row,col;
int new_grid[maxrow+2][maxcol+2];
for(row=1;row<=maxrow;row++)
for(col=1;col<=maxcol;col++)
switch(neighbor_count(row,col))
{
case 2:
new_grid[row][col]=grid[row][col];//细胞保持原样
break;
case 3:
new_grid[row][col]=1;//细胞现在是活的
break;
default:
new_grid[row][col]=0;//其他情况细胞都是死的
}
for(row=1;row<=maxrow;row++)
for(col=1;col<=maxcol;col++)
grid[row][col]=new_grid[row][col];
// }
}*/
//_____________________________________________________________________________________//
//Life 游戏的主程序
int main()
{
instruction();//调用用户说明
for(int i=0;i<7;i++)
{
Life configuration;//创建life的一个对象
configuration.initialization();
configuration.print();
cout<<"\n"<<endl;
configuration.update();//死在这里面了
configuration.print();
cout<<"---------------------------------------------------------------"<<endl;
cout<<"---------------------------------------------------------------"<<endl;
cout<<endl;
}
return 0;
}
0 0
- 生命游戏
- 生命游戏
- 生命游戏
- 生命游戏
- 生命游戏
- 生命游戏
- 生命游戏
- 生命游戏
- 生命游戏
- 生命游戏
- 生命游戏
- 游戏算法-生命游戏
- "生命游戏",VB
- 生命游戏 题解
- 【模拟】【RQNOJ】生命游戏
- QT 生命游戏模拟
- 算法思考--------生命游戏
- Algorithm Gossip:生命游戏
- c#中class和struct的区别
- 全局变量、局部变量、静态变量浅谈
- atoi()函数实现
- iOS学习之copy语法学习
- 面试题12:打印1到最大的n位数
- 生命游戏
- lnmp---环境搭建
- Java设计模式菜鸟系列(九)外观模式建模与实现
- Xcode快捷键盘
- cocos2d-x3.2与服务端框架Firefly的网络编程(初级网络通讯)
- 腾讯游戏分享汇:天天飞车六大研发经验
- c++中vector的用法详解
- 1014. Waiting in Line (30)
- XHTML基本语法规则