diff --git a/AircraftCarrier/AircraftCarrier/DrawingObjectWarship.cs b/AircraftCarrier/AircraftCarrier/DrawingObjectWarship.cs index 0f47d91..ff13390 100644 --- a/AircraftCarrier/AircraftCarrier/DrawingObjectWarship.cs +++ b/AircraftCarrier/AircraftCarrier/DrawingObjectWarship.cs @@ -27,7 +27,8 @@ namespace ProjectAircraftCarrier.MovementStrategy return null; } return new ObjectParameters(_drawingWarship.GetPosX, - _drawingWarship.GetPosY, _drawingWarship.GetWidth, _drawingWarship.GetHeight); + _drawingWarship.GetPosY, _drawingWarship.GetWidth, + _drawingWarship.GetHeight); } } public int GetStep => (int)(_drawingWarship?.EntityWarship?.Step ?? 0); diff --git a/AircraftCarrier/AircraftCarrier/DrawingWarship.cs b/AircraftCarrier/AircraftCarrier/DrawingWarship.cs index d304dbb..9d727f9 100644 --- a/AircraftCarrier/AircraftCarrier/DrawingWarship.cs +++ b/AircraftCarrier/AircraftCarrier/DrawingWarship.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using ProjectAircraftCarrier.Entities; +using ProjectAircraftCarrier.MovementStrategy; namespace ProjectAircraftCarrier.DrawingObjects { @@ -91,7 +92,6 @@ width, int height, int warshipWidth, int warshipHeight) _warshipHeight = warshipHeight; EntityWarship = new EntityWarship(speed, weight, bodyColor); } - /// /// Установка позиции /// @@ -99,13 +99,13 @@ width, int height, int warshipWidth, int warshipHeight) /// Координата Y public void SetPosition(int x, int y) { - if (x < 0 || y < 0 || x + _warshipWidth > _pictureWidth || y + _warshipHeight > _pictureHeight) - { - x = 0; - y = 0; - } _startPosX = x; _startPosY = y; + if (_startPosX < 0 || _startPosY < 0) + { + _startPosX = 50; + _startPosY = 50; + } } /// /// Проверка, что объект может переместится по указанному направлению @@ -212,5 +212,9 @@ width, int height, int warshipWidth, int warshipHeight) g.DrawEllipse(pen, _startPosX + 138, _startPosY + 22, 11, 11); g.FillEllipse(brBlack, _startPosX + 138, _startPosY + 22, 11, 11); } + /// + /// Получение объекта IMoveableObject из объекта DrawningCar + /// + public IMoveableObject GetMoveableObject => new DrawingObjectWarship(this); } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs index 9fccf12..de83fc6 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs @@ -40,16 +40,17 @@ namespace ProjectAircraftCarrier comboBoxStrategy = new ComboBox(); buttonStep = new Button(); buttonCreateWarship = new Button(); + buttonSelectWarship = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAircraftCarrier).BeginInit(); SuspendLayout(); // // buttonCreateAircraftCarrier // buttonCreateAircraftCarrier.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateAircraftCarrier.Location = new Point(4, 527); + buttonCreateAircraftCarrier.Location = new Point(8, 523); buttonCreateAircraftCarrier.Margin = new Padding(4, 3, 4, 3); buttonCreateAircraftCarrier.Name = "buttonCreateAircraftCarrier"; - buttonCreateAircraftCarrier.Size = new Size(193, 37); + buttonCreateAircraftCarrier.Size = new Size(200, 37); buttonCreateAircraftCarrier.TabIndex = 0; buttonCreateAircraftCarrier.Text = "Create Aircraft Carrier"; buttonCreateAircraftCarrier.UseVisualStyleBackColor = true; @@ -120,6 +121,7 @@ namespace ProjectAircraftCarrier // // comboBoxStrategy // + comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right; comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxStrategy.FormattingEnabled = true; comboBoxStrategy.Items.AddRange(new object[] { "Move to center", "Move to border" }); @@ -144,20 +146,32 @@ namespace ProjectAircraftCarrier // buttonCreateWarship // buttonCreateWarship.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateWarship.Location = new Point(206, 525); + buttonCreateWarship.Location = new Point(216, 523); buttonCreateWarship.Margin = new Padding(4, 3, 4, 3); buttonCreateWarship.Name = "buttonCreateWarship"; - buttonCreateWarship.Size = new Size(200, 38); + buttonCreateWarship.Size = new Size(200, 37); buttonCreateWarship.TabIndex = 8; buttonCreateWarship.Text = "Create Warship"; buttonCreateWarship.UseVisualStyleBackColor = true; buttonCreateWarship.Click += ButtonCreateWarship_Click; // + // buttonSelectWarship + // + buttonSelectWarship.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonSelectWarship.Location = new Point(424, 523); + buttonSelectWarship.Name = "buttonSelectWarship"; + buttonSelectWarship.Size = new Size(200, 37); + buttonSelectWarship.TabIndex = 9; + buttonSelectWarship.Text = "Select Warship"; + buttonSelectWarship.UseVisualStyleBackColor = true; + buttonSelectWarship.Click += ButtonSelectWarhip_Click; + // // FormAircraftCarrier // AutoScaleDimensions = new SizeF(10F, 25F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(1103, 567); + Controls.Add(buttonSelectWarship); Controls.Add(buttonCreateWarship); Controls.Add(buttonStep); Controls.Add(comboBoxStrategy); @@ -170,7 +184,7 @@ namespace ProjectAircraftCarrier Margin = new Padding(4, 3, 4, 3); Name = "FormAircraftCarrier"; StartPosition = FormStartPosition.CenterScreen; - Text = "AircraftCarrier"; + Text = "Aircraft Carrier"; ((System.ComponentModel.ISupportInitialize)pictureBoxAircraftCarrier).EndInit(); ResumeLayout(false); } @@ -186,5 +200,6 @@ namespace ProjectAircraftCarrier private ComboBox comboBoxStrategy; private Button buttonStep; private Button buttonCreateWarship; + private Button buttonSelectWarship; } } diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs index 3e0aa7d..d1ca416 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs @@ -15,11 +15,17 @@ namespace ProjectAircraftCarrier /// private AbstractStrategy? _abstractStrategy; /// + /// + /// + public DrawingWarship? SelectedWarship { get; private set; } + /// /// /// public FormAircraftCarrier() { InitializeComponent(); + SelectedWarship = null; + _abstractStrategy = null; } /// /// @@ -44,17 +50,25 @@ namespace ProjectAircraftCarrier private void ButtonCreateAircraftCarrier_Click(object sender, EventArgs e) { Random random = new(); + Color bodyColor = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + bodyColor = dialog.Color; + } + Color additColor = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + if (dialog.ShowDialog() == DialogResult.OK) + { + additColor = dialog.Color; + } _drawingWarship = new DrawingAircraftCarrier(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), bodyColor, + additColor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height); - _drawingWarship.SetPosition(random.Next(0, pictureBoxAircraftCarrier.Width), - random.Next(0, pictureBoxAircraftCarrier.Height)); + _drawingWarship.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } /// @@ -65,9 +79,16 @@ namespace ProjectAircraftCarrier private void ButtonCreateWarship_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 dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } _drawingWarship = new DrawingWarship(random.Next(100, 300), - random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), - random.Next(0, 256), random.Next(0, 256)), pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height); + random.Next(1000, 3000), color, + pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height); _drawingWarship.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } @@ -100,6 +121,11 @@ namespace ProjectAircraftCarrier } Draw(); } + private void ButtonSelectWarhip_Click(object sender, EventArgs e) + { + SelectedWarship = _drawingWarship; + DialogResult = DialogResult.OK; + } /// /// "" /// diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.resx b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.resx index 73ede97..c9a1a2e 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.resx +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.resx @@ -121,7 +121,7 @@ iVBORw0KGgoAAAANSUhEUgAAA5gAAAOYBAMAAABC5kGOAAAABGdBTUEAALGPC/xhBQAAABJQTFRF5ubm - ////AAAAIyMjycnJiIiI9drrEwAAAAlwSFlzAAAOwQAADsEBuJFr7QAAE2hJREFUeNrtncty01wWRqUK + ////AAAAIyMjycnJiIiI9drrEwAAAAlwSFlzAAAOvQAADr0BR/uQrQAAE2hJREFUeNrtncty01wWRqUK nkvV9gvIZE6bMCdtmJME3v9VmkC4JU4sW/t2vqxdXdX/YqKtvUqfFZ0juxvHoftZ488Cm0VkIhOsiMhE JlgRv1f/8E+gADIJIWQSQsgkhJBJCCGTEEImIYRMQgiZhBAyCSFkEkLIJISQSQghkxBCJiGDLAbqIDKR CVZEZCITrIjIRCZYEZGJTLAiIhOZYEVEJjLBiohMZIIVsWMxUAqZhBAyCSFkEkLIJISQSQghkxBCJiGE @@ -310,7 +310,7 @@ iVBORw0KGgoAAAANSUhEUgAAA5gAAAOYBAMAAABC5kGOAAAABGdBTUEAALGPC/xhBQAAABJQTFRF5ubm - ////AAAAIyMjycnJiIiI9drrEwAAAAlwSFlzAAAOwQAADsEBuJFr7QAAFo9JREFUeNrtnc162zgSRYuf + ////AAAAIyMjycnJiIiI9drrEwAAAAlwSFlzAAAOvQAADr0BR/uQrQAAFo9JREFUeNrtnc162zgSRYuf 23vqG+kFSHOvdpR9nHT2ktL9/q8yhghIsiPFtswf3MK5M5ujzRTrDBCAIGmLqRZ9QGE0OuEHjU74QaMT ftDohB+0+EsdfwKVkU44QjrhCOmEI6QTjpBOOEI64QjphCOkE46QTjhCOuEI6YQjpBOOkE44QjrhDI3D QBdodMIPGp3wg0Yn/KDRCT9odMIPGp3wg0Yn/KDRCT9odMIPGp3wg0Yn/KDRCT9odMIPGp3wgxwGekI6 @@ -413,7 +413,7 @@ iVBORw0KGgoAAAANSUhEUgAAA5gAAAOYBAMAAABC5kGOAAAABGdBTUEAALGPC/xhBQAAABJQTFRF5ubm - ////AAAAIyMjycnJiIiI9drrEwAAAAlwSFlzAAAOwQAADsEBuJFr7QAAFRpJREFUeNrt3cGS2zYWRmGw + ////AAAAIyMjycnJiIiI9drrEwAAAAlwSFlzAAAOvQAADr0BR/uQrQAAFRpJREFUeNrt3cGS2zYWRmGw 4uzJiv0CTXGvtuV9mEn2ouO8/6tMS6Laduva3ZJI4N4fRzWbk8WMiW+IgAAlp5Sa7vRJpw8ZORkJoWQk hJKREEpGQigZCaFkJISSkRBKRkIoGQmhZCSEkpEQSkZCKBkJoWQkZLLr2vkfzf+EDJtggkl6TDDBJD0m mGCSHhNMMEmPCSaYpMcEE0zSY4IJJukxE4eBUslICCUjIZSMhFAyEkLJSAglIyGUjIRQMhJCyUgIJSMh diff --git a/AircraftCarrier/AircraftCarrier/FormWarshipCollection.Designer.cs b/AircraftCarrier/AircraftCarrier/FormWarshipCollection.Designer.cs new file mode 100644 index 0000000..5c53066 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/FormWarshipCollection.Designer.cs @@ -0,0 +1,133 @@ +namespace ProjectAircraftCarrier +{ + partial class FormWarshipCollection + { + /// + /// 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() + { + groupBoxCollection = new GroupBox(); + maskedTextBoxNumber = new MaskedTextBox(); + buttonRefreshCollection = new Button(); + buttonRemoveWarship = new Button(); + buttonAddWarship = new Button(); + pictureBoxCollection = new PictureBox(); + groupBoxCollection.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + SuspendLayout(); + // + // groupBoxCollection + // + groupBoxCollection.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right; + groupBoxCollection.Controls.Add(maskedTextBoxNumber); + groupBoxCollection.Controls.Add(buttonRefreshCollection); + groupBoxCollection.Controls.Add(buttonRemoveWarship); + groupBoxCollection.Controls.Add(buttonAddWarship); + groupBoxCollection.Location = new Point(893, 3); + groupBoxCollection.Margin = new Padding(4, 5, 4, 5); + groupBoxCollection.Name = "groupBoxCollection"; + groupBoxCollection.Padding = new Padding(4, 5, 4, 5); + groupBoxCollection.Size = new Size(247, 743); + groupBoxCollection.TabIndex = 0; + groupBoxCollection.TabStop = false; + groupBoxCollection.Text = "Tools"; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new Point(56, 115); + maskedTextBoxNumber.Margin = new Padding(4, 5, 4, 5); + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(141, 31); + maskedTextBoxNumber.TabIndex = 4; + maskedTextBoxNumber.Text = "_"; + // + // buttonRefreshCollection + // + buttonRefreshCollection.Location = new Point(9, 300); + buttonRefreshCollection.Margin = new Padding(4, 5, 4, 5); + buttonRefreshCollection.Name = "buttonRefreshCollection"; + buttonRefreshCollection.Size = new Size(224, 45); + buttonRefreshCollection.TabIndex = 3; + buttonRefreshCollection.Text = "Refresh collection"; + buttonRefreshCollection.UseVisualStyleBackColor = true; + buttonRefreshCollection.Click += ButtonRefreshCollection_Click; + // + // buttonRemoveWarship + // + buttonRemoveWarship.Location = new Point(9, 187); + buttonRemoveWarship.Margin = new Padding(4, 5, 4, 5); + buttonRemoveWarship.Name = "buttonRemoveWarship"; + buttonRemoveWarship.Size = new Size(224, 45); + buttonRemoveWarship.TabIndex = 2; + buttonRemoveWarship.Text = "Remove warship"; + buttonRemoveWarship.UseVisualStyleBackColor = true; + buttonRemoveWarship.Click += ButtonRemoveWarship_Click; + // + // buttonAddWarship + // + buttonAddWarship.Location = new Point(9, 37); + buttonAddWarship.Margin = new Padding(4, 5, 4, 5); + buttonAddWarship.Name = "buttonAddWarship"; + buttonAddWarship.Size = new Size(230, 45); + buttonAddWarship.TabIndex = 0; + buttonAddWarship.Text = "Add warship"; + buttonAddWarship.UseVisualStyleBackColor = true; + buttonAddWarship.Click += ButtonAddWarship_Click; + // + // pictureBoxCollection + // + pictureBoxCollection.Location = new Point(0, 2); + pictureBoxCollection.Margin = new Padding(4, 5, 4, 5); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(893, 745); + pictureBoxCollection.TabIndex = 1; + pictureBoxCollection.TabStop = false; + // + // FormWarshipCollection + // + AutoScaleDimensions = new SizeF(10F, 25F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1143, 750); + Controls.Add(pictureBoxCollection); + Controls.Add(groupBoxCollection); + Margin = new Padding(4, 5, 4, 5); + Name = "FormWarshipCollection"; + Text = "Warship Collection"; + groupBoxCollection.ResumeLayout(false); + groupBoxCollection.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBoxCollection; + private Button buttonRefreshCollection; + private Button buttonRemoveWarship; + private Button buttonAddWarship; + private PictureBox pictureBoxCollection; + private MaskedTextBox maskedTextBoxNumber; + } +} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormWarshipCollection.cs b/AircraftCarrier/AircraftCarrier/FormWarshipCollection.cs new file mode 100644 index 0000000..c3922d4 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/FormWarshipCollection.cs @@ -0,0 +1,80 @@ +using ProjectAircraftCarrier.DrawingObjects; +using ProjectAircraftCarrier.Generics; +using ProjectAircraftCarrier.MovementStrategy; +namespace ProjectAircraftCarrier +{ + /// + /// Форма для работы с набором объектов класса DrawningCar + /// + public partial class FormWarshipCollection : Form + { + /// + /// Набор объектов + /// + private readonly WarshipsGenericCollection _warships; + /// + /// Конструктор + /// + public FormWarshipCollection() + { + InitializeComponent(); + _warships = new WarshipsGenericCollection(pictureBoxCollection.Width, + pictureBoxCollection.Height); + } + /// + /// Добавление объекта в набор + /// + /// + /// + private void ButtonAddWarship_Click(object sender, EventArgs e) + { + FormAircraftCarrier form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_warships + form.SelectedWarship != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _warships.ShowWarships(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта из набора + /// + /// + /// + private void ButtonRemoveWarship_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_warships - pos) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _warships.ShowWarships(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Обновление рисунка по набору + /// + /// + /// + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _warships.ShowWarships(); + } + } +} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormWarshipCollection.resx b/AircraftCarrier/AircraftCarrier/FormWarshipCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/FormWarshipCollection.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/AircraftCarrier/AircraftCarrier/Program.cs b/AircraftCarrier/AircraftCarrier/Program.cs index 94348f3..2a20b85 100644 --- a/AircraftCarrier/AircraftCarrier/Program.cs +++ b/AircraftCarrier/AircraftCarrier/Program.cs @@ -11,7 +11,7 @@ namespace ProjectAircraftCarrier // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormAircraftCarrier()); + Application.Run(new FormWarshipCollection()); } } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/SetGeneric.cs b/AircraftCarrier/AircraftCarrier/SetGeneric.cs new file mode 100644 index 0000000..ff8423d --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/SetGeneric.cs @@ -0,0 +1,88 @@ +namespace ProjectAircraftCarrier.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 warship) + { + if (_places[Count - 1] != null) + return -1; + return Insert(warship, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый автомобиль + /// Позиция + /// + public int Insert(T warship, 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] = warship; + 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]; + } + } +} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/WarshipsGenericCollection.cs b/AircraftCarrier/AircraftCarrier/WarshipsGenericCollection.cs new file mode 100644 index 0000000..775822b --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/WarshipsGenericCollection.cs @@ -0,0 +1,136 @@ +using ProjectAircraftCarrier.DrawingObjects; +using ProjectAircraftCarrier.MovementStrategy; + +namespace ProjectAircraftCarrier.Generics +{ + /// + /// Параметризованный класс для набора объектов DrawningCar + /// + /// + /// + internal class WarshipsGenericCollection + where T : DrawingWarship + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 196; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 58; + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + /// + /// Конструктор + /// + /// + /// + public WarshipsGenericCollection(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 +(WarshipsGenericCollection collect, T? obj) + { + if (obj == null) + { + return -1; + } + return collect._collection.Insert(obj); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(WarshipsGenericCollection collect, int pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + return collect._collection.Remove(pos); + } + return false; + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowWarships() + { + 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) + { + int inRow = _pictureWidth / _placeSizeWidth; + for (int i = 0; i < _collection.Count; i++) + { + DrawingWarship warship = _collection.Get(i); + if (warship != null) + { + warship.SetPosition(i % inRow * _placeSizeWidth + 3, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight + 5); + warship.DrawTransport(g); + } + } + } + } +}