Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+17 votes
asked in CSC305 Fall 2022 by (1 point)

3 Answers

+9 votes

There are lots of possibilities for interesting random board generation, and I encourage creativity about this.

However, if you don't want to come up with your own idea, here's one approach you could try:

Instead of choosing independent random values for each Tile, which can cause big jumps up and down, let's make it so that each tile is closer to the values of its neighboring tiles, which will result in a smoother landscape.

For the top row of the grid, start with a random elevation in the top-left corner, and then for each tile going along the top row, add a random number between -N and N to the elevation of the tile to it's left.

For the left-most column, do the same thing, but going down starting from the top-left corner.
Then, fill in the rest of the grid, where each tile's elevation comes from adding a random number (between -N and N) to the average of the heights of the two tiles that are above and left of it.

One more complication: when adding the number between -N and N, if the result goes below 0, then set it to 0, or if it goes above the maximum tile elevation, then set it to the maximum, so that your tile elevations never go outside of the allowed range.


Another option would be start with totally uniform random values at each grid square, but then run a smoothing filter (e.g. setting every tile's elevation to become the average of the elevations from its neighboring tiles) over the grid a few times. This should also result in a smoother but still random landscape.


Another approach is to have code that generates "features" (like little mountains, valleys, roads, etc), and run those feature-generating codes several times at several different random locations on a board that starts out as flat originally.

answered by (508 points)
+4

Thank you!

+7 votes

We used the built in Random class to set each tile's height to a random value and made sure to run through each tile to change the height to a random value.

answered by (2k points)
+4

Thank you!

+7 votes

I used this Wikipedia page to generate random mazes. https://en.wikipedia.org/wiki/Maze_generation_algorithm#Cellular_automaton_algorithms
If you have stored your tiles in a 2d array it will be easier to make mazes using cellular automata approach. We did a similar thing in one of the labs(game of life) in CSC 202.

answered by (1 point)
+1

Thank you !!

...