ПИбд-21 Ярускин Салих 4 лаб простая #4

Closed
Salikh wants to merge 3 commits from laba4 into laba3
6 changed files with 276 additions and 61 deletions

View File

@ -41,7 +41,7 @@ namespace AirBomber.Generics
public static bool operator -(BomberGenericCollection<T, U> collect, int
pos)
{
T obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj == null)
{
return false;
@ -51,7 +51,7 @@ namespace AirBomber.Generics
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
public Bitmap ShowBomber()
@ -84,16 +84,13 @@ namespace AirBomber.Generics
private void DrawObjects(Graphics g)
{
int numColumns = _pictureWidth / _placeSizeWidth;
int numRows = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count; i++)
foreach (var air in _collection.GetPlane())
{
DrawningBomber air = _collection.Get(i);
if (air != null)
{
int row = i / numColumns;
int column = numColumns - 1 - (i % numColumns);
// Вычисляем позицию объекта
int row = _collection.GetPlane().ToList().IndexOf(air) / (_pictureWidth / _placeSizeWidth);
int column = (_pictureWidth / _placeSizeWidth) - 1 - (_collection.GetPlane().ToList().IndexOf(air) % (_pictureWidth / _placeSizeWidth));
int x = column * _placeSizeWidth;
int y = row * _placeSizeHeight;

View File

@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirBomber.DrawningObjects;
using AirBomber.MovementStrategy;
namespace AirBomber.Generics
{
internal class BomberGenericStorage
{
readonly Dictionary<string, BomberGenericCollection<DrawningBomber, DrawningObjectBomber>> _bomberStorage;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _bomberStorage.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public BomberGenericStorage(int pictureWidth, int pictureHeight)
{
_bomberStorage = new Dictionary<string, BomberGenericCollection<DrawningBomber, DrawningObjectBomber>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
// TODO Прописать логику для добавления
if (!_bomberStorage.ContainsKey(name))
{
var bomberCollection = new BomberGenericCollection<DrawningBomber, DrawningObjectBomber>(_pictureWidth, _pictureHeight);
_bomberStorage.Add(name, bomberCollection);
}
}
public void DelSet(string name)
{
// TODO Прописать логику для удаления
if (_bomberStorage.ContainsKey(name))
{
_bomberStorage.Remove(name);
}
}
public BomberGenericCollection<DrawningBomber, DrawningObjectBomber>? this[string ind]
{
get
{
// TODO Продумать логику получения набора
if (_bomberStorage.ContainsKey(ind))
{
return _bomberStorage[ind];
}
return null;
}
}
}
}

View File

@ -124,6 +124,7 @@ namespace AirBomber.DrawningObjects
return;
}
Pen pen = new(Color.Black);
Brush bodycolor = new SolidBrush(EntityBomber.BodyColor);
//отрисовка крыла 1
GraphicsPath fly_1 = new GraphicsPath();
fly_1.AddLine(_startPosX + 80, _startPosY + 2, _startPosX + 80, _startPosY + 80);
@ -139,7 +140,7 @@ namespace AirBomber.DrawningObjects
g.FillPath(Brushes.Black, treygol);
g.DrawPath(Pens.Black, treygol);
//отрисовка корпуса
g.DrawRectangle(pen, _startPosX + 30, _startPosY + 80, 120, 25);
g.FillRectangle(bodycolor, _startPosX + 30, _startPosY + 80, 120, 25);
//отрисовка крыла 2
GraphicsPath fly_2 = new GraphicsPath();
fly_2.AddLine(_startPosX + 80, _startPosY + 105, _startPosX + 80, _startPosY + 185);

View File

@ -29,31 +29,88 @@
private void InitializeComponent()
{
Tools = new GroupBox();
Kit = new GroupBox();
RemoveKit = new Button();
AddKit = new Button();
KitTextbox = new TextBox();
listBoxStorages = new ListBox();
ButtonRefreshCollection = new Button();
ButtonRemoveBomber = new Button();
ButtonAddBomber = new Button();
MessageBoxBomber = new TextBox();
PicBoxBomberCollection = new PictureBox();
Tools.SuspendLayout();
Kit.SuspendLayout();
((System.ComponentModel.ISupportInitialize)PicBoxBomberCollection).BeginInit();
SuspendLayout();
//
// Tools
//
Tools.Controls.Add(Kit);
Tools.Controls.Add(ButtonRefreshCollection);
Tools.Controls.Add(ButtonRemoveBomber);
Tools.Controls.Add(ButtonAddBomber);
Tools.Controls.Add(MessageBoxBomber);
Tools.Location = new Point(538, 5);
Tools.Name = "Tools";
Tools.Size = new Size(250, 443);
Tools.Size = new Size(250, 556);
Tools.TabIndex = 0;
Tools.TabStop = false;
Tools.Text = "Инструменты";
//
// Kit
//
Kit.Controls.Add(RemoveKit);
Kit.Controls.Add(AddKit);
Kit.Controls.Add(KitTextbox);
Kit.Controls.Add(listBoxStorages);
Kit.Location = new Point(17, 33);
Kit.Name = "Kit";
Kit.Size = new Size(227, 282);
Kit.TabIndex = 4;
Kit.TabStop = false;
Kit.Text = "Наборы";
//
// RemoveKit
//
RemoveKit.Location = new Point(11, 231);
RemoveKit.Name = "RemoveKit";
RemoveKit.Size = new Size(193, 36);
RemoveKit.TabIndex = 3;
RemoveKit.Text = "Удалить набор";
RemoveKit.UseVisualStyleBackColor = true;
RemoveKit.Click += RemoveKit_Click;
//
// AddKit
//
AddKit.Location = new Point(11, 63);
AddKit.Name = "AddKit";
AddKit.Size = new Size(193, 36);
AddKit.TabIndex = 2;
AddKit.Text = "Добавить набор";
AddKit.UseVisualStyleBackColor = true;
AddKit.Click += AddKit_Click;
//
// KitTextbox
//
KitTextbox.Location = new Point(15, 30);
KitTextbox.Name = "KitTextbox";
KitTextbox.Size = new Size(189, 27);
KitTextbox.TabIndex = 1;
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 20;
listBoxStorages.Location = new Point(11, 109);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(193, 104);
listBoxStorages.TabIndex = 0;
listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(25, 243);
ButtonRefreshCollection.Location = new Point(28, 481);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(193, 37);
ButtonRefreshCollection.TabIndex = 3;
@ -63,7 +120,7 @@
//
// ButtonRemoveBomber
//
ButtonRemoveBomber.Location = new Point(25, 178);
ButtonRemoveBomber.Location = new Point(28, 416);
ButtonRemoveBomber.Name = "ButtonRemoveBomber";
ButtonRemoveBomber.Size = new Size(193, 41);
ButtonRemoveBomber.TabIndex = 2;
@ -73,7 +130,7 @@
//
// ButtonAddBomber
//
ButtonAddBomber.Location = new Point(25, 42);
ButtonAddBomber.Location = new Point(28, 321);
ButtonAddBomber.Name = "ButtonAddBomber";
ButtonAddBomber.Size = new Size(193, 41);
ButtonAddBomber.TabIndex = 1;
@ -83,7 +140,7 @@
//
// MessageBoxBomber
//
MessageBoxBomber.Location = new Point(25, 109);
MessageBoxBomber.Location = new Point(28, 368);
MessageBoxBomber.Name = "MessageBoxBomber";
MessageBoxBomber.Size = new Size(193, 27);
MessageBoxBomber.TabIndex = 0;
@ -107,6 +164,8 @@
Text = "FormBomberCollection";
Tools.ResumeLayout(false);
Tools.PerformLayout();
Kit.ResumeLayout(false);
Kit.PerformLayout();
((System.ComponentModel.ISupportInitialize)PicBoxBomberCollection).EndInit();
ResumeLayout(false);
}
@ -119,5 +178,10 @@
private Button ButtonRefreshCollection;
private Button ButtonRemoveBomber;
private Button ButtonAddBomber;
private GroupBox Kit;
private ListBox listBoxStorages;
private Button RemoveKit;
private Button AddKit;
private TextBox KitTextbox;
}
}

