种子随机绘制二维网格

本文关键字:维网格 绘制 随机 种子 | 更新日期: 2023-09-27 17:58:16

这件事已经困扰了我几个小时了,所以我想知道是否有人能帮我,因为我可能想错了。

我希望能够从宽度和高度无限的网格上的一组x和y坐标中获得布尔值。还有其他约束,沿着x轴,两个真值之间需要至少有n个位置,我还需要知道区域中从0,0到x,y的真值的数量。

给定给getTrueCoordinatesInArea的区域的宽度和高度等于x和y,因为它的区域是从0,0到x,y 创建的

如果这有道理的话。。

例如:

value = coordinateContainsTrue( x, y );//return true or false.
total = getTrueCoordinatesInArea( x , y );//Returns the total true values inside the area.

编辑:这样可以去除种子。

种子随机绘制二维网格

我完全不明白你需要什么,但我觉得这听起来是一个很好很有趣的练习。我希望这是你想要的它并没有按照我想要的方式编码。我宁愿使用bool[,]数组,但不知道如何使其动态,也不想为此编写自己的类,但也许我应该这样做。

但是,这个解决方案应该有效。

private List<List<bool>> grid = new List<List<bool>>();
private int N = 4;
Random randomNumberGenerator = new Random();
private bool getRandomBoolean()
{
    return this.randomNumberGenerator.Next(0, 2) == 1;
}
private void testProgram()
{
    var containsTrue = coordinateContainsTrue(0, 10);
    var total = getTrueCoordinatesInArea(14, 2);
    var isTrue = coordinateIsTrue(15, 11);
}
private bool coordinateIsTrue(int x, int y)
{
    expandGridOfNeeded(x, y);
    return grid[x][y];
}
private bool coordinateContainsTrue(int x, int y)
{
    expandGridOfNeeded(x, y);
    for (int xTemp = 0; xTemp < x; xTemp++) // Loop columns
    {
        for (int yTemp = 0; yTemp < y; yTemp++) // Loop rows
        {
            if (grid[xTemp][yTemp])
                return true; // Return true if any true was found
        }
    }
    return false;
}
private int getTrueCoordinatesInArea(int x, int y)
{
    expandGridOfNeeded(x, y);
    var total = 0;
    for (int xTemp = 0; xTemp < x; xTemp++) // Loop columns
    {
        for (int yTemp = 0; yTemp < y; yTemp++) // Loop rows
        {
            if (grid[xTemp][yTemp])
                total++; // Increase count if true was found
        }
    }
    return total;
}
private void expandGridOfNeeded(int x, int y)
{
    if (x < 0 || y < 0)
        return;
    // Add needed columns
    while (grid.Count <= x)
    {
        var column = new List<bool>();
        // Only add rows if the others have rows
        if (grid.Count > 0)
        {
            while (column.Count < grid[0].Count)
            {
                // Check if legal to add a true value, if it is then add random else add false
                bool forceFalseValue = false;
                for (int i = grid.Count - 1; i > grid.Count + N && forceFalseValue == false; i--)
                {
                    forceFalseValue = grid[i][column.Count - 1];
                }
                column.Add(forceFalseValue ? false : getRandomBoolean());
            }
        }
        grid.Add(column);
    }
    // Add needed rows
    while (grid[0].Count <= y)
    {
        var counter = N;
        foreach (var column in grid)
        {
            // Check if legal to add a true value, if it is then add random else add false
            if (counter < N)
            {
                column.Add(false);
                counter++;
            }
            else
            {
                var randomValue = getRandomBoolean();
                if (randomValue)
                    counter = 0;
                else
                    counter++;
                column.Add(randomValue);
            }
        }
    }
}