From 296f2fdacda93f45c15c72f8312d3c1ed8c8d567 Mon Sep 17 00:00:00 2001 From: AnnZhimol Date: Fri, 30 Sep 2022 13:02:18 +0400 Subject: [PATCH] generic classes --- Warship/Warship/MapWithSetWarshipsGeneric.cs | 119 +++++++++++++++++++ Warship/Warship/SetWarshipsGeneric.cs | 57 +++++++++ 2 files changed, 176 insertions(+) create mode 100644 Warship/Warship/MapWithSetWarshipsGeneric.cs create mode 100644 Warship/Warship/SetWarshipsGeneric.cs diff --git a/Warship/Warship/MapWithSetWarshipsGeneric.cs b/Warship/Warship/MapWithSetWarshipsGeneric.cs new file mode 100644 index 0000000..180a80f --- /dev/null +++ b/Warship/Warship/MapWithSetWarshipsGeneric.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Warship +{ + internal class MapWithSetWarshipsGeneric + where T : class, IDrawingObject + where U : AbstractMap + { + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 120; + private readonly int _placeSizeHeight = 50; + private readonly SetWarshipsGeneric _setWarship; + private readonly U _map; + + public MapWithSetWarshipsGeneric(int picWidth,int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight/_placeSizeHeight; + _setWarship = new SetWarshipsGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public static bool operator +(MapWithSetWarshipsGeneric map, T warship) + { + return map._setWarship.Insert(warship); + } + + public static bool operator -(MapWithSetWarshipsGeneric map, int position) + { + return map._setWarship.Remove(position); + } + + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureWidth); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawWarship(gr, _placeSizeWidth,_placeSizeHeight,_pictureWidth,_pictureHeight); + return bmp; + } + + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setWarship.Count; i++) + { + var warship = _setWarship.Get(i); + if (warship != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, warship); + } + } + return new(_pictureWidth, _pictureHeight); + } + + public Bitmap MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new(_pictureWidth, _pictureHeight); + } + + public void Shaking() + { + int j = _setWarship.Count - 1; + for (int i = 0; i < _setWarship.Count; i++) + { + if (_setWarship.Get(i) == null) + { + for (; j > i; j--) + { + var warship = _setWarship.Get(j); + if (warship != null) + { + _setWarship.Insert(warship, i); + _setWarship.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + private void DrawBackground(Graphics gr) + { + Pen pen = new(Color.Black, 3); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + gr.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + } + gr.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight/_placeSizeHeight) * _placeSizeHeight); + } + } + + private void DrawWarship(Graphics gr, int x,int y,int width,int height) + { + for (int i = 0; i < _setWarship.Count; i++) + { + _setWarship.Get(i)?.SetObject(x, y,width,height); + _setWarship.Get(i)?.DrawingObject(gr); + } + } + } +} diff --git a/Warship/Warship/SetWarshipsGeneric.cs b/Warship/Warship/SetWarshipsGeneric.cs new file mode 100644 index 0000000..ccf9090 --- /dev/null +++ b/Warship/Warship/SetWarshipsGeneric.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Warship +{ + internal class SetWarshipsGeneric + where T : class + { + private readonly T[] _places; + + public int Count => _places.Length; + + public SetWarshipsGeneric(int count) + { + _places = new T[count]; + } + + public bool Insert(T warship) + { + _places[0] = warship; + return true; + } + + public bool Insert(T warship, int position) + { + int EmptyEl=position; + + if (_places[position]!=null) + { + for (int i = position; i < Count; i++) + if (_places[i] == null) + { + EmptyEl = i; + break; + } + for (int j = EmptyEl; j > position; j--) + _places[j] = _places[j - 1]; + } + _places[position] = warship; + return true; + } + + public bool Remove(int position) + { + _places[position] = null; + return true; + } + + public T Get(int position) + { + return _places[position]; + } + } +}