PIbd_NikiforovaMV_lab4

This commit is contained in:
shoot 2023-12-16 11:14:14 +04:00
parent 0a9220bf86
commit d97e259186
11 changed files with 294 additions and 133 deletions

View File

@ -30,19 +30,23 @@
{ {
buttonAdd = new Button(); buttonAdd = new Button();
buttonDelete = new Button(); buttonDelete = new Button();
buttonUpdate = new Button(); labelTools = new Label();
label1 = new Label();
maskedTextBoxNumber = new TextBox(); maskedTextBoxNumber = new TextBox();
pictureBoxCollection = new PictureBox(); pictureBoxCollection = new PictureBox();
labelSet = new Label();
textBoxStorageName = new TextBox();
listBoxStorages = new ListBox();
buttonAddStorage = new Button();
buttonDeleteStorage = new Button();
buttonUpdate = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// buttonAdd // buttonAdd
// //
buttonAdd.Location = new Point(736, 80); buttonAdd.Location = new Point(647, 305);
buttonAdd.Margin = new Padding(3, 4, 3, 4);
buttonAdd.Name = "buttonAdd"; buttonAdd.Name = "buttonAdd";
buttonAdd.Size = new Size(165, 32); buttonAdd.Size = new Size(144, 24);
buttonAdd.TabIndex = 0; buttonAdd.TabIndex = 0;
buttonAdd.Text = "Добавить корабль"; buttonAdd.Text = "Добавить корабль";
buttonAdd.UseVisualStyleBackColor = true; buttonAdd.UseVisualStyleBackColor = true;
@ -50,64 +54,111 @@
// //
// buttonDelete // buttonDelete
// //
buttonDelete.Location = new Point(738, 217); buttonDelete.Location = new Point(645, 362);
buttonDelete.Margin = new Padding(3, 4, 3, 4);
buttonDelete.Name = "buttonDelete"; buttonDelete.Name = "buttonDelete";
buttonDelete.Size = new Size(163, 32); buttonDelete.Size = new Size(143, 24);
buttonDelete.TabIndex = 1; buttonDelete.TabIndex = 1;
buttonDelete.Text = "Удалить корабль"; buttonDelete.Text = "Удалить корабль";
buttonDelete.UseVisualStyleBackColor = true; buttonDelete.UseVisualStyleBackColor = true;
buttonDelete.Click += buttonDelete_Click; buttonDelete.Click += buttonDelete_Click;
// //
// buttonUpdate // labelTools
// //
buttonUpdate.Location = new Point(738, 292); labelTools.AutoSize = true;
buttonUpdate.Margin = new Padding(3, 4, 3, 4); labelTools.Location = new Point(677, 9);
buttonUpdate.Name = "buttonUpdate"; labelTools.Name = "labelTools";
buttonUpdate.Size = new Size(163, 32); labelTools.Size = new Size(83, 15);
buttonUpdate.TabIndex = 2; labelTools.TabIndex = 3;
buttonUpdate.Text = "Обновить коллекцию"; labelTools.Text = "Инструменты";
buttonUpdate.UseVisualStyleBackColor = true;
buttonUpdate.Click += buttonUpdate_Click;
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(774, 12);
label1.Name = "label1";
label1.Size = new Size(103, 20);
label1.TabIndex = 3;
label1.Text = "Инструменты";
// //
// maskedTextBoxNumber // maskedTextBoxNumber
// //
maskedTextBoxNumber.Location = new Point(738, 149); maskedTextBoxNumber.Location = new Point(645, 335);
maskedTextBoxNumber.Margin = new Padding(3, 4, 3, 4);
maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(162, 27); maskedTextBoxNumber.Size = new Size(142, 23);
maskedTextBoxNumber.TabIndex = 4; maskedTextBoxNumber.TabIndex = 4;
// //
// pictureBoxCollection // pictureBoxCollection
// //
pictureBoxCollection.Location = new Point(0, 0); pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Margin = new Padding(3, 4, 3, 4);
pictureBoxCollection.Name = "pictureBoxCollection"; pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(731, 531); pictureBoxCollection.Size = new Size(640, 386);
pictureBoxCollection.TabIndex = 5; pictureBoxCollection.TabIndex = 5;
pictureBoxCollection.TabStop = false; pictureBoxCollection.TabStop = false;
// //
// labelSet
//
labelSet.AutoSize = true;
labelSet.Location = new Point(654, 31);
labelSet.Name = "labelSet";
labelSet.Size = new Size(43, 15);
labelSet.TabIndex = 6;
labelSet.Text = "Набор";
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(647, 49);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(142, 23);
textBoxStorageName.TabIndex = 7;
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 15;
listBoxStorages.Location = new Point(647, 134);
listBoxStorages.Margin = new Padding(3, 2, 3, 2);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(145, 94);
listBoxStorages.TabIndex = 8;
listBoxStorages.SelectedIndexChanged += buttonUpdate_Click;
//
// buttonAddStorage
//
buttonAddStorage.Location = new Point(647, 75);
buttonAddStorage.Name = "buttonAddStorage";
buttonAddStorage.Size = new Size(144, 24);
buttonAddStorage.TabIndex = 9;
buttonAddStorage.Text = "Добавить набор";
buttonAddStorage.UseVisualStyleBackColor = true;
buttonAddStorage.Click += ButtonAddObject_Click;
//
// buttonDeleteStorage
//
buttonDeleteStorage.Location = new Point(647, 242);
buttonDeleteStorage.Name = "buttonDeleteStorage";
buttonDeleteStorage.Size = new Size(144, 24);
buttonDeleteStorage.TabIndex = 11;
buttonDeleteStorage.Text = "Удалить набор";
buttonDeleteStorage.UseVisualStyleBackColor = true;
buttonDeleteStorage.Click += ButtonDelObject_Click;
//
// buttonUpdate
//
buttonUpdate.Location = new Point(647, 105);
buttonUpdate.Name = "buttonUpdate";
buttonUpdate.Size = new Size(143, 24);
buttonUpdate.TabIndex = 2;
buttonUpdate.Text = "Обновить набор";
buttonUpdate.UseVisualStyleBackColor = true;
buttonUpdate.Click += buttonUpdate_Click;
//
// FormShipCollection // FormShipCollection
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(914, 529); ClientSize = new Size(800, 393);
Controls.Add(buttonDeleteStorage);
Controls.Add(buttonAddStorage);
Controls.Add(listBoxStorages);
Controls.Add(textBoxStorageName);
Controls.Add(labelSet);
Controls.Add(pictureBoxCollection); Controls.Add(pictureBoxCollection);
Controls.Add(maskedTextBoxNumber); Controls.Add(maskedTextBoxNumber);
Controls.Add(label1); Controls.Add(labelTools);
Controls.Add(buttonUpdate); Controls.Add(buttonUpdate);
Controls.Add(buttonDelete); Controls.Add(buttonDelete);
Controls.Add(buttonAdd); Controls.Add(buttonAdd);
Margin = new Padding(3, 4, 3, 4);
Name = "FormShipCollection"; Name = "FormShipCollection";
Text = "Набор кораблей"; Text = "Набор кораблей";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
@ -119,9 +170,14 @@
private Button buttonAdd; private Button buttonAdd;
private Button buttonDelete; private Button buttonDelete;
private Button buttonUpdate; private Label labelTools;
private Label label1;
private TextBox maskedTextBoxNumber; private TextBox maskedTextBoxNumber;
private PictureBox pictureBoxCollection; private PictureBox pictureBoxCollection;
private Label labelSet;
private TextBox textBoxStorageName;
private ListBox listBoxStorages;
private Button buttonAddStorage;
private Button buttonDeleteStorage;
private Button buttonUpdate;
} }
} }

