PIbd-21. Afanasev S.S. Lab work 04 #5

Closed
Stepan wants to merge 1 commits from Lab4 into Lab3
5 changed files with 366 additions and 90 deletions

View File

@ -70,7 +70,7 @@ namespace Bulldozer.Generics
public static bool operator -(BulldozerGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
return collect._collection.Remove(pos);
@ -84,7 +84,7 @@ namespace Bulldozer.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
@ -126,32 +126,31 @@ namespace Bulldozer.Generics
{
int c = 11;
int k = 3;
for (int i = 0; i < _collection.Count; i++)
foreach (var tractor in _collection.GetBulldozer())
{
if (i % 3 == 0 && i != 0)
if (tractor != null)
{
c = c - 3;
}
int i = _collection.GetBulldozer().ToList().IndexOf(tractor);
if (i % 3 == 0 && i != 0)
{
c = c - 3;
}
if (k != 0)
{
k--;
}
else
{
k = 2;
}
if (k != 0)
{
k--;
}
else
{
k = 2;
}
T? obj = _collection.Get(i);
if (obj != null)
{
// Получение объекта
IMoveableObject moveableObject = obj.GetMoveableObject;
// Установка позиции
int x = k % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth;
int y = c / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight + 10;
moveableObject.SetPosition(x, y);
tractor.SetPosition(x, y);
// Прорисовка объекта
moveableObject.Draw(g);
tractor.DrawTrasport(g);
}
}
}

View File

@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bulldozer.DrawningObjects;
using Bulldozer.Generics;
using Bulldozer.MovementStrategy;
using Bulldozer.Drawnings;
namespace Bulldozer.Generics
{
internal class BulldozersGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, BulldozerGenericCollection<DrawningBulldozer,
DrawningObjectBulldozer>> _tractorStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _tractorStorages.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 BulldozersGenericStorage(int pictureWidth, int pictureHeight)
{
_tractorStorages = new Dictionary<string,
BulldozerGenericCollection<DrawningBulldozer, DrawningObjectBulldozer>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
// TODO Прописать логику для добавления
if (!_tractorStorages.ContainsKey(name))
{
var tractorCollection = new BulldozerGenericCollection<DrawningBulldozer, DrawningObjectBulldozer>(_pictureWidth, _pictureHeight);
_tractorStorages.Add(name, tractorCollection);
}
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
// TODO Прописать логику для удаления
if (_tractorStorages.ContainsKey(name))
{
_tractorStorages.Remove(name);
}
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public BulldozerGenericCollection<DrawningBulldozer, DrawningObjectBulldozer>? this[string ind]
{
get
{
// TODO Продумать логику получения набора
if (_tractorStorages.ContainsKey(ind))
{
return _tractorStorages[ind];
}
return null;
}
}
}
}

View File

@ -34,14 +34,21 @@
pictureBoxCollection = new PictureBox();
maskedTextBoxNumber = new MaskedTextBox();
groupBox1 = new GroupBox();
groupBox2 = new GroupBox();
listBoxStorage = new ListBox();
ButtonDelObject = new Button();
ButtonAddObject = new Button();
textBoxStorageName = new TextBox();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
groupBox1.SuspendLayout();
groupBox2.SuspendLayout();
SuspendLayout();
//
// ButtonAddBulldozer
//
ButtonAddBulldozer.Location = new Point(668, 45);
ButtonAddBulldozer.Location = new Point(26, 314);
ButtonAddBulldozer.Name = "ButtonAddBulldozer";
ButtonAddBulldozer.Size = new Size(166, 31);
ButtonAddBulldozer.Size = new Size(166, 40);
ButtonAddBulldozer.TabIndex = 0;
ButtonAddBulldozer.Text = "Добавить трактор";
ButtonAddBulldozer.UseVisualStyleBackColor = true;
@ -49,9 +56,9 @@
//
// ButtonRemoveBulldozer
//
ButtonRemoveBulldozer.Location = new Point(668, 129);
ButtonRemoveBulldozer.Location = new Point(26, 400);
ButtonRemoveBulldozer.Name = "ButtonRemoveBulldozer";
ButtonRemoveBulldozer.Size = new Size(166, 31);
ButtonRemoveBulldozer.Size = new Size(166, 39);
ButtonRemoveBulldozer.TabIndex = 1;
ButtonRemoveBulldozer.Text = "Удалить трактор";
ButtonRemoveBulldozer.UseVisualStyleBackColor = true;
@ -59,9 +66,9 @@
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(668, 215);
ButtonRefreshCollection.Location = new Point(26, 478);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(166, 31);
ButtonRefreshCollection.Size = new Size(166, 40);
ButtonRefreshCollection.TabIndex = 2;
ButtonRefreshCollection.Text = "Обновить коллекцию";
ButtonRefreshCollection.UseVisualStyleBackColor = true;
@ -71,7 +78,7 @@
//
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(624, 457);
pictureBoxCollection.Size = new Size(637, 530);
pictureBoxCollection.SizeMode = PictureBoxSizeMode.Zoom;
pictureBoxCollection.TabIndex = 3;
pictureBoxCollection.TabStop = false;
@ -79,7 +86,7 @@
// maskedTextBoxNumber
//
maskedTextBoxNumber.Font = new Font("Showcard Gothic", 9F, FontStyle.Regular, GraphicsUnit.Point);
maskedTextBoxNumber.Location = new Point(698, 100);
maskedTextBoxNumber.Location = new Point(56, 371);
maskedTextBoxNumber.Mask = "00";
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(100, 22);
@ -88,29 +95,83 @@
//
// groupBox1
//
groupBox1.Location = new Point(646, 12);
groupBox1.Controls.Add(groupBox2);
groupBox1.Controls.Add(ButtonAddBulldozer);
groupBox1.Controls.Add(ButtonRefreshCollection);
groupBox1.Controls.Add(maskedTextBoxNumber);
groupBox1.Controls.Add(ButtonRemoveBulldozer);
groupBox1.Location = new Point(643, 12);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(209, 412);
groupBox1.Size = new Size(209, 533);
groupBox1.TabIndex = 5;
groupBox1.TabStop = false;
groupBox1.Text = "Инструменты";
//
// groupBox2
//
groupBox2.Controls.Add(listBoxStorage);
groupBox2.Controls.Add(ButtonDelObject);
groupBox2.Controls.Add(ButtonAddObject);
groupBox2.Controls.Add(textBoxStorageName);
groupBox2.Location = new Point(6, 22);
groupBox2.Name = "groupBox2";
groupBox2.Size = new Size(197, 274);
groupBox2.TabIndex = 6;
groupBox2.TabStop = false;
groupBox2.Text = "Наборы";
//
// listBoxStorage
//
listBoxStorage.FormattingEnabled = true;
listBoxStorage.ItemHeight = 15;
listBoxStorage.Location = new Point(20, 106);
listBoxStorage.Name = "listBoxStorage";
listBoxStorage.Size = new Size(158, 109);
listBoxStorage.TabIndex = 9;
listBoxStorage.SelectedIndexChanged += listBoxStorage_SelectedIndexChanged;
//
// ButtonDelObject
//
ButtonDelObject.Location = new Point(33, 235);
ButtonDelObject.Name = "ButtonDelObject";
ButtonDelObject.Size = new Size(136, 33);
ButtonDelObject.TabIndex = 8;
ButtonDelObject.Text = "Удалить набор";
ButtonDelObject.UseVisualStyleBackColor = true;
ButtonDelObject.Click += ButtonDelObject_Click;
//
// ButtonAddObject
//
ButtonAddObject.Location = new Point(33, 54);
ButtonAddObject.Name = "ButtonAddObject";
ButtonAddObject.Size = new Size(136, 33);
ButtonAddObject.TabIndex = 7;
ButtonAddObject.Text = "Добавить набор";
ButtonAddObject.UseVisualStyleBackColor = true;
ButtonAddObject.Click += ButtonAddObject_Click;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(20, 22);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(158, 23);
textBoxStorageName.TabIndex = 0;
//
// FormBulldozerCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(877, 469);
Controls.Add(ButtonRefreshCollection);
Controls.Add(ButtonRemoveBulldozer);
Controls.Add(maskedTextBoxNumber);
Controls.Add(ButtonAddBulldozer);
ClientSize = new Size(867, 584);
Controls.Add(groupBox1);
Controls.Add(pictureBoxCollection);
Name = "FormBulldozerCollection";
Text = "Набор трактористов";
Text = "Набор тракторов";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
groupBox2.ResumeLayout(false);
groupBox2.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
#endregion
@ -121,5 +182,10 @@
private PictureBox pictureBoxCollection;
private MaskedTextBox maskedTextBoxNumber;
private GroupBox groupBox1;
private GroupBox groupBox2;
private TextBox textBoxStorageName;
private ListBox listBoxStorage;
private Button ButtonDelObject;
private Button ButtonAddObject;
}
}

