简体   繁体   中英

Creating a List of Arrays in C#?

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?

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;
}

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM