Welcome to Dream.In.Code
Getting C++ Help is Easy!

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




Trying to find the error..

 
Reply to this topicStart new topic

Trying to find the error.., Ants & Doodlebugs Program

gilpunflo
14 May, 2007 - 11:57 PM
Post #1

New D.I.C Head
*

Joined: 14 May, 2007
Posts: 1


My Contributions
I can't seem to find the error in my code.. yes I know it isn't documented yet but it's fairly straightforward and simple. I honestly can't figure out why it crashes when run, it doesn't crash in the same sequence or at the same call everytime and without a debugger I am having real issues figuring out my problem.

Thanks in advance for any helpful clues.

CODE

Simulation.h
#include <cstdlib>
#include <iostream>

using namespace std;

class Organism
{
      public:
             Organism();
             Organism(int posX, int posY, Organism *array[][20]);
             virtual void move() = 0;
             virtual void breed() = 0;
             virtual char getSymbol()= 0;
             int level, x, y;
             bool alreadyMoved;
             Organism* (*world)[20];
};

class Ant : public Organism
{
      public:
            Ant(int posX, int posY, Organism *array[][20]);
            ~Ant();
            virtual void move();
            virtual void breed();
            virtual char getSymbol();
};


class Doodlebug : public Organism
{
      public:
            Doodlebug(int posX, int posY, Organism *array[][20]);
            ~Doodlebug();
            virtual void move();
            virtual void breed();
            virtual char getSymbol();
            void starve();
            int hunger;
};

class Universe
{
      public:
            Universe();
            Organism* array[20][20];
            void display();
            void step();
            void fill();
            void moveReset();
            int doodlebug, ant;
};


CODE

Simulation.cpp

#include "Simulation.h"

Organism::Organism(int posX, int posY, Organism *array[][20])
: level(0), x(posX), y(posY), world(array), alreadyMoved(false)
{
}
Ant::Ant(int posX, int posY, Organism *array[][20])
: Organism(posX, posY, array)
{
}
Doodlebug::Doodlebug(int posX, int posY, Organism *array[][20])
: Organism(posX, posY, array)
{
}
void Ant::move()
{
    cout << "Ant move" << endl;
    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
        && alreadyMoved == false)
    {
        world[x + changeX][y + changeY] = world[x][y];
        world[x][y] = NULL;
        x = x + changeX;
        y = y + changeY;
    }
    if (level >= 3)
    {
        breed();
    }
    alreadyMoved = true;
}

void Ant::breed()
{
    cout << "Ant bred" << 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 Ant((x + i), (y + j), world);
                level = 0;
                i = 2;
                j = 2;
            }
        }
    }
}

char Ant::getSymbol()
{
    return '*';
}

void Doodlebug::move()
{
    cout << "Doodlebug move" << endl;
    cout << x << " " << y << endl;
    int changeX, changeY;
    level++;
    hunger++;
    
    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() == '*'
            && alreadyMoved == 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
        && alreadyMoved == false)
    {
                world[x + changeX][y + changeY] = world[x][y];
                world[x][y] = NULL;
                x = x + changeX;
                y = y + changeY;
    }
    
    if (hunger >= 3)
    {
        starve();
    }
    else if (level >= 8)
    {
        breed();
    }
    alreadyMoved = true;

}

void Doodlebug::breed()
{
    //cout << "Doodlebug bred" << 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 Doodlebug((x + i), (y + j), world);
                level = 0;
                i = 2;
                j = 2;
            }
        }
    }
}

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

char Doodlebug::getSymbol()
{
    return '@';
}

Universe::Universe()
{
    for (int i = 0; i <= 19; i++)
    {
         for (int j = 0; j <= 19; j++)
         {
            array[i][j] = NULL;
         }
    }
    fill();
}

void Universe::moveReset()
{
     for (int i = 0; i <= 19; i++)
     {
         for (int j = 0; j <= 19; j++)
         {
            if (array[i][j] != NULL)
            {
                array[i][j]->alreadyMoved = false;
            }
         }
     }
     //cout << "Move Reset" << endl;
}
void Universe::fill()
{
    int newX, newY;
    
    srand (time(NULL));
    for(int i = 0; i < 5; i++)
    {
        do
        {
            newX = rand() % 20;
            newY = rand() % 20;
        }while(array[newX][newY] != NULL);
        
        array[newX][newY] = new Doodlebug(newX, newY, array);
        
    }
    for(int i = 0; i < 100; i++)
    {
        do
        {
            newX = rand() % 20;
            newY = rand() % 20;
        }while(array[newX][newY] != NULL);
        
        array[newX][newY] = new Ant(newX, newY, array);
    }
    
    //cout << "Filled" << endl;
}
void Universe::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 Universe::step()
{
     for (int i = 0; i <= 19; i++)
     {
         for (int j = 0; j <= 19; j++)
         {
            if (array[i][j] != NULL && array[i][j]->getSymbol() == '@')
            {
                array[i][j]->move();
            }
         }
     }
    
     for (int i = 0; i <= 19; i++)
     {
         for (int j = 0; j <= 19; j++)
         {
            if (array[i][j] != NULL && array[i][j]->getSymbol() == '*')
            {
                array[i][j]->move();
            }
         }
     }
     display();
     moveReset();
     //cout << "Stepped" << endl;
}


CODE

main.cpp

#include <cstdlib>
#include <iostream>
#include <windows.h>
#include "Simulation.h"

using namespace std;

void clrscr2();

int main()
{
    Universe anthill;
    while(true)
    {
        //system("PAUSE");
        clrscr2();
        anthill.display();
        anthill.step();
        Sleep(1000);    //1000 ms = 1 second
    }

    system("PAUSE");
    return EXIT_SUCCESS;
}

void clrscr2()
{
  HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  COORD coord = {0, 0};
  DWORD count;

  CONSOLE_SCREEN_BUFFER_INFO csbi;
  GetConsoleScreenBufferInfo(hStdOut, &csbi);

  FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);

  SetConsoleCursorPosition(hStdOut, coord);
}

User is offlineProfile CardPM
+Quote Post

girasquid
RE: Trying To Find The Error..
15 May, 2007 - 12:30 PM
Post #2

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,266



Thanked: 14 times
Dream Kudos: 650
My Contributions
What exactly is going wrong? We can't help you without knowing what problem you're having exactly.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:33PM

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