2009-06-17 28 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)); 
    } 
}