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;
}