2 zeroyetj zeroyetj 于 2015.06.02 14:30 提问

C++课程设计 求大神帮忙写下构造函数和析构函数

1.网格世界类
网格中每个元素存放各种生物对象的指针或者为空。模拟过程中,我们需要移动生物,还有繁殖和饥饿(死亡),所以在网格类中,我们可以将一只生物放到网格中;可以读取网格中的某个指定位置的生物,获取它的指针,以便在每一个time step中对它进行处理(移动、繁殖和饥饿死亡)。在一个time step中,网格中每只生物都要处理一遍,先狮蚁,后蚂蚁。另外,应该有一个显示网格的成员函数。
2.有机生物类
生物要能够放到网格中,所以每一只生物都有相关的函数。可以是构造函数,即构造生物的时候,同时指明放到网格的哪一个位置上。
有Move函数,Breed函数,Starve函数(是否饿死)。这些函数在派生类中有派生类自己的实现。所以,在有机生物类中,这些函数应该是纯虚函数,有机生物类是一个抽象类。
网格世界类中,从某个网格位置读取生物并处理时,需要知道是哪一种类型的生物(是狮蚁还是蚂蚁),所以,有机生物类里应该考虑如何返回生物的类型。简单的办法是,用不同的常量来表示狮蚁和蚂蚁。例如,用整数1表示狮蚁,2表示蚂蚁。在有机生物类中定义纯虚函数GetType,返回生物的具体类型。
3.蚂蚁类
实现Move函数,Breed函数,Starve函数和GetType函数。
4.狮蚁类
实现Move函数,Breed函数,Starve函数和GetType函数。
(二)细化:
1.网格世界类:
(1)属性。数据结构:网格世界中如何存放蚂蚁或狮蚁对象?
20*20的网格,用一个20*20的二维数组来表示,数组元素是什么类型?
(2)方法。网格世界类应该有哪些成员函数?
构造函数:初始化网格。
析构函数:释放掉所有生物。
Set函数:指定x,y位置(二维数组的下标),以及一只生物的地址(指针),将这只生物存入网格中。
Get函数:获取网格中x,y坐标上的生物的地址(指针)。
OneStep函数:在一个time step中,网格中每只生物都要处理一遍,包括移动、繁殖和饥饿。首先,把网格中的每一只生物在本轮是否移动过的标记moved先都标记为假(为什么?);其次,移动,先狮蚁,后蚂蚁,同时把该生物的moved标记为真;再次,把饥饿的狮蚁消灭掉;最后,繁殖。
Display函数:显示函数,显示网格中的具体情况。每个网格,如果是空,显示”.”;如果是蚂蚁,显示”o”;如果是狮蚁,显示”x”。
2.有机生物类
(1)属性
要记录每只生物的一些基本属性:属于哪个网格对象,具体在哪个位置上(x,y坐标),本轮是否移动过。另外,为了记录是否到了繁殖时间,要有一个跟踪器,即记录上次繁殖之后,又经历了多少次time step。
(2)方法
构造函数:
带参数的构造函数:指定网格对象和x、y位置,把构造的生物放到网格中。
析构函数:
Move函数,Breed函数,Starve函数和GetType函数为纯虚函数。
3.蚂蚁类
(1)属性
不需要再添加属性。
(2)方法
构造函数:
带参数的构造函数:指定网格对象和x、y位置,把构造的蚂蚁放到网格中。
析构函数:
Move函数:随机选择一个方向,看是否能移动,否则保持在原位置。
Breed函数:繁殖跟踪器+1.如果是3的倍数,就需要繁殖,同时跟踪器清零。
Starve函数:本模拟中蚂蚁不会饿死,所以仅返回false值即可。
GetType函数:返回蚂蚁的类型标记。
4.狮蚁类
(1)属性
狮蚁会饿死,需要添加一个属性,跟踪记录狮蚁饥饿了多少次time step。
(2)方法
构造函数:
带参数的构造函数:指定网格对象和x、y位置,把构造的狮蚁放到网格中。
析构函数:
Move函数:若有相邻蚂蚁,移动到单元网格,吃掉蚂蚁;否则,随机选择一个方向,看是否能移动,不能移动则保持在原位置。
Breed函数:繁殖跟踪器+1,如果是8的倍数,就需要繁殖,同时跟踪器清零。
Starve函数:饥饿跟踪器+1,如果是3的倍数,则饥饿并死亡,从网格中拿掉该狮蚁,同时跟踪器清零。
GetType函数:返回狮蚁的类型标记。
四、其它
(一)所有涉及的常量定义为类型常量。如:
const int ANTBREED = 3; //蚂蚁的繁殖周期为3个time steps
const int DOODLEBREED = 8; //狮蚁的繁殖周期为8个time steps
初始化网格世界时,用的狮蚁只数和蚂蚁只数,分别为5只和100只,也定义为类型常量。如:
const int INITIALANTS = 100;
const int INITIALBUGS = 5;

