From b066252a729d3782d7976e4602e402d93e17ff08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BE=D0=BB=D0=BE=D0=B4=D1=8F?= Date: Mon, 10 Oct 2022 18:08:45 +0300 Subject: [PATCH] generic classes --- .../MapWithSetPlainGeneric.cs | 124 ++++++++++++++++++ .../AirPlaneWithRadar/SetPlaneGeneric.cs | 38 ++++++ 2 files changed, 162 insertions(+) create mode 100644 AirPlaneWithRadar/AirPlaneWithRadar/MapWithSetPlainGeneric.cs create mode 100644 AirPlaneWithRadar/AirPlaneWithRadar/SetPlaneGeneric.cs diff --git a/AirPlaneWithRadar/AirPlaneWithRadar/MapWithSetPlainGeneric.cs b/AirPlaneWithRadar/AirPlaneWithRadar/MapWithSetPlainGeneric.cs new file mode 100644 index 0000000..b607f05 --- /dev/null +++ b/AirPlaneWithRadar/AirPlaneWithRadar/MapWithSetPlainGeneric.cs @@ -0,0 +1,124 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirPlaneWithRadar +{ + internal class MapWithSetPlainGeneric + + where T : class, IDrawingObject + where U : AbstractMap + { + private readonly int _pictureWidth; + + private readonly int _pictureHeight; + + private readonly int _placeSizeWidth = 180; + + private readonly int _placeSizeHeight = 120; + + private readonly SetPlaneGeneric _setPlains; + + private readonly U _map; + + public MapWithSetPlainGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setPlains = new SetPlaneGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public static bool operator +(MapWithSetPlainGeneric map, T plain) + { + return map._setPlains.Insert(plain); + } + + public static bool operator -(MapWithSetPlainGeneric map, int position) + { + return map._setPlains.Remove(position); + } + + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawPlains(gr); + return bmp; + } + + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setPlains.Count; i++) + { + var plain = _setPlains.Get(i); + if (plain != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, plain); + } + } + return new(_pictureWidth, _pictureHeight); + } + + public Bitmap MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new(_pictureWidth, _pictureHeight); + } + + private void Shaking() + { + int j = _setPlains.Count - 1; + for (int i = 0; i < _setPlains.Count; i++) + { + if (_setPlains.Get(i) == null) + { + for (; j > i; j--) + { + var plain = _setPlains.Get(j); + if (plain != null) + { + _setPlains.Insert(plain, i); + _setPlains.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + private void DrawBackground(Graphics g) + { + Brush BrushRazmetka = new SolidBrush(Color.DarkGray); + + Pen pen = new(Color.White, 5); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + } + private void DrawPlains(Graphics g) + { + + + } + } +} diff --git a/AirPlaneWithRadar/AirPlaneWithRadar/SetPlaneGeneric.cs b/AirPlaneWithRadar/AirPlaneWithRadar/SetPlaneGeneric.cs new file mode 100644 index 0000000..b1505bd --- /dev/null +++ b/AirPlaneWithRadar/AirPlaneWithRadar/SetPlaneGeneric.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirPlaneWithRadar +{ + internal class SetPlaneGeneric + where T : class + + { + private readonly T[] _places; + public int Count => _places.Length; + public SetPlaneGeneric(int count) + { + _places = new T[count]; + } + public bool Insert(T plain) + { + return true; + } + public bool Insert(T plain, int position) + { + return true; + + } + public bool Remove(int position) + { + return false; + } + public T Get(int position) + { + return _places[position]; + } + + } +}