Time for action – generating the track
To generate the track for our Mars Runner, perform the following steps:
1. Add the
Helper Methods
region, along with theGenerateTrack()
method to theMarsTrack
class as follows:#region Helper Methods private int[] GenerateTrack(int length) { Random rand = new Random(); int[] track = new int[length]; for (int x = 0; x < length; x++) { track[x] = 0; if (x < 5) continue; if (x > length - 6) continue; if (track[x - 1] != 1) { if (rand.Next(0, 4) == 0) { track[x] = 1; } } } return track; } #endregion
What just happened?
We loop through each segment of the track, beginning with the assumption that each segment will represent solid ground (a value of zero). If we are within the first or last five segments of the track, we continue the loop, we do not want to have crater segments...