5个回答

gamefinity
gamefinity   Rxr 2015.06.02 14:35
已采纳
  • 你是第三个在这里问这个的了。以前的问题找一找就有答案
  • SOURCE.H

#ifndef __SOURCE_H__
#define __SOURCE_H__

struct Step
{
    int dx;
    int dy;
};

enum AnimalType
{
    IsAnt,
    IsDoodlebug
};

class Animal;
class Cell
{
public:
    int x;
    int y;
    Animal *animal;

public:
    Cell(int x0, int y0);
    Cell *nearbyCell(int dir);
    bool in(Animal *animal0);
    Animal *leave();
};

class Animal
{
protected:
    int maxBreedTime;
    int breedTime;
    int maxStarveTime;
    int starveTime;
public:
    AnimalType type;
    Animal *next;
    Animal *prior;
public:
    Animal();
    virtual ~Animal();

public://virtual
    virtual Animal *breedChild() = 0;
    virtual bool canEat(AnimalType animal) = 0;
    virtual bool eat() = 0;

public:
    Cell *cell;
    bool breed();
    bool move();
    void step();
};

class Ant :public Animal
{
public:
    Ant() ;
public:
    virtual Animal *breedChild();
    virtual bool canEat(AnimalType t);
    virtual bool eat();
};

class Doodlebug :public Animal
{
public:
    Doodlebug() ;
public:
    virtual Animal *breedChild();
    virtual bool canEat(AnimalType t);
    virtual bool eat();
};


Step Steps[4] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
int maxWidth;
int maxHeight;
Cell ***Status;
Animal *DoodleBugs;
Animal *Ants;

#endif //__SOURCE_H__

SOURCE.CPP


#include <stdio.h>
#include <stdlib.h>
#include "Source.h"

