[英]Select unknown item from multi-dimensional array using 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 應該能夠查詢多維 arrays,但我還沒有找到任何關於如何做到這一點的文檔。
有什么建議,還是我走錯了路?
LINQ 沒有看到您想要的多維 arrays,因為它們沒有實現IEnumerable<T>
(盡管單個索引 arrays 實現了,這讓人們感到驚訝)。 有幾種解決方法:您可以避免使用 LINQ 來搜索多維數據集,或者您可以編寫自己的擴展方法來執行更傳統的遍歷。
在這種情況下,我不會使用 LINQ 來進行搜索,但更重要的是,我可能會在一個更易於更新和管理的簡單結構(可能是字典)中保留對各種演奏片段的一些引用。 作為一個想法,你的棋子 object 會知道它在棋盤上的位置,並且可以在立方體移動時通過將自己從一個單元格中移除並將自己添加到另一個單元格來更新立方體。
了解單個單元格是否可以包含多個部分很重要:如果是這樣,每個單元格也需要是某種類型的列表(在您的代碼中以這種方式出現)。 一旦你到達這一點,如果玩的棋子比細胞少得多,我可能永遠不會真正將“立方體”本身創建為數據結構。 它將被繪制並通過一些直接從片段列表而不是數組中提取的 Z 順序繪圖算法顯示片段。 不過,這將取決於游戲的風格:如果棋子具有屬性並且數量很少,這將可行。 如果游戲更像是 3D Go 或類似的游戲,那么您的原始立方體就有意義了……這實際上取決於您的棋子具有多少“個性”(以及數據)。
將 int[] currentLocation 聲明移動到您的 Person class 中的頂層並提供 getter/setter 方法對我來說更有意義。 然后每個人存儲自己的位置。
對於 3 個整數的 memory 成本,您每次想要檢索此人的位置時都不必查詢 1000 個數據庫條目。
我認為這個人應該告訴董事會他在哪里,而不是問董事會。 換句話說,我會創建一個 Location3D class (x,y,z),在棋盤上所有其他東西繼承的 GamePiece class 中使用它。 存儲位置,然后每個 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));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.