diff --git a/WarmlyShip/DrawningObjects/DrawningShip.cs b/WarmlyShip/DrawningObjects/DrawningShip.cs
index 7113b93..bd54335 100644
--- a/WarmlyShip/DrawningObjects/DrawningShip.cs
+++ b/WarmlyShip/DrawningObjects/DrawningShip.cs
@@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WarmlyShip.Entities;
+using WarmlyShip.MovementStrategy;
namespace WarmlyShip.DrawningObjects
{
@@ -16,6 +17,12 @@ namespace WarmlyShip.DrawningObjects
/// Класс-сущность
///
public EntityShip? EntityShip { get; protected set; }
+
+ ///
+ /// Получение объекта IMoveableObject из объекта DrawningShip
+ ///
+ public IMoveableObject GetMoveableObject => new DrawningObjectShip(this);
+
///
/// Ширина окна
///
diff --git a/WarmlyShip/FormShipCollection.Designer.cs b/WarmlyShip/FormShipCollection.Designer.cs
new file mode 100644
index 0000000..37938ce
--- /dev/null
+++ b/WarmlyShip/FormShipCollection.Designer.cs
@@ -0,0 +1,135 @@
+namespace WarmlyShip
+{
+ partial class FormShipCollection
+ {
+ ///
+ /// Required designer variable.
+ ///
+ private System.ComponentModel.IContainer components = null;
+
+ ///
+ /// Clean up any resources being used.
+ ///
+ /// true if managed resources should be disposed; otherwise, false.
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing && (components != null))
+ {
+ components.Dispose();
+ }
+ base.Dispose(disposing);
+ }
+
+ #region Windows Form Designer generated code
+
+ ///
+ /// Required method for Designer support - do not modify
+ /// the contents of this method with the code editor.
+ ///
+ private void InitializeComponent()
+ {
+ pictureBoxCollection = new PictureBox();
+ toolsLabel = new Panel();
+ label1 = new Label();
+ ButtonRefreshCollection = new Button();
+ ButtonDeleteShip = new Button();
+ maskedTextBoxNumber = new TextBox();
+ ButtonAddShip = new Button();
+ ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
+ toolsLabel.SuspendLayout();
+ SuspendLayout();
+ //
+ // pictureBoxCollection
+ //
+ pictureBoxCollection.Location = new Point(0, 0);
+ pictureBoxCollection.Name = "pictureBoxCollection";
+ pictureBoxCollection.Size = new Size(655, 395);
+ pictureBoxCollection.SizeMode = PictureBoxSizeMode.AutoSize;
+ pictureBoxCollection.TabIndex = 0;
+ pictureBoxCollection.TabStop = false;
+ //
+ // toolsLabel
+ //
+ toolsLabel.Controls.Add(label1);
+ toolsLabel.Controls.Add(ButtonRefreshCollection);
+ toolsLabel.Controls.Add(ButtonDeleteShip);
+ toolsLabel.Controls.Add(maskedTextBoxNumber);
+ toolsLabel.Controls.Add(ButtonAddShip);
+ toolsLabel.Location = new Point(660, 12);
+ toolsLabel.Name = "toolsLabel";
+ toolsLabel.Size = new Size(212, 383);
+ toolsLabel.TabIndex = 0;
+ //
+ // label1
+ //
+ label1.AutoSize = true;
+ label1.Location = new Point(58, -3);
+ label1.Name = "label1";
+ label1.Size = new Size(83, 15);
+ label1.TabIndex = 1;
+ label1.Text = "Инструменты";
+ //
+ // ButtonRefreshCollection
+ //
+ ButtonRefreshCollection.Location = new Point(35, 187);
+ ButtonRefreshCollection.Name = "ButtonRefreshCollection";
+ ButtonRefreshCollection.Size = new Size(142, 37);
+ ButtonRefreshCollection.TabIndex = 2;
+ ButtonRefreshCollection.Text = "Обновить коллекцию";
+ ButtonRefreshCollection.UseVisualStyleBackColor = true;
+ ButtonRefreshCollection.Click += ButtonRefreshCollection_Click;
+ //
+ // ButtonDeleteShip
+ //
+ ButtonDeleteShip.Location = new Point(35, 128);
+ ButtonDeleteShip.Name = "ButtonDeleteShip";
+ ButtonDeleteShip.Size = new Size(142, 37);
+ ButtonDeleteShip.TabIndex = 2;
+ ButtonDeleteShip.Text = "Удалить корабль";
+ ButtonDeleteShip.UseVisualStyleBackColor = true;
+ ButtonDeleteShip.Click += ButtonDeleteShip_Click;
+ //
+ // maskedTextBoxNumber
+ //
+ maskedTextBoxNumber.Location = new Point(58, 83);
+ maskedTextBoxNumber.Name = "maskedTextBoxNumber";
+ maskedTextBoxNumber.Size = new Size(100, 23);
+ maskedTextBoxNumber.TabIndex = 2;
+ //
+ // ButtonAddShip
+ //
+ ButtonAddShip.Location = new Point(35, 31);
+ ButtonAddShip.Name = "ButtonAddShip";
+ ButtonAddShip.Size = new Size(142, 36);
+ ButtonAddShip.TabIndex = 2;
+ ButtonAddShip.Text = "Добавить корабль";
+ ButtonAddShip.UseVisualStyleBackColor = true;
+ ButtonAddShip.Click += ButtonAddShip_Click;
+ //
+ // FormShipCollection
+ //
+ AutoScaleDimensions = new SizeF(7F, 15F);
+ AutoScaleMode = AutoScaleMode.Font;
+ ClientSize = new Size(884, 395);
+ Controls.Add(toolsLabel);
+ Controls.Add(pictureBoxCollection);
+ Name = "FormShipCollection";
+ Text = "Набор кораблей";
+ ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
+ toolsLabel.ResumeLayout(false);
+ toolsLabel.PerformLayout();
+ ResumeLayout(false);
+ PerformLayout();
+ }
+
+ #endregion
+
+ private PictureBox pictureBoxCollection;
+ private Panel toolsLabel;
+ private Button ButtonAddShip;
+ private Label label1;
+ private TextBox maskedTextBoxNumber;
+ private Button ButtonDeleteShip;
+ private Button ButtonRefreshCollection;
+ }
+}
\ No newline at end of file
diff --git a/WarmlyShip/FormShipCollection.cs b/WarmlyShip/FormShipCollection.cs
new file mode 100644
index 0000000..d6fc78c
--- /dev/null
+++ b/WarmlyShip/FormShipCollection.cs
@@ -0,0 +1,93 @@
+using WarmlyShip.Generics;
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Data;
+using System.Drawing;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Forms;
+using WarmlyShip.DrawningObjects;
+using WarmlyShip.MovementStrategy;
+
+namespace WarmlyShip
+{
+ ///
+ /// Форма для работы с набором объектов класса DrawningShip
+ ///
+ public partial class FormShipCollection : Form
+ {
+ ///
+ /// Набор объектов
+ ///
+ private readonly ShipsGenericCollection _theShips;
+
+ ///
+ /// Конструктор
+ ///
+ public FormShipCollection()
+ {
+ InitializeComponent();
+ _theShips = new ShipsGenericCollection
+ (pictureBoxCollection.Width, pictureBoxCollection.Height);
+ }
+
+ ///
+ /// Добавление объекта в набор
+ ///
+ ///
+ ///
+ private void ButtonAddShip_Click(object sender, EventArgs e)
+ {
+ FormWarmlyShip form = new();
+ if (form.ShowDialog() == DialogResult.OK)
+ {
+ if (_theShips + form.SelectedShip != -1)
+ {
+ MessageBox.Show("Объект добавлен");
+ pictureBoxCollection.Image = _theShips.ShowShips();
+ }
+ else
+ {
+ MessageBox.Show("Не удалось добавить объект");
+ }
+ }
+ }
+
+ ///
+ /// Удаление объекта из набора
+ ///
+ ///
+ ///
+ private void ButtonDeleteShip_Click(object sender, EventArgs e)
+ {
+ if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
+ {
+ return;
+ }
+
+ int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
+
+ if (_theShips - pos != true)
+ {
+ MessageBox.Show("Объект удален");
+ pictureBoxCollection.Image = _theShips.ShowShips();
+ }
+ else
+ {
+ MessageBox.Show("Не удалось удалить объект");
+ }
+ }
+
+ ///
+ /// Обновление рисунка по набору
+ ///
+ ///
+ ///
+ private void ButtonRefreshCollection_Click(object sender, EventArgs e)
+ {
+ pictureBoxCollection.Image = _theShips.ShowShips();
+ }
+ }
+}
diff --git a/WarmlyShip/FormShipCollection.resx b/WarmlyShip/FormShipCollection.resx
new file mode 100644
index 0000000..af32865
--- /dev/null
+++ b/WarmlyShip/FormShipCollection.resx
@@ -0,0 +1,120 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ text/microsoft-resx
+
+
+ 2.0
+
+
+ System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
\ No newline at end of file
diff --git a/WarmlyShip/FormWarmlyShip.Designer.cs b/WarmlyShip/FormWarmlyShip.Designer.cs
index 7d45a4f..d51e926 100644
--- a/WarmlyShip/FormWarmlyShip.Designer.cs
+++ b/WarmlyShip/FormWarmlyShip.Designer.cs
@@ -37,6 +37,7 @@
buttonCreateShip = new Button();
buttonStep = new Button();
buttonCreateWarmlyShip = new Button();
+ ButtonSelectedShip = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxWarmlyShip).BeginInit();
SuspendLayout();
//
@@ -142,11 +143,23 @@
buttonCreateWarmlyShip.UseVisualStyleBackColor = true;
buttonCreateWarmlyShip.Click += buttonCreateWarmlyShip_Click;
//
+ // ButtonSelectedShip
+ //
+ ButtonSelectedShip.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
+ ButtonSelectedShip.Location = new Point(213, 407);
+ ButtonSelectedShip.Name = "ButtonSelectedShip";
+ ButtonSelectedShip.Size = new Size(105, 42);
+ ButtonSelectedShip.TabIndex = 10;
+ ButtonSelectedShip.Text = "Добавить корабль";
+ ButtonSelectedShip.UseVisualStyleBackColor = true;
+ ButtonSelectedShip.Click += ButtonSelectedShip_Click;
+ //
// FormWarmlyShip
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 461);
+ Controls.Add(ButtonSelectedShip);
Controls.Add(buttonCreateWarmlyShip);
Controls.Add(buttonStep);
Controls.Add(buttonCreateShip);
@@ -176,5 +189,6 @@
private Button buttonCreateShip;
private Button buttonStep;
private Button buttonCreateWarmlyShip;
+ private Button ButtonSelectedShip;
}
}
\ No newline at end of file
diff --git a/WarmlyShip/FormWarmlyShip.cs b/WarmlyShip/FormWarmlyShip.cs
index ae61f1c..53e6c21 100644
--- a/WarmlyShip/FormWarmlyShip.cs
+++ b/WarmlyShip/FormWarmlyShip.cs
@@ -13,13 +13,21 @@ namespace WarmlyShip
///
/// Стратегия перемещения
///
- private AbstractStrategy? _abstractStrategy;
+ private AbstractStrategy? _strategy;
+
+ ///
+ /// Выбранный корабль
+ ///
+ public DrawningShip? SelectedShip { get; private set; }
+
///
/// Инициализация формы
///
public FormWarmlyShip()
{
InitializeComponent();
+ _strategy = null;
+ SelectedShip = null;
}
///
/// Метод прорисовки корабля
@@ -43,10 +51,24 @@ namespace WarmlyShip
private void buttonCreateWarmlyShip_Click(object sender, EventArgs e)
{
Random random = new();
+ Color color = Color.FromArgb(random.Next(0, 256),
+ random.Next(0, 256), random.Next(0, 256));
+ ColorDialog dialogColor = new();
+ if (dialogColor.ShowDialog() == DialogResult.OK)
+ {
+ color = dialogColor.Color;
+ }
+
+ Color dopColor = Color.FromArgb(random.Next(0, 256),
+ random.Next(0, 256), random.Next(0, 256));
+ ColorDialog dialogDopColor = new();
+ if (dialogDopColor.ShowDialog() == DialogResult.OK)
+ {
+ dopColor = dialogDopColor.Color;
+ }
+
_drawningShip = new DrawningWarmlyShip(random.Next(100, 300),
- random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
- random.Next(0, 256)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
- random.Next(0, 256)), Convert.ToBoolean(random.Next(0, 2)),
+ random.Next(1000, 3000), color, dopColor, Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)),
pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
@@ -60,9 +82,16 @@ namespace WarmlyShip
private void buttonCreateShip_Click(object sender, EventArgs e)
{
Random random = new();
+ Color color = Color.FromArgb(random.Next(0, 256),
+ random.Next(0, 256), random.Next(0, 256));
+ ColorDialog dialogColor = new();
+ if (dialogColor.ShowDialog() == DialogResult.OK)
+ {
+ color = dialogColor.Color;
+ }
+
_drawningShip = new DrawningShip(random.Next(100, 300), random.Next(1000, 3000),
- Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
- pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
+ color, pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
@@ -110,33 +139,37 @@ namespace WarmlyShip
}
if (comboBoxStrategy.Enabled)
{
- _abstractStrategy = comboBoxStrategy.SelectedIndex
- switch
+ _strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
- if (_abstractStrategy == null)
+ if (_strategy == null)
{
return;
}
- _abstractStrategy.SetData(new
- DrawningObjectShip(_drawningShip), pictureBoxWarmlyShip.Width,
- pictureBoxWarmlyShip.Height);
- comboBoxStrategy.Enabled = false;
+ _strategy.SetData(_drawningShip.GetMoveableObject,
+ pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height);
}
- if (_abstractStrategy == null)
+ if (_strategy == null)
{
return;
}
- _abstractStrategy.MakeStep();
+ comboBoxStrategy.Enabled = false;
+ _strategy.MakeStep();
Draw();
- if (_abstractStrategy.GetStatus() == Status.Finish)
+ if (_strategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
- _abstractStrategy = null;
+ _strategy = null;
}
}
+
+ private void ButtonSelectedShip_Click(object sender, EventArgs e)
+ {
+ SelectedShip = _drawningShip;
+ DialogResult = DialogResult.OK;
+ }
}
}
\ No newline at end of file
diff --git a/WarmlyShip/Generics/SetGeneric.cs b/WarmlyShip/Generics/SetGeneric.cs
new file mode 100644
index 0000000..2a1b235
--- /dev/null
+++ b/WarmlyShip/Generics/SetGeneric.cs
@@ -0,0 +1,114 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace WarmlyShip.Generics
+{
+ ///
+ /// Параметризованный набор объектов
+ ///
+ ///
+ internal class SetGeneric
+ where T : class
+ {
+ ///
+ /// Массив объектов, которые храним
+ ///
+ private readonly T?[] _places;
+
+ ///
+ /// Количество объектов в массиве
+ ///
+ public int Count => _places.Length;
+
+ ///
+ /// Конструктор
+ ///
+ ///
+ public SetGeneric(int count)
+ {
+ _places = new T?[count];
+ }
+
+ ///
+ /// Добавление объекта в набор
+ ///
+ /// Добавляемый корабль
+ ///
+ public int Insert(T ship)
+ {
+ return Insert(ship, 0);
+ }
+
+ ///
+ /// Добавление объекта в набор на конкретную позицию
+ ///
+ /// Добавляемый корабль
+ /// Позиция
+ ///
+ public int Insert(T ship, int position)
+ {
+ int nullIndex = -1, i;
+
+ if (position < 0 || position >= Count)
+ {
+ return -1;
+ }
+
+ for (i = position; i < Count; i++)
+ {
+ if (_places[i] == null)
+ {
+ nullIndex = i;
+ break;
+ }
+ }
+
+ if (nullIndex < 0)
+ {
+ return -1;
+ }
+
+ for (i = nullIndex; i > position; i--)
+ {
+ _places[i] = _places[i - 1];
+ }
+
+ _places[position] = ship;
+ return position;
+ }
+
+ ///
+ /// Удаление объекта из набора с конкретной позиции
+ ///
+ ///
+ ///
+ public bool Remove(int position)
+ {
+ if (position < 0 || position >= Count)
+ {
+ return false;
+ }
+
+ _places[position] = null;
+ return true;
+ }
+
+ ///
+ /// Получение объекта из набора по позиции
+ ///
+ ///
+ ///
+ public T? Get(int position)
+ {
+ if (position < 0 || position >= Count)
+ {
+ return null;
+ }
+
+ return _places[position];
+ }
+ }
+}
diff --git a/WarmlyShip/Generics/ShipsGenericCollection.cs b/WarmlyShip/Generics/ShipsGenericCollection.cs
new file mode 100644
index 0000000..6eb2675
--- /dev/null
+++ b/WarmlyShip/Generics/ShipsGenericCollection.cs
@@ -0,0 +1,153 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Microsoft.VisualBasic;
+using WarmlyShip.DrawningObjects;
+using WarmlyShip.MovementStrategy;
+
+namespace WarmlyShip.Generics
+{
+ ///
+ /// Параметризованный класс для набора объектов DrawningShip
+ ///
+ ///
+ ///
+ internal class ShipsGenericCollection
+ where T : DrawningShip
+ where U : IMoveableObject
+ {
+ ///
+ /// Ширина окна прорисовки
+ ///
+ private readonly int _pictureWidth;
+
+ ///
+ /// Высота окна прорисовки
+ ///
+ private readonly int _pictureHeight;
+
+ ///
+ /// Размер занимаемого объектом места (ширина)
+ ///
+ private readonly int _placeSizeWidth = 210;
+
+ ///
+ /// Размер занимаемого объектом места (высота)
+ ///
+ private readonly int _placeSizeHeight = 90;
+
+ ///
+ /// Набор объектов
+ ///
+ private readonly SetGeneric _collection;
+
+ ///
+ /// Конструктор
+ ///
+ ///
+ ///
+ public ShipsGenericCollection(int picWidth, int picHeight)
+ {
+ int width = picWidth / _placeSizeWidth;
+ int height = picHeight / _placeSizeHeight;
+ _pictureWidth = picWidth;
+ _pictureHeight = picHeight;
+ _collection = new SetGeneric(width * height);
+ }
+
+ ///
+ /// Перегрузка оператора сложения
+ ///
+ ///
+ ///
+ ///
+ public static int operator +(ShipsGenericCollection collect, T? obj)
+ {
+ if (obj == null)
+ {
+ return -1;
+ }
+ return collect._collection.Insert(obj);
+ }
+
+ ///
+ /// Перегрузка оператора вычитания
+ ///
+ ///
+ ///
+ ///
+ public static bool operator -(ShipsGenericCollection collect, int
+ pos)
+ {
+ T? obj = collect._collection.Get(pos);
+ if (obj != null)
+ {
+ collect._collection.Remove(pos);
+ }
+ return false;
+ }
+
+ ///
+ /// Получение объекта IMoveableObject
+ ///
+ ///
+ ///
+ public U? GetU(int pos)
+ {
+ return (U?)_collection.Get(pos)?.GetMoveableObject;
+ }
+
+ ///
+ /// Вывод всего набора объектов
+ ///
+ ///
+ public Bitmap ShowShips()
+ {
+ Bitmap bmp = new(_pictureWidth, _pictureHeight);
+ Graphics gr = Graphics.FromImage(bmp);
+ DrawBackground(gr);
+ DrawObjects(gr);
+ return bmp;
+ }
+
+ ///
+ /// Метод отрисовки фона
+ ///
+ ///
+ 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 DrawObjects(Graphics g)
+ {
+ for (int i = 0; i < _collection.Count; i++)
+ {
+ DrawningShip ship = _collection.Get(i);
+
+ if (ship != null)
+ {
+ int inRow = _pictureWidth / _placeSizeWidth;
+ ship.SetPosition((inRow - 1 - (i % inRow)) * _placeSizeWidth, i / inRow * _placeSizeHeight);
+ ship.DrawTransport(g);
+ }
+ }
+ }
+ }
+}
diff --git a/WarmlyShip/Program.cs b/WarmlyShip/Program.cs
index 71da933..2dda87b 100644
--- a/WarmlyShip/Program.cs
+++ b/WarmlyShip/Program.cs
@@ -11,7 +11,7 @@ namespace WarmlyShip
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
- Application.Run(new FormWarmlyShip());
+ Application.Run(new FormShipCollection());
}
}
}
\ No newline at end of file