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 somthing but it wasn't a proper land mass, so I've re-attempted that algorthm in a different way and well, it now generates this.

Code
Github

My github repot for this can be found here, to run it is simple, clone, bundle install and then just ruby test.rb and it should display somthing simmilair to below.

The algorithm specifically lives here.

The method that handles base execution is #main, the main loop is actually a recursive call to #step and the methods #diamond_step and #square_step handle each step.

The old version of the algorthm lives in the commented out #diamond_step and #square_step methods (though they won't work without some tweaking).

Code Source

The code used in the current algorithm was adapted from: here.

The code from the older algorithm was taken from a question on gamedev stack but I can't find the question/answer I took the code from.

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

Umm, my edit post button seems to have vanishes so I'll add somthing here, I mentioned that the old methods that had been commented out would need tweaking, before they will work, that is not the case.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.