
-----------------------------------
Raknarg
Wed Dec 18, 2013 2:13 pm

random map generator
-----------------------------------
I know it's not anything new, but I'm pretty proud of it as I've been trying to figure this out for a very long time.

A while ago I looked up the square diamond algorithm for map generation, and it made sense but I could not put it in code. However, this morning I realized I could do something like it in the opposite direction. Basically with a square diamond algorithm you're supposed to start with a map, and fill in the gaps as you go. However what I did here was start with a 2x2 square and progressively make it bigger as I go, creating gaps and filling it in like the square diamond method is supposed to. Produces great results.

Note I use pygame to display it, so if you don't have it it will not show anything, but the algorithm still works. It seems fairly fast as well, even though I think it's probably fairly inefficient.


from __future__ import division
from random import randint, random
import pygame
import sys

# Shifts an element in a from shiftID to position shiftTargetID
def shift_down(a, shiftID, shiftTargetID):
    newa = a
    for i in range(shiftTargetID+1, shiftID+1)

-----------------------------------
Raknarg
Wed Dec 18, 2013 2:35 pm

RE:random map generator
-----------------------------------
For some cool colours you can replace the colour def with this:

def scaler(min, max, n):
    return (n-min)/(max-min)
def colour(n, r):
    scale = scaler(r

EDIT: or if you like fire

if scale >> a = 7
>>> AddOneToNum(a)
>>> a
7

def AddOneToList(lst):
	lst += 

So lists objects can be modified, so what? Shoudn't a new list be created every time we call our function?

Well, functions are first class values in python (i.e. they are objects) and def is an executable statement that creates a function object and binds it to an identifier (the function name). Hence why we can't have functions and other values with the same identifier (name).
a = 6
>>> def a():
	return 0

>>> a


According to def has to create a function object with a default value for arguments that makes sense at the time the function was defined, for example
>>> a = 5
>>> def f(num = a):
	return num

>>> a = 7
>>> f()
5     # < ---- Not the current value of a
So hopefully, you can see why a default parameter has to be evaluated when the function definition is executed. Now what happens when we have a mutable value as our default? We create the mutable object once and every time we call the function the default value for the argument is the same object (which we can modify). Here's an example like Zren's which shows that the list is only created once.
>>> # Idea shamelessly borrowed from http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument
>>> def a():
	print("Creating empty list")
	return 
Hopefully, this explains everything. It really is one of those things that is unexpected for almost everybody the first time they encounter it.

-----------------------------------
Raknarg
Thu Dec 19, 2013 10:24 am

RE:random map generator
-----------------------------------
Oh wow, that's interesting. I'll keep that in mind for the future.
