diff --git a/WarmlyShip/WarmlyShip/Direction.cs b/WarmlyShip/WarmlyShip/Direction.cs index 09a6267..3669bef 100644 --- a/WarmlyShip/WarmlyShip/Direction.cs +++ b/WarmlyShip/WarmlyShip/Direction.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace WarmlyShip { - internal enum Direction //Направление при перемещении + public enum Direction //Направление при перемещении { None = 0, Left = 1, //Влево diff --git a/WarmlyShip/WarmlyShip/DrawingWarmlyShip.cs b/WarmlyShip/WarmlyShip/DrawingWarmlyShip.cs index aa3dab2..05f9aff 100644 --- a/WarmlyShip/WarmlyShip/DrawingWarmlyShip.cs +++ b/WarmlyShip/WarmlyShip/DrawingWarmlyShip.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace WarmlyShip { - internal class DrawingWarmlyShip + public class DrawingWarmlyShip { public EntityWarmlyShip warmlyShip { protected set; get; } //Класс-сущность protected float _startPosX; //Координаты отрисовки по оси x diff --git a/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs b/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs index 1da24c8..e36f167 100644 --- a/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs +++ b/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace WarmlyShip { - internal class EntityWarmlyShip + public class EntityWarmlyShip { public int Speed { get; private set; } //Скорость public float Weight { get; private set; } //Вес diff --git a/WarmlyShip/WarmlyShip/FormClass.Designer.cs b/WarmlyShip/WarmlyShip/FormClass.Designer.cs index d24e401..7e5c168 100644 --- a/WarmlyShip/WarmlyShip/FormClass.Designer.cs +++ b/WarmlyShip/WarmlyShip/FormClass.Designer.cs @@ -39,6 +39,7 @@ this.buttonDown = new System.Windows.Forms.Button(); this.ButtonCreate = new System.Windows.Forms.Button(); this.buttonCreateModif = new System.Windows.Forms.Button(); + this.buttonSelectShip = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.statusStrip.SuspendLayout(); this.SuspendLayout(); @@ -154,11 +155,22 @@ this.buttonCreateModif.UseVisualStyleBackColor = true; this.buttonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click); // + // buttonSelectShip + // + this.buttonSelectShip.Location = new System.Drawing.Point(522, 402); + this.buttonSelectShip.Name = "buttonSelectShip"; + this.buttonSelectShip.Size = new System.Drawing.Size(75, 23); + this.buttonSelectShip.TabIndex = 9; + this.buttonSelectShip.Text = "Выбрать"; + this.buttonSelectShip.UseVisualStyleBackColor = true; + this.buttonSelectShip.Click += new System.EventHandler(this.ButtonSelectShip_Click); + // // FormClass // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.buttonSelectShip); this.Controls.Add(this.buttonCreateModif); this.Controls.Add(this.ButtonCreate); this.Controls.Add(this.buttonDown); @@ -190,5 +202,6 @@ private Button buttonDown; private Button ButtonCreate; private Button buttonCreateModif; + private Button buttonSelectShip; } } \ No newline at end of file diff --git a/WarmlyShip/WarmlyShip/FormClass.cs b/WarmlyShip/WarmlyShip/FormClass.cs index 2e0cb90..7fdaa22 100644 --- a/WarmlyShip/WarmlyShip/FormClass.cs +++ b/WarmlyShip/WarmlyShip/FormClass.cs @@ -4,6 +4,8 @@ namespace WarmlyShip { private DrawingWarmlyShip _warmlyShip; + public DrawingWarmlyShip SelectedShip { get; private set; } + public FormClass() { InitializeComponent(); @@ -71,5 +73,11 @@ namespace WarmlyShip SetData(); Draw(); } + + private void ButtonSelectShip_Click(object sender, EventArgs e) + { + SelectedShip = _warmlyShip; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs b/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs new file mode 100644 index 0000000..476b183 --- /dev/null +++ b/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs @@ -0,0 +1,122 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyShip +{ + internal class MapWithSetShipGeneric + where T : class, IDrawningObject + where U : AbstractMap + { + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 210; + private readonly int _placeSizeHeight = 90; + private readonly SetShipGeneric _setShips; + private readonly U _map; + + public MapWithSetShipGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setShips = new SetShipGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public static bool operator +(MapWithSetShipGeneric map, T ship) + { + return map._setShips.Insert(ship); + } + + public static bool operator -(MapWithSetShipGeneric map, int position) + { + return map._setShips.Remove(position); + } + + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawShips(gr); + return bmp; + } + + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setShips.Count; i++) + { + var ship = _setShips.Get(i); + if (ship != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, ship); + } + } + 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 = _setShips.Count - 1; + for (int i = 0; i < _setShips.Count; i++) + { + if (_setShips.Get(i) == null) + { + for (; j > i; j--) + { + var ship = _setShips.Get(j); + if (ship != null) + { + _setShips.Insert(ship, i); + _setShips.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 3); + 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 DrawShips(Graphics g) + { + for (int i = 0; i < _setShips.Count; i++) + { + // TODO установка позиции + _setShips.Get(i)?.DrawningObject(g); + } + } + + } +} diff --git a/WarmlyShip/WarmlyShip/SetShipGeneric.cs b/WarmlyShip/WarmlyShip/SetShipGeneric.cs new file mode 100644 index 0000000..e334b1d --- /dev/null +++ b/WarmlyShip/WarmlyShip/SetShipGeneric.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyShip +{ + internal class SetShipGeneric + where T : class + { + private readonly T[] _places; + public int Count => _places.Length; + public SetShipGeneric(int count) + { + _places = new T[count]; + } + public bool Insert(T ship) + { + // TODO вставка в начало набора + return true; + } + + public bool Insert(T ship, int position) + { + // TODO проверка позиции + // TODO проверка, что элемент массива по этой позиции пустой, если нет, то + // проверка, что после вставляемого элемента в массиве есть пустой элемент + // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + // TODO вставка по позиции + _places[position] = ship; + return true; + } + + public bool Remove(int position) + { + // TODO проверка позиции + // TODO удаление объекта из массива, присовив элементу массива значение null + return true; + } + + public T Get(int position) + { + // TODO проверка позиции + return _places[position]; + } + + } +}