使用LINQ多维数组选择未知项目 [英] Select unknown item from multi-dimensional array using LINQ

查看:115
本文介绍了使用LINQ多维数组选择未知项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我自己的个人娱乐,我写的是我希望将来的游戏的基础。目前,我正在从事游戏板。请考虑以下方面:

For my own personal amusement, I'm writing what I hope will be the foundation of a game to come later. At current, I'm working on the game "board". Please consider the following:

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


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

我知道这是错的, ,但这是最粗糙的切割。

I know this is so wrong it's probably not even funny, but this is the roughest of rough cuts.

我试图获取 GetLocation 返回特定索引 Person 所在的 Cube 所以如果这个人在 Board.GameBoard [1,2,10] ,我将能够检索该位置(可能作为 int [] 如上所列)。但是,目前,我无法编译,由于以下错误:

I'm trying to get GetLocation to return the specific index of the Cube in which the Person is located. So that if the person is in Board.GameBoard[1, 2, 10] I'll be able to retrieve that location (probably as an int[] as listed above). However, at current, I'm unable to compile due to the following error:

Could not find an implementation of the query pattern for source type 'Cubes.Cube[*,*,*]'. 'Where' not found.'

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

I was pretty sure that LINQ should be able to query multi-dimensional arrays, but I haven't found any documentation on how to do it.

任何建议,或者我在完全错误的轨道上吗?

Any suggestions, or am I on the completly wrong track here?

推荐答案

LINQ没有看到多维数组,因为他们没有实现 IEnumerable< T> (虽然单索引数组,这是什么惊喜人)。有几个解决方法:您可以避免LINQ搜索多维数据集,或者您可以编写自己的扩展方法,这是更传统的步行。

LINQ does not see multidimential arrays as you want it to because they do not implement IEnumerable<T> (although single index arrays do, which is what surprises people). There are several workarounds: you can avoid LINQ for searching the cube or you can write an extension method of your own that does the more traditional walks.

这是一种情况我不会使用LINQ做搜索,但更多的,我可能会保持一些参考的各种播放部分在一个简单的结构(可能是一个字典),更容易更新和管理。作为一个想法,你的作品对象会知道 它是在电路板本身,并可以通过删除自己从一个单元格,并将自己添加到另一个单元格更新多维数据集。

This is a case where I wouldn't use LINQ do to the search, but more than that I probably would keep some references to the various playing pieces in a simple structure (probably a dictionary) that is easier to update and manage. As an idea, your piece object would know where it is on the board itself and could update the cube as it moved by removing itself from one cell and adding itself to another.

重要的是知道单个单元格是否可以包含多个部分:如果是,每个单元格也需要是一个类型的列表(在代码中看起来像这样)中。一旦你达到这一点,如果有比单元格少得多的播放部分,我可能永远不会真正创建立方体本身作为一个数据结构。它将被绘制,并通过一些Z顺序绘图算法直接从片列表,而不是一个数组显示的片段显示。这将取决于游戏的风格虽然:如果这些片段有属性,并且数量很少这将工作。如果游戏更像3D Go或类似游戏,那么你原来的多维数据集会有意义...这取决于你的作品有多少个性(因此数据)。

It would be important to know if a single cell can contain more than one piece: if so, each cell would need to be a list of some type as well (it appears that way in your code). And once you get to this point, if there are vastly fewer playing pieces than cells, I probably would never actually create the "cube" itself as a datastructure. It would be drawn and the pieces displayed via some Z order drawing algorithm that pulled directly from the piece list, rather than an array. This would depend on the style of game though: if the pieces have attributes and are small in number this would work. If the game is more like 3D Go or similar, then your original cube would make sense... it really depends on how much "personality" (and thus data) your pieces have.

这篇关于使用LINQ多维数组选择未知项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