问题描述
康威生命游戏 连载版
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
/* speed */
#define VFAST 100
#define FAST 10
#define SLOW 1
/* array sizes */
#define NI 20
#define NJ 50
/* number of time steps */
#define NSTEPS 1000
int main(int argc,char* argv[])
{
clock_t t1,t2; // t1 = start,t2 = end
t1 = clock(); // time seen by my cpu
int speed = SLOW; // Generation speed
int i,j,n,im,ip,jm,jp,ni,nj,nsum,isum,oldisum = 0;
float x;
/* allocate arrays */
ni = NI + 2;
/* add 2 for left and right ghost cells */
nj = NJ + 2;
int(*oldMatrix)[NJ + 2] = new int[NI + 2][NJ + 2]; // oldmatrix = Current world
int(*newMatrix)[NJ + 2] = new int[NI + 2][NJ + 2]; // newmatrix = Updated world
/* initialize elements of old to 0 or 1 */
for (i = 1; i <= NI; i++) {
for (j = 1; j <= NJ; j++) {
x = rand() / ((float)RAND_MAX + 1); // x = value between 0-1
if (x < 0.25) { // 0.25 = 25% of cells are dead at the beginning = 1 OF THE RULES
oldMatrix[i][j] = 0;
}
else {
oldMatrix[i][j] = 1;
}
}
}
/* time steps */
for (n = 0; n < NSTEPS; n++) {
// Initialise ghost cells
/* corner boundary conditions */
oldMatrix[0][0] = oldMatrix[NI][NJ];
oldMatrix[0][NJ + 1] = oldMatrix[NI][1];
oldMatrix[NI + 1][NJ + 1] = oldMatrix[1][1];
oldMatrix[NI + 1][0] = oldMatrix[1][NJ];
/* left-right boundary conditions */
for (i = 1; i <= NI; i++) {
oldMatrix[i][0] = oldMatrix[i][NJ];
oldMatrix[i][NJ + 1] = oldMatrix[i][1];
}
/* top-bottom boundary conditions */
for (j = 1; j <= NJ; j++) {
oldMatrix[0][j] = oldMatrix[NI][j];
oldMatrix[NI + 1][j] = oldMatrix[1][j];
}
// Update each cells and put values in new generation array
for (i = 1; i <= NI; i++) {
for (j = 1; j <= NJ; j++) {
im = i - 1;
ip = i + 1;
jm = j - 1;
jp = j + 1;
nsum = oldMatrix[im][jp] + oldMatrix[i][jp] + oldMatrix[ip][jp]
+ oldMatrix[im][j] + oldMatrix[ip][j]
+ oldMatrix[im][jm] + oldMatrix[i][jm] + oldMatrix[ip][jm];
switch (nsum) {
case 3:
newMatrix[i][j] = 1;
break;
case 2:
newMatrix[i][j] = oldMatrix[i][j];
break;
default:
newMatrix[i][j] = 0;
}
}
}
// getting ready for next generation: copy new values in old array
isum = 0;
for (i = 1; i <= NI; i++) {
for (j = 1; j <= NJ; j++) {
/* copy new state into old state */
oldMatrix[i][j] = newMatrix[i][j];
/* Iterations are done; sum the number of live cells */
isum = isum + newMatrix[i][j];
}
}
// Screen display - NEED TO CHANGE SOMETHING HERE
float a = float(n + 1) / speed;
float b = floor(float(float(n + 1) / speed));
if ((isum == oldisum) || ((float(n + 1) / speed) == floor(float((n + 1) / speed)))) //stable state or speed
{
for (i = 1; i <= NI; i++) {
for (j = 1; j <= NJ; j++) {
if (newMatrix[i][j] == 0)
printf(".");
else
printf("X");
}
printf("\n");
}
printf("end of generation %d\n",n + 1);
system("pause");
}
oldisum = isum;
}
/* Iterations are done */
t2 = clock();
for (i = 1; i <= NI; i++) {
for (j = 1; j <= NJ; j++) {
if (newMatrix[i][j] == 0)
printf(".");
else
printf("X");
}
printf("\n");
}
printf("end of generation %d\n\n\n\n",n + 1);
printf("\nNumber of live cells = %d\n",isum);
float diff((float)t2 - (float)t1);
printf("nProcessing time %f\n",diff);
system("pause");
return 0;
}
这是需要放入案例1和案例2的代码,但我不确定如何实现它。如果有人可以帮助我,我将不胜感激。
// if the current cell is empty,check to
// see if a shark or a fish is born
if (*current == 0)
{
if ((fish >= 4 && (fish > sharks) && (fishbreed > 2))
(*uu) (i,j) = 1;
{
if ((sharks >= 4) && (sharks > fish) && (sharksbreed > 2))
(*uu) (i,j) = -1;
else
(*uu) (i,j) = (*current);
}
continue;
)
问题与规则:猎物/捕食者模拟模型
这里使用的猎物/捕食者模拟模型使用细胞 自动机。它处理海洋中的鱼类和鲨鱼种群, 它由一个单元格数组表示,每个单元格可以容纳 一条鱼或一条鲨鱼(但不能两者兼有)。
给定参数:
出生时,年龄=1 鱼类繁殖年龄>=2,鲨鱼繁殖年龄>=3, 最初,有随机分布的鱼群、鲨鱼群、 和遵循这些比例的空单元格:50% 的单元格是 被鱼占据 25% 被鲨鱼占据 25% 是空的
鱼和鲨鱼的行为受一套简单的规则支配 处理:繁殖老化突然死亡饥饿喂养 人口过剩
育种规则(仅适用于空单元格)对于给定的空单元格,
如果一个物种有 >= 4 个邻居,并且其中 >= 3 个属于 繁殖年龄,其他种类有
然后创建一个该类型的物种
鱼规则:对于包含一条鱼的给定单元格,
鱼可以存活 10 代 如果 >=5 邻居是鲨鱼,鱼就会死亡 (鲨鱼食物)如果8个邻居都是鱼,鱼就会死(人口过剩) 如果鱼没有死,增加年龄
鲨鱼规则:对于包含一条鱼的给定单元格,
鲨鱼可以存活 20 代 如果 >=6 邻居是鲨鱼,而鱼 邻居=0,然后鲨鱼死(饥饿)鲨鱼有 1/32 (.031) 由于随机原因死亡的几率 如果鲨鱼没有死亡, 增加年龄
注意:您可能希望使用以下方案对状态进行编码 一个单元格
值 >0 如果鱼是现值
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)