This is a game i have made, but I have some problems in the breed & eat functions I dont know what is the problem & i really need help. the code works probably but the prey & the predator doesnt breed or eat,,,ANY HELP PLZZ

#include <iostream>
#include<windows.h>
#include<conio.h>
using namespace std;

char grid[20][20];                 //creatures grid[20][20];

class Organism{                   // class for all the creatures
    protected:
    int die;
    char name;

    public:
    Organism();
    char getname();
};
Organism :: Organism(){
    name = ' ';
    die = 0;
}
char Organism :: getname(){
    return name;
}

class predators : public Organism{   //class predator child of creatures class
    public:

    virtual bool eat(int i, int j)=0;
    predators();
    void breed();
    void die();
    void move();
    int moves;
    int x,y;
};
predators::predators(){
  moves = 0;
  x=rand()%17+1;
    y=rand()%17+1;


}
void predators :: move(){
     HANDLE hConsole;                               // colouring function
        hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
    grid[x][y]='X';


    char c=_getch();
    //cout<<(int)c<<endl;
    if(c==119)//up press W
    {
        if(x==1){
            return;
        }
        if(eat(x-1,y)){
        grid[x][y]=' ';
        x--;
        grid[x][y]=name;
        moves++;
        breed();
    }
    }
    else if(c==115)//down press S
    {
        if(x==18){
            return;
        }
        if(eat(x+1,y)){
        grid[x][y]=' ';
        x++;
        grid[x][y]=name;
        moves++;
        breed();
        }
    }

    else if(c==100)//right press D
    {
        if(y==18){
            return;
        }
        if(eat(x,y+1)){
        grid[x][y]=' ';
        y++;
        grid[x][y]=name;
        moves++;
        breed();
        }
    }

     else if(c==97)//left press A
    {
        if(y==1){
            return;
        }
        if(eat(x,y-1)){
        grid[x][y]=' ';
        y--;
        grid[x][y]=name;
        moves++;
        breed();
        }
    }
    system("cls");            // function to delete old array and print a new one after move


    for(int i=0;i<20;i++)
    {
        for(int j=0;j<20;j++)
            cout<<grid[i][j];
        cout<<endl;
    }

}
void predators::die(){
    if(moves==3){
        int x,y;
        grid[x][y]=' ';
        x = -1;
        y = -1;

    }
}
void predators :: breed(){
    if(moves==8){
        int x,y;
        x = rand()%17+1;
        y = rand()%17+1;
        if(grid[x][y] = ' '){
            grid[x][y] = name;
        }
    }
}

class prey : public Organism{       //class prey child of creatures class
    public:

    prey();
    void breed();
    void move();
    private:
    int x,y;
    int moves;
};
prey :: prey (){
        x = rand()%17+1;
        y = rand()%17+1;
        grid[x][y]=name;
        moves = 0;
}
void prey :: move(){
//    for(;;){
//        system("cls");
//        cout<<rand()%13+1;
//        getch();
//    }

        char c;
        if(rand()%4==0)
        {c=119;}
         else if(rand()%4==1)
        {c=115;}
        else if(rand()%4==2)
        {c=100;}
        else if(rand()%4==3)
        {c=97;}
    //_getch();
    //cout<<(int)c<<endl;
    if(c==119)//up press W
    {
        if(x==1){
            return;
        }
        grid[x][y]=' ';
        x--;
        grid[x][y]=name;
        moves++;
    }
    else if(c==115)//down press S
    {
        if(x==18){
            return;
        }
        grid[x][y]=' ';
        x++;
        grid[x][y]=name;
        moves++;

    }

    else if(c==100)//right press D
    {
        if(y==18){
            return;
        }
        grid[x][y]=' ';
        y++;
        grid[x][y]=name;
        moves++;

    }

     else if(c==97)//left press A
    {
        if(y==1){
            return;
        }
        grid[x][y]=' ';
        y--;
        grid[x][y]=name;
        moves++;

    }

    //system("cls");            // function to delete old array and print a new one after move
    //for(int i=0;i<14;i++)
    //{
      //  for(int j=0;j<20;j++)
        //    cout<<grid[i][j];
        //cout<<endl;
    //}
    }
void prey :: breed(){
    if(moves==3){
        int x,y;
        x = rand()%17+1;
        y = rand()%17+1;
        if(grid[x][y] = ' '){
            grid[x][y] = name;
        }
    }
}

class doodlebugs : public predators{      //class snake child of predators class
  public:
  doodlebugs(){
                  HANDLE hConsole;                               // colouring function
        hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
                SetConsoleTextAttribute
        (hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
      name ='X';
      x=rand()%17+1;
      y=rand()%17+1;
  }
  virtual bool eat(int i, int j){
      if(grid[i][j] == 'W' || grid[i][j] == 'B' || grid[i][j] == ' '){
          return true;
      }
      else
      return false;
  }

};

/*class scorpion : public predators{   //class scorpion child of predators class
    public:
    scorpion(){
        name = 'X';
        x=rand()%11+1;
        y=rand()%11+1;
        }
    virtual bool eat(int i, int j){
      if(grid[i][j] == 'A' || grid[i][j] == 'B' || grid[i][j] == ' '){
          return true;
      }
      else
      return false;
  }
};*/

class ants : public prey{            //class ants child of prey class
    public:
    ants(){
        name = 'O';
    }
};

class bugs : public prey{            //class bugs child of prey class
    public:
    bugs(){
        name = 'B';
    }
};

class worms : public prey{           //class worms child of prey class
    public:
    worms(){
        name = 'W';
    }
};

class game{                          //a class to run da game from
    public:

