From 67a94b5b5c51b50ddedc61aa8ec78c143e8a2e1d Mon Sep 17 00:00:00 2001 From: Daniya_Youdakova Date: Sun, 25 Dec 2022 17:45:48 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BB=D0=B0=D0=B1-8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AircraftCarrier/AbstractMap.cs | 45 ++++++++++-- .../AircraftCarrier/AircraftCarrier.csproj | 18 ++--- .../AircraftCarrierCompareByColor.cs | 72 +++++++++++++++++++ .../AircraftCarrierCompareByType.cs | 56 +++++++++++++++ 4 files changed, 176 insertions(+), 15 deletions(-) create mode 100644 AircraftCarrier/AircraftCarrier/AircraftCarrierCompareByColor.cs create mode 100644 AircraftCarrier/AircraftCarrier/AircraftCarrierCompareByType.cs diff --git a/AircraftCarrier/AircraftCarrier/AbstractMap.cs b/AircraftCarrier/AircraftCarrier/AbstractMap.cs index b0946f6..3bacad5 100644 --- a/AircraftCarrier/AircraftCarrier/AbstractMap.cs +++ b/AircraftCarrier/AircraftCarrier/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace AircraftCarrier { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; protected int[,] _map = null; @@ -17,6 +17,43 @@ namespace AircraftCarrier protected readonly Random _random = new(); protected readonly int _freeRoad = 0; protected readonly int _barrier = 1; + public bool Equals(AbstractMap? other) + { + if (other == null) + { + return false; + } + var otherMap = other as AbstractMap; + if (otherMap == null) + { + return false; + } + if (_width != otherMap._width) + { + return false; + } + if (_height != otherMap._height) + { + return false; + } + if (_size_x != otherMap._size_x) + { + return false; + } + if (_size_y != otherMap._size_y) + { + return false; + } + for (int i = 0; i < _map.GetLength(0); i++) + { + for (int j = 0; j < _map.GetLength(1); j++) + { + if (_map[i, j] != otherMap._map[i, j]) + return false; + } + } + return true; + } public Bitmap CreateMap(int width, int height, IDrawningObject drawningObject) { @@ -70,7 +107,6 @@ namespace AircraftCarrier yNumOfCells = (int)Math.Ceiling(_drawningObject.Step / _size_y); xObjOffset = (int)(Left / _size_x); yObjOffset = (int)Math.Ceiling(Bottom / _size_y); - for (int i = 0; i < yNumOfCells; i++) { if (!roadIsClear) @@ -91,13 +127,11 @@ namespace AircraftCarrier } } break; - case Direction.Left: xNumOfCells = (int)Math.Ceiling(_drawningObject.Step / _size_x); yNumOfCells = (int)Math.Ceiling((Bottom - Top) / _size_y); xObjOffset = (int)Math.Floor(Left / _size_x); yObjOffset = (int)(Top / _size_y); - for (int i = 0; i < yNumOfCells; i++) { if (!roadIsClear) @@ -118,13 +152,11 @@ namespace AircraftCarrier } } break; - case Direction.Right: xNumOfCells = (int)Math.Ceiling(_drawningObject.Step / _size_x); yNumOfCells = (int)Math.Ceiling((Bottom - Top) / _size_y); xObjOffset = (int)(Right / _size_x); yObjOffset = (int)Math.Ceiling(Top / _size_y); - for (int i = 0; i < yNumOfCells; i++) { if (!roadIsClear) @@ -165,7 +197,6 @@ namespace AircraftCarrier int yNumOfCells = (int)Math.Ceiling(Bottom / _size_y) - (int)Math.Floor(Top / _size_y); int xObjOffset = (int)(x / _size_x); int yObjOffset = (int)(y / _size_y); - while (y < _height - (Bottom - Top)) { while (x < _width - (Right - Left)) diff --git a/AircraftCarrier/AircraftCarrier/AircraftCarrier.csproj b/AircraftCarrier/AircraftCarrier/AircraftCarrier.csproj index 8b2ac69..60d9aa8 100644 --- a/AircraftCarrier/AircraftCarrier/AircraftCarrier.csproj +++ b/AircraftCarrier/AircraftCarrier/AircraftCarrier.csproj @@ -7,16 +7,17 @@ true enable - + - - + + - - - Always - - + + +Always + + + @@ -24,6 +25,7 @@ + diff --git a/AircraftCarrier/AircraftCarrier/AircraftCarrierCompareByColor.cs b/AircraftCarrier/AircraftCarrier/AircraftCarrierCompareByColor.cs new file mode 100644 index 0000000..593b82c --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/AircraftCarrierCompareByColor.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftCarrier +{ + internal class AircraftCarrierCompareByColor : IComparer + { + public int Compare(IDrawningObject? x, IDrawningObject? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xAircraftCarrier = x as DrawningObjectAircraftCarrier; + var yAircraftCarrier = y as DrawningObjectAircraftCarrier; + if (xAircraftCarrier == null && yAircraftCarrier == null) + { + return 0; + } + if (xAircraftCarrier == null && yAircraftCarrier != null) + { + return 1; + } + if (xAircraftCarrier != null && yAircraftCarrier == null) + { + return -1; + } + string xAircraftCarrierColor = xAircraftCarrier.GetAircraftCarrier.AircraftCarrier.BodyColor.Name; + string yAircraftCarrierColor = yAircraftCarrier.GetAircraftCarrier.AircraftCarrier.BodyColor.Name; + if (xAircraftCarrierColor != yAircraftCarrierColor) + { + return xAircraftCarrierColor.CompareTo(yAircraftCarrierColor); + } + if (xAircraftCarrier.GetAircraftCarrier.GetType().Name != yAircraftCarrier.GetAircraftCarrier.GetType().Name) + { + if (xAircraftCarrier.GetAircraftCarrier.GetType().Name == "DrawningAircraftCarrier") + { + return -1; + } + return 1; + } + if (xAircraftCarrier.GetAircraftCarrier.AircraftCarrier is EntityModernAircraftCarrier xModernAircraftCarrier && + yAircraftCarrier.GetAircraftCarrier.AircraftCarrier is EntityModernAircraftCarrier yModernAircraftCarrier) + { + string xAircraftCarrierDopColor = xModernAircraftCarrier.DopColor.Name; + string yAircraftCarrierDopColor = yModernAircraftCarrier.DopColor.Name; + var dopColorCompare = xAircraftCarrierDopColor.CompareTo(yAircraftCarrierDopColor); + if (dopColorCompare != 0) + { + return dopColorCompare; + } + } + var speedCompare = xAircraftCarrier.GetAircraftCarrier.AircraftCarrier.Speed.CompareTo(yAircraftCarrier.GetAircraftCarrier.AircraftCarrier.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xAircraftCarrier.GetAircraftCarrier.AircraftCarrier.Weight.CompareTo(yAircraftCarrier.GetAircraftCarrier.AircraftCarrier.Weight); + } + } +} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/AircraftCarrierCompareByType.cs b/AircraftCarrier/AircraftCarrier/AircraftCarrierCompareByType.cs new file mode 100644 index 0000000..b20145a --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/AircraftCarrierCompareByType.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftCarrier +{ + internal class AircraftCarrierCompareByType : IComparer + { + public int Compare(IDrawningObject? x, IDrawningObject? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xAircraftCarrier = x as DrawningObjectAircraftCarrier; + var yAircraftCarrier = y as DrawningObjectAircraftCarrier; + if (xAircraftCarrier == null && yAircraftCarrier == null) + { + return 0; + } + if (xAircraftCarrier == null && yAircraftCarrier != null) + { + return 1; + } + if (xAircraftCarrier != null && yAircraftCarrier == null) + { + return -1; + } + if (xAircraftCarrier.GetAircraftCarrier.GetType().Name != yAircraftCarrier.GetAircraftCarrier.GetType().Name) + { + if (xAircraftCarrier.GetAircraftCarrier.GetType().Name == "DrawningAircraftCarrier") + { + return -1; + } + return 1; + } + var speedCompare = + xAircraftCarrier.GetAircraftCarrier.AircraftCarrier.Speed.CompareTo(yAircraftCarrier.GetAircraftCarrier.AircraftCarrier.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xAircraftCarrier.GetAircraftCarrier.AircraftCarrier.Weight.CompareTo(yAircraftCarrier.GetAircraftCarrier.AircraftCarrier.Weight); + } + } +} \ No newline at end of file