//========================================
//
// Cell
//
Cell::Cell(int x0, int y0)
{
    this->x = x0;
    this->y = y0;
    this->animal = NULL;
}
Cell *Cell::nearbyCell(int dir)
{
    int x0 = this->x + Steps[dir].dx;
    int y0 = this->y + Steps[dir].dy;
    if (x0 < 0 || y0 < 0 || x0 >= maxWidth || y0 >= maxHeight)
        return NULL;
    else
    {
        return Status[x0][y0];
    }
}
bool Cell::in(Animal *animal0)
{
    if (this->animal != NULL)
        return false;
    else
    {
        animal0->cell = this;
        this->animal = animal0;
        return true;
    }
}
Animal *Cell::leave()
{
    Animal *theAnimal = this->animal;
    theAnimal->cell = NULL;
    this->animal = NULL;
    return theAnimal;
}
//========================================
//
// Animal
//
Animal::Animal()
{
    this->cell = NULL;
    this->breedTime = 0;
    this->starveTime = 0;

    this->prior = NULL;
    this->next = NULL;
}
bool Animal::breed()
{
    if (this->breedTime >= maxBreedTime)
    {
        for (int i = 0; i < 4; i++)
        {
            Cell *c = this->cell->nearbyCell(i);
            if (c != NULL && c->animal == NULL)
            {
                c->in(this->breedChild());
                this->breedTime = 0;
                return true;
            }
        }
    }
    return false;
}
bool Animal::move()
{
    int dir = rand() % 4;
    Cell *c = this->cell->nearbyCell(dir);
    if (c == NULL)
        return false;
    else if (c->animal == NULL)
    {
        c->in(this->cell->leave());
        return true;
    }
}
void Animal::step()
{
    bool dosth = false;
    this->breedTime++;
    this->starveTime++;

    dosth |= this->breed();
    dosth |= this->eat();
    if (!dosth)
    {
        this->move();
    }

    if (this->maxStarveTime > 0 && this->starveTime >= this->maxStarveTime)
    {
        //starve
        this->cell->leave();
        delete (this);
    }
}
Animal::~Animal()
{
    Animal *t = this->next;
    this->prior->next = t;
    if (t != NULL)
        t->prior = this->prior;
}
//========================================
//
// Ant
//
Ant::Ant() : Animal()
{
    this->type = IsAnt;
    this->maxStarveTime = -1;
    this->maxBreedTime = 3;

    if (Ants != NULL)
    {
        this->prior = Ants;
        this->next = Ants->next;
        if (Ants->next != NULL) Ants->next->prior = this;
        Ants->next = this;
    }
}
Animal *Ant::breedChild()
{
    return new Ant();
}
bool Ant::canEat(AnimalType t)
{
    return false;
}
bool Ant::eat()
{
    return false;
}
//========================================
//
// Doodlebug
//
Doodlebug::Doodlebug() :Animal()
{
    this->type = IsDoodlebug;
    this->maxStarveTime = 3;
    this->maxBreedTime = 8;

    if (DoodleBugs != NULL)
    {
        this->prior = DoodleBugs;
        this->next = DoodleBugs->next;
        if (DoodleBugs->next != NULL) DoodleBugs->next->prior = this;
        DoodleBugs->next = this;
    }
}
Animal *    Doodlebug::breedChild()
{
    return new Doodlebug();
}
bool    Doodlebug::canEat(AnimalType t)
{
    if (t == IsAnt)
        return true;
    else
        return false;
}
bool    Doodlebug::eat()
{
    for (int i = 0; i < 4; i++)
    {
        Cell *c = this->cell->nearbyCell(i);
        if (c!=NULL && c->animal != NULL && this->canEat(c->animal->type))
        {
            Animal *theAnimal = c->leave();
            delete(theAnimal);
            this->starveTime = 0;
            return true;
        }
    }
    return false;
}
//========================================
//
// main
//
void randomSet(Animal *animal0)
{
    srand(0);
    int x;
    int y;
    do
    {
        x = rand() % maxWidth;
        y = rand() % maxHeight;
    }
    while (Status[x][y]->animal != NULL);
    Status[x][y]->in(animal0);
}
void  printoutHead()
{
    printf("+");
    for (int i = 0; i < maxWidth; i++)
    {
        printf("=");
    }
    printf("+\n");
}
void printoutDetail(int r)
{
    printf("|");
    for (int i = 0; i < maxWidth; i++)
    {
        if (Status[i][r]->animal == NULL)
            printf(" ");
        else
        {
            switch (Status[i][r]->animal->type)
            {
            case IsAnt:
                printf("O");
                break;
            case IsDoodlebug:
                printf("X");
                break;
            default:
                printf("?");
                break;
            }
        }
    }
    printf("|\n");
}
void printout()
{
    printoutHead();
    for (int i = 0; i < maxHeight; i++)
        printoutDetail(i);
    printoutHead();
}
void main()
{
    int nDoodleBug;
    int nAnt;

    printf("请输入区域宽度:");
    scanf("%d", &maxWidth);
    printf("请输入区域高度:");
    scanf("%d", &maxHeight);
    printf("请输入初始狮蚁数量:");
    scanf("%d", &nDoodleBug);
    printf("请输入初始蚂蚁数量:");
    scanf("%d", &nAnt);

    //maxWidth = 3;
    //maxHeight = 4;
    //nDoodleBug = 2;
    //nAnt = 5;

    Status = new Cell**[maxWidth];
    DoodleBugs = new Doodlebug();
    Ants = new Ant();

    for (int i = 0; i < maxWidth; i++)
    {
        Status[i] = new Cell*[maxHeight];
        for (int j = 0; j < maxHeight; j++)
        {
            Status[i][j] = new Cell(i, j);
        }
    }

    for (int i = 0; i < nDoodleBug; i++)
    {
        randomSet(new Doodlebug());
    }
    for (int i = 0; i < nAnt; i++)
    {
        randomSet(new Ant());
    }

    printout();
    system("pause");
    while (true)
    {
        Animal *a = DoodleBugs->next;
        Animal *a0;
        for ( a0 = (a == NULL ? NULL : a->next); a != NULL; a = a0, a0 = (a == NULL ? NULL : a->next))
            a->step();
        a = Ants->next;
        for ( a0 = (a == NULL ? NULL : a->next); a != NULL; a = a0, a0 = (a == NULL ? NULL : a->next))
            a->step();
        printout();

        system("pause");
    }
}
zeroyetj
zeroyetj 好的
2 年多之前 回复
gamefinity
gamefinity 回复zeroyetj: 邮件:gamefinity@163.com
2 年多之前 回复
gamefinity
gamefinity 邮件:gamefinity@163.com
2 年多之前 回复
zeroyetj
zeroyetj 你用QQ吗?我想让你帮忙看下我的代码哪里不对,这老师基本已经失踪了
2 年多之前 回复
gamefinity
gamefinity 回复zeroyetj: 你说的网格世界类就是我的Cell类,你说的有机动物类就是我的Animal类
2 年多之前 回复
zeroyetj
zeroyetj 谢谢,你的代码其实之前我就看过好多次了,功能实现我没什么大问题,就是这老师非要我们照着她的模式来,很无语啊
2 年多之前 回复
wsh1992o
wsh1992o   Rxr 2015.06.02 15:07

