Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,222 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,243 people online right now. Registration is fast and FREE... Join Now!




Simulation help

 
Reply to this topicStart new topic

Simulation help, Ants and Doodlebugs

ibaraku
2 Dec, 2007 - 11:03 AM
Post #1

D.I.C Head
Group Icon

Joined: 12 May, 2007
Posts: 176



Thanked: 2 times
My Contributions
I couple of days ago I came across an Ants and Doodlebugs simulator here on DIC, The author said he had a couple of errors but didn;t state what it was, so i tried to do it, but couldn't get it to work, the project compiles, but when I try to build it, it give me 4 errors
[quote]
--------------------Configuration: main - Win32 Debug--------------------
Linking...
main.obj : error LNK2001: unresolved external symbol "public: void __thiscall World::step(void)" (?step@World@@QAEXXZ)
main.obj : error LNK2001: unresolved external symbol "public: void __thiscall World::display(void)" (?display@World@@QAEXXZ)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall World::World(void)" (??0World@@QAE@XZ)
Debug/main.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.

main.exe - 4 error(s), 0 warning(s)
[\quote]
any help is appreciated



here's .cpp
[code]

//*******************CONSTRUCTORS!!!***********************************
Organism::Organism(int posX, int posY, Organism *array[][20])
:level(0), x(posX), y(posY), moved(false){
//Deliberately Empty
}//End Constructor

Ant::Ant(int posX, int posY, Organism *array[][20])
:Organism(posX, posY, array){
//Deliberitaly empty
}//End Constructor

Doodlebugs::Doodlebugs(int posX, int posY, Organism *array[][20])
:Organism(posX, posY, array){
//Deliberately Empty
}//End Constructor


//construct grid
World::World(){
for(int i=0; i<=19; i++){
for(int j=0; j<=19; j++){
array[i][j]=NULL;
}
}
fill();
}//end world


//*****************END CONSTRUCTORS!!!!*******************************

//fill the world with ants and doodlebugs
void World::fill(){
int moveX, moveY;
srand(time(NULL));
for(int NoDB=0; NoDB<=5; NoDB++){//NoDB=number of dooodlebugs
do{
moveX=rand()%20;
moveY=rand()%20;
}while(array[moveX][moveY]!=NULL);
array[moveX][moveY]=new Doodlebugs(moveX, moveY, array);
}//end for
for(int NoAnt=0; NoAnt<=100; NoAnt++){
do{
moveX=rand()%20;
moveY=rand()%20;
}while(array[moveX][moveY]!=NULL);
array[moveX][moveY]=new Ant(moveX, moveY, array);
}//end for

}//end World::fill()

void World::moveReset(){
for(int i=0; i<=19; i++){
for(int j=0; j<=19; j++){
if(array[i][j]!=NULL){
array[i][j]->moved=false;
}//end if
}//end for
}//end for
}//end moveReset

void World::display(){
for(int i=0; i<=19; i++){
for(int j=0; j<=19; j++){
if(array[i][j]!=NULL){
cout<<array[i][j]->getSymbol();
}
else{
cout << " ";
}

}
cout<<endl;
}
//cout << "Displayed" << endl;
}



void World::step(){
for(int i=0; i<=19; i++){
for(int j=0; j<=19; j++){
if(array[i][j] != NULL && array[i][j]->getSymbol() == 'X'){
array[i][j]->move();
}
}
}
for(int a=0; a<=19; a++){
for(int b=0; b<=19; b++){
if(array[a][b]!=NULL && array[a][b]->getSymbol() == 'O'){
array[a][b]->move();
}
}
}
display();
moveReset();
//cout << "Stepped" << endl;
}
//End World creation and population




//Start of move
void Ant::move(){
cout<<"Ant moved:\n";
cout<<x<<" "<<y<<endl;
int changeX, changeY;
level++;
srand(time(NULL));

do{
changeX=rand()%3-1;
changeY=rand()%3-1;
}while(changeX!=changeY);
if (x + changeX <= 19 && x + changeX>=0
&& y + changeY <= 19 && y + changeY>=0
&& changeX!=-changeY
&& world[x+changeX][y+changeY]==NULL
&& moved == false)
{
world[x + changeX][y + changeY] = world[x][y];
world[x][y] = NULL;
x = x + changeX;
y = y + changeY;
}
if (level >= 3){
breed();
}
moved = true;
}//end Ant::move


//Start breeding ants after 3 moves
void Ant::breed(){
cout<<"Ants breed after 3 moves...\n";
for(int i=-1; i<2; i++){
for(int j=-1; j<2; j++){
if (x+i<=19 && y+j<=19
&& x+i>=0 && y+j>=0
&& i!=j && i!=-j
&& world[x+i][y+j]==NULL){
world[x+i][y+j]=new Ant((x+i), (y+j), world);
level=0;
i=2;
j = 2;
}//end if
}//end for
}//end for
}//end breed()


//Ants represented by O
char Ant::getSymbol(){
return 'O';
}

void Doodlebugs::move(){
cout<<"Moving Doodlebugs\n";
cout<<x<<" "<<y<<endl;
int changeX, changeY;//the change in axis
level++;
hunger++;//after 3 moves the doodlebug will starve

bool hasMoved=false;
for(int i=-1; i<2; i++){
for(int j=-1; j<2; j++){
if (x+i >= 0 && y+j >= 0
&& x+i<=19 && y+j<=19
&& i!=j && i!=-j
&& world[x+i][y+j]!=NULL
&& world[x+i][y+j]->getSymbol()=='O'
&& moved == false)
{
delete world[x+i][y+j];
world[x+i][y+j]=world[x][y];
world[x][y]=NULL;
x=x+i;
y=y+j;
i=2;
j=2;
hunger=0;
hasMoved=true;
}
}
}
srand(time(NULL));
do{
changeX = rand() % 3 - 1;
changeY = rand() % 3 - 1;
}while(changeX!=changeY);

if(x+changeX<=19 && x+changeX>=0
&& y+changeY<=19 && y+changeY>=0
&& changeX!=-changeY
&& world[x+changeX][y+changeY]==NULL
&& hasMoved==false
&& moved==false){

world[x + changeX][y + changeY] = world[x][y];
world[x][y] = NULL;
x = x + changeX;
y = y + changeY;
}//end if

if (hunger >= 3){//if doodlebug hasn't eaten after 3 time steps, starve
starve();
}//end if
else if (level >= 8){//afetr 8 time steps. breed
breed();
}//end if
moved = true;
}//End doodlebug move


void Doodlebugs::breed(){
cout << "Doodlebug breeding..."<<endl;
for(int i=-1; i<2; i++){
for(int j=-1; j<2; j++){
if(x+i<=19 && y+j<=19
&& x+i>=0 && y+j>=0
&& i!=j && i!=-j
&& world[x+i][y+j]==NULL)
{
world[x+i][y+j]=new Doodlebugs((x+i), (y+j), world);
level=0;
i=2;
j=2;
}//end if
}//end for
}//end for
}//End doodlebug breed


void Doodlebugs::starve(){
cout << "Doodlebug starved" << endl;
delete world[x][y];
world[x][y]==NULL;
}

char Doodlebugs::getSymbol()
{
return 'X';
}
[\code]

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 05:53AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month