使用LINQ从多维数组中选择未知项

All*_*enG 3 c# linq multidimensional-array

为了我自己的个人娱乐,我写的是我希望以后能够成为游戏的基础.目前,我正在制作游戏"棋盘".请考虑以下事项:

class Board
{
    private Cube[,,] gameBoard;
    public Cube[, ,] GameBoard { get; }
    private Random rnd;
    private Person person;
    public Person _Person { get; }

    //default constructor
    public Board()
    {
        person = new Person(this);
        rnd = new Random();
        gameBoard = new Cube[10, 10, 10];
        gameBoard.Initialize();
        int xAxis = rnd.Next(11);
        int yAxis = rnd.Next(11);
        int zAxis = rnd.Next(11);

        gameBoard[xAxis, yAxis, zAxis].AddContents(person);
    }
}
Run Code Online (Sandbox Code Playgroud)

还有这个:

class Person : IObject
{
    public Board GameBoard {get; set;}
    public int Size { get; set; }
    public void Move()
    {
        throw new NotImplementedException();
    }

    public void Move(Cube startLocation, Cube endLocation)
    {
        startLocation.RemoveContents(this);
        endLocation.AddContents(this);
    }

    public Person(Board gameBoard)
    {
        Size = 1;
        GameBoard = gameBoard;
    }

    public int[] GetLocation()
    {
        int[] currentLocation;
        var location =
            from cubes in GameBoard.GameBoard
            where cubes.GetContents.Contains(this)
            select cubes;
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道这是错的,它甚至可能都不好笑,但这是最粗糙的削减.

我试图让GetLocation返回的具体指标Cube将在Person所在.因此,如果该人在Board.GameBoard[1, 2, 10]我将能够检索该位置(可能int[]如上所列).但是,目前,由于以下错误,我无法编译:

Could not find an implementation of the query pattern for source type 'Cubes.Cube[*,*,*]'. 'Where' not found.'
Run Code Online (Sandbox Code Playgroud)

我很确定LINQ应该能够查询多维数组,但我还没有找到任何关于如何执行它的文档.

有什么建议,还是我在完全错误的轨道上?

God*_*eke 8

LINQ没有像你想要的那样看到多维数组,因为它们没有实现IEnumerable<T>(尽管单个索引数组确实如此,这让人们感到惊讶).有几种解决方法:您可以避免使用LINQ搜索多维数据集,或者您可以编写自己的扩展方法来执行更传统的步骤.

这是一个我不会使用LINQ做搜索的情况,但更重要的是我可能会在一个简单的结构(可能是字典)中保留一些更容易更新和管理的各种游戏片段.作为一个想法,你的作品对象将知道哪里是在板本身和它移动从一个细胞去除自身将自身添加到另一个可以更新多维数据集.

重要的是要知道单个单元格是否可以包含多个单元格:如果是这样,每个单元格也需要是某种类型的列表(它在您的代码中以这种方式出现).一旦你达到这一点,如果游戏块比单元格少得多,我可能永远不会真正创建"立方体"本身作为数据结构.它将被绘制,并通过直接从片段列表而不是数组拉出的一些Z阶绘制算法显示这些片段.这取决于游戏的风格:如果棋子具有属性并且数量很少,这将起作用.如果游戏更像3D Go或类似游戏,那么你的原始立方体就会有意义......这实际上取决于你的作品有多少"个性"(以及数据).