这么多东西跟构造函数有多大的关系?你是想让人帮你写作业吧?

zeroyetj
zeroyetj 没有啊,我之前写了一个不用指针的东西,老师不要,出了这个要求,所以其他函数我都知道怎么写,就不知道构造函数和析构,感觉指针总出不来,没有伸手的意思
2 年多之前 回复
monaso
monaso   2015.06.02 17:40

#ifndef SOURCE_H
#define SOURCE_H

struct Step
{
int dx;
int dy;
};

enum AnimalType
{
IsAnt,
IsDoodlebug
};

class Animal;
class Cell
{
public:
int x;
int y;
Animal *animal;

public:
Cell(int x0, int y0);
Cell *nearbyCell(int dir);
bool in(Animal *animal0);
Animal *leave();
};

class Animal
{
protected:
int maxBreedTime;
int breedTime;
int maxStarveTime;
int starveTime;
public:
AnimalType type;
Animal *next;
Animal *prior;
public:
Animal();
virtual ~Animal();

public://virtual
virtual Animal *breedChild() = 0;
virtual bool canEat(AnimalType animal) = 0;
virtual bool eat() = 0;

public:
Cell *cell;
bool breed();
bool move();
void step();
};

class Ant :public Animal
{
public:
Ant() ;
public:
virtual Animal *breedChild();
virtual bool canEat(AnimalType t);
virtual bool eat();
};

class Doodlebug :public Animal
{
public:
Doodlebug() ;
public:
virtual Animal *breedChild();
virtual bool canEat(AnimalType t);
virtual bool eat();
};

Step Steps[4] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
int maxWidth;
int maxHeight;
Cell ***Status;
Animal *DoodleBugs;
Animal *Ants;

#endif //__SOURCE_H__

SOURCE.CPP

#include
#include
#include "Source.h"

