1

So I'm working on a game that uses a coordinate system, and I want to populate the map with a certain number of trees. The way I'm doing it (and it may not be the best way) is picking a random set of coordinates, checking to see if those coordinates are in the list of locations with trees in them, and if not adding the tree to the map, and adding those coordinates to the list. My instinct was to store the coordinates as an array, however I can't seem to figure out the syntax for it. Here's what I have:

int boardWidth = 10;
int boardHeight = 10;
int numTrees = 75;
List<int[]> hasTree = new List<int[]>();
public Transform tree;

Transform[,] SetTrees(Transform[,] map) {

    int treex = Random.Range(0,boardWidth-1);
    int treey = Random.Range(0,boardHeight-1);
    int[] treeCoords = new int[] { treex,treey };
    int treeCount = 0;

    while(treeCount < numTrees){

        if (hasTree.Contains(treeCoords)){
            treex = Random.Range(0,boardWidth-1);
            treey = Random.Range(0,boardHeight-1);
        }
        else{
            map[treex,treey] = (Transform)Instantiate(tree, new Vector3(i*tileWidth, 10, j*tileHeight), Quaternion.AngleAxis(90, Vector3.left));
            hasTree.Add(treeCoords);
            treex = Random.Range(0,boardWidth-1);
            treey = Random.Range(0,boardHeight-1);
            treeCount++;
        }
    }

    return(map);
}

Any thoughts?

3
  • hasTree.Add(treeCoords); ????? what is that "hasTree" object ? its not defined in the above code ? Commented Dec 21, 2013 at 3:40
  • i would simplify the code to begin with ... Commented Dec 21, 2013 at 3:41
  • Sorry, added the variables that were defined earlier. Commented Dec 21, 2013 at 3:43

3 Answers 3

3

If I were you I'd try something like this:

int boardWidth = 10;
int boardHeight = 10;
int numTrees = 75;

var rnd = new Random();

var query =
    from x in Enumerable.Range(0, boardWidth)
    from y in Enumerable.Range(0, boardHeight)
    orderby rnd.NextDouble()
    select new { x, y };

var board = new bool[boardWidth, boardHeight];

foreach (var pair in query.Take(numTrees))
{
    board[pair.x, pair.y] = true;
}
Sign up to request clarification or add additional context in comments.

1 Comment

that's epic ... i love linq :) ... odd that some people just naturally think in queries like this ... i wish i had your brain !!!
0

Keep It Simple Silly:

Transform[,] SetTrees(Transform[,] map) {
    for(int treeCount = 0; treeCount < numTrees; treeCount++){
        int treex = Random.Range(0,boardWidth-1);
        int treey = Random.Range(0,boardHeight-1);

        map[treex,treey] = new TreeTransform(treex, treey}
    }
    return(map);
}

Bury the details of Tree creation in its constructor TreeTransform, where it belongs.

Who cares about a creation ordering of the trees on the board? it has no use.

There is no reason for the number of trees to be exact, so just ignore duplicates.

9 Comments

didn't you just cut out a whole load of functionality?
@Wardy: Until a use for it is described, it simply obscures the issue of crating trees. If a sorted list of trees is needed, create a sorted list; but an unsorted list has no value. As I sad as intro - K.I.S.S. Put the other functionality where it's needed.
@Wardy: It's an x-y problem: meta.stackexchange.com/questions/66377/what-is-the-xy-problem. OP has given us a bad solution, and asked us to correct it. I am trying to point out the possibility of other solutions, not remedies for the bad one.
the functionality you cut out wasn't "how to create a tree" but "does a tree exist here already", the logic being worked on here is clearly after a set number of trees on the map after this function call exits.
@Wardy: Of what possible use is there, in a real game, for exactly 3890 tees instead of 3889 or 3888?
|
0

Simplify the code then it might be easier to determine your best course of action. I simplify by breaking down the problem until its so simple I can't really see how not to do it !!!

I am guessing how some of this code works here but I think you want something like this ...

Transform[,] SetTrees(Transform[,] map) {

    for (int i = 0; i < numTrees; i++){
       if(!AddTreeTo(map)){
          --i;
       }
    }

    return(map);
}

bool AddTreeToMap(Transform[,] map)
{
        int[] treeCoord = GetRandomCoord(map.Width, map.Height);

        if (!map[treeCoord[0],treeCoord[1]].HasTree()){
            map[treex,treey] = (Transform)Instantiate(tree, new Vector3(i*tileWidth, 10, j*tileHeight), Quaternion.AngleAxis(90, Vector3.left));
            map[treeCoord[0],treeCoord[1]].Add(treeCoords);
            return true;
        }
        return false;
}

int[] GetRandomTreeCoord(int maxX, int maxY)
{
    int treex = Random.Range(0,maxX-1);
    int treey = Random.Range(0,maxY-1);
    int[] treeCoords = new int[] { treex,treey };

    return treeCoords;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.