Trying to impliment the diamond square algorthm in ruby but it's not comming out quite right

1
votes

Posted by: Thermatix on 08/25/2017 | Add Revision

I've been trying to create the diamond square algorithm in ruby and whilst it works (that is to say it doesn't error) it doesn't output as expected, so far here's the output at various states:

So far

First attempt

First Attempt

This is the output of when I first got it working, as you can see it's in straight lines and there's no colourisation difference

Fixed colourisation

fixed colourisation

I realised what I was doing wrong and fixed the colourisation problem by flipping the case select, I should have been checking for the highest hight first and then go downwards, because I was looking for the lowest first, it would hit that and stop, hence everything was ether water or sand.

Fixed repeating lines issue

fixed repeating lines issue

I realised that it was how I was initialising the array that was causing the array to be filled with lines, Instead of creating 10 arrays of 10 elements, it was creating one array of 10 elements with 10 references to the same array, changing the array in one place would change it for every array at the same place, changing how I initialised the array fixed it, it now generates a new array for each element in the main array.

Second Attempt at algorithm

second attempt at algorithm

As you can see from the last pic, it was generating something but it wasn't a proper land mass, so I've re-attempted that algorithm in a different way and well, it now generates this.

third attempt at algorithm

third attempt at algorithm

I re-wrote the algorithm yet again and as you can see it looks vaguely like land but it's still not quite right. You can see it seems to travel in a diagonal (top left to bottom right) increasing the amount of height as it goes.

Code

Github

My Github repo for this can be found here, to run it is simple, clone, bundle install and then just ruby test.rb and it should display something similar to below.

The algorithm specifically lives here.

The code for the algorithm lives in /app/generators/diamond_square.rb. The method that handles base execution is #main, the main loop is actually a call to #step which then recursively calls itself and the methods #diamond_step and #square_step handle each step.

So, where am I going wrong, why is my implementation not printing out a reasonable looking landmass?

Visit Website