This commit is contained in:
Ekaterina 2023-12-29 22:57:36 +04:00
parent bb1b96f3ff
commit 61099f558b
19 changed files with 207 additions and 169 deletions

Binary file not shown.

View File

@ -30,35 +30,76 @@
{
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.panelCollection = new System.Windows.Forms.Panel();
this.labelCollection = new System.Windows.Forms.Label();
this.buttonAddShip = new System.Windows.Forms.Button();
this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
this.buttonRemoveShip = new System.Windows.Forms.Button();
this.buttonRefreshCollection = new System.Windows.Forms.Button();
this.buttonRemoveShip = new System.Windows.Forms.Button();
this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
this.buttonAddShip = new System.Windows.Forms.Button();
this.labelCollection = new System.Windows.Forms.Label();
this.groupBoxSets = new System.Windows.Forms.GroupBox();
this.textBoxStorageName = new System.Windows.Forms.TextBox();
this.buttonnAddObject = new System.Windows.Forms.Button();
this.listBoxStorages = new System.Windows.Forms.ListBox();
this.buttonDelObject = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.panelCollection.SuspendLayout();
this.groupBoxSets.SuspendLayout();
this.SuspendLayout();
//
// pictureBoxCollection
//
this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0);
this.pictureBoxCollection.Name = "pictureBoxCollection";
this.pictureBoxCollection.Size = new System.Drawing.Size(700, 453);
this.pictureBoxCollection.Size = new System.Drawing.Size(864, 536);
this.pictureBoxCollection.TabIndex = 0;
this.pictureBoxCollection.TabStop = false;
//
// panelCollection
//
this.panelCollection.Controls.Add(this.groupBoxSets);
this.panelCollection.Controls.Add(this.buttonRefreshCollection);
this.panelCollection.Controls.Add(this.buttonRemoveShip);
this.panelCollection.Controls.Add(this.maskedTextBoxNumber);
this.panelCollection.Controls.Add(this.buttonAddShip);
this.panelCollection.Controls.Add(this.labelCollection);
this.panelCollection.Location = new System.Drawing.Point(699, 0);
this.panelCollection.Location = new System.Drawing.Point(870, 0);
this.panelCollection.Name = "panelCollection";
this.panelCollection.Size = new System.Drawing.Size(218, 453);
this.panelCollection.Size = new System.Drawing.Size(218, 536);
this.panelCollection.TabIndex = 1;
//
// buttonRefreshCollection
//
this.buttonRefreshCollection.Location = new System.Drawing.Point(16, 482);
this.buttonRefreshCollection.Name = "buttonRefreshCollection";
this.buttonRefreshCollection.Size = new System.Drawing.Size(171, 40);
this.buttonRefreshCollection.TabIndex = 4;
this.buttonRefreshCollection.Text = "Обновить коллекцию";
this.buttonRefreshCollection.UseVisualStyleBackColor = true;
//
// buttonRemoveShip
//
this.buttonRemoveShip.Location = new System.Drawing.Point(16, 424);
this.buttonRemoveShip.Name = "buttonRemoveShip";
this.buttonRemoveShip.Size = new System.Drawing.Size(171, 40);
this.buttonRemoveShip.TabIndex = 3;
this.buttonRemoveShip.Text = "Удалить теплоход";
this.buttonRemoveShip.UseVisualStyleBackColor = true;
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(16, 375);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(171, 27);
this.maskedTextBoxNumber.TabIndex = 2;
//
// buttonAddShip
//
this.buttonAddShip.Location = new System.Drawing.Point(16, 309);
this.buttonAddShip.Name = "buttonAddShip";
this.buttonAddShip.Size = new System.Drawing.Size(171, 40);
this.buttonAddShip.TabIndex = 1;
this.buttonAddShip.Text = "Добавить теплоход";
this.buttonAddShip.UseVisualStyleBackColor = true;
//
// labelCollection
//
this.labelCollection.AutoSize = true;
@ -68,45 +109,58 @@
this.labelCollection.TabIndex = 0;
this.labelCollection.Text = "Инструменты";
//
// buttonAddShip
// groupBoxSets
//
this.buttonAddShip.Location = new System.Drawing.Point(16, 38);
this.buttonAddShip.Name = "buttonAddShip";
this.buttonAddShip.Size = new System.Drawing.Size(171, 40);
this.buttonAddShip.TabIndex = 1;
this.buttonAddShip.Text = "Добавить теплоход";
this.buttonAddShip.UseVisualStyleBackColor = true;
this.groupBoxSets.Controls.Add(this.buttonDelObject);
this.groupBoxSets.Controls.Add(this.listBoxStorages);
this.groupBoxSets.Controls.Add(this.buttonnAddObject);
this.groupBoxSets.Controls.Add(this.textBoxStorageName);
this.groupBoxSets.Location = new System.Drawing.Point(12, 42);
this.groupBoxSets.Name = "groupBoxSets";
this.groupBoxSets.Size = new System.Drawing.Size(199, 242);
this.groupBoxSets.TabIndex = 5;
this.groupBoxSets.TabStop = false;
this.groupBoxSets.Text = "Наборы";
//
// maskedTextBoxNumber
// textBoxStorageName
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(16, 117);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(171, 27);
this.maskedTextBoxNumber.TabIndex = 2;
this.textBoxStorageName.Location = new System.Drawing.Point(0, 26);
this.textBoxStorageName.Name = "textBoxStorageName";
this.textBoxStorageName.Size = new System.Drawing.Size(193, 27);
this.textBoxStorageName.TabIndex = 0;
//
// buttonRemoveShip
// buttonnAddObject
//
this.buttonRemoveShip.Location = new System.Drawing.Point(16, 166);
this.buttonRemoveShip.Name = "buttonRemoveShip";
this.buttonRemoveShip.Size = new System.Drawing.Size(171, 40);
this.buttonRemoveShip.TabIndex = 3;
this.buttonRemoveShip.Text = "Удалить теплоход";
this.buttonRemoveShip.UseVisualStyleBackColor = true;
this.buttonnAddObject.Location = new System.Drawing.Point(4, 71);
this.buttonnAddObject.Name = "buttonnAddObject";
this.buttonnAddObject.Size = new System.Drawing.Size(189, 29);
this.buttonnAddObject.TabIndex = 1;
this.buttonnAddObject.Text = "Добавить набор";
this.buttonnAddObject.UseVisualStyleBackColor = true;
//
// buttonRefreshCollection
// listBoxStorages
//
this.buttonRefreshCollection.Location = new System.Drawing.Point(16, 239);
this.buttonRefreshCollection.Name = "buttonRefreshCollection";
this.buttonRefreshCollection.Size = new System.Drawing.Size(171, 40);
this.buttonRefreshCollection.TabIndex = 4;
this.buttonRefreshCollection.Text = "Обновить коллекцию";
this.buttonRefreshCollection.UseVisualStyleBackColor = true;
this.listBoxStorages.FormattingEnabled = true;
this.listBoxStorages.ItemHeight = 20;
this.listBoxStorages.Location = new System.Drawing.Point(6, 117);
this.listBoxStorages.Name = "listBoxStorages";
this.listBoxStorages.Size = new System.Drawing.Size(187, 84);
this.listBoxStorages.TabIndex = 2;
//
// buttonDelObject
//
this.buttonDelObject.Location = new System.Drawing.Point(6, 207);
this.buttonDelObject.Name = "buttonDelObject";
this.buttonDelObject.Size = new System.Drawing.Size(187, 29);
this.buttonDelObject.TabIndex = 3;
this.buttonDelObject.Text = "Удалить набор";
this.buttonDelObject.UseVisualStyleBackColor = true;
//
// FormShipCollection
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(918, 453);
this.ClientSize = new System.Drawing.Size(1093, 534);
this.Controls.Add(this.panelCollection);
this.Controls.Add(this.pictureBoxCollection);
this.Name = "FormShipCollection";
@ -114,6 +168,8 @@
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.panelCollection.ResumeLayout(false);
this.panelCollection.PerformLayout();
this.groupBoxSets.ResumeLayout(false);
this.groupBoxSets.PerformLayout();
this.ResumeLayout(false);
}
@ -127,5 +183,10 @@
private MaskedTextBox maskedTextBoxNumber;
private Button buttonAddShip;
private Label labelCollection;
private GroupBox groupBoxSets;
private Button buttonDelObject;
private ListBox listBoxStorages;
private Button buttonnAddObject;
private TextBox textBoxStorageName;
}
}

