a problem in maze (the worm pass the walls)
-
Hello all I am working on maze in turbo c(command prompt not windows). I write 90% of it. but there are 2 problems I do any work can't solve it. 1- When worm(** is my worm) reach a wall it don't stop. it can pass the wall? 2- the worm make longer but it should be ** in any time?
#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,1,1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,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,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; wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
if (dir==75){a-=1; wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
if (dir==77){a+=1; wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
if (dir==80){b+=1; wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);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;}
}
Please help me to solve the problems. Thanks in advance
-
Hello all I am working on maze in turbo c(command prompt not windows). I write 90% of it. but there are 2 problems I do any work can't solve it. 1- When worm(** is my worm) reach a wall it don't stop. it can pass the wall? 2- the worm make longer but it should be ** in any time?
#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,1,1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,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,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; wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
if (dir==75){a-=1; wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
if (dir==77){a+=1; wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
if (dir==80){b+=1; wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);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;}
}
Please help me to solve the problems. Thanks in advance
-
Hello all I am working on maze in turbo c(command prompt not windows). I write 90% of it. but there are 2 problems I do any work can't solve it. 1- When worm(** is my worm) reach a wall it don't stop. it can pass the wall? 2- the worm make longer but it should be ** in any time?
#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,1,1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,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,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; wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
if (dir==75){a-=1; wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
if (dir==77){a+=1; wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
if (dir==80){b+=1; wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);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;}
}
Please help me to solve the problems. Thanks in advance
You're not erasing where the "worm" is when you draw the new one on the screen and you're not testing where the new worm head is and ending the game (if that's the rule) when the worms head hits a wall. To get round these problems: - You can deal with the extra drawing by overwriting the worm's current position with spaces after you poll the keyboard but before you change the position of the worm. - When you change the worm's position do a check to see if the new position overwrites a 1 in your maze. If it does, end the game. Cheers, Ash
-
You're not erasing where the "worm" is when you draw the new one on the screen and you're not testing where the new worm head is and ending the game (if that's the rule) when the worms head hits a wall. To get round these problems: - You can deal with the extra drawing by overwriting the worm's current position with spaces after you poll the keyboard but before you change the position of the worm. - When you change the worm's position do a check to see if the new position overwrites a 1 in your maze. If it does, end the game. Cheers, Ash
Hello Ash; I wrote these codes you say me. but it doesn't work correctly. It stop in another position that there is no wall and sometime it pass a wall and stop on the next one. These code are right, but I can't write them in its own place. Please help me by writing codes if it is possible for you.