View File

@ -15,58 +15,126 @@ namespace ContainerShip
{ {
public partial class FormShipCollection : Form public partial class FormShipCollection : Form
{ {
private readonly ShipsGenericCollection<DrawningShip, DrawingObjectShip> _ship; private readonly ShipsGenericStorage _storage;
public FormShipCollection() public FormShipCollection()
{ {
InitializeComponent(); InitializeComponent();
_ship = new ShipsGenericCollection<DrawningShip, DrawingObjectShip>(pictureBoxCollection.Width, pictureBoxCollection.Height); _storage = new ShipsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
private void ReloadObjects()
{
int index = listBoxStorages.SelectedIndex;
listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorages.Items.Add(_storage.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 ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
}
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowShips();
}
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
}
} }
private void buttonAdd_Click(object sender, EventArgs e) private void buttonAdd_Click(object sender, EventArgs e)
{ {
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormShips form = new(); FormShips form = new();
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
if (_ship + form.SelectedShip != -1) if (obj + form.SelectedShip != -1)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _ship.ShowShips(); pictureBoxCollection.Image = obj.ShowShips();
} }
else else
{ {
MessageBox.Show("Не удалось добавить объект"); MessageBox.Show("Не удалось добавить объект");
} }
} }
} }
private void buttonDelete_Click(object sender, EventArgs e) private void buttonDelete_Click(object sender, EventArgs e)
{ {
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) if (listBoxStorages.SelectedIndex == -1)
{ {
return; return;
} }
int pos = 0; var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
try string.Empty];
if (obj == null)
{ {
pos = Convert.ToInt32(maskedTextBoxNumber.Text);
}
catch
{
MessageBox.Show("Ошибка ввода данных");
return; return;
} }
if (_ship - pos) if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (obj - pos)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _ship.ShowShips(); pictureBoxCollection.Image = obj.ShowShips();
} }
else else
{ {
MessageBox.Show("Не удалось удалить объект"); MessageBox.Show("Не удалось удалить объект");
} }
} }
private void buttonUpdate_Click(object sender, EventArgs e) private void buttonUpdate_Click(object sender, EventArgs e)
{ {
pictureBoxCollection.Image = _ship.ShowShips(); if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowShips();
} }
} }
} }

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

