From 64d37b837fe74022c934d7b50c0878ec10e30350 Mon Sep 17 00:00:00 2001 From: "A.Novopoltsev" Date: Fri, 16 Dec 2022 22:16:07 +0300 Subject: [PATCH] generic classes --- Warship/Warship/MapWithSetWarshipsGeneric.cs | 122 +++++++++++++++++++ Warship/Warship/SetWarshipsGeneric.cs | 81 ++++++++++++ 2 files changed, 203 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..7255d4c --- /dev/null +++ b/Warship/Warship/MapWithSetWarshipsGeneric.cs @@ -0,0 +1,122 @@ +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 int operator +(MapWithSetWarshipsGeneric map, T warship) + { + return map._setWarship.Insert(warship); + } + + public static T 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); + return bmp; + } + + public Bitmap ShowOnMap() + { + Shaking(); + foreach (var warship in _setWarship.GetWarships()) + { + 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[i] == null) + { + for (; j > i; j--) + { + var warship = _setWarship[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, 5); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + gr.DrawLine(pen, i * _placeSizeWidth + 20, j * _placeSizeHeight + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2); + gr.DrawLine(pen, i * _placeSizeWidth + 20, j * _placeSizeHeight + _placeSizeHeight / 2 + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight / 2 + 2); + gr.DrawLine(pen, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2); + gr.DrawLine(pen, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight); + } + } + } + + private void DrawWarship(Graphics gr) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + int i = 0; + + foreach (var warship in _setWarship.GetWarships()) + { + warship.SetObject(i % width * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight, _pictureWidth, _pictureHeight); + warship.DrawingObject(gr); + i++; + } + } + } +} diff --git a/Warship/Warship/SetWarshipsGeneric.cs b/Warship/Warship/SetWarshipsGeneric.cs new file mode 100644 index 0000000..141ba87 --- /dev/null +++ b/Warship/Warship/SetWarshipsGeneric.cs @@ -0,0 +1,81 @@ +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 List _places; + + public int Count => _places.Count; + + private readonly int _maxCount; + + public SetWarshipsGeneric(int count) + { + _maxCount = count; + _places = new List(); + } + + public int Insert(T warship) + { + if (_places.Count + 1 >= _maxCount) + return -1; + _places.Insert(0, warship); + return 0; + } + + public int Insert(T warship, int position) + { + if (position >= _maxCount || position < 0) + return -1; + if (_places.Count + 1 >= _maxCount) + return -1; + _places.Insert(position, warship); + return position; + } + + public T Remove(int position) + { + if (position >= _maxCount || position < 0) + return null; + + T deleted = _places[position]; + _places.RemoveAt(position); + return deleted; + } + + public T this[int position] + { + get + { + if (position < 0 || position >= _maxCount) + return null; + return _places[position]; + } + set + { + if (position < 0 || position >= _maxCount) + Insert(value, position); + } + } + public IEnumerable GetWarships() + { + foreach (var warship in _places) + { + if (warship != null) + { + yield return warship; + } + else + { + yield break; + } + } + } + } +}