View File

@ -1,37 +1,121 @@
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 Bulldozer.DrawningObjects;
using Bulldozer.Drawnings;
using Bulldozer.Generics;
using Bulldozer.MovementStrategy;
namespace Bulldozer
{
/// <summary>
/// Форма для работы с набором объектов класса DrawningCar
/// </summary>
public partial class FormBulldozerCollection : Form
{
private readonly BulldozerGenericCollection<DrawningBulldozer, DrawningObjectBulldozer> _tractor;
/// <summary>
/// Набор объектов
/// </summary>
private readonly BulldozersGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public FormBulldozerCollection()
{
InitializeComponent();
_tractor = new BulldozerGenericCollection<DrawningBulldozer, DrawningObjectBulldozer>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new BulldozersGenericStorage(pictureBoxCollection.Width,
pictureBoxCollection.Height);
}
/// <summary>
/// Заполнение listBoxObjects
/// </summary>
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;
}
}
/// <summary>
/// Добавление набора в коллекцию
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
/// <summary>
/// Выбор набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBoxStorage_SelectedIndexChanged(object sender,
EventArgs e)
{
pictureBoxCollection.Image =
_storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty]?.ShowBulldozer();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonDelObject_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();
}
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddBulldozer_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorage.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
FastBulldozer form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_tractor + form.SelectedTractor > -1)
if (obj + form.SelectedTractor > -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _tractor.ShowBulldozer();
pictureBoxCollection.Image = obj.ShowBulldozer();
}
else
{
@ -39,27 +123,59 @@ namespace Bulldozer
}
}
}
/// <summary>
/// Удаление объекта из набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemoveBulldozer_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 (_tractor - pos != null)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _tractor.ShowBulldozer();
pictureBoxCollection.Image = obj.ShowBulldozer();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
/// <summary>
/// Обновление рисунка по набору
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs
e)
{
pictureBoxCollection.Image = _tractor.ShowBulldozer();
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorage.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowBulldozer();
}
}
}

View File

@ -10,18 +10,23 @@
/// <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>
/// Добавление объекта в набор
@ -41,32 +46,14 @@
public int Insert(T tractor, 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; 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] = tractor;
if (Count >= _maxCount)
return -1;
_places.Insert(position, tractor);
return position;
}
@ -79,13 +66,13 @@
{
// TODO проверка позиции
// Проверка позиции
if (position < 0 || position >= _places.Length)
if ((position < 0) || (position > _maxCount))
{
// Позиция недопустима
return false;
}
// TODO удаление объекта из массива, присвоив элементу массива значение null
_places[position] = null;
_places.RemoveAt(position);
return true;
}
/// <summary>
@ -93,15 +80,36 @@
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
public T? this[int position]
{
// TODO проверка позиции
if (position < 0 || position >= _places.Length)
get
{
// Позиция недопустима
return null;
if (position < 0 || position > _maxCount)
return null;
return _places[position];
}
set
{
if (position < 0 || position > _maxCount)
return;
_places[position] = value;
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetBulldozer(int? maxTractor = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxTractor.HasValue && i == maxTractor.Value)
{
yield break;
}
}
return _places[position];
}
}
}