@ -132,7 +132,6 @@ namespace ContainerShip
SelectedShip = _drawningShip; SelectedShip = _drawningShip;
DialogResult = DialogResult.OK; DialogResult = DialogResult.OK;
} }
} }
} }

View File

@ -9,9 +9,7 @@ namespace ContainerShip.MovementStrategy
{ {
public interface IMoveableObject public interface IMoveableObject
{ {
ObjectParameters? GetObjectPosition { get; } ObjectParameters? GetObjectPosition { get; }
int GetStep { get; } int GetStep { get; }
bool CheckCanMove(DirectionType direction); bool CheckCanMove(DirectionType direction);
void MoveObject(DirectionType direction); void MoveObject(DirectionType direction);

View File

@ -6,7 +6,6 @@ using System.Threading.Tasks;
namespace ContainerShip.MovementStrategy namespace ContainerShip.MovementStrategy
{ {
public class ObjectParameters public class ObjectParameters
{ {
private readonly int _x; private readonly int _x;

View File

@ -9,77 +9,71 @@ namespace ContainerShip.Generic
internal class SetGeneric<T> internal class SetGeneric<T>
where T : class where T : class
{ {
private readonly T?[] _places; private readonly List<T?> _places;
public int Count => _places.Length - 1; public int Count => _places.Count;
private readonly int _maxCount;
public SetGeneric(int count) public SetGeneric(int count)
{ {
_places = new T?[count]; _maxCount = count;
_places = new List<T?>(count);
} }
public int Insert(T ship) public int Insert(T ship)
{ {
int pos = Count - 1; _places.Insert(0, ship);
if (_places[Count] != null) return 0;
{
for (int i = pos; i >= 0; --i)
{
if (_places[i] == null)
{
pos = i;
break;
}
}
for (int i = pos + 1; i <= Count; ++i)
{
_places[i - 1] = _places[i];
}
}
_places[Count] = ship;
return pos;
} }
public bool Insert(T ship, int position) public bool Insert(T ship, int position)
{ {
if (position < 0 || position > Count) // TODO проверка позиции
if (position < 0 || position >= Count || Count >= _maxCount)
{ {
return false; return false;
} }
if (_places[Count] != null)
{
int pos = Count;
for (int i = Count; i > 0; --i)
{
if (_places[i] == null) // TODO вставка по позиции
{ _places.Insert(position, ship);
pos = i;
break;
}
}
for (int i = Count; i >= pos; --i)
{
_places[i - 1] = _places[i];
}
}
_places[Count] = ship;
return true; return true;
} }
public bool Remove(int position) public bool Remove(int position)
{ {
if (position < 0 || position > Count) if (position < 0 || position >= Count)
{ {
return false; return false;
} }
_places[position] = null; _places.RemoveAt(position);
return true; return true;
} }
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 || Count >= _maxCount)
{
return;
}
_places.Insert(position, value);
} }
return _places[position];
} }
public IEnumerable<T?> GetShip(int? maxShip = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxShip.HasValue && i == maxShip.Value)
{
yield break;
}
}
}
} }
} }

