1

I've been trying for several hours to make a random walk (a path) like this one. From top to down.

x  1  x
x  2  3
x  x  4
7  6  5
8  x  x
9  10 x

My greatest difficulty is to calculate the displacement from right to left because the cycles (for, while..) go from left to right.

I am not proficient in math, so I'm using a simple approach. I have two arrays. One with the position of the previous row.

$previousRow=array(1=>"x",2=>"1",3=>"x");

One with the current row I have to fill.

$currentRow=array(1=>"",2=>"",3=>"");

$p   //Is the current position. 1, 2 or 3. Example $currentRow[$p]
$last   //the last number that increases each time the path has a new step.

I'm using some cycles and conditions to set the displacement.

Is this approach wrong?

EDIT: further specifications as requested from comments:

  1. Start point is located in the middle point of the first row
  2. End point is located in the last row
  3. End point can be located in any column of the last row
STT LCU
  • 4,348
  • 4
  • 29
  • 47
Micky
  • 175
  • 1
  • 1
  • 8
  • Can you please share some context for what this is needed? – hakre Sep 03 '13 at 07:03
  • Is the starting point always in the top right corner and the ending point in the bottom left corner? can the path ever go upwards, or towards right? – STT LCU Sep 03 '13 at 07:04
  • `because the cycles (for, while..) go from left to right`. Not true. You can do `for($i = 5; $i > 0; $i--)`! – Andrius Naruševičius Sep 03 '13 at 07:06
  • @STT LCU the path ever go upwards. Can go only left, right and down – Micky Sep 03 '13 at 07:11
  • @Micky what about start and end points? – STT LCU Sep 03 '13 at 07:13
  • @STT LCU I need the start point in the middle of the first row. the end point in the last row, but is not important where – Micky Sep 03 '13 at 07:23
  • @Micky so basically it can end before it reaches the end of the x height on any row and it should always start from the middle of the first row? – Prix Sep 03 '13 at 07:27
  • @Micky then the example in your question is wrong: the start point isn't in the middle of first row. Please, edit your question and insert all these extra informations you gave in the comments! – STT LCU Sep 03 '13 at 07:27
  • @Micky the edit is invalid again: now you've shown a diagonal path (1-2) which I thought it wasn't possible. – STT LCU Sep 03 '13 at 07:33
  • @STT LCU now is correct ;) – Micky Sep 03 '13 at 07:35
  • Okay, now finally at least it is more clear what you're looking for (start point is clear, movement is clear, end point is clear). But I have the feeling another area which makes this difficult for you is the data-structure the path is stored into. Can you please provide some specs of it? Can you explain a bit more why this is causing you a problem to do the movement with your data-structure? – hakre Sep 03 '13 at 07:39
  • @Micky I've edited your question. Please feel free to keep adding your specifications to the list, to include any other relevant information... for example, what if the map has an even number of columns? there's no middle point with an even columned map ^^ – STT LCU Sep 03 '13 at 07:47
  • @Prix YES, It should always start from the middle of the first row. It can end before it reaches the end of the height, because I could resume the path at a later time. – Micky Sep 03 '13 at 07:49
  • And for even maps it should start any where not on the borders? – Prix Sep 03 '13 at 07:50
  • @Prix: Which is hardly possible for those maps that have two or less rows. :) – hakre Sep 03 '13 at 07:54
  • @hakre at the end, or during creation, I will save all the coordinates in an array. And I will use them to load tiles and create a map. Then I would need to update with new rows. But this is another story :) My biggest problem is to create the path. – Micky Sep 03 '13 at 08:18
  • @hakre that was based that a map would have a min of 3 columns per row. – Prix Sep 03 '13 at 08:37

1 Answers1

0

per each field you have three possibilities: left, right, forward.

some cases reduce this, e.g. there is no field to the left or right or that field was visited already.

so find out about possible moves, pick one at random and go on.

hakre
  • 193,403
  • 52
  • 435
  • 836