View File

@ -9,89 +9,75 @@ namespace WarmlyShip.Generics
internal class SetGeneric<T>
where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// </summary>
private readonly T?[] _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
private readonly List<T?> _places;
public int Count => _places.Count;
private readonly int _maxCount;
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(count);
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="warmlyship">Добавляемый теплоход</param>
/// <returns></returns>
public int Insert(T warmlyship)
public bool Insert(T warmlyship)
{
return Insert(warmlyship, 0);
if (_places.Count == _maxCount)
{
return false;
}
Insert(warmlyship, 0);
return true;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="warmlyship">Добавляемый теплоход</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T warmlyship, int position)
public bool Insert(T warmlyship, int position)
{
int nullIndex = -1;
int i;
if (position < 0 || position >= Count)
return -1;
for (i = position; i < Count; i++)
if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
{
if (_places[i] == null)
{
nullIndex = i;
break;
}
return false;
}
if (nullIndex < 0)
return -1;
for (i = nullIndex; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = warmlyship;
return position;
_places.Insert(position, warmlyship);
return true;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
{
if (position < 0 || position >= Count)
{
return false;
}
_places[position] = null;
_places.RemoveAt(position);
return true;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
public T? this[int position]
{
if (position < 0 || position >= Count)
get
{
return null;
if (position < 0 || position >= Count)
{
return null;
}
return _places[position];
}
set
{
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
{
return;
}
_places.Insert(position, value);
return;
}
}
public IEnumerable<T> GetShips(int? maxShips = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxShips.HasValue && i == maxShips.Value)
{
yield break;
}
}
return _places[position];
}
}
}

