From ac1a89b89c602d1921586cdfeed8fab28cfec3e0 Mon Sep 17 00:00:00 2001 From: Glen Husman Date: Sun, 13 Nov 2016 17:24:08 -0800 Subject: [PATCH 1/6] Overload equality and hashcode on Size coretype Uses method recommended by Jon Skeet on Stack Exchange --- ConsoleGameLib/CoreTypes/Size.cs | 36 +++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/ConsoleGameLib/CoreTypes/Size.cs b/ConsoleGameLib/CoreTypes/Size.cs index c92ce0b..e76b749 100644 --- a/ConsoleGameLib/CoreTypes/Size.cs +++ b/ConsoleGameLib/CoreTypes/Size.cs @@ -15,5 +15,39 @@ public Size(int width, int height) public int Width; public int Height; + + public override int GetHashCode() + { + // Uses http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode + unchecked // Overflow is fine, just wrap + { + int hash = 29; + hash = hash * 89 + Width.GetHashCode(); + hash = hash * 89 + Height.GetHashCode(); + return hash; + } + } + + public override bool Equals(object obj) + { + if (obj == null || !(obj is Size)) + { + return false; + } + Size oth = (Size)obj; + return Width == oth.Width && Height == oth.Height; + } + + public static bool operator ==(Size left, Size right) + { + // Struct, so no need for null check + return left.Width == right.Width && left.Height == right.Height; + } + + public static bool operator !=(Size left, Size right) + { + // Struct, so no need for null check + return left.Width != right.Width || left.Height != right.Height; + } } -} +} \ No newline at end of file From 4c458f5a53439f8c57208c228ee580e93e41138d Mon Sep 17 00:00:00 2001 From: Glen Husman Date: Sun, 13 Nov 2016 17:30:13 -0800 Subject: [PATCH 2/6] Overrode Point equality and hashcode --- ConsoleGameLib/CoreTypes/Point.cs | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/ConsoleGameLib/CoreTypes/Point.cs b/ConsoleGameLib/CoreTypes/Point.cs index 7884804..44d9c37 100644 --- a/ConsoleGameLib/CoreTypes/Point.cs +++ b/ConsoleGameLib/CoreTypes/Point.cs @@ -15,5 +15,39 @@ public Point(int x, int y) public int X; public int Y; + + public override int GetHashCode() + { + // Uses http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode + unchecked // Overflow is fine, just wrap + { + int hash = 37; + hash = hash * 67 + X.GetHashCode(); + hash = hash * 67 + Y.GetHashCode(); + return hash; + } + } + + public override bool Equals(object obj) + { + if (obj == null || !(obj is Point)) + { + return false; + } + Point oth = (Point)obj; + return X == oth.X && Y == oth.Y; + } + + public static bool operator ==(Point left, Point right) + { + // Struct, so no need for null check + return left.X == right.X && left.Y == right.Y; + } + + public static bool operator !=(Point left, Point right) + { + // Struct, so no need for null check + return left.X != right.X || left.Y != right.Y; + } } } From 6662b2f9d187bc1f268a0d88ebbe7b73b8a94c2b Mon Sep 17 00:00:00 2001 From: Glen Husman Date: Sun, 13 Nov 2016 17:32:18 -0800 Subject: [PATCH 3/6] Implemented Point addition, subtraction, and unary negation --- ConsoleGameLib/CoreTypes/Point.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ConsoleGameLib/CoreTypes/Point.cs b/ConsoleGameLib/CoreTypes/Point.cs index 44d9c37..0892ce4 100644 --- a/ConsoleGameLib/CoreTypes/Point.cs +++ b/ConsoleGameLib/CoreTypes/Point.cs @@ -49,5 +49,20 @@ public override bool Equals(object obj) // Struct, so no need for null check return left.X != right.X || left.Y != right.Y; } + + public static Point operator +(Point left, Point right) + { + return new Point(left.X + right.X, left.Y + right.Y); + } + + public static Point operator -(Point left, Point right) + { + return new Point(left.X - right.X, left.Y - right.Y); + } + + public static Point operator -(Point unary) + { + return new Point(-unary.X, -unary.Y); + } } } From 211ebbe3b0f6d67f34f3d0c2acca03d5657533d0 Mon Sep 17 00:00:00 2001 From: Glen Husman Date: Sun, 13 Nov 2016 17:33:10 -0800 Subject: [PATCH 4/6] Implemented Size addition and subtraction --- ConsoleGameLib/CoreTypes/Size.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ConsoleGameLib/CoreTypes/Size.cs b/ConsoleGameLib/CoreTypes/Size.cs index e76b749..0a91ccc 100644 --- a/ConsoleGameLib/CoreTypes/Size.cs +++ b/ConsoleGameLib/CoreTypes/Size.cs @@ -49,5 +49,15 @@ public override bool Equals(object obj) // Struct, so no need for null check return left.Width != right.Width || left.Height != right.Height; } + + public static Size operator +(Size left, Size right) + { + return new Size(left.Width + right.Width, left.Height + right.Height); + } + + public static Size operator -(Size left, Size right) + { + return new Size(left.Width - right.Width, left.Height - right.Height); + } } } \ No newline at end of file From f539fd3c64cb754d3a543aff6b5f02298dbc5e23 Mon Sep 17 00:00:00 2001 From: Glen Husman Date: Wed, 16 Nov 2016 20:15:02 -0800 Subject: [PATCH 5/6] Add scalar multiplication and divison operators to size --- ConsoleGameLib/CoreTypes/Size.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ConsoleGameLib/CoreTypes/Size.cs b/ConsoleGameLib/CoreTypes/Size.cs index 0a91ccc..7a7738a 100644 --- a/ConsoleGameLib/CoreTypes/Size.cs +++ b/ConsoleGameLib/CoreTypes/Size.cs @@ -59,5 +59,15 @@ public override bool Equals(object obj) { return new Size(left.Width - right.Width, left.Height - right.Height); } + + public static Size operator *(Size point, int scalar) + { + return new Size(point.Width * scalar, point.Height * scalar); + } + + public static Size operator /(Size point, int scalar) + { + return new Size(point.Width / scalar, point.Width / scalar); + } } } \ No newline at end of file From 6aba38794d29bccce86912f9d0f78910dbe2da17 Mon Sep 17 00:00:00 2001 From: Glen Husman Date: Wed, 16 Nov 2016 20:15:11 -0800 Subject: [PATCH 6/6] Add scalar multiplication and divison operators to point type --- ConsoleGameLib/CoreTypes/Point.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ConsoleGameLib/CoreTypes/Point.cs b/ConsoleGameLib/CoreTypes/Point.cs index 0892ce4..f199e75 100644 --- a/ConsoleGameLib/CoreTypes/Point.cs +++ b/ConsoleGameLib/CoreTypes/Point.cs @@ -64,5 +64,15 @@ public override bool Equals(object obj) { return new Point(-unary.X, -unary.Y); } + + public static Point operator *(Point point, int scalar) + { + return new Point(point.X * scalar, point.Y * scalar); + } + + public static Point operator /(Point point, int scalar) + { + return new Point(point.X / scalar, point.Y / scalar); + } } }