Nov 18, 2012 at 3:46pm UTC
How do i stop the program from closing when the robot reaches the boundaries of the grid?
[code]#include <iostream.h>
#include<conio.h>
#include <windows.h>
int mx=30;
int my=10;
int i, j;
char direction;
const char left_key='a', right_key='d';
char keypress;
char grid[24][79];
void gotoxy(short x, short y)
{
HANDLE hConsoleOutput;
COORD Cursor = {x,y};
hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor);
}
void printgrid()
{
for (i=0; i<24; i++)
{
for (j=0; j<79; j++)
{
cout<<grid[i][j];
}
cout<<endl;
}
}
#define VMAX 3
#define HMAX 3
void printRobot(char robot[][3],int x, int y);
void fillIn(int x,int y);
int main()
{
int x,y;
// clock_t pause = 50;
char robot[VMAX][HMAX]={
{'.','o','.'},
{'-','¦','-'},
{'/',' ','\\'}
};
HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
//SetConsoleTextAttribute(hOut, BACKGROUND_BLUE|
//BACKGROUND_INTENSITY);
SetConsoleTextAttribute(hOut, BACKGROUND_GREEN|
BACKGROUND_INTENSITY);
//loadgrid();
printgrid();
gotoxy(mx,my);
printRobot(robot,x,y);
while (mx<70 && my<20 && my>0 && mx>0)
{
if (kbhit())
{
keypress=getch(); //keypress=(char)getchar()
if ((keypress == right_key) || (keypress == left_key))
direction = keypress;
if (direction == 'a')
{
gotoxy(mx,my);
cout<<" "<<flush;
mx=mx-1;
gotoxy(mx,my);
printRobot(robot,x,y);
fillIn(x,y);
gotoxy(mx,my);
}
if (direction == 'd')
{
gotoxy(mx,my);
cout<<" "<<flush;
mx=mx+1;
gotoxy(mx,my);
printRobot(robot,x,y);
fillIn(x,y);
gotoxy(mx,my);
}
direction = ' ';
}
}
// cout << "NOTE: MAKE SURE ALL ENTERED LETTERS ARE LOWER CASE!!!!!!!";
return 0;
}
void printRobot(char robot[][3],int x, int y) {
int r,c;
for(c=0,r=2;c<HMAX;c++)
cout<<robot[r][c]<<flush;
gotoxy(x,y-1);
for(c=0,r=1;c<HMAX;c++)
cout<<robot[r][c]<<flush;
gotoxy(x,y-2);
for(c=0,r=0;c<HMAX;c++)
cout<<robot[r][c]<<flush;
}
void fillIn(int x,int y) {
int i;
for(i=VMAX;i>-1;i--) {
gotoxy(x,y-i);
cout<<" "<<flush;
}
}
[\code]
Last edited on Nov 18, 2012 at 3:47pm UTC