Thread: help w/ 2dimensional array maze

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    27

    help w/ 2dimensional array maze

    just trying to finish up this maze.. getting a little fustrated when getting close to completion. Anyone? I bolded the areas I'm a little lost on how to complete.

    Code:
    const int ROW_SIZE = 20, COL_SIZE = 17; 
    void printMaze(char [][COL_SIZE], char *, char);
    bool isMoveLegal(char );
    
    int _tmain(int argc, _TCHAR* argv[])
    {
       char board[ROW_SIZE][COL_SIZE] = {{'+','-','-','-','-','-','-','-','+','-','-','-','-','-','+','-','+'},
                                          {'|',' ',' ',' ',' ',' ',' ',' ','|',' ',' ',' ',' ',' ','|',' ','|'},
                                          {'|',' ','+','-','-','-','+',' ','|',' ','+','+',' ','+','+',' ','|'},
                                          {'|',' ','|',' ',' ',' ',' ',' ',' ',' ','|',' ',' ',' ','|',' ','|'},
                                          {'|',' ','|',' ','+',' ','+','-','+','-','+',' ','+',' ','+',' ','|'},
                                          {'|',' ','|',' ','|',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' ','|'},
                                          {'|',' ','+','-','+','-','+',' ','+','-','-','-','+',' ','+','-','+'},
                                          {'|',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' ','|',' ','|',' ','|'},
                                          {'+','-','-','-','+',' ','+','-','+','-','+',' ','+',' ','|',' ','|'},
                                          {'|',' ',' ',' ','|',' ','|',' ',' ',' ','|',' ',' ',' ','|',' ','|'},
                                          {'|',' ','+',' ','|',' ','+',' ','+','-','+',' ','+','-','+',' ','|'},
                                          {'|',' ','|',' ','|',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' ','|'},
                                          {'+','-','+',' ','|',' ','+','-','+','-','+',' ','+',' ','+','-','+'},
                                          {'|',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' ',' ',' ','|',' ','|'},
                                          {'+','+','+',' ','+','+','-','-','+',' ','+','+','-','-','+',' ','|'},
                                          {'+','+',' ',' ',' ','+',' ',' ',' ',' ',' ','|',' ',' ',' ',' ','|'},
                                          {'|',' ',' ','+',' ',' ',' ',' ','+','+',' ','|',' ','+','+',' ','|'},
                                          {'|',' ','+','+','-','+','-',' ','+','+',' ','+',' ','+','+',' ','|'},
                                          {'|',' ',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' ',' ','|','X','|'},
                                          {'+','-','-','-','-','+','-','-','-','+','-','-','-','-','+','-','+'}};
      
        char *position;
        position = &board[1][1];
        char symbol='^';
        
        while (!GetAsyncKeyState(VK_ESCAPE) && *position != 'X')
        {
            printMaze(board, position, symbol);
            char *nextPosition = position;
    
            
            if (GetAsyncKeyState(VK_UP))
            {
    		symbol = '^';
    // not complete need to update 
    	           
            }
            else if (GetAsyncKeyState(VK_DOWN))
            {
    		symbol = 'v';
            }
            else if (GetAsyncKeyState(VK_RIGHT))
            {
    		symbol = '>';
            }
            else if (GetAsyncKeyState(VK_LEFT))
            {
    			symbol = '<';
    	}
        
           // call isMoveLegal function to see if you can move to the new            // position. If so, then update the value of the position                 // variable to be equal to the nextPosition variable
    
            Sleep(100);
        }
    
        // probably insert if position of X = winning position cout you win     //etc.. 
    
        system("pause");
    }
    
    bool isMoveLegal(char Target)
    {
        bool result = false;
        
        if (Target == ' ' || Target == 'X') 
        {
            result = true;
        }
    
        return result;
    }
    
    void printMaze(char board[][COL_SIZE], char *player, char symbol)
    {
          System::Console::SetCursorPosition( 0, 0);

  2. #2
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Well, you're going to update the position as to where you're "pointing" correct? You know where you are, so if you go up, how does that change your [row][column] position? Same for the other 3 directions.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Quote Originally Posted by kcpilot View Post
    Well, you're going to update the position as to where you're "pointing" correct? You know where you are, so if you go up, how does that change your [row][column] position? Same for the other 3 directions.
    with that area of the loop. I'm just adding 1 to the column or row. problem being that I'm looking over my code and notice that I can't use a definate position #. Need to use the current position and change that.. the value must then be carried all through the program.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think that instead of keeping position as a char, maybe you should keep position as an ordered pair. Knowing that current position is ' ' doesn't tell you which ' ' you're standing at. You should keep the current [row][column] somewhere in some variables (you could maybe even call them row and column).

  5. #5
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Any maze has a starting and ending point (in a row/column position). So wherever you start from that is your row/column position and you update from that to determine your 'movement' in the maze (after you've checked to see if it would be a valid move, right?).

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Quote Originally Posted by kcpilot View Post
    Any maze has a starting and ending point (in a row/column position). So wherever you start from that is your row/column position and you update from that to determine your 'movement' in the maze (after you've checked to see if it would be a valid move, right?).
    no you're absolutely correct.. just as starting point is [1,1]. You location would always change the movement goes along. 17 columns, 20 rows.. as long as I'm not allowing myself to go through walls.. which is the reason for the function isMoveLegal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM