Лабораторная 4

This commit is contained in:
nikbel2004@outlook.com 2023-10-28 21:14:08 +04:00
parent b3d0980f05
commit 1dd901762a
8 changed files with 312 additions and 106 deletions

View File

@ -173,6 +173,7 @@
Margin = new Padding(3, 4, 3, 4);
Name = "FormTank";
Text = "FormTank";
Load += FormTank_Load;
((System.ComponentModel.ISupportInitialize)pictureBoxTank).EndInit();
ResumeLayout(false);
}

View File

@ -128,5 +128,10 @@ namespace Tank
SelectedTank = _Tank;
DialogResult = DialogResult.OK;
}
private void FormTank_Load(object sender, EventArgs e)
{
}
}
}

View File

@ -29,6 +29,12 @@
private void InitializeComponent()
{
panel1 = new Panel();
panel2 = new Panel();
DeleteCollectButton = new Button();
CollectionListBox = new ListBox();
AddCollectButton = new Button();
SetTextBox = new TextBox();
label2 = new Label();
UpdateButton = new Button();
DeleteButton = new Button();
AddButton = new Button();
@ -36,27 +42,94 @@
label1 = new Label();
DrawTank = new PictureBox();
panel1.SuspendLayout();
panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)DrawTank).BeginInit();
SuspendLayout();
//
// panel1
//
panel1.Controls.Add(panel2);
panel1.Controls.Add(UpdateButton);
panel1.Controls.Add(DeleteButton);
panel1.Controls.Add(AddButton);
panel1.Controls.Add(InputNum);
panel1.Controls.Add(label1);
panel1.Dock = DockStyle.Right;
panel1.Location = new Point(550, 0);
panel1.Location = new Point(568, 0);
panel1.Margin = new Padding(3, 2, 3, 2);
panel1.Name = "panel1";
panel1.Size = new Size(250, 451);
panel1.Size = new Size(207, 429);
panel1.TabIndex = 0;
//
// panel2
//
panel2.Controls.Add(DeleteCollectButton);
panel2.Controls.Add(CollectionListBox);
panel2.Controls.Add(AddCollectButton);
panel2.Controls.Add(SetTextBox);
panel2.Controls.Add(label2);
panel2.Location = new Point(14, 19);
panel2.Margin = new Padding(3, 2, 3, 2);
panel2.Name = "panel2";
panel2.Size = new Size(187, 165);
panel2.TabIndex = 6;
//
// DeleteCollectButton
//
DeleteCollectButton.Location = new Point(3, 136);
DeleteCollectButton.Margin = new Padding(3, 2, 3, 2);
DeleteCollectButton.Name = "DeleteCollectButton";
DeleteCollectButton.Size = new Size(182, 22);
DeleteCollectButton.TabIndex = 4;
DeleteCollectButton.Text = "Удалить набор";
DeleteCollectButton.UseVisualStyleBackColor = true;
DeleteCollectButton.Click += ButtonDelObject_Click;
//
// CollectionListBox
//
CollectionListBox.FormattingEnabled = true;
CollectionListBox.ItemHeight = 15;
CollectionListBox.Location = new Point(3, 68);
CollectionListBox.Margin = new Padding(3, 2, 3, 2);
CollectionListBox.Name = "CollectionListBox";
CollectionListBox.Size = new Size(182, 64);
CollectionListBox.TabIndex = 3;
CollectionListBox.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
//
// AddCollectButton
//
AddCollectButton.Location = new Point(3, 42);
AddCollectButton.Margin = new Padding(3, 2, 3, 2);
AddCollectButton.Name = "AddCollectButton";
AddCollectButton.Size = new Size(182, 22);
AddCollectButton.TabIndex = 2;
AddCollectButton.Text = "Добавить набор";
AddCollectButton.UseVisualStyleBackColor = true;
AddCollectButton.Click += ButtonAddObject_Click;
//
// SetTextBox
//
SetTextBox.Location = new Point(2, 17);
SetTextBox.Margin = new Padding(3, 2, 3, 2);
SetTextBox.Name = "SetTextBox";
SetTextBox.Size = new Size(183, 23);
SetTextBox.TabIndex = 1;
//
// label2
//
label2.AutoSize = true;
label2.Location = new Point(3, 0);
label2.Name = "label2";
label2.Size = new Size(52, 15);
label2.TabIndex = 0;
label2.Text = "Наборы";
//
// UpdateButton
//
UpdateButton.Location = new Point(10, 241);
UpdateButton.Location = new Point(9, 283);
UpdateButton.Margin = new Padding(3, 2, 3, 2);
UpdateButton.Name = "UpdateButton";
UpdateButton.Size = new Size(229, 37);
UpdateButton.Size = new Size(192, 28);
UpdateButton.TabIndex = 4;
UpdateButton.Text = "Обновить коллекцию";
UpdateButton.UseVisualStyleBackColor = true;
@ -64,9 +137,10 @@
//
// DeleteButton
//
DeleteButton.Location = new Point(10, 176);
DeleteButton.Location = new Point(9, 253);
DeleteButton.Margin = new Padding(3, 2, 3, 2);
DeleteButton.Name = "DeleteButton";
DeleteButton.Size = new Size(229, 37);
DeleteButton.Size = new Size(192, 28);
DeleteButton.TabIndex = 3;
DeleteButton.Text = "Удалить технику";
DeleteButton.UseVisualStyleBackColor = true;
@ -74,9 +148,10 @@
//
// AddButton
//
AddButton.Location = new Point(10, 53);
AddButton.Location = new Point(9, 194);
AddButton.Margin = new Padding(3, 2, 3, 2);
AddButton.Name = "AddButton";
AddButton.Size = new Size(229, 37);
AddButton.Size = new Size(192, 28);
AddButton.TabIndex = 2;
AddButton.Text = "Добавить технику";
AddButton.UseVisualStyleBackColor = true;
@ -84,17 +159,18 @@
//
// InputNum
//
InputNum.Location = new Point(10, 133);
InputNum.Location = new Point(9, 225);
InputNum.Margin = new Padding(3, 2, 3, 2);
InputNum.Name = "InputNum";
InputNum.Size = new Size(228, 27);
InputNum.Size = new Size(192, 23);
InputNum.TabIndex = 1;
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(2, 3);
label1.Location = new Point(2, 2);
label1.Name = "label1";
label1.Size = new Size(103, 20);
label1.Size = new Size(83, 15);
label1.TabIndex = 0;
label1.Text = "Инструменты";
//
@ -102,22 +178,26 @@
//
DrawTank.Dock = DockStyle.Fill;
DrawTank.Location = new Point(0, 0);
DrawTank.Margin = new Padding(3, 2, 3, 2);
DrawTank.Name = "DrawTank";
DrawTank.Size = new Size(550, 451);
DrawTank.Size = new Size(568, 429);
DrawTank.TabIndex = 1;
DrawTank.TabStop = false;
//
// CollectionsFrame
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 451);
ClientSize = new Size(775, 429);
Controls.Add(DrawTank);
Controls.Add(panel1);
Margin = new Padding(3, 2, 3, 2);
Name = "CollectionsFrame";
Text = "CollectionsFrame";
panel1.ResumeLayout(false);
panel1.PerformLayout();
panel2.ResumeLayout(false);
panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)DrawTank).EndInit();
ResumeLayout(false);
}
@ -131,5 +211,11 @@
private TextBox InputNum;
private Label label1;
private PictureBox DrawTank;
private Panel panel2;
private Button DeleteCollectButton;
private ListBox CollectionListBox;
private Button AddCollectButton;
private TextBox SetTextBox;
private Label label2;
}
}

View File

@ -15,23 +15,86 @@ namespace Tank
{
public partial class FormTanksCollections : Form
{
private readonly TanksGenericCollection<DrawArmoVehicle, DrawingObjectTank> _tanks;
private readonly TanksGenericStorage _storage;
// Конструктор
public FormTanksCollections()
{
InitializeComponent();
_tanks = new TanksGenericCollection<DrawArmoVehicle, DrawingObjectTank>(DrawTank.Width, DrawTank.Height);
_storage = new TanksGenericStorage(DrawTank.Width, DrawTank.Height);
}
private void ReloadObjects()
{
int index = CollectionListBox.SelectedIndex;
CollectionListBox.Items.Clear();
foreach (var key in _storage.Keys)
{
CollectionListBox.Items.Add(key);
}
if (CollectionListBox.Items.Count > 0 && (index == -1 || index
>= CollectionListBox.Items.Count))
{
CollectionListBox.SelectedIndex = 0;
}
else if (CollectionListBox.Items.Count > 0 && index > -1 &&
index < CollectionListBox.Items.Count)
{
CollectionListBox.SelectedIndex = index;
}
}
private void ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(SetTextBox.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(SetTextBox.Text);
ReloadObjects();
}
private void ListBoxObjects_SelectedIndexChanged(object sender,
EventArgs e)
{
DrawTank.Image =
_storage[CollectionListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowTanks();
}
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {CollectionListBox.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(CollectionListBox.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
}
}
private void ButtonAddTank_Click(object sender, EventArgs e)
{
FormTank form = new FormTank();
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormTank form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_tanks + form.SelectedTank != -1)
if (obj + form.SelectedTank)
{
MessageBox.Show("Объект добавлен");
DrawTank.Image = _tanks.ShowTanks();
DrawTank.Image = obj.ShowTanks();
}
else
{
@ -41,21 +104,27 @@ namespace Tank
}
private void ButtonRemoveTank_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[CollectionListBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = -1;
try
{
pos = Convert.ToInt32(InputNum.Text);
}
catch (Exception ex) { }
if (_tanks - pos)
int pos = Convert.ToInt32(InputNum.Text);
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
DrawTank.Image = _tanks.ShowTanks();
DrawTank.Image = obj.ShowTanks();
}
else
{
@ -65,7 +134,18 @@ namespace Tank
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
DrawTank.Image = _tanks.ShowTanks();
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[CollectionListBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
DrawTank.Image = obj.ShowTanks();
}
}
}

View File

@ -18,7 +18,7 @@
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing"">Blue</data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>

View File

@ -1,4 +1,5 @@
using System;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -8,84 +9,69 @@ namespace Tank
{
internal class SetGeneric<T> where T : class
{
// Массив объектов, которые храним
private readonly T?[] _places;
private readonly List<T?> _places;
public int Count => _places.Count;
// Количество объектов в массиве
public int Count => _places.Length;
private readonly int _maxCount;
// Конструктор
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(_maxCount);
}
//Добавление объекта в набор
public int Insert(T tank)
public bool Insert(T tank)
{
int index = -1;
for(int i = 0; i < _places.Length; i++)
{
if (_places[i] == null)
{
index = i; break;
}
}
if (index < 0)
{
return -1;
}
for(int i = index; i > 0; i--)
{
_places[i] = _places[i - 1];
}
_places[0] = tank;
return 0;
return Insert(tank, 0);
}
// Добавление объекта в набор на конкретную позицию
public bool Insert(T tank, int position)
{
if (position < 0 || position >= Count)
if (position < 0 || position >= _maxCount)
return false;
if (_places[position] == null)
{
_places[position] = tank;
return true;
}
int index = -1;
for(int i = position; i < Count; i++)
{
if (_places[i] == null)
{
index = i; break;
}
}
if (index < 0)
if (Count >= _maxCount)
return false;
for(int i = index; index > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = tank;
_places.Insert(0, tank);
return true;
}
// Удаление объекта из набора с конкретной позиции
public bool Remove(int position)
{
if (position < 0 || position >= Count)
if (position < 0 || position > _maxCount)
return false;
_places[position] = null;
if (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 > _maxCount)
return null;
return _places[position];
}
set
{
if (position < 0 || position > _maxCount)
return;
_places[position] = value;
}
}
public IEnumerable<T?> GetTanks(int? maxCars = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxCars.HasValue && i == maxCars.Value)
{
yield break;
}
}
}
}
}

View File

@ -35,30 +35,30 @@ namespace Tank.Generics
}
// Перегрузка оператора сложения
public static int? operator +(TanksGenericCollection<T, U> collect, T? obj)
public static bool operator +(TanksGenericCollection<T, U> collect, T? obj)
{
if (obj == null)
{
return -1;
}
return collect?._collection.Insert(obj);
}
// Перегрузка оператора вычитания
public static bool operator -(TanksGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
if (obj == null)
{
return false;
}
return collect._collection.Remove(pos);
return (bool)collect?._collection.Insert(obj);
}
// Перегрузка оператора вычитания
public static T? operator -(TanksGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
}
return obj;
}
// Получение объекта IMoveableObject
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
// Вывод всего набора объектов
@ -94,13 +94,15 @@ namespace Tank.Generics
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count; i++)
int i = 0;
foreach(var tank in _collection.GetTanks())
{
DrawArmoVehicle? tank = _collection.Get(i);
if (tank == null)
continue;
tank.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
tank.DrawTransport(g);
if (tank != null)
{
tank.SetPosition(i % width * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
tank.DrawTransport(g);
}
i++;
}
}
}

View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tank.Generics;
using Tank.DrawningObjects;
using Tank.MovementStrategy;
namespace Tank
{
internal class TanksGenericStorage
{
readonly Dictionary<string, TanksGenericCollection<DrawArmoVehicle, DrawingObjectTank>> _tankStorages;
public List<string> Keys => _tankStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public TanksGenericStorage(int pictureWidth, int pictureHeight)
{
_tankStorages = new Dictionary<string, TanksGenericCollection<DrawArmoVehicle, DrawingObjectTank>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
if (_tankStorages.ContainsKey(name)) return;
_tankStorages[name] = new TanksGenericCollection<DrawArmoVehicle, DrawingObjectTank>(_pictureWidth, _pictureHeight);
}
public void DelSet(string name)
{
if (!_tankStorages.ContainsKey(name)) return;
_tankStorages.Remove(name);
}
public TanksGenericCollection<DrawArmoVehicle, DrawingObjectTank>?
this[string ind]
{
get
{
if (_tankStorages.ContainsKey(ind)) return _tankStorages[ind];
return null;
}
}
}
}