PIbd-21. Shanygin A.V. Lab work 04 #4

Closed
Extrimal wants to merge 4 commits from laba4 into laba3
7 changed files with 308 additions and 93 deletions

View File

@ -31,7 +31,7 @@ namespace AircraftCarrier.Generics
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
/// Перегрузка оператора сложения
/// Перегрузка оператора сложения
public static int operator +(AircraftsGenericCollection<T, U> collect, T?
obj)
{
@ -44,7 +44,7 @@ namespace AircraftCarrier.Generics
/// Перегрузка оператора вычитания
public static bool operator -(AircraftsGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
return collect._collection.Remove(pos);
@ -54,10 +54,10 @@ namespace AircraftCarrier.Generics
/// Получение объекта IMoveableObject
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// Вывод всего набора объектов
public Bitmap ShowAircraft()
public Bitmap ShowAircrafts()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
@ -86,16 +86,17 @@ namespace AircraftCarrier.Generics
/// Метод прорисовки объектов
private void DrawObjects(Graphics g)
{
int inRow = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _collection.Count; i++)
int Width = _pictureWidth / _placeSizeWidth;
int Height = _pictureHeight / _placeSizeHeight;
int i = 0;
foreach (var aircraft in _collection.GetAircrafts())
{
DrawningAircraft aircraft = _collection.Get(i);
if (aircraft != null)
{
aircraft.SetPosition(i % inRow * _placeSizeWidth+3, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight + 5);
aircraft.SetPosition(i % Width * _placeSizeWidth + 3, (Height - i / Width - 1) * _placeSizeHeight +5);
aircraft.DrawTransport(g);
}
i++;
}
}
}

