私は楽しみのために c# で掃海艇プロジェクトを作成しています。新しいタイルを Tile クラス内の辞書に格納して、Tile が開始されたときに格納され、Tile[coords] を介してアクセスできるようにしたかったのですが、上記を取得し続けます。エラー。これは、私が Tile クラスに使用しているコードです (私の規則についてコメントしないでください。私は C# を初めて使用します。私は Java/Python プログラマーです:p)
class Tile
{
private static Dictionary <Coords, Tile> tiles = new Dictionary <Coords, Tile> ();
public int numMinesAdjacents { get; set; }
public readonly bool isMine;
public readonly Coords position;
private Tile [] aAdjacents = new Tile [8];
public Tile(int x, int y, bool isMine = false)
{
this.isMine = isMine;
position = new Coords(x, y);
Tile[position] = this;
}
public void init()
{
calculateAdjacents();
calculateNumMinesAdjacent();
}
private void calculateAdjacents()
{
int i = 0;
for (int y = -1; y < 1; y++)
{
if ((position.y - y) < 0 || (position.y + y) > Math.Sqrt(Program.MAX_TILES)) continue;
for (int x = -1; x < 1; x++)
{
if ((position.x - x) < 0 || (position.x + x) > Math.Sqrt(Program.MAX_TILES)) continue;
aAdjacents [i] = Tile[position + new Coords(x, y)];
i++;
}
}
}
private void calculateNumMinesAdjacent()
{
int n = 0;
foreach (Tile pTile in aAdjacents)
{
if (pTile.isMine) n++;
}
numMinesAdjacents = n;
}
/*private static void add(Tile pTile)
{
tiles.Add(pTile.position, pTile);
}*/
public /*static - if I use static nothing is different*/ Tile this [Coords coords]
{
get { return tiles [coords]; }
}
}
私が電話したら
タイル (0, 0); タイル (0, 1);
その後
タイル[新しい座標(0, 0)]
クラスで Tile[] が使用されている場所 (コンストラクターと calculateAdjacents) でもエラーが発生します。ここで何が問題なのですか?
ありがとう、ジェイミー
編集: 申し訳ありませんが、Tile[position] を意味していましたが、元に戻してタイプミスしました。問題は、これをオーバーロードしたことです。これは、別のクラスから呼び出された場合でも、Tile[coords] が有効であることを意味するはずです