Lifegame1.h (C++代码)
#include<iostream>
#include<cstring>
using namespace std;
#define OCCUPIED 1
#define UNOCCUPIED 0
class GameOfLife
{
private:
char **cell;
int numOfRow;
int numOfCol;
int generations;
public:
GameOfLife(); //构造函数
virtual ~GameOfLife();
void Input();
void Show(int genNo) const;
void Game();
};
// 生命游戏类GameOfLife的实现部分
GameOfLife::GameOfLife()
// 操作结果:生成空游戏对象——构造函数
{
cell = NULL; // cell为空表示游戏对象
}
void GameOfLife::Input()
{
cout<<"请输入棋盘行数,列数,生命世代数:"<<endl;
cin>>numOfRow>>numOfCol>>generations;
while(cin.get()!='\n'); //start
if(numOfRow<=0)
{
cout<<"numOfRow只能为正!"<<endl;
exit(1);
}
if(numOfCol<=0)
{
cout<<"numOfCol只能为正!"<<endl;
exit(2);
}
if(generations<=0)
{
cout<<"generations只能为正!"<<endl;
exit(3);
} //end 防止输入异常值
if(cell!=NULL) //start
for (int row=0;row<numOfRow;row++)
delete []cell[row];
delete []cell;
cell=NULL;
cell=new char *[numOfRow];
int row,col;
for (row=0;row<numOfRow;row++)
cell[row]=new char[numOfCol];
for (row=0;row<numOfRow;row++)
for (int col=0;col<numOfCol;col++)
cell[row][col]=UNOCCUPIED; //end 初始化棋盘
char *line=new char [numOfCol+1]; //start
int maxRow=0;
int maxCol=0;
for (row=0;row<numOfRow;row++)
{
cin.getline(line,numOfCol+1);
if(strlen(line)==0) break;
for (col=0;col<strlen(line);col++)
{
if(line[col]!=' ')
cell[row][col]=OCCUPIED;
if(maxRow<row)maxRow=row;
if(maxCol<col)maxCol=col;
}
} //end 输入开始的状态
int rowGap=(numOfRow-maxRow)/2; //start
int colGap=(numOfCol-maxCol)/2;
for (row=maxRow;row>=0;row--)
{
for (col=maxCol;col>=0;col--)
{
cell[row+rowGap][col+colGap]=cell[row][col];
}
for (col=colGap-1;col>=0;col--)
cell[row+rowGap][col]=UNOCCUPIED;
}
for (row=rowGap-1;row>=0;row--)
for (int col=0;col<numOfCol;col++)
cell[row][col]=UNOCCUPIED;
delete []line;
} //end 将图案转移到棋盘中央
void GameOfLife::Game()
{
if(cell==NULL)
{
cout<<"游戏对象为空"<<endl;
return;
}
Show(0);
char **workCopy;
bool existLife=true;
bool isStable=false;
workCopy=new char*[numOfRow];
int row,col;
for (row=0;row<numOfRow;row++)
workCopy[row]=new char[numOfCol];
for (int gen=1;gen<=generations&&existLife&&!isStable;gen++)
{
for(row=0;row<numOfRow;row++)
for(col=0;col<numOfCol;col++)
workCopy[row][col];
existLife=false;
isStable=true;
for(row=0;row<numOfRow;row++)
{
for(col=0;col<numOfCol;col++)
{
int top=(row==0)?0:row-1;
int bottom=(row==numOfRow-1)?numOfRow-1:row+1;
int left=(col==0)?0:col-1;
int right=(col==numOfCol-1)?numOfCol-1:col+1;
int neighbores=0;
for (int curRow=top;curRow<=bottom;curRow++)
for(int curCol=left;curCol<=right;curCol++)
if((curRow!=row||curCol!=col)&&workCopy[curRow][curCol]==OCCUPIED)
neighbores++;
if(workCopy[row][col]==OCCUPIED)
{
if(neighbores==2||neighbores==3)
cell[row][col]=OCCUPIED;
else
cell[row][col]=UNOCCUPIED;
}
else
{
if(neighbores==3)cell[row][col]=OCCUPIED;
else cell[row][col]=UNOCCUPIED;
}
if(cell[row][col]==OCCUPIED)existLife=true;
if(cell[row][col]!=workCopy[row][col])isStable=false;
}
}
if(!existLife)cout<<"所有生命体都已死亡了!"<<endl;
else if(isStable)cout<<"系统已稳定!"<<endl;
else Show(gen);
}
for (row=0;row<numOfRow;row++)
delete []workCopy[row];
delete []workCopy;
}
GameOfLife::~GameOfLife()
// 操作结果:释放游戏所占用空间——析构函数
{
if (cell != NULL)
{ // 释放棋盘网格占用空间
for (int row = 0; row < numOfRow; row++)
delete []cell[row]; // 释放第row行
delete []cell; // 释放整个棋盘cell
cell = NULL;
}
}
void GameOfLife::Show(int genNo) const
// 操作结果:显示生命状态
{
if (genNo == 0) cout << "初始生命状态" << endl;
else cout << "第" << genNo << "代生命状态" << endl;
cout << "+";
int row, col; // 临时变量
for (col = 0; col < numOfCol; col++) cout << "-";
cout << "+" << endl;
for (row = 0; row < numOfRow; row++)
{ // 显示第i行
cout << "|";
for (col = 0; col < numOfCol; col++)
cout << ((cell[row][col] == OCCUPIED) ? "*" : " ");
cout << "|" << endl;
}
cout << "+";
for (col = 0; col < numOfCol; col++) cout << "-";
cout << "+" << endl;
system("PAUSE"); // 调用库函数system()
}
Lifegame.cpp (主函数文件的代码)
#include <iostream> // 标准流操作
#include <cstring> // 包含C函数strlen()的声明(string.h与cstring是C的头文件)
#include <cstdlib> // 包含C函数exit()及system()的声明(stdlib.h与cstdlib是C的头文件)
using namespace std; // 标准库包含在命名空间std中
#include "lifegame1.h" // 生命游戏
int main()
{
GameOfLife objGame; // 生命游戏对象
objGame.Input(); // 读入数据,空行按组合键Ctrl+Z结束输入
objGame.Game(); // 运行游戏
return 0; // 返回值0, 返回操作系统
}
数据结构与算法课本上的实例,大家可以看一下。