1
\$\begingroup\$

This is my function to procedural generate an infinite random map of 1's and 0's it only calculates what you can see at the time and when the view moves it calculates the next rows.

Complexity is the size of the view that is on the screen e.g. if complexity = 7 then the view on the screen is a grid 7x7.

It works fine but how can I tidy this up, simplify the code and make it more elegant?

void CreateMap(){
    if(complexity%2==0) {
        complexity--;
    }

    if (randomseed) {
        levelseed=Time.time.ToString();
        Debug.Log ("Random");
    }

    psuedoRandom = new System.Random(levelseed.GetHashCode());
    offsetx=psuedoRandom.Next();
    levellocation=psuedoRandom.Next();
    offsety=psuedoRandom.Next();
    level = new int[complexity,complexity];
    for (int x=0;x<complexity;x++){
        for (int y=0;y<complexity;y++){
            level[x,y]=RandomReturn(x+offsetx,y+offsety);
        }
    }
}

int RandomReturn(int x, int y){
    psuedoRandom = new System.Random(x*y*levellocation.GetHashCode());  
    return (psuedoRandom.Next (0,2));
}

An example of moving right

void MoveRight(){
    offsetx++;
    for (int x=0;x<complexity-1;x++){
        for (int y=0;y<complexity;y++){
            level[x,y]=level[x+1,y];
        }
    }

    for (int y=0;y<complexity;y++){
        level[complexity-1,y]=RandomReturn(complexity-1+offsetx,y+offsety);
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Is it possible for you to post the whole code? It would give us more context to give a better review :) \$\endgroup\$
    – IEatBagels
    Sep 22 '15 at 19:22
1
\$\begingroup\$

I know that this is minor, but you should not stuff all your code together without spaces like:

for (int y=0;y<complexity;y++){
    level[complexity-1,y]=RandomReturn(complexity-1+offsetx,y+offsety);

Because spaces help differentiate tokens at a first glance, I suggest one space on each side of each operator.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.