Лабораторная 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); Margin = new Padding(3, 4, 3, 4);
Name = "FormTank"; Name = "FormTank";
Text = "FormTank"; Text = "FormTank";
Load += FormTank_Load;
((System.ComponentModel.ISupportInitialize)pictureBoxTank).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxTank).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }

View File

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

View File

@ -29,6 +29,12 @@
private void InitializeComponent() private void InitializeComponent()
{ {
panel1 = new Panel(); panel1 = new Panel();
panel2 = new Panel();
DeleteCollectButton = new Button();
CollectionListBox = new ListBox();
AddCollectButton = new Button();
SetTextBox = new TextBox();
label2 = new Label();
UpdateButton = new Button(); UpdateButton = new Button();
DeleteButton = new Button(); DeleteButton = new Button();
AddButton = new Button(); AddButton = new Button();
@ -36,27 +42,94 @@
label1 = new Label(); label1 = new Label();
DrawTank = new PictureBox(); DrawTank = new PictureBox();
panel1.SuspendLayout(); panel1.SuspendLayout();
panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)DrawTank).BeginInit(); ((System.ComponentModel.ISupportInitialize)DrawTank).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// panel1 // panel1
// //
panel1.Controls.Add(panel2);
panel1.Controls.Add(UpdateButton); panel1.Controls.Add(UpdateButton);
panel1.Controls.Add(DeleteButton); panel1.Controls.Add(DeleteButton);
panel1.Controls.Add(AddButton); panel1.Controls.Add(AddButton);
panel1.Controls.Add(InputNum); panel1.Controls.Add(InputNum);
panel1.Controls.Add(label1); panel1.Controls.Add(label1);
panel1.Dock = DockStyle.Right; 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.Name = "panel1";
panel1.Size = new Size(250, 451); panel1.Size = new Size(207, 429);
panel1.TabIndex = 0; 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
// //
UpdateButton.Location = new Point(10, 241); UpdateButton.Location = new Point(9, 283);
UpdateButton.Margin = new Padding(3, 2, 3, 2);
UpdateButton.Name = "UpdateButton"; UpdateButton.Name = "UpdateButton";
UpdateButton.Size = new Size(229, 37); UpdateButton.Size = new Size(192, 28);
UpdateButton.TabIndex = 4; UpdateButton.TabIndex = 4;
UpdateButton.Text = "Обновить коллекцию"; UpdateButton.Text = "Обновить коллекцию";
UpdateButton.UseVisualStyleBackColor = true; UpdateButton.UseVisualStyleBackColor = true;
@ -64,9 +137,10 @@
// //
// DeleteButton // 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.Name = "DeleteButton";
DeleteButton.Size = new Size(229, 37); DeleteButton.Size = new Size(192, 28);
DeleteButton.TabIndex = 3; DeleteButton.TabIndex = 3;
DeleteButton.Text = "Удалить технику"; DeleteButton.Text = "Удалить технику";
DeleteButton.UseVisualStyleBackColor = true; DeleteButton.UseVisualStyleBackColor = true;
@ -74,9 +148,10 @@
// //
// AddButton // 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.Name = "AddButton";
AddButton.Size = new Size(229, 37); AddButton.Size = new Size(192, 28);
AddButton.TabIndex = 2; AddButton.TabIndex = 2;
AddButton.Text = "Добавить технику"; AddButton.Text = "Добавить технику";
AddButton.UseVisualStyleBackColor = true; AddButton.UseVisualStyleBackColor = true;
@ -84,17 +159,18 @@
// //
// InputNum // 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.Name = "InputNum";
InputNum.Size = new Size(228, 27); InputNum.Size = new Size(192, 23);
InputNum.TabIndex = 1; InputNum.TabIndex = 1;
// //
// label1 // label1
// //
label1.AutoSize = true; label1.AutoSize = true;
label1.Location = new Point(2, 3); label1.Location = new Point(2, 2);
label1.Name = "label1"; label1.Name = "label1";
label1.Size = new Size(103, 20); label1.Size = new Size(83, 15);
label1.TabIndex = 0; label1.TabIndex = 0;
label1.Text = "Инструменты"; label1.Text = "Инструменты";
// //
@ -102,22 +178,26 @@
// //
DrawTank.Dock = DockStyle.Fill; DrawTank.Dock = DockStyle.Fill;
DrawTank.Location = new Point(0, 0); DrawTank.Location = new Point(0, 0);
DrawTank.Margin = new Padding(3, 2, 3, 2);
DrawTank.Name = "DrawTank"; DrawTank.Name = "DrawTank";
DrawTank.Size = new Size(550, 451); DrawTank.Size = new Size(568, 429);
DrawTank.TabIndex = 1; DrawTank.TabIndex = 1;
DrawTank.TabStop = false; DrawTank.TabStop = false;
// //
// CollectionsFrame // CollectionsFrame
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 451); ClientSize = new Size(775, 429);
Controls.Add(DrawTank); Controls.Add(DrawTank);
Controls.Add(panel1); Controls.Add(panel1);
Margin = new Padding(3, 2, 3, 2);
Name = "CollectionsFrame"; Name = "CollectionsFrame";
Text = "CollectionsFrame"; Text = "CollectionsFrame";
panel1.ResumeLayout(false); panel1.ResumeLayout(false);
panel1.PerformLayout(); panel1.PerformLayout();
panel2.ResumeLayout(false);
panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)DrawTank).EndInit(); ((System.ComponentModel.ISupportInitialize)DrawTank).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }
@ -131,5 +211,11 @@
private TextBox InputNum; private TextBox InputNum;
private Label label1; private Label label1;
private PictureBox DrawTank; 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 public partial class FormTanksCollections : Form
{ {
private readonly TanksGenericCollection<DrawArmoVehicle, DrawingObjectTank> _tanks; private readonly TanksGenericStorage _storage;
// Конструктор
public FormTanksCollections() public FormTanksCollections()
{ {
InitializeComponent(); 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) 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 (form.ShowDialog() == DialogResult.OK)
{ {
if (_tanks + form.SelectedTank != -1) if (obj + form.SelectedTank)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
DrawTank.Image = _tanks.ShowTanks(); DrawTank.Image = obj.ShowTanks();
} }
else else
{ {
@ -41,21 +104,27 @@ namespace Tank
} }
private void ButtonRemoveTank_Click(object sender, EventArgs e) 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("Удалить объект?", "Удаление", if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{ {
return; return;
} }
int pos = -1; int pos = Convert.ToInt32(InputNum.Text);
try if (obj - pos != null)
{
pos = Convert.ToInt32(InputNum.Text);
}
catch (Exception ex) { }
if (_tanks - pos)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
DrawTank.Image = _tanks.ShowTanks(); DrawTank.Image = obj.ShowTanks();
} }
else else
{ {
@ -65,7 +134,18 @@ namespace Tank
private void ButtonRefreshCollection_Click(object sender, EventArgs e) 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="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, 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="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"> <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value> <value>[base64 mime encoded serialized .NET Framework object]</value>
</data> </data>

View File

@ -1,4 +1,5 @@
using System; using Microsoft.VisualBasic;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -8,84 +9,69 @@ namespace Tank
{ {
internal class SetGeneric<T> where T : class internal class SetGeneric<T> where T : class
{ {
// Массив объектов, которые храним private readonly List<T?> _places;
private readonly T?[] _places; public int Count => _places.Count;
// Количество объектов в массиве private readonly int _maxCount;
public int Count => _places.Length;
// Конструктор
public SetGeneric(int count) public SetGeneric(int count)
{ {
_places = new T?[count]; _maxCount = count;
_places = new List<T?>(_maxCount);
} }
//Добавление объекта в набор public bool Insert(T tank)
public int Insert(T tank)
{ {
int index = -1; return Insert(tank, 0);
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;
} }
// Добавление объекта в набор на конкретную позицию
public bool Insert(T tank, int position) public bool Insert(T tank, int position)
{ {
if (position < 0 || position >= Count) if (position < 0 || position >= _maxCount)
return false; return false;
if (_places[position] == null)
{ if (Count >= _maxCount)
_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)
return false; return false;
for(int i = index; index > position; i--) _places.Insert(0, tank);
{
_places[i] = _places[i - 1];
}
_places[position] = tank;
return true; return true;
} }
// Удаление объекта из набора с конкретной позиции
public bool Remove(int position) public bool Remove(int position)
{ {
if (position < 0 || position >= Count) if (position < 0 || position > _maxCount)
return false; return false;
_places[position] = null; if (position >= Count)
return false;
_places.RemoveAt(position);
return true; return true;
} }
// Получение объекта из набора по позиции public T? this[int position]
public T? Get(int position)
{ {
if (position < 0 || position >= Count) get
{
if (position < 0 || position > _maxCount)
return null; return null;
return _places[position]; 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) if (obj == null)
{ {
return false; 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 // Получение объекта IMoveableObject
public U? GetU(int pos) public U? GetU(int pos)
{ {
return (U?)_collection.Get(pos)?.GetMoveableObject; return (U?)_collection[pos]?.GetMoveableObject;
} }
// Вывод всего набора объектов // Вывод всего набора объектов
@ -94,14 +94,16 @@ namespace Tank.Generics
{ {
int width = _pictureWidth / _placeSizeWidth; int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight; 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)
if (tank == null) {
continue; tank.SetPosition(i % width * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
tank.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
tank.DrawTransport(g); 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;
}
}
}
}