[Help]Random Walk

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crystal2005
    New Member
    • Apr 2007
    • 44

    [Help]Random Walk

    Hi, another thread from me ^_^

    This time i am required to write a “random walk” function across a 10 x 10 array. The array will contain characters (all ‘.’ initially). Until this point, to the best of my knowledge, i just create 2D array and assign all [10][10] with '.' initial.

    Code:
    		for(i=0;i<10;i++)
    		{
    			for(j=0;j<10;j++)
    			{
    				a[i][j] = '.';
    			}
    		}

    The function must randomly “walk” from element to element, always going up, down, left, or right by one element. The elements visited by the program will be labeled with the letters A through Z, in the order visited.

    So the output should look like this

    Code:
    A . . . . . . . . .
    B C D . . . . . . .
    . F E . . . . . . .
    H G . . . . . . . .
    I . . . . . . . . .
    J . . . . . . . Z .
    K . . R S T U V Y .
    L M P Q . . . W X .
    . N O . . . . . . .
    . . . . . . . . . .
    At this point i'm completely clueless T_T

    The only thing i can tell is the use of srand or rand function is a must to generate random numbers, However to use those randomly generated numbers, i have no idea. I hope someone can help me, thank you for any suggestion to get this program done.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    There are a few subproblems to solve:

    1) how to generate up/down/left or right randomly;

    This one is easy: define two small arrays:

    Code:
    int dx[]= { 0, 0, -1, 1 } // x component of up/down/left/right
    int dy[]= { -1, 1, 0, 0 } // y component of up/down/left/right
    ...
    int x, y;
    int d= rand()%4; // pick a direction
    
    int new_x= x+dx[d];
    int new_y= y+dy[d];
    The (new_x, new_y) position is selected randomly; now you have to solve:

    2) is the new position on the board;

    You can test the values for < 0 or > 9 but you can also add a one row/column border around your board not containing a dot '.' indicating that you can't move to that position; the empty board will look like this:

    Code:
    - - - - - - - - - - - - 
    - . . . . . . . . . . -
    - . . . . . . . . . . -
    - . . . . . . . . . . -
    - . . . . . . . . . . -
    - . . . . . . . . . . -
    - . . . . . . . . . . -
    - . . . . . . . . . . -
    - . . . . . . . . . . -
    - . . . . . . . . . . -
    - . . . . . . . . . . -
    - - - - - - - - - - - -
    A dot '.' means a 'free' location, any other value means its a 'taken' value. Of course you only print values for coordinates in the range [1, 10] i.e. you never see that border. Doing it this way eliminates checking whether or not you are still 'on the board'.

    Last you have to keep a counter, starting at 'A' and increment it until it reaches the value 'Z'. Those are the values you have to put on the board while moving.

    Take care that you can get 'stuck', i.e.

    Code:
    - - - - - - - - - - - - 
    - A B C . . . . . . . -
    - H I D . . . . . . . -
    - G F E . . . . . . . -
    - . . . . . . . . . . -
    - . . . . . . . . . . -
    - . . . . . . . . . . -
    - . . . . . . . . . . -
    - . . . . . . . . . . -
    - . . . . . . . . . . -
    - . . . . . . . . . . -
    - - - - - - - - - - - -
    You should be able to solve the rest of the details.

    kind regards,

    Jos

    Comment

    • crystal2005
      New Member
      • Apr 2007
      • 44

      #3
      Thanks for the guidance... I'll try my best first :)

      Comment

      Working...