View File

@ -36,7 +36,7 @@ namespace ContainerShip.Generic
} }
public static bool operator -(ShipsGenericCollection<T, U> collect, int pos) public static bool operator -(ShipsGenericCollection<T, U> collect, int pos)
{ {
if (collect._collection.Get(pos) == null) if (collect._collection[pos] == null)
{ {
return false; return false;
} }
@ -44,7 +44,7 @@ namespace ContainerShip.Generic
} }
public U? GetU(int pos) public U? GetU(int pos)
{ {
return (U?)_collection.Get(pos)?.GetMoveableObject; return (U?)_collection[pos]?.GetMoveableObject;
} }
public Bitmap ShowShips() public Bitmap ShowShips()
{ {
@ -59,8 +59,9 @@ namespace ContainerShip.Generic
Pen pen = new(Color.Black, 3); Pen pen = new(Color.Black, 3);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{ {
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) for (int j = 0; j < _pictureHeight / _placeSizeHeight +
{//линия разметки места 1; ++j)
{
g.DrawLine(pen, i * _placeSizeWidth, j * g.DrawLine(pen, i * _placeSizeWidth, j *
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j *
_placeSizeHeight); _placeSizeHeight);
@ -71,31 +72,20 @@ namespace ContainerShip.Generic
} }
private void DrawObjects(Graphics g) private void DrawObjects(Graphics g)
{ {
int x; int x = _pictureWidth / _placeSizeWidth - 1;
int y; int y = _pictureHeight / _placeSizeHeight - 1;
int index = -1;
for (int i = 0; i <= _collection.Count; ++i)
{
DrawningShip _ship = _collection.Get(i);
x = 0;
y = _pictureHeight / _placeSizeHeight - 1;
if (_ship != null)
{
index = _collection.Count - i;
while ((index - _pictureWidth / _placeSizeWidth) >= 0)
{
y--;
index -= _pictureWidth / _placeSizeWidth;
}
if (index > 0)
{
x += index;
}
x = _pictureWidth / _placeSizeWidth - 1 - x;
_ship.SetPosition(_placeSizeWidth * x, _placeSizeHeight * y);
for (int i = 0; i < _collection.Count; i++)
{
DrawningShip _ship = _collection[i];
if (x < 0)
{
x = _pictureWidth / _placeSizeWidth - 1;
y--;
}
_ship.SetPosition(_placeSizeWidth * x, _placeSizeHeight * y);
_ship.DrawTransport(g); _ship.DrawTransport(g);
} x--;
} }
} }
} }

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ContainerShip.MovementStrategy;
using ContainerShip.DrawningObjects;
namespace ContainerShip.Generic
{
internal class ShipsGenericStorage
{
readonly Dictionary<string, ShipsGenericCollection<DrawningShip, 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<DrawningShip, DrawingObjectShip>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
foreach (string nameStorage in Keys)
{
if (nameStorage == name)
{
MessageBox.Show("Набор с заданным именем уже существует", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
_shipStorages.Add(name, new ShipsGenericCollection<DrawningShip, DrawingObjectShip>(_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{
if (_shipStorages.ContainsKey(name))
{
_shipStorages.Remove(name);
}
}
public ShipsGenericCollection<DrawningShip, DrawingObjectShip>? this[string ind]
{
get
{
if (_shipStorages.ContainsKey(ind))
{
return _shipStorages[ind];
}
return null;
}
}
}
}