View File

@ -15,28 +15,67 @@ namespace AirBomber
{
public partial class FormBomberCollection : Form
{
private readonly BomberGenericCollection<DrawningBomber, DrawningObjectBomber> _bomber;
private readonly BomberGenericStorage _bomber;
public FormBomberCollection()
{
InitializeComponent();
_bomber = new BomberGenericCollection<DrawningBomber, DrawningObjectBomber>(PicBoxBomberCollection.Width, PicBoxBomberCollection.Height);
_bomber = new BomberGenericStorage(PicBoxBomberCollection.Width, PicBoxBomberCollection.Height);
}
private void ReloadObjects()
{
int index = listBoxStorages.SelectedIndex;
listBoxStorages.Items.Clear();
for (int i = 0; i < _bomber.Keys.Count; i++)
{
listBoxStorages.Items.Add(_bomber.Keys[i]);
}
if (listBoxStorages.Items.Count > 0 && (index == -1 || index
>= listBoxStorages.Items.Count))
{
listBoxStorages.SelectedIndex = 0;
}
else if (listBoxStorages.Items.Count > 0 && index > -1 &&
index < listBoxStorages.Items.Count)
{
listBoxStorages.SelectedIndex = index;
}
}
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
PicBoxBomberCollection.Image = _bomber.ShowBomber();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _bomber[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
PicBoxBomberCollection.Image = obj.ShowBomber();
}
private void ButtonAddBomber_Click(object sender, EventArgs e)
{
FormAirBomber form = new FormAirBomber();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _bomber[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormAirBomber form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_bomber + form.SelectedBomber != 1)
if (obj + form.SelectedBomber != 1)
{
MessageBox.Show("Объект добавлен");
PicBoxBomberCollection.Image = _bomber.ShowBomber();
PicBoxBomberCollection.Image = obj.ShowBomber();
}
else
{
@ -47,20 +86,62 @@ namespace AirBomber
private void ButtonRemoveBomber_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _bomber[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(MessageBoxBomber.Text);
if (_bomber - pos != null)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
PicBoxBomberCollection.Image = _bomber.ShowBomber();
PicBoxBomberCollection.Image = obj.ShowBomber();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void AddKit_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(KitTextbox.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_bomber.AddSet(KitTextbox.Text);
ReloadObjects();
}
private void RemoveKit_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_bomber.DelSet(listBoxStorages.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
}
}
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
{
PicBoxBomberCollection.Image =
_bomber[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBomber();
}
}
}

View File

@ -17,18 +17,23 @@ namespace ProjectBomber.Generics
/// <summary>
/// Массив объектов, которые храним
/// </summary>
private readonly T[] _places;
private readonly List<T?> _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
public int Count => _places.Count;
/// <summary>
/// Максимальное количество объектов в списке
/// </summary>
private readonly int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T?>(count);
}
/// <summary>
/// Добавление объекта в набор
@ -47,32 +52,13 @@ namespace ProjectBomber.Generics
/// <returns></returns>
public int Insert(T plane, int position)
{
// TODO проверка позиции
if (position < 0 && position > Count)
{
if (position < 0 || position >= _maxCount)
return -1;
}
if (_places[position] != null)
{
int d = 0;
for (int j = 1; j < Count - position; j++)
{
if (_places[position + j] == null)
{
d = position + j;
break;
}
}
if (d == 0)
{
return -1;
}
for (int j = d; j > position; j--)
{
_places[j] = _places[j - 1];
}
}
_places[position] = plane;
if (Count >= _maxCount)
return -1;
_places.Insert(position, plane);
return position;
}
/// <summary>
@ -83,10 +69,8 @@ namespace ProjectBomber.Generics
public bool Remove(int position)
{
/// Проверка позиции
if (position < 0 || position >= _places.Length)
return false;
/// Удаление объекта из массива, присвоив элементу массива значение null
_places[position] = null;
if ((position < 0) || (position > _maxCount)) return false;
_places.RemoveAt(position);
return true;
}
/// <summary>
@ -94,12 +78,31 @@ namespace ProjectBomber.Generics
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position)
public T? this[int position]
{
// Проверка позиции
if (position < 0 || position >= _places.Length)
return null;
return _places[position];
get
{
if (position < 0 || position > _maxCount)
return null;
return _places[position];
}
set
{
if (position < 0 || position > _maxCount)
return;
_places[position] = value;
}
}
public IEnumerable<T?> GetPlane(int? maxPlane = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxPlane.HasValue && i == maxPlane.Value)
{
yield break;
}
}
}
}
}