    /*void score(){
        int score = 0;
        doodlebugs d;

        if(d.eat = 'W' || d.eat = 'B' || d.eat = 'O'){
            score++;
        }
        cout<<"Score:"<<score<<endl;
    }*/


    void run(){

    doodlebugs x;
    //scorpion x;

    prey* p1[18];

    for(int i=0;i<18;i++)
    {
        p1[i]=new bugs;
    }

    prey* p2[18];

    for(int i=0;i<18;i++)
    {
        p2[i]=new ants;
    }

        prey* p3[18];

    for(int i=0;i<18;i++)
    {
        p3[i]=new worms;
    }

    srand((unsigned int)time(0));
  game a;
    a.borders();
    prey c;
    a.display();
    for(;;){
        for(int i=0;i<18;i++)
        {
            p1[i]->move();
        }
        for(int i=0;i<18;i++)
        {
            p2[i]->move();
        }
        for(int i=0;i<18;i++)
        {
            p3[i]->move();
        }
    x.move();
        system("CLS");
    a.display();
    //x.move();
     system("CLS");
    a.display();
    c.move();
    system("CLS");
    a.display();
    }
    }

void borders(){
            HANDLE hConsole;                               // colouring function
        hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
                SetConsoleTextAttribute
        (hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);

    for(int i = 0; i<20;i++)
    {
        grid[i][0] = '#';
        grid[i][19] = '#';
        grid[0][i] = '#';
        grid[19][i] = '#';
    }
    for(int i=1;i<19;i++){
        for(int j=1;j<19;j++)
           grid[i][j]=' ';
    }
}
void intro(){
                HANDLE hConsole;                               // colouring function
        hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
                SetConsoleTextAttribute
        (hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);

cout<< "********************************************************************************"<<endl;
cout<< "** Predator & Prey Game.. All Rights Reserved to group :                      **"<<endl;
cout<< "** Created By:                                                                **"<<endl;
cout<< "**            Ahmed Mohamed Adel El-Sayary                                    **"<<endl;
cout<< "**            Ahmed Khaled Kassem                                             **"<<endl;
cout<< "**            Abdelaziz Hassan Gaballah                                       **"<<endl;
cout<< "**                                                                            **"<<endl;
cout<< "**  Any use of this program without the knowledge or permission of the        **"<<endl;
cout<< "** Creators will put you in the court room with handcuffs and sign on your    **"<<endl;
cout<< "** Back saying im a LOSER.                                                    **"<<endl;
cout<< "**                                                                            **"<<endl;
cout<< "** Have fun playing the game.. NOW its FREE soon it WONT BE !!!               **"<<endl;
cout<< "**                                                                            **"<<endl;
cout<< "** Cast:                                                                      **"<<endl;
cout<< "**    Predators :  King Cobra - The Scorpion King.                            **"<<endl;
cout<< "**    Prey : The Ant - The Worm - Bugz.                                       **"<<endl;
cout<< "**                                                                            **"<<endl;
cout<< "**                                                                            **"<<endl;
cout<< "********************************************************************************"<<endl;
cout<< "please type your name then press enter to start the game..."<<endl;
char l;
cin>>l;

}
void display(){
    for(int i=0;i<20;i++){
        for(int j=0;j<20;j++)
            cout<<grid[i][j];
        cout<<endl;
    }
}

};

int main(){
    game a;
    a.intro();
    a.run();
      return 0;
}

This is a game I have made. It is a prey predator game the predator eats the prey & breeds every 8 turns & so is the prey but every 3 turns. the game works probably but some thing is wrong with the breed & eat function any help plzz

In prey::breed(), you check whether moves==3, but you don't reset it to zero, so that prey instance will never breed again. You probably have similiar problems in the other methods.

While I was looking I also noticed that you randomly place prey and predators in the constructors, but don't check whether the space is empty. You should have a loop that checks for that. Keep in mind that if your choices of 3 and 8 don't make sense, the grid may become over-populated, and then the loop may take a while to pick a random location which is still empty, or will eventually loop forever. Take this into consideration -- I don't want to tell you a way of doing this yet, see if you can figure out something for yourself first. You also place the name of the prey (and where does that come from, you don't have a name yet when you call prey::prey()!) into the grid at the corresponding location, but not the name of the predator, is that intentional?

Finally, your random-movement logic in prey::move() is flawed: since you're picking a random number each time, there is a substantial chance that you won't pick any direction at all. Instead assign rand()%4 to a temporary variable, and then check that variable to decide which keystroke to use for it. Or initialize an array int keystrokes[] = {119, 115, 100, 97}; and then you can use the temporary variable (or even rand()%4 directly) as an index into it to get the one you want.

Let's see where that gets you! :)

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.