I'm trying to implement a basic sprite game in C#. I'm testing to see if two dictionaries are equal to each other and I'm having an issue with Containskey() as well as my Equals function in the sprite class.
public class Sprite
{
private Point p;
private uint Horiz;
private uint Vert;
private uint Health;
private uint Shield;
private Dictionary<Item, int> inv;
private string[] QuickSlots;
/* Default constructor
* set all values to 0
*/
//string[] empty;
//string [] mt;
public Sprite()
{
Point newpoint = new Point();
newpoint.SetX(0);
newpoint.SetY(0);
newpoint.SetZ(0);
p = newpoint;
Horiz = 0;
Vert = 0;
Health = 0;
Shield = 0;
inv = null;
QuickSlots = null;
}
public override int GetHashCode()
{
return (int)(p.GetX() ^ (p.GetY() << 8) ^ (p.GetZ() << 16) ^
Horiz ^ (Vert << 9) ^
(Health << 18) ^ (Shield << 25));
}
public bool DictionaryEquals(Dictionary<Item, int> inv2)
{
foreach(KeyValuePair<Item, int> entry in inv2)
{
if(inv.ContainsKey(entry.Key)) //(inv[entry.Key] == inv2[entry.Key]))
{
if((inv[entry.Key] == inv2[entry.Key]))
{
continue;
}
else{
return false;
}
}
else
{
return false;
}
}
return true;
}
public override bool Equals(Object obj)
{
//Check for null and compare run-time types.
if ((obj == null) || ! this.GetType().Equals(obj.GetType()))
{
return false;
}
else
{
Sprite s = (Sprite) obj;
// this.PrintInventory();
s.PrintInventory();
return
((p.Equals(s.p)) &&
(inv.Equals(s.inv)) &&
//(QuickSlots == s.QuickSlots) &&
(Horiz == s.Horiz) &&
(Vert == s.Vert) &&
(Health == s.Health) &&
(Shield == s.Shield));
}
}
public bool HasItem(string item)
{
return inv.ContainsKey(StringtoItem(item, this));
}
public void TakeItem(string item)
{
Console.WriteLine("Does this even work\n");
if(HasItem(item))
{
Console.WriteLine("Does this even work2\n");
int x = inv[StringtoItem(item, this)] - 1;
if(x > 0)
{
Console.WriteLine("Does this even work3\n");
inv[StringtoItem(item, this)] = x;
}
else
{
Console.WriteLine("Does this even work4\n");
inv.Remove(StringtoItem(item, this));
}
}
}
}
Here, the dictionary.Equals() function works correctly but none of my testSprites match my expected Sprites because when I use an item, it doesn't decrement correctly from the testSprite inventory. The takeItem function calls the hasItem function to check if a dictionary/inventory has an item and it always returns false because the containskey function is always returning false even though I know for a fact that the dictionary contains the key when print out the contents of the dictionary to the console.
Below is the Item.cs file.
public class Item
{
private string name;
protected Sprite holder;
public Item(string n, Sprite h)
//, int hc, int sc, int ec,
// uint mh, uint ms, bool i, uint s, uint md)
{
name = n;
holder = h;
}
public string GetItemName()
{
return name;
}
public override int GetHashCode()
{
return (int)(name.GetHashCode() ^ (holder.GetPoint().GetX() << 8) ^ (holder.GetPoint().GetY() << 16) ^
(holder.GetPoint().GetZ() << 16) ^
holder.GetHoriz() ^ (holder.GetVert() << 9) ^
(holder.GetHealth() << 18) ^ (holder.GetShield() << 25));
}
public override bool Equals(Object obj)
{
//Check for null and compare run-time types.
if ((obj == null) || ! this.GetType().Equals(obj.GetType()))
{
return false;
}
else
{
Item i = (Item) obj;
return
((name == i.name) &&
(holder.Equals(i.holder)));
}
}
}
I suspect that something is wrong with one of the GetHashCode functions but I am not entirely sure how to fix it so any help is appreciated. Thanks