//========================================
//
// Cell
//
Cell::Cell(int x0, int y0)
{
this->x = x0;
this->y = y0;
this->animal = NULL;
}
Cell *Cell::nearbyCell(int dir)
{
int x0 = this->x + Steps[dir].dx;
int y0 = this->y + Steps[dir].dy;
if (x0 < 0 || y0 < 0 || x0 >= maxWidth || y0 >= maxHeight)
return NULL;
else
{
return Status[x0][y0];
}
}
bool Cell::in(Animal *animal0)
{
if (this->animal != NULL)
return false;
else
{
animal0->cell = this;
this->animal = animal0;
return true;
}
}
Animal *Cell::leave()
{
Animal *theAnimal = this->animal;
theAnimal->cell = NULL;
this->animal = NULL;
return theAnimal;
}
//========================================
//
// Animal
//
Animal::Animal()
{
this->cell = NULL;
this->breedTime = 0;
this->starveTime = 0;

this->prior = NULL;
this->next = NULL;

}
bool Animal::breed()
{
if (this->breedTime >= maxBreedTime)
{
for (int i = 0; i < 4; i++)
{
Cell *c = this->cell->nearbyCell(i);
if (c != NULL && c->animal == NULL)
{
c->in(this->breedChild());
this->breedTime = 0;
return true;
}
}
}
return false;
}
bool Animal::move()
{
int dir = rand() % 4;
Cell *c = this->cell->nearbyCell(dir);
if (c == NULL)
return false;
else if (c->animal == NULL)
{
c->in(this->cell->leave());
return true;
}
}
void Animal::step()
{
bool dosth = false;
this->breedTime++;
this->starveTime++;

dosth |= this->breed();
dosth |= this->eat();
if (!dosth)
{
    this->move();
}

if (this->maxStarveTime > 0 && this->starveTime >= this->maxStarveTime)
{
    //starve
    this->cell->leave();
    delete (this);
}

}
Animal::~Animal()
{
Animal *t = this->next;
this->prior->next = t;
if (t != NULL)
t->prior = this->prior;
}
//========================================
//
// Ant
//
Ant::Ant() : Animal()
{
this->type = IsAnt;
this->maxStarveTime = -1;
this->maxBreedTime = 3;

if (Ants != NULL)
{
    this->prior = Ants;
    this->next = Ants->next;
    if (Ants->next != NULL) Ants->next->prior = this;
    Ants->next = this;
}

}
Animal *Ant::breedChild()
{
return new Ant();
}
bool Ant::canEat(AnimalType t)
{
return false;
}
bool Ant::eat()
{
return false;
}
//========================================
//
// Doodlebug
//
Doodlebug::Doodlebug() :Animal()
{
this->type = IsDoodlebug;
this->maxStarveTime = 3;
this->maxBreedTime = 8;

if (DoodleBugs != NULL)
{
    this->prior = DoodleBugs;
    this->next = DoodleBugs->next;
    if (DoodleBugs->next != NULL) DoodleBugs->next->prior = this;
    DoodleBugs->next = this;
}

}
Animal * Doodlebug::breedChild()
{
return new Doodlebug();
}
bool Doodlebug::canEat(AnimalType t)
{
if (t == IsAnt)
return true;
else
return false;
}
bool Doodlebug::eat()
{
for (int i = 0; i < 4; i++)
{
Cell *c = this->cell->nearbyCell(i);
if (c!=NULL && c->animal != NULL && this->canEat(c->animal->type))
{
Animal *theAnimal = c->leave();
delete(theAnimal);
this->starveTime = 0;
return true;
}
}
return false;
}
//========================================
//
// main
//
void randomSet(Animal *animal0)
{
srand(0);
int x;
int y;
do
{
x = rand() % maxWidth;
y = rand() % maxHeight;
}
while (Status[x][y]->animal != NULL);
Status[x][y]->in(animal0);
}
void printoutHead()
{
printf("+");
for (int i = 0; i < maxWidth; i++)
{
printf("=");
}
printf("+\n");
}
void printoutDetail(int r)
{
printf("|");
for (int i = 0; i < maxWidth; i++)
{
if (Status[i][r]->animal == NULL)
printf(" ");
else
{
switch (Status[i][r]->animal->type)
{
case IsAnt:
printf("O");
break;
case IsDoodlebug:
printf("X");
break;
default:
printf("?");
break;
}
}
}
printf("|\n");
}
void printout()
{
printoutHead();
for (int i = 0; i < maxHeight; i++)
printoutDetail(i);
printoutHead();
}
void main()
{
int nDoodleBug;
int nAnt;

printf("请输入区域宽度:");
scanf("%d", &maxWidth);
printf("请输入区域高度:");
scanf("%d", &maxHeight);
printf("请输入初始狮蚁数量:");
scanf("%d", &nDoodleBug);
printf("请输入初始蚂蚁数量:");
scanf("%d", &nAnt);

//maxWidth = 3;
//maxHeight = 4;
//nDoodleBug = 2;
//nAnt = 5;

Status = new Cell**[maxWidth];
DoodleBugs = new Doodlebug();
Ants = new Ant();

for (int i = 0; i < maxWidth; i++)
{
    Status[i] = new Cell*[maxHeight];
    for (int j = 0; j < maxHeight; j++)
    {
        Status[i][j] = new Cell(i, j);
    }
}

for (int i = 0; i < nDoodleBug; i++)
{
    randomSet(new Doodlebug());
}
for (int i = 0; i < nAnt; i++)
{
    randomSet(new Ant());
}

printout();
system("pause");
while (true)
{
    Animal *a = DoodleBugs->next;
    Animal *a0;
    for ( a0 = (a == NULL ? NULL : a->next); a != NULL; a = a0, a0 = (a == NULL ? NULL : a->next))
        a->step();
    a = Ants->next;
    for ( a0 = (a == NULL ? NULL : a->next); a != NULL; a = a0, a0 = (a == NULL ? NULL : a->next))
        a->step();
    printout();

    system("pause");
}

}
好厉害

