I have only started learning borland c++ programming a few months ago, I'm actually a math student, so my knowledge of c++ is pretty limited. I have an assignment where X represents an ant that moves around in a 7x7 grid box. It can move up, left, down or right by 1 step at a time. I have already written the code, but sometimes the 1st 7x7 grid displayed is sometimes distorted. It only displayed a 7x3 grid. Please let me know what is the mistake I made. I know my course is not so efficient, I just want to get it working first. Please help me! I'm dying! =(
#include<iostream.h>
#include<conio.h>
#define size 20
void main()
{
srand(time(0));
int box;
int i,j,y=0, count, random, key;
for (i=0;i<7;i++)
for (j=0;j<7;j++){
box[j]=y+1;
y= box[j];
}
i=3;
j=3;
for (count=1;;count++)
{
random = rand()%4;
if (count==1)
key= box[3][3];
else if (random==0)
key= box[j=j+1];
else if (random==1)
key= box[j=j-1];
else if (random==2)
key= box[j];
else if (random==3)
key= box[j];
if (i<0||j<0)
goto out;
if (i>=7||j>=7)
goto out;
{
cout<<" ";
for(int line=0;line<7;line++)
cout<<"--- ";
}
cout<<endl;
for(int k=0;k<7;k++)
{
for(int l=0;l<7;l++)
{
if(key!=box[k][l])
cout<<"| ";
else if(key==box[k][l])
cout<<"| X ";
}
cout<<"|";
cout<<endl;
{
cout<<" ";
for(int line=0;line<7;line++)
cout<<"--- ";
cout<<endl;
}
}
cout<<endl<<endl;
}
out: cout<<"\nExit from program.";
getch();
}