View File

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AircraftCarrier.DrawningObjects;
using AircraftCarrier.MovementStrategy;
namespace AircraftCarrier.Generics
{
/// Класс для хранения коллекции
internal class AircraftsGenericStorage
{
/// Словарь (хранилище)
readonly Dictionary<string, AircraftsGenericCollection<DrawningAircraft,
DrawningObjectAircraft>> _AircraftStorages;
/// Возвращение списка названий наборов
public List<string> Keys => _AircraftStorages.Keys.ToList();
/// Ширина окна отрисовки
private readonly int _pictureWidth;
/// Высота окна отрисовки
private readonly int _pictureHeight;
/// Конструктор
public AircraftsGenericStorage(int pictureWidth, int pictureHeight)
{
_AircraftStorages = new Dictionary<string,
AircraftsGenericCollection<DrawningAircraft, DrawningObjectAircraft>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// Добавление набора
public void AddSet(string name)
{
if (!_AircraftStorages.ContainsKey(name))
{
_AircraftStorages.Add(name, new AircraftsGenericCollection<DrawningAircraft, DrawningObjectAircraft>(_pictureWidth, _pictureHeight));
}
}
/// Удаление набора
public void DelSet(string name)
{
if (_AircraftStorages.ContainsKey(name))
{
_AircraftStorages.Remove(name);
}
}
/// Доступ к набору
public AircraftsGenericCollection<DrawningAircraft, DrawningObjectAircraft>?
this[string ind]
{
get
{
if (_AircraftStorages.ContainsKey(ind))
{
return _AircraftStorages[ind];
}
return null;
}
}
}
}

View File

@ -46,7 +46,7 @@
pictureBox.Dock = DockStyle.Fill;
pictureBox.Location = new Point(0, 0);
pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(882, 453);
pictureBox.Size = new Size(1033, 724);
pictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox.TabIndex = 0;
pictureBox.TabStop = false;
@ -57,7 +57,7 @@
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = Properties.Resources.Icon_Up;
buttonUp.BackgroundImageLayout = ImageLayout.Zoom;
buttonUp.Location = new Point(791, 366);
buttonUp.Location = new Point(942, 637);
buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(30, 30);
buttonUp.TabIndex = 2;
@ -69,7 +69,7 @@
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = Properties.Resources.Icon_Left;
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom;
buttonLeft.Location = new Point(755, 402);
buttonLeft.Location = new Point(906, 673);
buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(30, 30);
buttonLeft.TabIndex = 3;
@ -81,7 +81,7 @@
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.Icon_Down;
buttonDown.BackgroundImageLayout = ImageLayout.Zoom;
buttonDown.Location = new Point(791, 402);
buttonDown.Location = new Point(942, 673);
buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(30, 30);
buttonDown.TabIndex = 4;
@ -93,7 +93,7 @@
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.Icon_Right;
buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
buttonRight.Location = new Point(827, 402);
buttonRight.Location = new Point(978, 673);
buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(30, 30);
buttonRight.TabIndex = 5;
@ -103,7 +103,7 @@
// ButtonCreateAircraftCarrier
//
ButtonCreateAircraftCarrier.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
ButtonCreateAircraftCarrier.Location = new Point(234, 393);
ButtonCreateAircraftCarrier.Location = new Point(234, 664);
ButtonCreateAircraftCarrier.Name = "ButtonCreateAircraftCarrier";
ButtonCreateAircraftCarrier.Size = new Size(204, 39);
ButtonCreateAircraftCarrier.TabIndex = 6;
@ -114,7 +114,7 @@
// ButtonCreateAircraft
//
ButtonCreateAircraft.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
ButtonCreateAircraft.Location = new Point(12, 393);
ButtonCreateAircraft.Location = new Point(12, 664);
ButtonCreateAircraft.Name = "ButtonCreateAircraft";
ButtonCreateAircraft.Size = new Size(204, 39);
ButtonCreateAircraft.TabIndex = 7;
@ -128,7 +128,7 @@
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToBorder" });
comboBoxStrategy.Location = new Point(706, 12);
comboBoxStrategy.Location = new Point(857, 12);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(151, 28);
comboBoxStrategy.TabIndex = 8;
@ -136,7 +136,7 @@
// buttonStep
//
buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonStep.Location = new Point(763, 46);
buttonStep.Location = new Point(914, 46);
buttonStep.Name = "buttonStep";
buttonStep.Size = new Size(94, 29);
buttonStep.TabIndex = 9;
@ -146,7 +146,8 @@
//
// buttonSelectAircraft
//
buttonSelectAircraft.Location = new Point(457, 393);
buttonSelectAircraft.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonSelectAircraft.Location = new Point(456, 664);
buttonSelectAircraft.Name = "buttonSelectAircraft";
buttonSelectAircraft.Size = new Size(204, 39);
buttonSelectAircraft.TabIndex = 10;
@ -158,7 +159,7 @@
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(882, 453);
ClientSize = new Size(1033, 724);
Controls.Add(buttonSelectAircraft);
Controls.Add(buttonStep);
Controls.Add(comboBoxStrategy);

View File

@ -50,7 +50,6 @@ namespace AircraftCarrier
}
Draw();
}
private void ButtonCreateAircraft_Click(object sender, EventArgs e)
{
Random random = new();
@ -67,7 +66,6 @@ namespace AircraftCarrier
100));
Draw();
}
private void ButtonCreateAircraftCarrier_Click(object sender, EventArgs e)
{
Random random = new();
@ -90,7 +88,6 @@ namespace AircraftCarrier
_drawingAircraft.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonStep_Click(object sender, EventArgs e)
{
if (_drawingAircraft == null)

View File

@ -28,40 +28,116 @@
/// </summary>
private void InitializeComponent()
{
PictureBoxCollection = new PictureBox();
pictureBoxCollection = new PictureBox();
PanelTools = new Panel();
labelTools = new Label();
PanelSets = new Panel();
ButtonRemoveObject = new Button();
listBoxStorage = new ListBox();
ButtonAddObject = new Button();
textBoxStorageName = new TextBox();
labelSets = new Label();
ButtonRefreshCollection = new Button();
ButtonRemoveAircraft = new Button();
MaskedTextBoxNumber = new MaskedTextBox();
ButtonAddAircraft = new Button();
LabelTools = new Label();
((System.ComponentModel.ISupportInitialize)PictureBoxCollection).BeginInit();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
PanelTools.SuspendLayout();
PanelSets.SuspendLayout();
SuspendLayout();
//
// PictureBoxCollection
// pictureBoxCollection
//
PictureBoxCollection.Location = new Point(0, 0);
PictureBoxCollection.Name = "PictureBoxCollection";
PictureBoxCollection.Size = new Size(599, 450);
PictureBoxCollection.TabIndex = 0;
PictureBoxCollection.TabStop = false;
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(793, 724);
pictureBoxCollection.TabIndex = 0;
pictureBoxCollection.TabStop = false;
//
// PanelTools
//
PanelTools.BorderStyle = BorderStyle.FixedSingle;
PanelTools.Controls.Add(labelTools);
PanelTools.Controls.Add(PanelSets);
PanelTools.Controls.Add(ButtonRefreshCollection);
PanelTools.Controls.Add(ButtonRemoveAircraft);
PanelTools.Controls.Add(MaskedTextBoxNumber);
PanelTools.Controls.Add(ButtonAddAircraft);
PanelTools.Location = new Point(599, 12);
PanelTools.Location = new Point(799, 12);
PanelTools.Name = "PanelTools";
PanelTools.Size = new Size(202, 438);
PanelTools.Size = new Size(232, 712);
PanelTools.TabIndex = 1;
//
// labelTools
//
labelTools.AutoSize = true;
labelTools.Location = new Point(16, -4);
labelTools.Name = "labelTools";
labelTools.Size = new Size(103, 20);
labelTools.TabIndex = 5;
labelTools.Text = "Инструменты";
//
// PanelSets
//
PanelSets.Controls.Add(ButtonRemoveObject);
PanelSets.Controls.Add(listBoxStorage);
PanelSets.Controls.Add(ButtonAddObject);
PanelSets.Controls.Add(textBoxStorageName);
PanelSets.Controls.Add(labelSets);
PanelSets.Location = new Point(16, 37);
PanelSets.Name = "PanelSets";
PanelSets.Size = new Size(204, 339);
PanelSets.TabIndex = 4;
//
// ButtonRemoveObject
//
ButtonRemoveObject.Location = new Point(20, 286);
ButtonRemoveObject.Name = "ButtonRemoveObject";
ButtonRemoveObject.Size = new Size(168, 41);
ButtonRemoveObject.TabIndex = 4;
ButtonRemoveObject.Text = "Удалить набор";
ButtonRemoveObject.UseVisualStyleBackColor = true;
ButtonRemoveObject.Click += ButtonRemoveObject_Click;
//
// listBoxStorage
//
listBoxStorage.FormattingEnabled = true;
listBoxStorage.ItemHeight = 20;
listBoxStorage.Location = new Point(29, 147);
listBoxStorage.Name = "listBoxStorage";
listBoxStorage.Size = new Size(150, 124);
listBoxStorage.TabIndex = 3;
listBoxStorage.SelectedIndexChanged += listBoxStorage_SelectedIndexChanged;
//
// ButtonAddObject
//
ButtonAddObject.Location = new Point(20, 83);
ButtonAddObject.Name = "ButtonAddObject";
ButtonAddObject.Size = new Size(168, 41);
ButtonAddObject.TabIndex = 2;
ButtonAddObject.Text = "Добавить набор";
ButtonAddObject.UseVisualStyleBackColor = true;
ButtonAddObject.Click += ButtonAddObject_Click;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(41, 41);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(125, 27);
textBoxStorageName.TabIndex = 1;
//
// labelSets
//
labelSets.AutoSize = true;
labelSets.Location = new Point(11, 9);
labelSets.Name = "labelSets";
labelSets.Size = new Size(55, 20);
labelSets.TabIndex = 0;
labelSets.Text = "Набор";
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(15, 206);
ButtonRefreshCollection.Location = new Point(36, 572);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(168, 41);
ButtonRefreshCollection.TabIndex = 3;
@ -71,7 +147,7 @@
//
// ButtonRemoveAircraft
//
ButtonRemoveAircraft.Location = new Point(15, 137);
ButtonRemoveAircraft.Location = new Point(36, 497);
ButtonRemoveAircraft.Name = "ButtonRemoveAircraft";
ButtonRemoveAircraft.Size = new Size(168, 41);
ButtonRemoveAircraft.TabIndex = 2;
@ -81,7 +157,7 @@
//
// MaskedTextBoxNumber
//
MaskedTextBoxNumber.Location = new Point(36, 95);
MaskedTextBoxNumber.Location = new Point(57, 438);
MaskedTextBoxNumber.Name = "MaskedTextBoxNumber";
MaskedTextBoxNumber.Size = new Size(125, 27);
MaskedTextBoxNumber.TabIndex = 1;
@ -89,7 +165,7 @@
//
// ButtonAddAircraft
//
ButtonAddAircraft.Location = new Point(15, 19);
ButtonAddAircraft.Location = new Point(36, 391);
ButtonAddAircraft.Name = "ButtonAddAircraft";
ButtonAddAircraft.Size = new Size(168, 41);
ButtonAddAircraft.TabIndex = 0;
@ -97,40 +173,37 @@
ButtonAddAircraft.UseVisualStyleBackColor = true;
ButtonAddAircraft.Click += ButtonAddAircraft_Click;
//
// LabelTools
//
LabelTools.AutoSize = true;
LabelTools.Location = new Point(615, 0);
LabelTools.Name = "LabelTools";
LabelTools.Size = new Size(103, 20);
LabelTools.TabIndex = 0;
LabelTools.Text = "Инструменты";
//
// FormAircraftCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(LabelTools);
ClientSize = new Size(1033, 724);
Controls.Add(PanelTools);
Controls.Add(PictureBoxCollection);
Controls.Add(pictureBoxCollection);
Name = "FormAircraftCollection";
Text = "FormAircraftCollection";
((System.ComponentModel.ISupportInitialize)PictureBoxCollection).EndInit();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
PanelTools.ResumeLayout(false);
PanelTools.PerformLayout();
PanelSets.ResumeLayout(false);
PanelSets.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
#endregion
private PictureBox PictureBoxCollection;
private PictureBox pictureBoxCollection;
private Panel PanelTools;
private MaskedTextBox MaskedTextBoxNumber;
private Button ButtonAddAircraft;
private Label LabelTools;
private Button ButtonRefreshCollection;
private Button ButtonRemoveAircraft;
private Panel PanelSets;
private Label labelTools;
private Label labelSets;
private Button ButtonAddObject;
private TextBox textBoxStorageName;
private Button ButtonRemoveObject;
private ListBox listBoxStorage;
}
}

View File

@ -14,40 +14,105 @@ namespace AircraftCarrier
{
public partial class FormAircraftCollection : Form
{
private readonly AircraftsGenericCollection<DrawningAircraft, DrawningObjectAircraft> _Aircrafts;
private readonly AircraftsGenericStorage _storage;
public FormAircraftCollection()
{
InitializeComponent();
_Aircrafts = new AircraftsGenericCollection<DrawningAircraft, DrawningObjectAircraft>(PictureBoxCollection.Width, PictureBoxCollection.Height);
_storage = new AircraftsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
/// Заполнение listBoxObjects
private void ReloadObjects()
{
int index = listBoxStorage.SelectedIndex;
listBoxStorage.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorage.Items.Add(_storage.Keys[i]);
}
if (listBoxStorage.Items.Count > 0 && (index == -1 || index >= listBoxStorage.Items.Count))
{
listBoxStorage.SelectedIndex = 0;
}
else if (listBoxStorage.Items.Count > 0 && index > -1 && index < listBoxStorage.Items.Count)
{
listBoxStorage.SelectedIndex = index;
}
}
private void ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
}
private void listBoxStorage_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image = _storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty]?.ShowAircrafts();
}
private void ButtonRemoveObject_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект{listBoxStorage.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorage.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
}
}
private void ButtonAddAircraft_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormAircraftCarrier form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_Aircrafts + form.SelectedAircraft != -1)
if (obj + form.SelectedAircraft != -1)
{
MessageBox.Show("Объект добавлен");
PictureBoxCollection.Image = _Aircrafts.ShowAircraft();
pictureBoxCollection.Image = obj.ShowAircrafts();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void ButtonRemoveAircraft_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorage.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(MaskedTextBoxNumber.Text);
if (_Aircrafts - pos != null)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
PictureBoxCollection.Image = _Aircrafts.ShowAircraft();
pictureBoxCollection.Image = obj.ShowAircrafts();
}
else
{
@ -56,7 +121,17 @@ namespace AircraftCarrier
}
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
PictureBoxCollection.Image = _Aircrafts.ShowAircraft();
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorage.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowAircrafts();
}
}
}

