Click here to Skip to main content
15,886,771 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
C++
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(short x, short y,int value);

	short get_x() ;
	short get_y()  ;
	int   get_value();

	void set_x(short newx );
	void set_y(short newy);
	void set_value(int newvalue);

	int generate_number();
	void  generate_random_number();



};


this is the main header file of class moving

C++
#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)   //for setting the cordinates
{
	COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

void moving ::gotoxy(short x ,short y,bool clear) //setting the cordinates and remove the back value
{
	COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
cout<<" ";
}

void moving::shiftright(short x ,short y ,int value ) //function for shifting
{
		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(short newx)
{
	x=newx;
}

void moving ::set_y(short newy)
{
	y=newy ;
}

void moving ::set_value(int newvalue)
{
	value=newvalue ;
}





int moving::generate_number() //generate random number
{
    srand(time(NULL));
    int value= rand() % 10;
    return value;
}

void moving ::generate_random_number() //generate random cordinates and pass the the random number
{       srand(time(NULL));
	 x= rand() % 50;

    y= rand() % 50;

    //return y;

    gotoxy(x,y);
    int rand_num=generate_number();
    cout<<rand_num;
	//return rand_num;
}

this is the ccp file of moving


C++
#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());

   m2.generate_random_number();

   //while(m1.get_value() <= 500)
    //{
        unsigned char  a;


             while(1)
             {
                a=getch();


                switch(a)
                {
                    case 77:
                        m1.gotoxy(m1.get_y(),m1.get_x(),m1.get_value());
                        m1.set_y(m1.get_y() + 1);
                        if(m1.get_y()>50)
                        m1.set_y(50);
                        m1.shiftright(m1.get_y(),m1.get_x(),m1.get_value());
                        break;
                    case 75:
                        m1.gotoxy(m1.get_y(),m1.get_x(),m1.get_value());
                        m1.set_y(m1.get_y()-1);
                        if(m1.get_y()<0)
                        m1.set_y(0);
                        m1.shiftright(m1.get_y(),m1.get_x(),m1.get_value());
                        break;
                    case 72:
                         m1.gotoxy(m1.get_y(),m1.get_x(),m1.get_value());
                        m1.set_x(m1.get_x()-1);
                        if(m1.get_x()<0)
                        m1.set_x(0);
                        m1.shiftright(m1.get_y(),m1.get_x(),m1.get_value());
                        break;
                    case 80:
                         m1.gotoxy(m1.get_y(),m1.get_x(),m1.get_value());
                        m1.set_x(m1.get_x()+1);
                        if(m1.get_x()>50)
                        m1.set_x(50);
                        m1.shiftright(m1.get_y(),m1.get_x(),m1.get_value());
                        break;
                    case 'e':
                    break;
                } //switch

                 if(m1.get_x()==m2.get_x() && m1.get_y()==m2.get_y())
                 {
                     //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.shiftright(m2.get_x(), m2.get_y(), m2.get_value());
                     m2.generate_random_number();
                 }
             } //while cordinates matches
     //} // while <500

}//itn main

this is the main program

the problem em facing is that when i run the program it display a random number and then i get that number with arrows key then it not generate the other randon number mean that if statement is not working
Posted
Updated 30-Sep-13 3:30am
v2
Comments
pasztorpisti 30-Sep-13 9:29am    
I think the if statement works perfectly but you have a bug. Debug it. If you don't know how to use a debugger then learn using it or simply use the poor man's debugger: logging/printf.
Member 10306637 30-Sep-13 10:57am    
how to use debugger
pasztorpisti 30-Sep-13 11:04am    
Depending on your development environment I recommend using a good C++ IDE where the editor and the debugger is integerated. A good example on windows is Visual Studio, another free and crossplatform solution is code::blocks. use google to find tutorials about debugging in these IDEs.
Member 10306637 30-Sep-13 11:20am    
i am using code block let me see if the problem is solve with the debugger

It looks like you've flipped your X and Y coordinates when setting the moving's coordinates in the main program.
C++
m1.gotoxy(m1.get_y(),m1.get_x(),m1.get_value());

to
C++
m1.gotoxy(m1.get_x(),m1.get_y(),m1.get_value());


Or change
C++
gotoxy(x,y);

in moving::generate_random_number() to
C++
gotoxy(y,x);

It's quicker.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
Member 10306637 30-Sep-13 11:11am    
i tried it but the if statement is still not working
Member 10306637 30-Sep-13 11:38am    
the problem is solve by changing the gotoxy but after that there are no more problem it not changes the value of 0
Fredrik Bornander 1-Oct-13 3:16am    
What should change from 0?
Should the 0 become the value it "eats"?
Having looked at the program, I've come to the same conclusion as Fredrik.
I also note that used hard-coded values for keyCodes.
Also, you only need to call srand once per program execution. I've removed all calls to it from functions that are repeatedly called, instead placing it as the first call inside main.

I suggest the following code works and gives an output that resembles the game 'snake'.

C++
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <ctime> // to use the time function
#include <cstdlib>
#include <cstdio>

#include <iostream>
using namespace std;


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(short x, short y,int value);

    short get_x() ;
    short get_y()  ;
    int   get_value();

    void set_x(short newx );
    void set_y(short newy);
    void set_value(int newvalue);

    int generate_number();
    void  generate_random_number();
};

////////////////////////////////////////////////////////////

//#include "moving.h"
moving ::moving(short xa ,short ya,int va)
{
    x = xa ;
    y = ya ;
    value =va ;
}

void moving::gotoxy(short x , short y)   //for setting the cordinates
{
    COORD pos = {x, y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

void moving ::gotoxy(short x ,short y,bool clear) //setting the cordinates and remove the back value
{
    COORD pos = {x, y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
    cout<<" ";
}

void moving::shiftright(short x ,short y ,int value ) //function for shifting
{
    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(short newx)
{
    x=newx;
}

void moving ::set_y(short newy)
{
    y=newy ;
}

void moving ::set_value(int newvalue)
{
    value=newvalue ;
}

int moving::generate_number() //generate random number
{
    int value= rand() % 10;
    return value;
}

void moving ::generate_random_number() //generate random cordinates and pass the the random number
{
    x= rand() % 50;
    y= rand() % 50;

    gotoxy(x,y);
    int rand_num=generate_number();
    cout<<rand_num;
}






const int leftKey = 75;
const int rightKey = 77;
const int upKey = 72;
const int downKey = 80;

////////////////////////////////////////////////////////////

int main ()
{
    srand(time(NULL));

    moving m1(0,0,0);
    moving m2 (0,0,'*');


    m1.shiftright(m1.get_x(), m1.get_y(), m1.get_value());
    m2.shiftright(m2.get_x(), m2.get_y(), m2.get_value());

    m2.generate_random_number();
    printf("m1: %d,%d,%c\n", m1.get_x(), m1.get_y(), m1.get_value());
    printf("m2: %d,%d,%c\n", m2.get_x(), m2.get_y(), m2.get_value());
    getch();

    //while(m1.get_value() <= 500)
    //{
    unsigned char  a;


    while(1)
    {
        a=getch();

        switch(a)
        {
        case downKey:
            m1.gotoxy(m1.get_x(),m1.get_y(),m1.get_value());
            m1.set_y(m1.get_y() + 1);
            if(m1.get_y()>50)
                m1.set_y(50);
            m1.shiftright(m1.get_x(),m1.get_y(),m1.get_value());
            break;
        case upKey:
            m1.gotoxy(m1.get_x(),m1.get_y(),m1.get_value());
            m1.set_y(m1.get_y()-1);
            if(m1.get_y()<0)
                m1.set_y(0);
            m1.shiftright(m1.get_x(),m1.get_y(),m1.get_value());
            break;
        case leftKey:
            m1.gotoxy(m1.get_x(),m1.get_y(),m1.get_value());
            m1.set_x(m1.get_x()-1);
            if(m1.get_x()<0)
                m1.set_x(0);
            m1.shiftright(m1.get_x(),m1.get_y(),m1.get_value());
            break;
        case rightKey:
            m1.gotoxy(m1.get_x(),m1.get_y(),m1.get_value());
            m1.set_x(m1.get_x()+1);
            if(m1.get_x()>50)
                m1.set_x(50);
            m1.shiftright(m1.get_x(),m1.get_y(),m1.get_value());
            break;
        case 'e':
            break;
        }

        if(m1.get_x()==m2.get_x() && m1.get_y()==m2.get_y())
        {
            m2.generate_random_number();
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900