简体   繁体   English

在C#中创建数组列表?

[英]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. 将Tree创建的细节隐藏在它所属的构造函数TreeTransform中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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