Hey Cplusplus community.

I am a long time reader of these forum threads but this is my first actual post so please tell me if there is anything I should do differently in future.

I am posting about a problem I am having with one of my OOP c++ assignments.

I am trying to recreate an ASCII simulation of a modified version of Conways game of life.

In this version there are 3 main organisms, Bacteria, Creepers, and Creeperphages

In a nutshell I have generated a 2D grid of which I will also generate a number of the different organisms mentioned above. It is a typical predator vs prey setup where each organism has different traits.

Bacteria do not move
Creepers move in 4 directions, and eat Bacteria to stay alive

I have programmed quite a lot of the initial setup, however I am not sure how efficient it is and I would like to learn the most efficient techniques for using pointers, inheritance, structs, etc.

My main two problems come with the movement and the maps dimensions to make it dynamic.

First the movement, I am not sure how I can make it so that I can always check whether a maps x,y coordinates are occupied by a creature.

Second, I still have no easy way for my classes to be able to access the maps dimensions. So at the moment my simulation is set to a static dimension however I would like the user to be able to input the dimensions at the start and then for the Creeper class movement function to check whether it is still within the grid.

I have tried making the map into a singleton (Read up about that today) to no avail

Here is my code so far, really any help at all would be extremely valuable.

I believe I could probably fumble through and come up with a dirty solution in a couple of weeks which is what I always end up doing. So I thought this time I would ask for help and hopefully use an efficient coding technique to work this out.

[code]
//Main.cpp
  #include <iostream>
using std::cout;
using std::endl;

#include <vector>
using std::vector;

#include <typeinfo>
#include "time.h"
#include "Organism.h"
#include "WorldMap.h"
#include "Bacteria.h"
#include "Creeper.h"
int MapWidth = 10;
using namespace std;
WorldMap map;
int main()
{
    int noBacteria;
    int noCreeper;
    srand(time(0));

    map.setWidth(5);
    map.setHeight(6);



    //map.Initialise();
    map.initialiseMap();
    //map.PrintMap();
    //cout << "Maps dimensions are width:  "<< map.getWidth()<<" & height: "<<map.getHeight()<<"\n";
    cout << "How many Bacteria will there be?\n";
    cin >> noBacteria;
    //Would normally get the number of Bacteria here
    vector < Organism * > bacteria( noBacteria );


    for (int i=0;i<noBacteria;i++){
        bacteria[i] = new Bacteria(0,0,'B');
        map.setCoord(bacteria[i]->getCoordX(),bacteria[i]->getCoordY(),bacteria[i]->getIcon());
    }

    cout << "How many Creeper will there be?\n";
    cin >> noCreeper;
     vector < Organism * > creeper( noCreeper );

     for (int i=0;i<noCreeper;i++){
         creeper[i] = new Creeper(0,0,'C',10);
         map.setCoord(creeper[i]->getCoordX(),creeper[i]->getCoordY(),creeper[i]->getIcon());
     }


    //vector< Organism * > organism()
    map.PrintMap();

}
[/code]

[code]
//WorldMap.h

#ifndef WORLDMAP_H
#define WORLDMAP_H

// Include the relevant libraries
#include<iostream>
#include<cstdlib>
#include<cstdio>

#include "Organism.h"
using namespace std;//Use std namespace

class WorldMap
{
public:
    WorldMap();
    void setWidth(int);
    void setHeight(int);
    int getWidth();
    int getHeight();
    void PrintMap();
    void setCoord(int,int,char);
    void setIcon(char);
    char getIcon(int,int);
    void initialiseMap();
    char Map[100][100];



private:
    int _width;
    int _height;
    char _icon;
    int _CoordX;
    int _CoordY;

};

#endif // WORLDMAP_H
[/code]


[code]
//WorldMap.cpp

#include "WorldMap.h"

WorldMap::WorldMap()
{

    /*if (width<=0)
    {
        cout << "width cannot be negative, default width of 10 has been set";
        setWidth(10);
    }
    else
    {
        setWidth(width);
    }
    if (height<=0)
    {
        cout << "height cannot be negative, default height of 10 has been set";
        setHeight(10);
    }
    else
    {
        setHeight(height);
    }
    //Map.resize[width][height];*/
}



void WorldMap::setWidth(int width)
{
    _width = width;
}

int WorldMap::getWidth(){
    return _width;
}


void WorldMap::setHeight(int height)
{
    _height = height;
}

int WorldMap::getHeight(){
    return _height;
}

void WorldMap::PrintMap(){
    for (int i=0;i<=_height-1;i++)
    {
        for(int j=0;j<=_width-1;j++)
        {
            cout << Map[i][j];
        }
        cout << "\n";
    }
}


void WorldMap::setCoord(int CoordX,int CoordY, char icon)
{
    _CoordX=CoordX;
    _CoordY=CoordY;
    _icon = icon;
    Map[_CoordX][_CoordY] = icon;
}

void WorldMap::setIcon(char icon)
{
    _icon = icon;
}

char WorldMap::getIcon(int CoordX,int CoordY)
{
    return _icon;
}

void WorldMap::initialiseMap(){
    for (int i=0;i<=_height-1;i++)
    {
        for(int j=0;j<=_width-1;j++)
        {
            Map[i][j] ='0';
        }
    }
}

[/code]


[code]
//Bacteria.h
#ifndef BACTERIA_H
#define BACTERIA_H

#include "Organism.h"
#include "WorldMap.h"
class Bacteria : public Organism{

public:
    Bacteria(int,int,char);


protected:



};

#endif // BACTERIA_H
[/code]

[code]
#include "Bacteria.h"
#include "WorldMap.h"
#include "Organism.h"

Bacteria::Bacteria(int CoordX,int CoordY, char icon):Organism(CoordX, CoordY,icon)
{
    icon = 'B';
    //char getIcon();

}
[/code]

[code]

#ifndef CREEPER_H
#define CREEPER_H

#include "Organism.h"
#include "WorldMap.h"

class Creeper :public Organism{
public:
    Creeper(int,int,char,int);
    void getMovement(int, int);
protected:
    int weight;
    int CoordXnew;
    int CoordYnew;
    int CoordX;
    int CoordY;
};

#endif // CREEPER_H

[/code]

[code]
//Creeper.cpp
#include "Creeper.h"
#include "WorldMap.h"
#include "Organism.h"

Creeper::Creeper(int CoordX,int CoordY, char icon, int weight):Organism(CoordX, CoordY,icon)
{
    icon = 'C';
    //cout << getCoordX();
}

void Creeper:: getMovement(int CoordX, int CoordY){

}

[/code]

[code]

#ifndef ORGANISM_H
#define ORGANISM_H

#include "WorldMap.h"

class Organism
{
public:
    Organism(int CoordX,int CoordY,char icon);

    void setCoordXY(void);
    int getCoordX();
    int getCoordY();
    void setIcon(char);
    char getIcon(void);
protected:
    int _CoordX;
    int _CoordY;
    char _icon;

};

#endif // ORGANISM_H

[/code]
//Organism.cpp
[code]

#include "Organism.h"
#include "time.h"
#include "WorldMap.h"
Organism::Organism(int CoordX,int CoordY,char icon)
{
    setCoordXY();
    setIcon(icon);
}



void Organism::setCoordXY()
{
    _CoordX = rand()%6;
    _CoordY = rand()%5;
}

int Organism::getCoordX()
{
    return _CoordX;
}

int Organism::getCoordY()
{
    return _CoordY;
}

void Organism::setIcon(char icon){
    _icon = icon;
}

char Organism:: getIcon()
{
    return _icon;
}

[/code]

You need to post your error and the line that the error points to.
You have different headers that we dont have access to test your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.