The use of an int[]
for your word map coordinates is a poor choice for a number of reasons, not least of which I could send through zero, one, three, or more values when it seems like you specifically are using only two.
You would be far better off creating you own read-only class to hold your coordinates instead.
Try this class:
public sealed class WorldMapCoord : IEquatable<WorldMapCoord>
{
private readonly int _X;
private readonly int _Y;
public int X { get { return _X; } }
public int Y { get { return _Y; } }
public WorldMapCoord(int X, int Y)
{
_X = X;
_Y = Y;
}
public override bool Equals(object obj)
{
if (obj is WorldMapCoord)
return Equals((WorldMapCoord)obj);
return false;
}
public bool Equals(WorldMapCoord obj)
{
if (obj == null) return false;
if (!EqualityComparer<int>.Default.Equals(_X, obj._X)) return false;
if (!EqualityComparer<int>.Default.Equals(_Y, obj._Y)) return false;
return true;
}
public override int GetHashCode()
{
int hash = 0;
hash ^= EqualityComparer<int>.Default.GetHashCode(_X);
hash ^= EqualityComparer<int>.Default.GetHashCode(_Y);
return hash;
}
public override string ToString()
{
return String.Format("{{ X = {0}, Y = {1} }}", _X, _Y);
}
}
Now your code would look like this:
public Dictionary<WorldMapCoord, string> worldMap = new Dictionary<WorldMapCoord, string>();
for (int x=0; x <= 10; x++)
{
for (int y=0; y <= 10; y++)
{
worldMap.Add(new WorldMapCoord(x, y), "empty");
}
}
Since the class is read-only, and it implements GetHashCode
& Equals
you can use this code to retrieve values from the dictionary:
for (int x=0; x <= 10; x++)
{
for (int y=0; y <= 10; y++)
{
string value = worldMap[new WorldMapCoord(x, y)];
}
}