View File

@ -8,14 +8,17 @@ namespace AircraftCarrier.Generics
internal class SetGeneric<T>
where T : class
{
/// Массив объектов, которые храним
private readonly T?[] _places;
/// Список объектов, которые храним
private readonly List<T?> _places;
/// Количество объектов в массиве
public int Count => _places.Length;
public int Count => _places.Count;
/// Максимальное количество объектов в списке
private readonly int _maxCount;
/// Конструктор
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(count);
}
/// Добавление объекта в набор
public int Insert(T Aircraft)
@ -25,43 +28,48 @@ namespace AircraftCarrier.Generics
/// Добавление объекта в набор на конкретную позицию
public int Insert(T Aircraft, int position)
{
int nullIndex = -1, i;
if (position < 0 || position >= Count)
if (position < 0 || position > Count || Count >= _maxCount)
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] = Aircraft;
_places.Insert(position, Aircraft);
return position;
}
/// Удаление объекта из набора с конкретной позиции
public bool Remove(int position)
{
if (position < 0 || position >= Count) return false;
_places[position] = null;
if (position < 0 || position >= Count)
return false;
_places.RemoveAt(position);
return true;
}
/// Получение объекта из набора по позиции
public T? Get(int position)
public T? this[int position]
{
if (position < 0 || position >= Count)
return null;
return _places[position];
get
{
if (position < 0 || position >= Count)
return null;
return _places[position];
}
set
{
if (position < 0 || position > Count || Count >= _maxCount)
return;
_places.Insert(position, value);
}
}
/// Проход по списку
public IEnumerable<T?> GetAircrafts(int? maxAircrafts = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxAircrafts.HasValue && i == maxAircrafts.Value)
{
yield break;
}
}
}
}
}