4

I'm trying to conduct a random walk through a collection of named regular polygons. I have a routine that creates a collection of regular hexagons using TikZ shapes.geometric. Each hexagon is named x-Cy, where x is it's horizontal position within the row and y is the row value. The rows contain elements 0 to 20 and the rows are numbered 0-30. This portion of the code works just fine (see http://imgur.com/68CD2Sm).

My next step is have a routine pick a random node along the top row and conduct a random walk down the page, coloring hexagons blue as they go. The routine should end once we've hit the edge of the array of hexagons (exited the map).

I'm trying to accomplish this by recursion, but breakforeach seems to fail to trip and break my loop. Code below.

\foreach \i in {1} {
   \node[waterTile,anchor=corner 1]() at (#1-C#2.corner 1) {}; %make the hex x-Cy a water tile

    \pgfmathtruncatemacro{\nextRiverTileX}{\nextRiverTileX+random(0,2)-1} %randomly move in the x direction
    \pgfmathtruncatemacro{\nextRiverTileY}{\nextRiverTileY+random(0,1)} %randomly move in the y direction

    %if we moved off the map, break the loop
    \ifnum \nextRiverTileX>20 \breakforeach \fi

    \ifnum \nextRiverTileX<0 \breakforeach \fi

    \ifnum \nextRiverTileY>30 \breakforeach \fi

    \ifnum \nextRiverTileY<0 \breakforeach \fi

    %if we didn't move off the map, repeat the process
    \makeRiver{\nextRiverTileX}{\nextRiverTileY}

    %once the loop breaks, break the prior loop
    \breakforeach
}
} %makeRiver

Is there a better way to handle this? As indicated, the map creation process itself works just fine. It's the procedural coloring that is giving me problems right now. If I specify certain hexagons to be colored, that works. If I specify a certain number of rows to have a hex colored in, that works. But I don't seem to be able to have this recur until we exit the map (that is, use a macro-based breakforeach).

  • What is the use of a link which others can't open? – user11232 Oct 31 '15 at 23:44
1

Resolved the issue; breakforeach needed to be in a nested if block in order to prevent calling the function again. Fixed code below:

\newcommand{\makeRiver}[2] { %{x}{y}

    \foreach \i in {1} {
       \node[waterTile,anchor=corner 1]() at (#1-C#2.corner 1) {}; %make the hex x-Cy a water tile

        \pgfmathtruncatemacro{\index}{random(0,1)}
        \ifodd\index
            \pgfmathtruncatemacro{\nextRiverTileX}{\nextRiverTileX+random(0,2)-1} %randomly move in the x direction
        \else
            \pgfmathtruncatemacro{\nextRiverTileY}{\nextRiverTileY+random(0,1)} %randomly move in the y direction
        \fi

        %if we moved off the map, break the loop
        \ifnum  \nextRiverTileX>20 \breakforeach
        \else   \ifnum \nextRiverTileX<0 \breakforeach
                \else   \ifnum \nextRiverTileY>30 \breakforeach
                        \else   \ifnum \nextRiverTileY<0 \breakforeach
                                \else \makeRiver{\nextRiverTileX}{\nextRiverTileY} %if we didn't move off the map, repeat the process
                                \fi
                        \fi
                \fi
        \fi

        %once the loop breaks, break the prior loop
        \breakforeach
    }
} %makeRiver

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.