u012377333
u012377333   Rxr 2015.06.02 19:00

额!好多代码,看不下去了。

xy707707
xy707707   2015.06.02 22:04

#ifndef SOURCE_H
#define SOURCE_H

struct Step
{
int dx;
int dy;
};

enum AnimalType
{
IsAnt,
IsDoodlebug
};

class Animal;
class Cell
{
public:
int x;
int y;
Animal *animal;

public:
Cell(int x0, int y0);
Cell *nearbyCell(int dir);
bool in(Animal *animal0);
Animal *leave();
};

class Animal
{
protected:
int maxBreedTime;
int breedTime;
int maxStarveTime;
int starveTime;
public:
AnimalType type;
Animal *next;
Animal *prior;
public:
Animal();
virtual ~Animal();

public://virtual
virtual Animal *breedChild() = 0;
virtual bool canEat(AnimalType animal) = 0;
virtual bool eat() = 0;

public:
Cell *cell;
bool breed();
bool move();
void step();
};

class Ant :public Animal
{
public:
Ant() ;
public:
virtual Animal *breedChild();
virtual bool canEat(AnimalType t);
virtual bool eat();
};

class Doodlebug :public Animal
{
public:
Doodlebug() ;
public:
virtual Animal *breedChild();
virtual bool canEat(AnimalType t);
virtual bool eat();
};

Step Steps[4] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
int maxWidth;
int maxHeight;
Cell ***Status;
Animal *DoodleBugs;
Animal *Ants;

#endif //__SOURCE_H__

SOURCE.CPP

#include
#include
#include "Source.h"

