Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. a problem in maze (the worm pass the walls)

a problem in maze (the worm pass the walls)

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hasani2007
    wrote on last edited by
    #1

    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

    L A 2 Replies Last reply
    0
    • H hasani2007

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Please do not repost http://www.codeproject.com/Questions/88698/a-problem-in-maze.aspx[^].Choose only one forum for your question.

      Life is a stage and we are all actors!

      1 Reply Last reply
      0
      • H hasani2007

        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

        A Offline
        A Offline
        Aescleal
        wrote on last edited by
        #3

        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

        H 1 Reply Last reply
        0
        • A Aescleal

          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

          H Offline
          H Offline
          hasani2007
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups