class moving
{
private :
short x;
short y;
int value;
public :
moving (short xa ,short ya,int va);
void gotoxy(short x, short y);
void gotoxy(short x, short y,bool clear);
void shiftright(int x, int y,int value);
short get_x() ;
short get_y() ;
int get_value();
void set_x(int newx );
void set_y(int newy);
void set_value(int newvalue);
int generate_number();
int generate_random_number();
};
#include "moving.h"
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <ctime> // to use the time function
#include <cstdlib>
using namespace std;
moving ::moving(short xa ,short ya,int va)
{
x = xa ;
y = ya ;
value =va ;
}
void moving :: gotoxy(short x , short y)
{
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void moving ::gotoxy(short x ,short y,bool clear)
{
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
cout<<" ";
}
void moving::shiftright(int x ,int y ,int value )
{
gotoxy(x,y);
cout<<value;
}
short moving ::get_x()
{
return x ;
}
short moving ::get_y()
{
return y ;
}
int moving ::get_value()
{
return value ;
}
void moving ::set_x(int newx)
{
x=newx;
}
void moving ::set_y(int newy)
{
y=newy ;
}
void moving ::set_value(int newvalue)
{
value=newvalue ;
}
int moving::generate_number()
{
srand(time(NULL));
int value= rand() % 100;
return value;
}
int moving ::generate_random_number()
{ srand(time(NULL));
int x= rand() % 50;
int y= rand() % 50;
//return y;
gotoxy(x,y);
int rand_num=generate_number();
cout<<rand_num;
return rand_num;
}
#include <moving.h>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <ctime> // to use the time function
#include <cstdlib>
using namespace std;
int main ()
{ moving m1(0,0,0);
moving m2 (0,0,0);
m1.shiftright(m1.get_x(), m1.get_y(), m1.get_value());
m2.shiftright(m2.get_x(), m2.get_y(), m2.get_value());
while(m2.get_value() < 500)
{
unsigned char a;
int x=0;
int y=0;
int v =0 ;
x=m1.get_x();
y=m1.get_y();
v=m1.get_value();
while(m1.get_x() != m2.get_x() && m1.get_y() != m2.get_y())
{
a=getch();
switch(a)
{
case 77:
m1.gotoxy(y,x,true);
y=y+1;
if(y>50)
m1.set_y(50);
m1.shiftright(y,x,v);
break;
case 75:
m1.gotoxy(y,x,true);
y=y-1;
if(y<0)
m1.set_y(0);
m1.shiftright(y,x,v);
break;
case 72:
m1.gotoxy(y,x,true);
x=x-1;
if(x<0)
m1.set_x(0);
m1.shiftright(y,x,v);
break;
case 80:
m1.gotoxy(y,x,true);
x=x+1;
if(x>50)
m1.set_x(50);
m1.shiftright(y,x,v);
break;
case 'e':
break;
} //switch
} //while cordinates matches
m1.set_value(m1.get_value() + m2.get_value());
m1.set_x(0);
m1.set_y(0);
m1.shiftright(m1.get_x(), m1.get_y(), m1.get_value());
m2.generate_random_number();
m2.shiftright(m2.get_x(), m2.get_y(), m2.get_value());
} // while <500
}//itn main
when run the program its start generating the random numbers without catching the number i might think this is happening because of 2nd while condition but i dont know what else i write in the condition