dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Pleas help out a beginner's maze program

  1. #1
    Join Date
    Mar 2008
    Posts
    2

    Pleas help out a beginner's maze program

    Im trying to program a maze. It works but for some reason it stops in the middle of the thing. Its suppose to find the 2 but it dosent...



    class maze{

    public static void main(String args[]){

    int mr = 0;
    int mc = 0;
    int m[][] = {{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {0,1,1,2,0,0,0,0,1,1,1,1,1,1,1},
    {0,1,1,1,1,1,1,0,1,1,1,1,1,1,1},
    {0,0,0,0,0,1,1,0,1,1,1,1,1,1,1},
    {1,0,1,1,0,1,1,0,1,1,1,1,0,1,1},
    {1,1,1,1,0,1,1,0,1,1,1,1,0,1,1},
    {1,1,1,1,0,1,1,0,1,1,1,1,0,1,1},
    {1,1,1,1,0,0,0,0,0,0,0,0,0,1,1},
    {1,1,1,1,1,1,1,0,1,1,1,1,1,1,1},
    {1,1,1,1,1,1,1,0,1,1,1,1,1,1,1},
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};


    int ctr = 0;

    while ( ctr < 100 ){
    if (m[mr][mc] == 2 ){
    System.out.print("Cheese found");
    }
    else if (m[mr+1][mc] ==0 ){
    ++mr;
    }
    else if (m[mr][mc+1] !=1 ){
    ++mc;
    }
    else if (m[mr-1][mc] != 1 ){
    --mr;
    }
    else if (m[mr][mc-1] != 1 ){
    --mc;
    }

    m[mr][mc]= 3;

    ++ctr;

    for ( int r = 0; r < m.length; ++r){

    for( int c = 0; c < m[0].length; ++c)

    System.out.print( m[r][c] + " ");

    System.out.println();
    }

    System.out.println();

    }//end while loop

    } //end main

    }//end class

  2. #2
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923

    Re: Pleas help out a beginner's maze program

    It's probably because you try to access an index out of bounds. You do stuff like: m[mr+1][mc] or m[mr-1][mc]. If mr is 0 or is mr+1 is outside the array, you will have problem. To solve that, you need to carefully verify those values before trying to access the array.

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  3. #3
    Join Date
    Mar 2008
    Posts
    2

    Re: Pleas help out a beginner's maze program

    thanks ill look into that.

    just wanted to show my problem. When i run it it turns out like this

    0 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    3 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    3 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    3 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    3 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    3 1 1 2 0 0 0 0 1 1 1 1 1 1 1
    3 1 1 1 1 1 1 0 1 1 1 1 1 1 1
    3 3 3 3 3 1 1 0 1 1 1 1 1 1 1
    1 3 1 1 3 1 1 0 1 1 1 1 3 1 1
    1 1 1 1 3 1 1 0 1 1 1 1 3 1 1
    1 1 1 1 3 1 1 0 1 1 1 1 3 1 1
    1 1 1 1 3 3 3 3 3 3 3 3 3 1 1
    1 1 1 1 1 1 1 3 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 3 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    as you can see it stops without reaching 2.

    but when i change up the maze a bit like this...
    0 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    3 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    3 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    3 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    3 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    3 1 1 2 0 0 3 3 1 1 1 1 1 1 1
    3 1 1 1 1 1 1 3 1 1 1 1 1 1 1
    3 3 3 3 3 1 1 3 1 1 1 1 1 1 1
    1 3 1 1 3 1 1 3 1 1 1 1 1 1 1
    1 1 1 1 3 1 1 3 1 1 1 1 1 1 1
    1 1 1 1 3 1 1 3 1 1 1 1 1 1 1
    1 1 1 1 3 3 3 3 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 3 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 3 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

    it works better but it still dosen't complete.

  4. #4
    Join Date
    Mar 2008
    Posts
    9

    Re: Pleas help out a beginner's maze program

    One of your problems is that your 'mouse' doesn't backtrack once it hits a dead end. That's why it ends the way it does in your first example.

    edit: alright maybe it is somewhat. it would be less confusing if you could tell exactly where the mouse was in any given step.
    Last edited by Phlox; March 30th, 2008 at 03:34 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)