Wednesday, September 30, 2009

Game Program


#include
#include
#include

class Nasa
{
int x;
int y;
char dir;

void moveNorth()
{
this->y++;
}

void moveSouth()
{
this->y--;
}

void moveEast()
{
this->x++;
}

void moveWest()
{
this->x--;
}

public:

int run()
{
char ch='n';
do
{
clrscr();
char *mov;
cout<<"\n\nEnter coordinates :\n"; cout<<"X:"; cin>>this->x;
cout<<"Y:"; cin>>this->y;
cout<<"direction[N/S/E/W]:"; cin>>this->dir;
if(dir<='z' && dir>='a')
dir-=32;

cout<<"\n\nEnter movement string:\n"; cin>>mov;
strupr(mov);
for(int i=0;mov[i]!=NULL;i++)
{
switch(mov[i])
{
case 'L':
switch(dir)
{
case 'N':
dir='W';
break;
case 'S':
dir='E';
break;
case 'E':
dir='N';
break;
case 'W':
dir='S';
break;
}
break;
case 'R':
switch(dir)
{
case 'N':
dir='E';
break;
case 'S':
dir='W';
break;
case 'E':
dir='S';
break;
case 'W':
dir='N';
break;
}
break;
case 'M':
switch(this->dir)
{
case 'N':
moveNorth();
break;
case 'S':
moveSouth();
break;
case 'E':
moveEast();
break;
case 'W':
moveWest();
break;
}
}
}
cout<<"\n\n"<<<" "<<<" "<<<"\n\n continue [Y/N] :"; cin>>ch;
}
while(ch!='n' && ch!='N');
return 0;
}
};

int main()
{
Nasa n;
n.run();

}

3 comments:

  1. Dear Varun,
    Inclusion of library files is not shown. An HTML issue?

    Please revert.
    Regards
    Amit

    ReplyDelete
  2. Your class tags for <pre> is wrong;

    you need to use
    <pre class="brush:cpp">
    your code here...
    </pre>

    ReplyDelete
  3. INCOMPLETE PROGRAM, PLEASE PROVIDE COMPLETE PROGRAM TO PROCEED

    ReplyDelete