//========================================
//
// Cell
//
Cell::Cell(int x0, int y0)
{
this->x = x0;
this->y = y0;
this->animal = NULL;
}
Cell *Cell::nearbyCell(int dir)
{
int x0 = this->x + Steps[dir].dx;
int y0 = this->y + Steps[dir].dy;
if (x0 < 0 || y0 < 0 || x0 >= maxWidth || y0 >= maxHeight)
return NULL;
else
{
return Status[x0][y0];
}
}
bool Cell::in(Animal *animal0)
{
if (this->animal != NULL)
return false;
else
{
animal0->cell = this;
this->animal = animal0;
return true;
}
}
Animal *Cell::leave()
{
Animal *theAnimal = this->animal;
theAnimal->cell = NULL;
this->animal = NULL;
return theAnimal;
}
//========================================
//
// Animal
//
Animal::Animal()
{
this->cell = NULL;
this->breedTime = 0;
this->starveTime = 0;

this->prior = NULL;
this->next = NULL;

}
bool Animal::breed()
{
if (this->breedTime >= maxBreedTime)
{
for (int i = 0; i < 4; i++)
{
Cell *c = this->cell->nearbyCell(i);
if (c != NULL && c->animal == NULL)
{
c->in(this->breedChild());
this->breedTime = 0;
return true;
}
}
}
return false;
}
bool Animal::move()
{
int dir = rand() % 4;
Cell *c = this->cell->nearbyCell(dir);
if (c == NULL)
return false;
else if (c->animal == NULL)
{
c->in(this->cell->leave());
return true;
}
}
void Animal::step()
{
bool dosth = false;
this->breedTime++;
this->starveTime++;

dosth |= this->breed();
dosth |= this->eat();
if (!dosth)
{
    this->move();
}

if (this->maxStarveTime > 0 && this->starveTime >= this->maxStarveTime)
{
    //starve
    this->cell->leave();
    delete (this);
}

}
Animal::~Animal()
{
Animal *t = this->next;
this->prior->next = t;
if (t != NULL)
t->prior = this->prior;
}
//========================================
//
// Ant
//
Ant::Ant() : Animal()
{
this->type = IsAnt;
this->maxStarveTime = -1;
this->maxBreedTime = 3;

if (Ants != NULL)
{
    this->prior = Ants;
    this->next = Ants->next;
    if (Ants->next != NULL) Ants->next->prior = this;
    Ants->next = this;
}

}
Animal *Ant::breedChild()
{
return new Ant();
}
bool Ant::canEat(AnimalType t)
{
return false;
}
bool Ant::eat()
{
return false;
}
//========================================
//
// Doodlebug
//
Doodlebug::Doodlebug() :Animal()
{
this->type = IsDoodlebug;
this->maxStarveTime = 3;
this->maxBreedTime = 8;

if (DoodleBugs != NULL)
{
    this->prior = DoodleBugs;
    this->next = DoodleBugs->next;
    if (DoodleBugs->next != NULL) DoodleBugs->next->prior = this;
    DoodleBugs->next = this;
}

}
Animal * Doodlebug::breedChild()
{
return new Doodlebug();
}
bool Doodlebug::canEat(AnimalType t)
{
if (t == IsAnt)
return true;
else
return false;
}
bool Doodlebug::eat()
{
for (int i = 0; i < 4; i++)
{
Cell *c = this->cell->nearbyCell(i);
if (c!=NULL && c->animal != NULL && this->canEat(c->animal->type))
{
Animal *theAnimal = c->leave();
delete(theAnimal);
this->starveTime = 0;
return true;
}
}
return false;
}
//========================================
//
// main
//
void randomSet(Animal *animal0)
{
srand(0);
int x;
int y;
do
{
x = rand() % maxWidth;
y = rand() % maxHeight;
}
while (Status[x][y]->animal != NULL);
Status[x][y]->in(animal0);
}
void printoutHead()
{
printf("+");
for (int i = 0; i < maxWidth; i++)
{
printf("=");
}
printf("+\n");
}
void printoutDetail(int r)
{
printf("|");
for (int i = 0; i < maxWidth; i++)
{
if (Status[i][r]->animal == NULL)
printf(" ");
else
{
switch (Status[i][r]->animal->type)
{
case IsAnt:
printf("O");
break;
case IsDoodlebug:
printf("X");
break;
default:
printf("?");
break;
}
}
}
printf("|\n");
}
void printout()
{
printoutHead();
for (int i = 0; i < maxHeight; i++)
printoutDetail(i);
printoutHead();
}
void main()
{
int nDoodleBug;
int nAnt;

printf("请输入区域宽度:");
scanf("%d", &maxWidth);
printf("请输入区域高度:");
scanf("%d", &maxHeight);
printf("请输入初始狮蚁数量:");
scanf("%d", &nDoodleBug);
printf("请输入初始蚂蚁数量:");
scanf("%d", &nAnt);

//maxWidth = 3;
//maxHeight = 4;
//nDoodleBug = 2;
//nAnt = 5;

Status = new Cell**[maxWidth];
DoodleBugs = new Doodlebug();
Ants = new Ant();

for (int i = 0; i < maxWidth; i++)
{
    Status[i] = new Cell*[maxHeight];
    for (int j = 0; j < maxHeight; j++)
    {
        Status[i][j] = new Cell(i, j);
    }
}

for (int i = 0; i < nDoodleBug; i++)
{
    randomSet(new Doodlebug());
}
for (int i = 0; i < nAnt; i++)
{
    randomSet(new Ant());
}

printout();
system("pause");
while (true)
{
    Animal *a = DoodleBugs->next;
    Animal *a0;
    for ( a0 = (a == NULL ? NULL : a->next); a != NULL; a = a0, a0 = (a == NULL ? NULL : a->next))
        a->step();
    a = Ants->next;
    for ( a0 = (a == NULL ? NULL : a->next); a != NULL; a = a0, a0 = (a == NULL ? NULL : a->next))
        a->step();
    printout();

    system("pause");
}

}

Csdn user default icon
上传中...
上传图片
插入图片