View File

@ -6,7 +6,6 @@ using System.Threading.Tasks;
using WarmlyShip.DrawingObjects;
using WarmlyShip.MovementStrategy;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace WarmlyShip.Generics
{
@ -14,31 +13,11 @@ namespace WarmlyShip.Generics
where T : DrawingWarmlyShip
where U : IMoveableObject
{
/// <summary>
/// Ширина окна прорисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна прорисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Размер занимаемого объектом места (ширина)
/// </summary>
private readonly int _placeSizeWidth = 185;
/// <summary>
/// Размер занимаемого объектом места (высота)
/// </summary>
private readonly int _placeSizeHeight = 185;
/// <summary>
/// Набор объектов
/// </summary>
private readonly SetGeneric<T> _collection;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
public ShipsGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
@ -47,50 +26,29 @@ namespace WarmlyShip.Generics
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static int operator +(ShipsGenericCollection<T, U> collect, T?
public static bool operator +(ShipsGenericCollection<T, U> collect, T?
obj)
{
if (obj == null)
{
return -1;
return false;
}
return collect._collection.Insert(obj);
return (bool)collect?._collection.Insert(obj);
}
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static bool operator -(ShipsGenericCollection<T, U> collect, int
public static T? operator -(ShipsGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
}
return false;
return obj;
}
/// <summary>
/// Получение объекта IMoveableObject
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public Bitmap ShowShips()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
@ -99,10 +57,6 @@ namespace WarmlyShip.Generics
DrawObjects(gr);
return bmp;
}
/// <summary>
/// Метод отрисовки фона
/// </summary>
/// <param name="g"></param>
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 3);
@ -110,7 +64,7 @@ namespace WarmlyShip.Generics
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight +
1; ++j)
{//линия рамзетки места
{
g.DrawLine(pen, i * _placeSizeWidth, j *
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j *
_placeSizeHeight);
@ -119,32 +73,19 @@ namespace WarmlyShip.Generics
_placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
/// <summary>
/// /// Метод прорисовки объектов
/// </summary>
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
T? ship;
int width = _pictureWidth / _placeSizeWidth;
int diff = 1;
int currWidth = 0;
for (int i = 0; i < _collection.Count; i++)
int i = 0;
foreach (var ship in _collection.GetShips())
{
currWidth++;
if (currWidth > width)
{
diff++;
currWidth = 1;
}
ship = _collection.Get(i);
if (ship != null)
{
int width = _pictureWidth / _placeSizeWidth;
ship.SetPosition((width - 1 - (i % width)) * _placeSizeWidth, i / width * _placeSizeHeight);
ship.DrawTransport(g);
}
i++;
}
}
}
}

View File

@ -0,0 +1,50 @@
using System.Text;
using System.Threading.Tasks;
using WarmlyShip.DrawingObjects;
using WarmlyShip.MovementStrategy;
namespace WarmlyShip.Generics
{
internal class ShipsGenericStorage
{
readonly Dictionary<string, ShipsGenericCollection<DrawingWarmlyShip, DrawingObjectShip>> _shipStorages;
public List<string> Keys => _shipStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public ShipsGenericStorage(int pictureWidth, int pictureHeight)
{
_shipStorages = new Dictionary<string,
ShipsGenericCollection<DrawingWarmlyShip, DrawingObjectShip>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
if (_shipStorages.ContainsKey(name))
{
return;
}
_shipStorages[name] = new ShipsGenericCollection<DrawingWarmlyShip, DrawingObjectShip>(_pictureWidth, _pictureHeight);
}
public void DelSet(string name)
{
if (!_shipStorages.ContainsKey(name))
{
return;
}
_shipStorages.Remove(name);
}
public ShipsGenericCollection<DrawingWarmlyShip, DrawingObjectShip>? this[string ind]
{
get
{
if (_shipStorages.ContainsKey(ind))
{
return _shipStorages[ind];
}
return null;
}
}
}
}

View File

@ -1 +1 @@
06cd6889c77069e0f55068eddb8f6c6d07ea4abf
621cbda5ca6541382078a80e2a7b1378f7a5c582