How to find the correct coordinate?
-
Hello all this is my maze. when the worm reach to wall it passes the wall and sometime it stops.
#include <stdio.h>
#include <conio.h>int dir=0;
char wall[40][50]={
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},
{1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,1},
{1,1,0,1,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1},
{1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,1,1,0,1},
{1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1},
{1,1,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,1,1,0,0,0,1,1,0,1},
{1,0,1,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,1,1,1,1,1,1,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1},
{1,1,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,1,1,0,0,0,1,1,0,1},
{1,0,1,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};/* prototypes */
create_wall();
arrow_keys();
/* end of prototypes */void main()
{
int a=3,b=2;
create_wall();
lbl : arrow_keys();
if (dir==72){
b-=1;
if (wall[a][b]==0){wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
else b+=1;goto lbl;
}
if (dir==75){
a-=1;
if (wall[a][b]==0){wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
else a+=1;goto lbl;
}
if (dir==77){
a+=1;
if (wall[a][b]==0){wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
else a-=1;goto lbl;
}
if (dir==80){
b+=1;
if (wall[a][b]==0){wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
else b-=1;goto lbl;
}
if (dir==0) {printf("exit");}
}/* function to create the walls */
create_wall(){
clrscr();
int i,j;
for(i=0;i<15;i++){
for(j=0;j<26;j++){
if (wall[i][j]==1) printf("²");
else printf(" ");
}
printf("\n");
}
gotoxy(2,2);
printf("**");
}
/* end of create_wall function */
////////////////////////////////////
/* function to define arrow keys */
int arrow_keys()
{
int ch;
while(1)
{
ch=getch();
if(ch==0)
{
ch=getch();
if(ch==72) {dir=72; break;}
if(ch==75) {dir=75; break;}
if(ch==77) {dir=77; break;}
if(ch==80) {dir=80; break;}
}
else dir=0;
break;}
}
Wher
-
Hello all this is my maze. when the worm reach to wall it passes the wall and sometime it stops.
#include <stdio.h>
#include <conio.h>int dir=0;
char wall[40][50]={
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},
{1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,1},
{1,1,0,1,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1},
{1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,1,1,0,1},
{1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1},
{1,1,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,1,1,0,0,0,1,1,0,1},
{1,0,1,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,1,1,1,1,1,1,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1},
{1,1,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,1,1,0,0,0,1,1,0,1},
{1,0,1,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};/* prototypes */
create_wall();
arrow_keys();
/* end of prototypes */void main()
{
int a=3,b=2;
create_wall();
lbl : arrow_keys();
if (dir==72){
b-=1;
if (wall[a][b]==0){wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
else b+=1;goto lbl;
}
if (dir==75){
a-=1;
if (wall[a][b]==0){wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
else a+=1;goto lbl;
}
if (dir==77){
a+=1;
if (wall[a][b]==0){wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
else a-=1;goto lbl;
}
if (dir==80){
b+=1;
if (wall[a][b]==0){wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
else b-=1;goto lbl;
}
if (dir==0) {printf("exit");}
}/* function to create the walls */
create_wall(){
clrscr();
int i,j;
for(i=0;i<15;i++){
for(j=0;j<26;j++){
if (wall[i][j]==1) printf("²");
else printf(" ");
}
printf("\n");
}
gotoxy(2,2);
printf("**");
}
/* end of create_wall function */
////////////////////////////////////
/* function to define arrow keys */
int arrow_keys()
{
int ch;
while(1)
{
ch=getch();
if(ch==0)
{
ch=getch();
if(ch==72) {dir=72; break;}
if(ch==75) {dir=75; break;}
if(ch==77) {dir=77; break;}
if(ch==80) {dir=80; break;}
}
else dir=0;
break;}
}
Wher
Here are a few ideas for you to make life easier in the future: 1. Don't use global variables when you don't have to. In your case, wall might be acceptable, but dir should not be global. 2. Don't confuse others by writing wall[40][50] and only initialize 15/26. 3. If wall is global, add globals for the wall bounds as well. You don't want to use hard coded values (like 15 and 26) anywhere else but very close to you wall definition. (you can't trust sizeof() operator either the way you have done it) 4. Be consistent in your naming. Don't use indexes a and b in one place, and i and j in another for the same thing. x and y would do just fine. 5. Do not use labels and goto where a simple loop will be enough. Your code will be hard to follow and maintain, even if it's correct. If you need a label, and it will be a long time before you do, give it a meaningful name. 6. Let arrow_keys return its value instead of changing a global variable. It's defined to return an int, but never does. So is also create_wall() by the way. 7. Use constant identifiers instead of hard coded numbers. An enum type is well suited for your direction variable. Again, think of naming. 8. printf("%c", ...)? Have a look at putc(). 9. Indentation looks a bit random, and so does placement of curly brackets. Adopt a consistent style. That said, finding these kinds of bugs is an easy job if you step through the code in a debugger, inspecting indexes into your wall matrix. But clean up your code first.