2 Dimensional Arrays

Mar 24, 2014 at 3:03am
Any ideas on how to simulate a random walk using 2D arrays? You can only go up, down, left or right. You would start in the center and the direction you moved would be from a random number generator.

For example:

00000
00000
00110
00010
00000

The 1's are a counter for how many times that space has been "visited". I have an idea of how it might work but i can't quite figure it out. Any ideas?
Mar 24, 2014 at 10:32am
Keep track of current position (row,col). Each step changes position and then increases the counter of the new position.

Check for being on edge before moving.
Mar 24, 2014 at 1:58pm
I've got the random walk working but how would I check to see if it was on the edge?
Mar 24, 2014 at 2:44pm
You have the size of the array, right? You can simply check to see, for example, if you are at x=0 (left edge), y=0 (top edge), x=size-1 (right edge) and y=size-1 (bottom edge). If you are on an edge, generate a direction like normal, but if that direction would cause you to go out of bounds simply perform the calculation again until you have a valid direction.
Mar 24, 2014 at 2:51pm
Thank you!
Topic archived. No new replies allowed.