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

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    120

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

    I just started to learn arrays in my Computer Science class. Well now I have to write a game called "Moles and Mazes." The mole is in a grid with 8 rows and 8 columns. The mole starts his adventure at row 3, column 2. The escape route is at row 3, column nine.

    The program will generate a random number between 1 and 4(I know how to do that). Number 1=right, 2=up, 3=left, 4=down. (I know how to do this, I think. I was going to try and use a case statement, or an if statement.

    I do not know how to begin the program though, and here is why. The program is supposed to play 500 games and print out each escape path using a coordinate system (3, 2) (3,4) (4,4)..... (3,9)

    The next part says to store the game paths in two separate one dimensional arrays named row and column.

    So basically I don't know how to make it play a max of 500 games and display how many games were won and how the mole achieved to win(show the coordinates). I also don't know how to score the escape path into the two arrays. Can someone please help me get started with this? I usually post my code when I have a question but this one has me stuck. Thank you!
    Last edited by nick753; 10-06-2010 at 06:32 PM.

  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    31
    Have you covered Classes in your course yet? If so, you could create a Path class which consists of Xs and Ys, and create an array which stores Path objects. You'd probably have to use a vector, though, to avoid wasting mass amounts of memory, unless you maybe only stored a single path before printing it to file. Have you learned file I/O yet? I think that would be the most sensible way to attack this problem.

    But I mean, if you were to use ONLY straight-up arrays, one for keeping track of all Xs and one for all Ys, I really have no clue how to come up with a practical solution. If you haven't covered dynamic memory, vectors, printing to file yet, you've got some serious issues to grapple with. How could you account for the fact that any one run through the maze has the potential to last an infinite (theoretically, IRL at least a large) number of turns?

    I'm thinking we're misunderstanding the specifics of the question. Could you post it exactly as your prof has it worded?

    PS: Don't forget to create a wall. You won't get through one round if you don't have a wall.
    Last edited by frog; 10-06-2010 at 06:15 PM. Reason: More info

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Thanks for the reply, we have not covered classes or vectors or pointers. I forgot to mention that if you go behind column 1, beyond column 8, above row 1, or below row 8, the mole falls of the "grid" and the game is over. So the only way the mole wins is if he ends up in row 3, column 9. But like I said if he ends up in column 9 but in anything other than row 3, he falls off the grid. so basically row 0, row 9, column 0, and column 9(other than column 9, row 3) the game is over and a new one starts.
    Last edited by nick753; 10-06-2010 at 06:17 PM.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    So right now I'm trying to start writing the program. I can't even figure out how to declare the starting position. How do I declare a starting position of row 3 AND column 2? Thanks

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    Oh I re-read your post and I get it... ya.. did you learn ofstream yet?
    Last edited by IKnew; 10-07-2010 at 12:10 AM.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Quote Originally Posted by IKnew View Post
    Oh I re-read your post and I get it... ya.. did you learn ofstream yet?
    No, were basically supposed to do this with some if's and some arrays.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    33
    Why not start off with a simplified version, whereby the number of moves per game is restricted to say 100, else failed attempt. Then you can easily get started with one-dimensional arrays.

    Create your main loop zero to 499.
    Initialize arrays to zero.
    Start position row[0] = 3, col[0] = 2.
    Create an inner loop one to 99.
    Test direction.
    Move accordingly, col[j] = col[j-1] + 1;
    Test for boundary.
    Test for successful attempt.
    Print out number of failed attempts and successful sequence.

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Thanks. Can you tell me what initializing the array to zero is actually doing? Isn't that saying that I'm reserving 0 spots to store data into the array?

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    33
    Start position: row[0] = 3, col[0] = 2 index 0 of row array contains value 3
    Move right: row[1] = 3, col[1] = 3
    Move right: row[2] = 3, col[2] = 4
    move down: row[3] = 4, col[3] = 4
    Move right: row[4] = 4, col[4] = 5
    Move right: row[5] = 4, col[5] = 6
    Move right: row[6] = 4, col[6] = 7
    Move up: row[7] = 3, col[7] = 7
    Move left: row[8] = 3, col[8] = 6
    Move right: row[9] = 3, col[9] = 7
    Move right: row[10] = 3, col[10] = 8 successful attempt.

    Attempt number 1 of 500 might last 25 moves and then fail, so the array will hold 25 values. If the second run is successful in 12 moves and you didn’t clean out the array before hand, then you will print out the 12 successful moves followed by the last 13 failed attempts.

    So all those values need to reset to zero.

    Just declare your arrays after the main loop and before the shorter loop is the simplest way.

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Here is my attempt so far. I am wondering if someone can look this over and just let me know if i'm even on the right track. I have a few concerns;

    1; My random number generator I believe is flawed because it just goes from one to 4 in order and it displays like a hundred of each, I believe I can fix that pretty easily but I'm wondering if it's in the right spot at ;east.

    2. I don't know how to allow the column to go into row 9 and make it win only if it's at row three.

    3. How am I going to make it print only the winning games coordinates to the screen?

    Thank you guys for your help!

    Code:
    #include<iostream>
    #include<ctime>
    using namespace std;
    int main()
    {
    
    	int row[8] = {1, 2, 3, 4, 5, 6, 7, 8};
    	int column[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    	int rowstart = 3;
    	int columnstart = 2;
    	int r, k, c, e;
    	
    	
    	for(int i = 0; i < 500; i++)
    	{
    		r = rowstart;
    		c = columnstart;
    		
    		
    
    		while(r < 8 || c < 8)
    		{
    		srand(time(NULL));
    		k = rand() % 5;
    		
    		switch(k)
    		  {
    		case 1:
    		column[c+1];
    		case 2:
    		row[r - 1];
    		case 3:
    		column[c - 1];
    		case 4:
    		row[r + 1];
    
    		cout << k << endl;
    		  }
    		}
    	}
    	
    		system("pause");
    		return 0;
    	}
    Last edited by nick753; 10-08-2010 at 11:01 AM.

  11. #11
    Registered User
    Join Date
    Sep 2010
    Posts
    31
    You should be including <cstdlib> to use srand() and rand().

  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    33
    First thing, decide whether your grid is 8X8 or 9X9, you keep changing.

    Now on a piece of paper, draw a grid like a chessboard, 8x8.

    Now number the squares along the top from 1 to 8 going from left to right.
    Now number the squares on the left side from 1 to 8 going from top to bottom.

    We can reference the top left square with the coordinate (1,1).
    We can reference the top right square with the coordinate (1,8).
    We can reference the bottom left square with the coordinate (8,1), etc.

    We can reference the starting position of your mole with the coordinates (3,2).

    I’m assuming you are going to keep this simple to start off, and not dynamically allocating memory for your arrays, and you are restricting the number of moves per game to say 100.

    Code:
    int row[100] = {0}; 
    int col[100] = {0};
    
    row[0] = 3;
    col[0] = 2;
    The first occurrence of your row array, index 0, holds the start position 3.
    The first occurrence of your col array, index 0, holds the start position 2.

    Put a mark on your diagram at position (3,2).

    Now if we get a random number 1 then we will move 1 square to the right.
    So mark the new position on your diagram as (3,3).
    So now the second occurrence of your array row[1] = 3 and col[1] = 3.

    Now if we get a random number 2 then we will move 1 square up.
    So mark the new position on your diagram as (2,3).
    So now the second occurrence of your array row[2] = 2 and col[2] = 3.

    So in a loop, if j = 0, then row[j] = 3 and col[j] = 2.
    So in a loop, if j = 1, then row[j] = 3 and col[j] = 3.
    So in a loop, if j = 2, then row[j] = 2 and col[j] = 3 etc.

    So we can plot the movement as (3,2) (3,3) (2,3) etc.

    Now if we now get a random number 1 then we will move 1 square to the right.

    Put a mark on your diagram at new position (2,4)

    So now j = 3 in loop, and value in row[3] stays the same as value in row[2], so row[j] = row[j-1].
    And value in col[3] = value in col[2] incremented by 1, so co[j] = col[j-1] + 1.

    So your inner loop might look like this if you used a for loop:

    Code:
    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 )
            {
                break; // outside right boundary
            }
        }
    Last edited by Michael432000; 10-08-2010 at 12:42 PM.

  13. #13
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Michael, I cannot thank you enough for all your help. I took your advice and came up with some code. It compiles and everything, but when I print the random numbers to the screen, it displays like a hundred of each number before moving to the next number, I'm pretty sure it does this because it can only generate one new random number per second, so for one second it prints that one random number a bunch of times. Is there anyway to fix that? Below is my code, can you please tell me if I'm on the right track so far? Thank you guys again!!

    P.S. When I loop it and the row does not change, why is it row[j] = row[j - 1]. Where does the -1 come from? Is it because the spot in the array is one less than j because the array starts at zero and not one?

    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;
    			cout << k << endl;
    			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];
    			}
    
    		}
    	}
    		system("pause");
    		return 0;
    	}
    Last edited by nick753; 10-08-2010 at 02:49 PM.

  14. #14
    Registered User
    Join Date
    Sep 2010
    Posts
    33
    Call to srand() goes at start of program before the loops.

    Make sure you understand what the rand statement is doing.

    I noticed you coded rand() % 5.

    When rand() returns a value of 10, then 10 divided by 5 = 2, remainder = 0.
    When rand() returns a value of 14, then 14 divided by 5 = 2, remainder = 4.

    0 to 4 = 5 possibilities but we only want 4 possibilities.

    Whilst rand() % 4 + 1:

    When rand() returns a value of 11, then 11 divided by 4 = 2, remainder = 3. and 3 + 1 = 4
    When rand() returns a value of 8, then 8 divided by 4 = 2, remainder = 0, and 0 + 1 = 1.

    1 to 4 = 4 possibilities.

    Call to srand() goes at start of program before the loops.
    Call to rand() goes within the inside loop.

    *

    How to print out winning sequences?

    If ( move == 4)
    Do move down stuff
    if (row[j] == 3 && col[j] == 8)
    loop to print out array from row[0], col[0] to row[max], col[max] where max = J + 1. (J+1 because we assigned start position 3,2 before the j loop)

    Here’s a winning sequence of 6 moves:

    (3,2), and (3,3) (3,4) (3,5) (3,6) (3,7) (3,8)

    We store these values in our array thus:

    Row[0] = 3, col[0] = 2
    Row[1] = 3, col[1] = 3
    Row[2] = 3, col[2] = 4
    Row[3] = 3, col[3] = 5
    Row[4] = 3, col[4] = 6
    Row[5] = 3, col[5] = 7
    Row[6] = 3, col[6] = 8
    Row[7] = 0, col[7] = 0 etc etc

    Row[99] = 0, col[99] = 0

    And print out successful sequence using loop 0 to 6


    *

    row[j] = row[j - 1] because row[j], which is initialized to 0, now holds the same value as the move before, row[j-1].

    *

    Don’t forget to check for boundary’s if (row[j] < 1)

    I’m happy to be of help, but I want you to understand it.
    Last edited by Michael432000; 10-08-2010 at 04:09 PM.

  15. #15
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    I'm sorry for sounding so ignorant about all this, but I really do not understand much of what you are saying. I still cannot figure out where the j-1 comes from.
    I also don't know why row[7] = 0, column[7] = 0. I also tried to make a loop to loop and print all of the winning games but that didn't go well either. Here is what I'm trying
    Code:
     if(row[j] == 3 && column[j] == 9)
    			{
    			 for (j = 0; j < 100 + 1; j++)
    				 cout << row[j] << "," << column[j] << endl;
    		     }
    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!

Popular pages Recent additions subscribe to a feed