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. move function in maze does not work correctly

move function in maze does not work correctly

Scheduled Pinned Locked Moved C / C++ / MFC
5 Posts 5 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 It's a maze program,but is not completed. I want to define move function to move the ** (worm). when I ask the value of arrow_keys() it has no response(see move function) in other word move function doesn't work(it is not completed).

    #include <stdio.h>
    #include <conio.h>

    /* prototypes */
    create_wall();
    arrow_keys();
    move();
    /* end of prototypes */

    void main()
    {
    create_wall();
    arrow_keys();
    move();
    }

    /* function to create the walls */
    create_wall(){
    clrscr();
    int i,j;
    char wall[15][26]={
    {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,1,1,1,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,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,1},
    };
    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("*");
    gotoxy(3,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) return 72;
    if(ch==75) return 75;
    if(ch==77) return 77;
    if(ch==80) return 80;
    }
    else break;
    }
    }

    /* function for move */
    // up=72 , down=80 , left=75 , right=77
    move(){

    if (arrow_keys()==75)
    {
    clrscr();
    printf("bvccvbncvnvbnvb");
    }
    }

    N C D S 4 Replies Last reply
    0
    • H hasani2007

      Hello It's a maze program,but is not completed. I want to define move function to move the ** (worm). when I ask the value of arrow_keys() it has no response(see move function) in other word move function doesn't work(it is not completed).

      #include <stdio.h>
      #include <conio.h>

      /* prototypes */
      create_wall();
      arrow_keys();
      move();
      /* end of prototypes */

      void main()
      {
      create_wall();
      arrow_keys();
      move();
      }

      /* function to create the walls */
      create_wall(){
      clrscr();
      int i,j;
      char wall[15][26]={
      {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,1,1,1,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,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,1},
      };
      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("*");
      gotoxy(3,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) return 72;
      if(ch==75) return 75;
      if(ch==77) return 77;
      if(ch==80) return 80;
      }
      else break;
      }
      }

      /* function for move */
      // up=72 , down=80 , left=75 , right=77
      move(){

      if (arrow_keys()==75)
      {
      clrscr();
      printf("bvccvbncvnvbnvb");
      }
      }

      N Offline
      N Offline
      Nilesh Hamane
      wrote on last edited by
      #2

      Only by reading the input keys, **(worm) will not move, for that you need to add some code here

      if (arrow_keys()==75)
      {
      // worm moving logic will be here
      }

      Don't die, until you do. http://nnhamane.googlepages.com

      1 Reply Last reply
      0
      • H hasani2007

        Hello It's a maze program,but is not completed. I want to define move function to move the ** (worm). when I ask the value of arrow_keys() it has no response(see move function) in other word move function doesn't work(it is not completed).

        #include <stdio.h>
        #include <conio.h>

        /* prototypes */
        create_wall();
        arrow_keys();
        move();
        /* end of prototypes */

        void main()
        {
        create_wall();
        arrow_keys();
        move();
        }

        /* function to create the walls */
        create_wall(){
        clrscr();
        int i,j;
        char wall[15][26]={
        {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,1,1,1,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,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,1},
        };
        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("*");
        gotoxy(3,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) return 72;
        if(ch==75) return 75;
        if(ch==77) return 77;
        if(ch==80) return 80;
        }
        else break;
        }
        }

        /* function for move */
        // up=72 , down=80 , left=75 , right=77
        move(){

        if (arrow_keys()==75)
        {
        clrscr();
        printf("bvccvbncvnvbnvb");
        }
        }

        C Offline
        C Offline
        chandu004
        wrote on last edited by
        #3

        if i understood it correctly, your problem is that, you are unable to scan the keys Right??. i dont think getch will do for you here in this context. there is some other mechanism to get the key code here. let me refer my backups and let you know. parallelly, you can also try it.

        -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

        1 Reply Last reply
        0
        • H hasani2007

          Hello It's a maze program,but is not completed. I want to define move function to move the ** (worm). when I ask the value of arrow_keys() it has no response(see move function) in other word move function doesn't work(it is not completed).

          #include <stdio.h>
          #include <conio.h>

          /* prototypes */
          create_wall();
          arrow_keys();
          move();
          /* end of prototypes */

          void main()
          {
          create_wall();
          arrow_keys();
          move();
          }

          /* function to create the walls */
          create_wall(){
          clrscr();
          int i,j;
          char wall[15][26]={
          {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,1,1,1,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,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,1},
          };
          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("*");
          gotoxy(3,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) return 72;
          if(ch==75) return 75;
          if(ch==77) return 77;
          if(ch==80) return 80;
          }
          else break;
          }
          }

          /* function for move */
          // up=72 , down=80 , left=75 , right=77
          move(){

          if (arrow_keys()==75)
          {
          clrscr();
          printf("bvccvbncvnvbnvb");
          }
          }

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          hasani2007 wrote:

          when I ask the value of arrow_keys() it has no response...

          Possibly because not every logic path has a return value. If the first getch() returns a non-zero value, the while() loop breaks and arrow_keys() will return what?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Man who follows car will be exhausted." - Confucius

          1 Reply Last reply
          0
          • H hasani2007

            Hello It's a maze program,but is not completed. I want to define move function to move the ** (worm). when I ask the value of arrow_keys() it has no response(see move function) in other word move function doesn't work(it is not completed).

            #include <stdio.h>
            #include <conio.h>

            /* prototypes */
            create_wall();
            arrow_keys();
            move();
            /* end of prototypes */

            void main()
            {
            create_wall();
            arrow_keys();
            move();
            }

            /* function to create the walls */
            create_wall(){
            clrscr();
            int i,j;
            char wall[15][26]={
            {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,1,1,1,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,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,1},
            };
            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("*");
            gotoxy(3,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) return 72;
            if(ch==75) return 75;
            if(ch==77) return 77;
            if(ch==80) return 80;
            }
            else break;
            }
            }

            /* function for move */
            // up=72 , down=80 , left=75 , right=77
            move(){

            if (arrow_keys()==75)
            {
            clrscr();
            printf("bvccvbncvnvbnvb");
            }
            }

            S Offline
            S Offline
            Software_Developer
            wrote on last edited by
            #5

            Hello hasani2007. Nice work on the maze.

            #include <windows.h>
            #include <stdio.h>
            #include <stdlib.h>
            #include <conio.h>

            #define UP 72
            #define DOWN 80
            #define LEFT 75
            #define RIGHT 77
            #define SPACE 32
            #define ENTER 13
            #define ESCAPE 27

            /******************/
            /* */
            /******************/

            typedef struct t_worm
            { int x,y,dirx,diry; } ;

            t_worm worm;
            int ch;
            int i,j;
            char wall[15][26]={
            {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,1,1,1,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,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,1},
            };

            /******************/
            /* */
            /******************/

            /* prototypes */
            void create_wall();
            void arrow_keys();
            void move();
            void clrscr();
            void gotoxy(int x, int y);
            /* end of prototypes */

            /******************/
            /* */
            /******************/

            void main()
            {
            create_wall();

            while(ch!=ESCAPE)
            {
            arrow_keys();
            move();
            }

            gotoxy(1,21);printf("\\n");
            

            }

            /* function to create the walls */

            /******************/
            /* function for */
            /******************/

            void create_wall(){
            clrscr();

            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("*");
            gotoxy(3,2);
            printf("*");

            }

            /* end of create_wall function */
            ////////////////////////////////////
            /* function to define arrow keys */

            /******************/
            /* function for */
            /******************/

            void arrow_keys()
            {

            if (kbhit())
            {
               ch=getch();
               switch (ch)
               {
                   /////////////////////
                   case UP:
                          worm.y--;
            	   break;
                   /////////
            
            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