// Project !!!.cpp : Defines the entry point for the console application.
//
#include<windows.h>
#include <iostream>
#include <conio.h>

using namespace std;


class Predator
{private:
        int  x;
        int  y;
        char z;
        char board[20][20];
        int count1;
        int count2;

 public:
        Predator(){
            for (int j=0; j<20; j++)
            {
               board[0][j]  = '@';
               board[19][j] = '@';
            }

            for (int i=0; i<20; i++)
            {
               board[i][0]  = '@';
               board[i][19] = '@';
            }
            for (int j=1; j<19; j++)
            {
                for (int i=1; i<19; i++)
                {
                    board[i][j] = ' ';
                }
            }
            }
            int getx(){
                return x;
            }
            int gety(){
                return y;
            }
        void displayBoard()
        {
            for (int i=0; i<20; i++)
            {
                for (int j=0; j<20; j++)
                {
                    cout<< board[i][j];
                }
                cout<<endl;
            }
        }


       void set1st(int l, int m, char h)
        {
            x=l;
            y=m;
            z=h;
            board[x][y] = z;
        }
        void move()
        {
            int c,t = 1;
            c=getch();
            t = getch();

            if(t == 27)
            {
                exit(1);
            }
            if(t == 72)
            {
                moveUp(x, y, z);
                count1 ++;
                count2 ++;
                x--;
            }
            if(t == 80)
            {
                moveDown(x, y, z);
                count1 ++;
                count2 ++;
                x++;
            }
            if(t == 77)
            {
                moveRight(x, y, z);
                count1 ++;
                count2 ++;
                y++;
            }
            if(t == 75)
            {
                moveLeft(x, y, z);
                count1 ++;
                count2 ++;
                y--;
            }

        }


        void moveUp(int x, int y, char z)
        {
            if(x>1)
            {
                set1st((x-1),y,z);
                set1st(x,y,' ');
            }

        }

        void moveDown(int x, int y, char z )
        {
           if(x<18)
           {
                set1st((x+1),y,z);
                set1st(x,y,' ');
           }

        }

        void moveRight(int x, int y, char z)
        {
            if(y<18)
            {
                set1st(x,(y+1),z);
                set1st(x,y,' ');
            }
        }

        void moveLeft(int x, int y, char z)
        {
            if(y>1)
            {
                set1st(x,(y-1),z);
                set1st(x,y,' ');
            }
        }


        void breed(int x , int y, char z)
        {
            if( board[x][y-1] = ' ' )
                set1st(x, y-1, 'X');

            else if( board[x+1][y] = ' ' )
                set1st(x+1, y, 'X');

            else if( board[x][y+1] = ' ' )
                set1st(x, y+1, 'X');

            else if( board[x-1][y] = ' ' )
                set1st(x-1, y, 'X');
        }

};


/*class Prey
{



};*/



int main()
{
    int x = 0;
    int y = 0;

    int count1 = 0;
    int count2 = 0;

    Predator pred;

    pred.set1st(7, 7, 'X');
    pred.displayBoard();
    cout<<pred.getx()<<" "<<pred.gety();
    pred.move();
    system("cls");
    pred.displayBoard();
    cout<<pred.getx()<<" "<<pred.gety();
    pred.move();
    system("cls");
    pred.displayBoard();
    cout<<pred.getx()<<" "<<pred.gety();
	return 0;
}

I have 3 problems with the move & starve & breed functions:
1-move: the character in the grid moves only one move then it disappears.
2-starve & breed: i cant make them work.
so i need any advice plzz:S

i forgot to say what shall breed & starve do,,,,breed should duplicate the character after 24 move & starve should delete it after 12 move,,,,their is a missing function that i am working on right now & that is eat which if the character doesnt eat another character for 12 move it dies(delete),,,& if it eats & survived for 24 move it breeds(duplicate in the one of the four direction around it)

void breed(int x , int y, char z)
        {
            if( board[x][y-1] = ' ' )
                set1st(x, y-1, 'X');

        }

};

using single '=' instead of '==' ?

i have fixed that,,,thanks:),,,,but what about the move function i dnt know why it moves only one move then it disappears & the program ends itself,,,bec. i dnt know what is wrong with the move function i cant fix the other two functions

This article has been dead for over six months. Start a new discussion instead.