2D捕食者 - 食饵模拟 [英] 2D Predator–Prey Simulation

查看:64
本文介绍了2D捕食者 - 食饵模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个模拟中,猎物是蚂蚁,掠食者是doodlebugs。这些生物生活在20 * 20格的细胞中。一次只能有一个生物占据一个细胞。网格是封闭的,所以不允许小动物从世界的边缘移动
。时间按步骤模拟。每个生物每一步都会执行一些动作。

蚂蚁的行为符合以下模式:

•移动:对于每个时间步,蚂蚁随机尝试向上移动,向下,向左或向右。如果所选方向上的相邻

单元格被占用或者将蚂蚁从网格移开,那么蚂蚁将停留在

当前单元格中。

•品种:如果蚂蚁在三个时间步中存活,在时间步的末尾(即移动后),蚂蚁将会产生b $ b品种。这是通过在相邻(向上,向下,向左或向右)单元格中创建一个新的蚂蚁来模拟的,该单元格是空的。如果没有可用的空细胞,则不会发生繁殖。一旦后代产生,蚂蚁

不能再产生一个后代,直到它经过三个以上的时间步。

doodlebugs的行为符合以下模式:

•移动。对于每一个步骤,doodlebug将移动到包含蚂蚁的相邻细胞并吃掉

蚂蚁。如果相邻细胞中没有蚂蚁,则doodlebug按照与

蚂蚁相同的规则移动。请注意,doodlebug不能吃其他doodlebug。

•品种。如果一个doodlebug存活了八个时间步,在时间步结束时它会以与蚂蚁相同的方式产生一个新的

doodlebug。

•饿死。如果一个doodlebug没有在三个步骤内吃掉一只蚂蚁,那么在第三步结束时它将会饿死并死亡。然后将doodlebug从细胞网格中移除。

在一个回合中,所有的doodlebugs应该在蚂蚁之前移动。





我几乎完成了整个代码,但有一些我无法找到的错误请帮助我

In this simulation, the prey are ants and the predators are doodlebugs. These creatures live in a 20 * 20 grid
of cells. Only one creature may occupy a cell at a time. The grid is enclosed, so a critter is not allowed to move
off the edges of the world. Time is simulated in steps. Each creature performs some action every time step.
The ants behave according to the following model:
• Move: For every time step, the ants randomly try to move up, down, left, or right. If the neighboring
cell in the selected direction is occupied or would move the ant off the grid, then the ant stays in the
current cell.
• Breed: If an ant survives for three time steps, at the end of the time step (i.e., after moving) the ant will
breed. This is simulated by creating a new ant in an adjacent (up, down, left, or right) cell that is
empty. If there is no empty cell available, no breeding occurs. Once an offspring is produced, an ant
cannot produce an offspring again until it has survived three more time steps.
The doodlebugs behave according to the following model:
• Move. For every time step, the doodlebug will move to an adjacent cell containing an ant and eat the
ant. If there are no ants in adjoining cells, the doodlebug moves according to the same rules as the
ant. Note that a doodlebug cannot eat other doodlebugs.
• Breed. If a doodlebug survives for eight time steps, at the end of the time step it will spawn off a new
doodlebug in the same manner as the ant.
• Starve. If a doodlebug has not eaten an ant within three time steps, at the end of the third time step it
will starve and die. The doodlebug should then be removed from the grid of cells.
During one turn, all the doodlebugs should move before the ants.


I make the whole code almost but there are some errers which i am unable to find please help me

推荐答案

for(int i=0;i<acount;i++)
    {
     do {
      m=rand()%20;
     n=rand()%20;
       }while(arr[m][n]!=0);
     a[i].birth(m,n);
    }
   for(int i=0;i<dcount;i++)
    {
     do{
     m=rand()%20;
    n=rand()%20;
       }while(arr[m][n]!=0);
     d[i].birth(m,n);
    }
    do{
    system("cls");
    display();

    for(int i=0;i<dcount;i++)
    {
        if(d[i].getstep()==8){
            d[i].setstep(0);
            //dcount++;
            d[dcount+1].breed(d[i].getX(),d[i].getY());
        }

        d[i].move();
    }
    system("cls");
    display();
    system("pause");

    for(int i=0;i<acount;i++)
    {
        if(a[i].getstep()==3){
            a[i].setstep(0);
            //acount++;
            a[acount+1].breed(a[i].getX(),a[i].getY());
        }
        a[i].move();

    }
    cout<<"Ant count : "<<acount<<endl;
    cout<<"Doodlebug count : "<<dcount<<endl;
    cout<<"Enter y For Next Step : ";
    cin>>ch;
    }while(ch =='y');
    system("pause");
    return 0;
}


这篇关于2D捕食者 - 食饵模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