diff --git a/Battleship/Battleship/MapWithSetBattleshipGeneric.cs b/Battleship/Battleship/MapWithSetBattleshipGeneric.cs new file mode 100644 index 0000000..3582ddd --- /dev/null +++ b/Battleship/Battleship/MapWithSetBattleshipGeneric.cs @@ -0,0 +1,124 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Battleship +{ + /// + /// Карта с набром объектов под нее + /// + /// + /// + internal class MapWithSetBattleshipGeneric + where T : class, IDrawningObject + where U : AbstractMap + { + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 130; + private readonly int _placeSizeHeight = 40; + private readonly SetBattleshipGeneric _setBattleship; + private readonly U _map; + + public MapWithSetBattleshipGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setBattleship = new SetBattleshipGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public static bool operator +(MapWithSetBattleshipGeneric map, T battleship) + { + return map._setBattleship.Insert(battleship); + } + + public static bool operator -(MapWithSetBattleshipGeneric map, int position) + { + return map._setBattleship.Remove(position); + } + + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureWidth); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawBattleship(gr, _placeSizeWidth, _placeSizeHeight, _pictureWidth, _pictureHeight); + return bmp; + } + + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setBattleship.Count; i++) + { + var battleship = _setBattleship.Get(i); + if (battleship != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, battleship); + } + } + 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 = _setBattleship.Count - 1; + for (int i = 0; i < _setBattleship.Count; i++) + { + if (_setBattleship.Get(i) == null) + { + for (; j > i; j--) + { + var battleship = _setBattleship.Get(j); + if (battleship != null) + { + _setBattleship.Insert(battleship, i); + _setBattleship.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 DrawBattleship(Graphics gr, int x, int y, int width, int height) + { + for (int i = 0; i < _setBattleship.Count; i++) + { + _setBattleship.Get(i)?.SetObject(x, y, width, height); + _setBattleship.Get(i)?.DrawningObject(gr); + } + } + } +} diff --git a/Battleship/Battleship/SetBattleshipGeneric.cs b/Battleship/Battleship/SetBattleshipGeneric.cs new file mode 100644 index 0000000..70f68f7 --- /dev/null +++ b/Battleship/Battleship/SetBattleshipGeneric.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Battleship +{ + /// + /// Параметризованный набор объектов + /// + /// + internal class SetBattleshipGeneric + where T : class + { + private readonly T[] _places; + + public int Count => _places.Length; + + public SetBattleshipGeneric(int count) + { + _places = new T[count]; + } + + public bool Insert(T battleship) + { + _places[0] = battleship; + return true; + } + + public bool Insert(T battleship, 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] = battleship; + return true; + } + + public bool Remove(int position) + { + _places[position] = null; + return true; + } + + public T Get(int position) + { + return _places[position]; + } + } +}