Thread: c++- Please helmp me with making a "Moles and Mazes" program

  1. #16
    Registered User
    Join Date
    Sep 2010
    Posts
    31
    Quote Originally Posted by nick753 View Post
    I also don't know why it wins if row[j] = 3 and column[j] = 8. Isn't the column supposed to be column[j] = 9? Again thank you for your help!
    He misunderstood your set-up. He is treating the whole grid like a perfect square, where one of the gridspaces on the outside edge is the exit point, rather than having one extra square "hanging loose". You need to give the grid 9 columns, but remember to treat all but one of the gridsquares in that column as off the map. You could account for this with something to the effect of:

    Code:
    if (COLUMN == 9 && ( ROW < 3 || ROW > 3) ) { Fail; }

  2. #17
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Here is my most up to date code. It runs but it doesn't give me any winning output. Right now I have it set to just display 3,9 every time it wins because I don't know how to make it display the whole path. Also, I still don't know what row[j] = row[j-1] means. Again, any help is appreciated! Please help me find errors:

    Code:
    #include<iostream>
    #include<ctime>
    using namespace std;
    int main()
    {
    
    	int row[100] = {0};
    	int column[100] = {0};
    	int rowstart = 3;
    	int columnstart = 2;
    	int k;
    	
    	
    	for(int i = 0; i < 500; i++)
    	{
    		row[0] = 3;
    		column[0] = 2;
    		
    		
    
    		for(int j = 0; j < 100; j++)
    		{
    			srand(time(NULL));
    			k = rand() % 4 + 1;
    			
    			if (k ==1 ) // Move Right
    			{
    				row[j] = row[j-1];
    				column[j] = column[j-1] + 1;
    			}
    			else if(k==2) //Move Up
    			{
    				row[j] = row[j-1] - 1;
    				column[j] = column[j-1];
    			}
    
    			else if(k==3) // Move Left
    			{
    				row[j]= row[j-1];
    				column[j]= column[j-1] - 1;
    			}
    
    			else { // move down 
    					row[j] = row[j - 1] + 1;
    					column[j] = column[j - 1];
    			}
    			
    			if(row[j] < 1 || row[j] > 8)
    			{
                          row[0] = 3;
                          column[0] = 2;
                }
                
                else if ( column[j] > 8 && row[j] != 3)
                     {
                        row[0] = 3;
                        column[0] = 2;
                        }
                        
                else if(column[j] == 9 && row[j] == 3)
                {
                     cout << row[j] << column[j];
                }
                
                else if(column[j] < 1)
                {
                row[0] = 3;
                column[0] = 2;
                }
    		}
    	}
    		system("pause");
    		return 0;
    	}

  3. #18
    Registered User
    Join Date
    Sep 2010
    Posts
    31
    Quote Originally Posted by nick753 View Post
    Here is my most up to date code. It runs but it doesn't give me any winning output. Right now I have it set to just display 3,9 every time it wins because I don't know how to make it display the whole path. Also, I still don't know what row[j] = row[j-1] means.
    Every gridspace has two coordinates, ROW and COLUMN. The moves are stored in corresponding slots of both ROW and COLUMN arrays, namely slot J, where J is the number of completed iterations. By referring to slot [J - 1], you are calling on the coordinates of the previous move. This is useful because it allows you to calculate the present location relatively, instead of being forced to use absolute values. Here is a brief illustration:

    Move #0: J = 0: Coordinates are (3,2).
    Move #1: J = 1: (let's say the mole moves to the right.) Relative coordinates are (+1,0) We take the coordinates from J-1 (which is 0), and add to them the new relative coordinates, you get (4,2).
    Move #2: J = 2: (mole moves up). Relative coordinates are (0,+1). Take coordinates from J-1 and add to them the relative coordinates. You get (4,3).

    Hopefully that's cleared up! Now, to print the whole path, you just have to create a for() loop that runs indefinitely until the coordinates of slot [ i ] are the winning coordinates.
    Code:
    for (int i = 0; /*blank*/ ; i++) {
    cout << row[i] << "," << column[i] << endl;
    if (row[i] == 3 && column[i] == 9)
        break;
    }

  4. #19
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Thank you, Frog! I do understand now why j-1 makes sense! By looking at my code, can you tell me if I had the right idea about resetting the starting points if it goes outside the boundaries? I'm thinking that may be my program doesn't work properly? Thank you!

  5. #20
    Registered User
    Join Date
    Sep 2010
    Posts
    33
    You’re doing fine, keep going, I’m not very good at explaining things, lol.

    Ok, so it’s an 8*8 grid and if the mole is at position (3,8) and the next move is to the right, then he reaches the escape square 3,9.

    Thanks frog.

    Here’s some code that works. I’ve tried to keep it simple by checking for a successful attempt at the end rather than in the check move right code, and no else statements.

    Ignore the counters, cnt_fr (failed right) etc.

    Understanding j-1 is the most important part, so keep asking until you really get it.

    Code:
    srand((unsigned)time(0));
    
    for (int i = 0; i < 500; i++)
    {
        int row[100] = {0};
        int col[100] = {0};
        row[0] = 3;
        col[0] = 2;
    
        for (int j = 1; j < 100; j++) //
        {
            move = rand() % 4 + 1;
            if (move == 1)  //move right
            {
                row[j] = row[j-1];
                col[j] = col[j-1] + 1;
                //if (col[j] > 8 )
                if ((col[j] > 8) && (row[j] != 3))
                {
                    cnt_fr = cnt_fr + 1;
                    break; // outside right boundary
                }
            }
            if (move == 2) // move up
            {
                row[j] = row[j-1] - 1;
                col[j] = col[j-1];
                if (row[j] < 1)
                {
                    cnt_fu = cnt_fu + 1;
                    break;
                }
            }
            if (move == 3) // move left
            {
                row[j] = row[j-1];
                col[j] = col[j-1] - 1;
                if (col[j] < 1)
                {
                    cnt_fl = cnt_fl + 1;
                    break;
                }
            }
            if (move == 4) // move down
            {
                row[j] = row[j-1] + 1;
                col[j] = col[j-1];
                if (row[j] > 8)
                {
                    cnt_fd = cnt_fd + 1;
                    break;
                }
            }
            //if (row[j] == 3 && col[j] == 8)
            if (row[j] == 3 && col[j] == 9)

  6. #21
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Michael, I see after the mole crosses a boundary you have a break statement. Will the break statement reset the mole at the beginning coordinates then make the mole start a new game?

  7. #22
    Registered User
    Join Date
    Sep 2010
    Posts
    33
    Yes.

    But, the break statement simply breaks out of the current loop.

    In the above code the next statement to be executed would be the main for loop with i incremented by 1, and off we go again.

  8. #23
    Registered User
    Join Date
    Oct 2010
    Posts
    45

    Grid

    Not that I am anything beyond a beginner here, but shouldn't he be using a 10x10 grid. With 0 and 9 being out of bounds and 1-8 being the grid itself?

    I think the problem is a silly one as well, since as someone pointed out right away you could infinately move around within the grid without either falling out or exiting... Your teacher should clarify this.

  9. #24
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Quote Originally Posted by Michael432000 View Post
    Yes.

    But, the break statement simply breaks out of the current loop.

    In the above code the next statement to be executed would be the main for loop with i incremented by 1, and off we go again.
    So would it be better for me to put in break statements instead of the method I'm using?

  10. #25
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Here is my latest code guys. While looking through it I noticed something. Where in the heck in my program is it saying what size the grid needs to be? I would have thought to put it inside the brackets of row[] and column[], but someone said to put 100 in there to make that the number of moves it makes. So where/how would I go about setting up my grid size?? My program as of now seems like it makes sense, but it never prints out when it escapes, but that's probably because no grid is set up? Please take a look at tell me what you think. Thanks for the help!

    Code:
    #include<iostream>
    #include<ctime>
    using namespace std;
    int main()
    {
    
        int row[100] = {0};
        int column[100] = {0};
        int rowstart = 3;
        int columnstart = 2;
        int k;
        
        
        for(int i = 0; i < 500; i++)
        {
            row[0] = 3;
            column[0] = 2;
            
            
    
            for(int j = 0; j < 100; j++)
            {
                srand(time(NULL));
                k = rand() % 4 + 1;
                
                if (k ==1 ) // Move Right
                {
                    row[j] = row[j-1];
                    column[j] = column[j-1] + 1;
                    if(column[j] > 8 && row[j] != 3) { break; }
                }
                else if(k==2) //Move Up
                {
                    row[j] = row[j-1] - 1;
                    column[j] = column[j-1];
                    if(row[j] < 1) { break; }
                }
    
                else if(k==3) // Move Left
                {
                    row[j]= row[j-1];
                    column[j]= column[j-1] - 1;
                    if(column[j] < 1) { break;}
                }
    
                else { // move down 
                        row[j] = row[j - 1] + 1;
                        column[j] = column[j - 1];
                        if(row[j] > 8) { break; }
                     }
                
                
                        
                 if(column[j] == 9 && row[j] == 3)
                {
                     cout << row[j] << column[j];
                }
                
                
            }
        }
            system("pause");
            return 0;
        }

  11. #26
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by nick753 View Post
    Here is my latest code guys. While looking through it I noticed something. Where in the heck in my program is it saying what size the grid needs to be? I would have thought to put it inside the brackets of row[] and column[], but someone said to put 100 in there to make that the number of moves it makes. So where/how would I go about setting up my grid size?? My program as of now seems like it makes sense, but it never prints out when it escapes, but that's probably because no grid is set up? Please take a look at tell me what you think. Thanks for the help!
    Well, I can answer one question: Look at your own code. The grid is set up with the series of if...than checks you have when you parse the random value. For example, in the block:

    Code:
    if (k ==1 ) // Move Right
                {
                    row[j] = row[j-1];
                    column[j] = column[j-1] + 1;
                    if(column[j] > 8 && row[j] != 3) { break; }
                }
    The line, if (column[j] > 8 && row[j] != 3) is your boundary check. That's essentially saying, "If the mole is outside of the grid on the right hand side, and is not in the third row down, then the mole has fallen off the grid and fails."

    As for why it's not printing, that's because you break out of the loop you're in before the call to print ever happens. Remember, a break statement is saying, "Short-circut the rest of the loop and exit now." So in your code, since your block:

    Code:
     if(column[j] == 9 && row[j] == 3)
                {
                     cout << row[j] << column[j];
                }
    is inside of the loop you just broke out of, it'll never get called. Think about where you could place it where it will be called once you break out of the inner loop. (Although I can also see that you aren't checking for both a win condition or a loss condition and handling them - you need to look into doing that.)

    There's quite a few bugs that you have lurking around there, to be honest. Maybe you should take a step back and write everything out in psuedocode so that you can more clearly see where you're heading in your program.

  12. #27
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    I have moved the statement to print the result inside the 'move right' statement. But, it still will not print anything. I'm not sure if there is a better spot or not, but it seems pretty logical to put it there?

    Code:
    [if (k ==1 ) // Move Right
    			{
    				row[j] = row[j-1];
    				column[j] = column[j-1] + 1;
                       
    				  if(column[j] > 8 && row[j] != 3) { break; }
    				  else if(column[j] == 9 && row[j] == 3)
                       cout << row[j] << column[j];

  13. #28
    Registered User
    Join Date
    Sep 2010
    Posts
    33
    Don’t worry; when you get it working and the results are printed out it will make more sense to you.

    Don’t forget, you are only printing out the results for successful attempts, so if the mole crosses a boundary you just ignore it (break) and restart the j loop.

    I put the successful attempt check at the end of the loop simply to make it easier for you to understand. You are now putting it in the correct place but you need a separate loop to print out the whole successful sequence from the beginning, then you need to break out of the j loop.

    Using a function just keeps the code tidier but it’s ok to write the loop where you do your check.

    Code:
            if (row[j] == 3 && col[j] == 9) // successful attempt
                {
                   successful_attempt(row, 100, col, 100, j + 1);
                   cnt_sa = cnt_sa + 1;
                   break;
                }
               
    
    
    
    
    void successful_attempt(int arg1[], int len1, int arg2[], int len2, int max)
    {
        cout << "*** Successful Attempt ***" << endl;
        for (int j= 0; j < max; j++)
        {
        if (j % 13 == 0 ) // display 13 coordinates per line
            cout << "(" << arg1[j] << "," << arg2[j] << ") " << endl;
        else
            cout << "(" << arg1[j] << "," << arg2[j] << ") ";
        }
        cout << endl << endl;
    
    }


    Note1: srand(time(NULL)); goes at the start of the program before the loops.

  14. #29
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Thank you Michael. The reason I'm not printing out the results yet is because I can't even get it to print the coordinates when it wins(which in my program, is never). So obviously I'm doing something wrong to where my program never can win. Maybe because it only does 100 moves? I don't know but here is my code.

    Code:
    #include<iostream>
    #include<ctime>
    using namespace std;
    int main()
    {
    
    	int row[100] = {0};
    	int column[100] = {0};
    	int rowstart = 3;
    	int columnstart = 2;
    	int k;
    	srand(time(NULL));
    	
    	for(int i = 0; i < 500; i++)
    	{
    		row[0] = 3;
    		column[0] = 2;
    		
    		
    
    		for(int j = 0; j < 100; j++)
    		{
    			
    			k = rand() % 4 + 1;
    			
    			if (k ==1 ) // Move Right
    			{
    				row[j] = row[j-1];
    				column[j] = column[j-1] + 1;
                       
    				  if(column[j] > 8 && row[j] != 3) { break; }
    				  else if(column[j] == 9 && row[j] == 3)
                       cout << row[j] << column[j];
    			}
    			else if(k==2) //Move Up
    			{
    				row[j] = row[j-1] - 1;
    				column[j] = column[j-1];
    				if(row[j] < 1) { break; }
    			}
    
    			else if(k==3) // Move Left
    			{
    				row[j]= row[j-1];
    				column[j]= column[j-1] - 1;
    				if(column[j] < 1) { break;}
    			}
    
    			else { // move down 
    					row[j] = row[j - 1] + 1;
    					column[j] = column[j - 1];
    					if(row[j] > 8) { break; }
    			     }
    			
    			
                        
                
                
                
    		}
    	}
    		system("pause");
    		return 0;
    	}

  15. #30
    Registered User
    Join Date
    Sep 2010
    Posts
    33
    This should get something printing out for you, which will hopefully make it more clear.

    Code:
    int main()
    {
    
    	////////////int row[100] = {0};
    	////////////int column[100] = {0};
    	////////////int rowstart = 3;
    	////////////int columnstart = 2;
    	int k;
    	srand(time(NULL));
    
    	for(int i = 0; i < 500; i++)
    	{
            int row[100] = {0};         ////////
            int column[100] = {0};      ////////
    		row[0] = 3;
    		column[0] = 2;
    
    
    
    		//////////////for(int j = 0; j < 100; j++)
    		for(int j =1; j < 100; j++)
    		{
    
    			k = rand() % 4 + 1;
    
    			if (k ==1 ) // Move Right
    			{
    				row[j] = row[j-1];
    				column[j] = column[j-1] + 1;
    
    				  if(column[j] > 8 && row[j] != 3) { break; }
    				  else if(column[j] == 9 && row[j] == 3)
                       ////////////////////////////cout << row[j] << column[j];
    
    
                                        //Add this here
                                        {
                                            cout << "*** Successful Attempt ***" << endl;
                                            for (int n = 0; n < (j+1); n++)
                                            {
                                                if (n % 13 == 0 ) // display 13 coordinates per line
                                                    cout << "(" << row[n] << "," << column[n] << ") " << endl;
                                                else
                                                    cout << "(" << row[n] << "," << column[n] << ") ";
                                            }
                                            cout << endl << endl;
                                            break;
                                        }
    
    
    
    
    
    			}
    			else if(k==2) //Move Up
    			{
    				row[j] = row[j-1] - 1;
    				column[j] = column[j-1];
    				if(row[j] < 1) { break; }
    			}
    
    			else if(k==3) // Move Left
    			{
    				row[j]= row[j-1];
    				column[j]= column[j-1] - 1;
    				if(column[j] < 1) { break;}
    			}
    
    			else { // move down
    					row[j] = row[j - 1] + 1;
    					column[j] = column[j - 1];
    					if(row[j] > 8) { break; }
    			     }
    
    
    
    
    
    
    		}
    	}
    		system("pause");
    		return 0;
    	}

Popular pages Recent additions subscribe to a feed