// 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