2009-06-17 35 views
3

为了我个人的娱乐目的,我写了我希望成为稍后游戏的基础。目前,我正在制作游戏“棋盘”。请考虑以下几点:使用LINQ从多维数组中选择未知项目

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返回其中Person所在的Cube的特定索引。因此,如果该人在Board.GameBoard[1, 2, 10]中,我将能够检索该位置(可能是上面列出的int[])。然而,在目前,我无法编译由于以下错误:

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

我敢肯定,LINQ应该能够查询多维数组,但我还没有找到如何任何文件去做吧。

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

回答

8

LINQ没有看到多维数组,因为他们没有实现IEnumerable<T>(尽管单个索引数组的做法,这是人们的惊喜)。有几种解决方法:您可以避免使用LINQ来搜索多维数据集,也可以编写自己的扩展方法,进行更传统的步骤。

这是我不会使用LINQ做搜索的情况下,但更重要的,我可能会继续到各个棋子一些参考以简单的结构(可能是一个字典)更易于更新和管理。作为一个想法,你的棋子对象会知道,其中它在棋盘上,并且可以在移动时通过从一个单元中移除自身并将其自身添加到另一个单元来更新该多维数据集。

知道单个单元格是否可以包含多个单元是很重要的:如果是这样,每个单元格也需要是某种类型的列表(它在代码中看起来如此)。一旦你明白了这一点,如果玩的棋子比单元格少得多,我可能永远不会创建“立方体”本身作为数据结构。它将被绘制,并且通过直接从片段列表中拉出的一些Z顺序绘图算法显示片段,而不是数组。这取决于游戏的风格:如果棋子具有属性并且数量很少,则可以工作。如果游戏更像3D Go或类似的游戏,那么你的原始魔方就会有意义......这取决于你的作品有多少“个性”(以及数据)。

+1

谢谢...我很害怕这一点。 然后我发现我只问了我需要答案的部分问题。哎呦。 – AllenG 2009-06-17 21:09:30

0

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

对于3个整数的内存成本,您不必每次都需要查询1000个数据库条目以检索该人的位置。

0

我认为这个人应该告诉董事会他在哪里,而不是问董事会。换句话说,我将创建一个Location3D类(x,y,z),在GamePiece类中使用该类,以便板上的所有其他事物都继承自该类。那存储位置,然后每个peice知道它的位置。

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