代码之家  ›  专栏  ›  技术社区  ›  AllenG

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

  •  3
  • AllenG  · 技术社区  · 17 年前

    为了我个人的娱乐,我写的是我希望以后的游戏的基础。目前,我正在制作“棋盘”游戏。请考虑以下事项:

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

    我知道这是非常错误的,甚至可能不好笑,但这是最粗糙的切割。

    我正试着去 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.'
    

    有什么建议吗,还是我完全走错了方向?

    3 回复  |  直到 10 年前
        1
  •  8
  •   Godeke    17 年前

    LINQ没有看到您希望看到的多维数组,因为它们没有实现 IEnumerable<T> (尽管单索引数组确实如此,但这让人们感到惊讶)。有几种解决方法:可以避免使用LINQ搜索多维数据集,也可以编写自己的扩展方法来执行更传统的漫游。

    在这种情况下,我不会使用LINQ do进行搜索,但更重要的是,我可能会在一个简单的结构(可能是字典)中保留一些对各种播放片段的引用,以便更容易更新和管理。作为一个想法,您的工件对象将知道 它位于电路板上,可以通过将自身从一个单元中移除并添加到另一个单元来更新立方体。

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

        2
  •  0
  •   muusbolla    17 年前

    对我来说,将int[]currentLocation声明移动到Person类的顶层并提供getter/setter方法更有意义。然后每个人存储自己的位置。

    对于3个整数的内存开销,您无需每次检索此人的位置时查询1000个数据库条目。

        3
  •  0
  •   NerdFury    17 年前

    public class Location3D
    {
      public Location3D(int x, int y, int z) { X = x; Y = y; Z = z; }
      public int X { get; set; }
      public int Y { get; set; }
      public int Z { get; set; }
    }
    
    public abstract GamePiece
    {
      public Location3d { get; set; }
    
    public class Person: GamePiece // inherit GamePiece
    {
      // everything else about Person
    }
    
    public class Board
    {
      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);
    
        var location = new Location3D(xAxis, yAxis, zAxis);
        person.Location = location;
    
        GetCubeAt(location).AddContents(person);
      }
    
      public Cube GetCubeAt(Location3D location)
      {
        return gameBoard[location.X, location.Y, location.Z];
      }
    
      public Cube GetCubeAt(int x, int y, int z)
      {
        return GetCubeAt(new Location3D(x,y,z));
      }
    }