PIbd_NikiforovaMV_lab4 #4

Closed
Michelty wants to merge 1 commits from lab4 into lab3
11 changed files with 294 additions and 133 deletions

View File

@ -30,7 +30,7 @@ namespace ContainerShip.DrawningObjects
SolidBrush(containerShip.AdditionalColor);
if (containerShip.Load)
{
g.FillRectangle(additionalBrush, _startPosX + 100, _startPosY + 32, 30, 20);
g.FillRectangle(additionalBrush, _startPosX + 100, _startPosY + 32, 30, 20);
}
base.DrawTransport(g);
if (containerShip.Crane)

View File

@ -16,7 +16,7 @@ namespace ContainerShip.DrawningObjects
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected readonly int _shipWidth = 150;
protected readonly int _shipWidth = 150;
protected readonly int _shipHeight = 65;
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;

View File

@ -30,19 +30,23 @@
{
buttonAdd = new Button();
buttonDelete = new Button();
buttonUpdate = new Button();
label1 = new Label();
labelTools = new Label();
maskedTextBoxNumber = new TextBox();
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();
SuspendLayout();
//
// buttonAdd
//
buttonAdd.Location = new Point(736, 80);
buttonAdd.Margin = new Padding(3, 4, 3, 4);
buttonAdd.Location = new Point(647, 305);
buttonAdd.Name = "buttonAdd";
buttonAdd.Size = new Size(165, 32);
buttonAdd.Size = new Size(144, 24);
buttonAdd.TabIndex = 0;
buttonAdd.Text = "Добавить корабль";
buttonAdd.UseVisualStyleBackColor = true;
@ -50,64 +54,111 @@
//
// buttonDelete
//
buttonDelete.Location = new Point(738, 217);
buttonDelete.Margin = new Padding(3, 4, 3, 4);
buttonDelete.Location = new Point(645, 362);
buttonDelete.Name = "buttonDelete";
buttonDelete.Size = new Size(163, 32);
buttonDelete.Size = new Size(143, 24);
buttonDelete.TabIndex = 1;
buttonDelete.Text = "Удалить корабль";
buttonDelete.UseVisualStyleBackColor = true;
buttonDelete.Click += buttonDelete_Click;
//
// buttonUpdate
// labelTools
//
buttonUpdate.Location = new Point(738, 292);
buttonUpdate.Margin = new Padding(3, 4, 3, 4);
buttonUpdate.Name = "buttonUpdate";
buttonUpdate.Size = new Size(163, 32);
buttonUpdate.TabIndex = 2;
buttonUpdate.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 = "Инструменты";
labelTools.AutoSize = true;
labelTools.Location = new Point(677, 9);
labelTools.Name = "labelTools";
labelTools.Size = new Size(83, 15);
labelTools.TabIndex = 3;
labelTools.Text = "Инструменты";
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(738, 149);
maskedTextBoxNumber.Margin = new Padding(3, 4, 3, 4);
maskedTextBoxNumber.Location = new Point(645, 335);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(162, 27);
maskedTextBoxNumber.Size = new Size(142, 23);
maskedTextBoxNumber.TabIndex = 4;
//
// pictureBoxCollection
//
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Margin = new Padding(3, 4, 3, 4);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(731, 531);
pictureBoxCollection.Size = new Size(640, 386);
pictureBoxCollection.TabIndex = 5;
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;
Review

С чего это?

С чего это?
//
// 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
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleDimensions = new SizeF(7F, 15F);
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(maskedTextBoxNumber);
Controls.Add(label1);
Controls.Add(labelTools);
Controls.Add(buttonUpdate);
Controls.Add(buttonDelete);
Controls.Add(buttonAdd);
Margin = new Padding(3, 4, 3, 4);
Name = "FormShipCollection";
Text = "Набор кораблей";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
@ -119,9 +170,14 @@
private Button buttonAdd;
private Button buttonDelete;
private Button buttonUpdate;
private Label label1;
private Label labelTools;
private TextBox maskedTextBoxNumber;
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
{
private readonly ShipsGenericCollection<DrawningShip, DrawingObjectShip> _ship;
private readonly ShipsGenericStorage _storage;
public FormShipCollection()
{
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)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormShips form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_ship + form.SelectedShip != -1)
if (obj + form.SelectedShip != -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _ship.ShowShips();
pictureBoxCollection.Image = obj.ShowShips();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void buttonDelete_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
int pos = 0;
try
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
pos = Convert.ToInt32(maskedTextBoxNumber.Text);
}
catch
{
MessageBox.Show("Ошибка ввода данных");
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("Объект удален");
pictureBoxCollection.Image = _ship.ShowShips();
pictureBoxCollection.Image = obj.ShowShips();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
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="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

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

View File

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

View File

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

View File

@ -9,77 +9,71 @@ namespace ContainerShip.Generic
internal class SetGeneric<T>
where T : class
{
private readonly T?[] _places;
public int Count => _places.Length - 1;
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);
}
public int Insert(T ship)
{
int pos = Count - 1;
if (_places[Count] != null)
{
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;
_places.Insert(0, ship);
return 0;
}
public bool Insert(T ship, int position)
{
if (position < 0 || position > Count)
// TODO проверка позиции
if (position < 0 || position >= Count || Count >= _maxCount)
{
return false;
}
if (_places[Count] != null)
{
int pos = Count;
for (int i = Count; i > 0; --i)
{
if (_places[i] == null)
{
pos = i;
break;
}
}
for (int i = Count; i >= pos; --i)
{
_places[i - 1] = _places[i];
}
}
_places[Count] = ship;
// TODO вставка по позиции
_places.Insert(position, ship);
return true;
}
public bool Remove(int position)
{
if (position < 0 || position > Count)
if (position < 0 || position >= Count)
{
return false;
}
_places[position] = null;
_places.RemoveAt(position);
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)
{
if (collect._collection.Get(pos) == null)
if (collect._collection[pos] == null)
{
return false;
}
@ -44,7 +44,7 @@ namespace ContainerShip.Generic
}
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
public Bitmap ShowShips()
{
@ -59,8 +59,9 @@ namespace ContainerShip.Generic
Pen pen = new(Color.Black, 3);
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 *
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j *
_placeSizeHeight);
@ -71,31 +72,20 @@ namespace ContainerShip.Generic
}
private void DrawObjects(Graphics g)
{
int x;
int y;
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);
int x = _pictureWidth / _placeSizeWidth - 1;
int y = _pictureHeight / _placeSizeHeight - 1;
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);
}
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);
Review

В классах логики не пишут в консоль, не выводят сообщения. Максимум - запись в логи идет

В классах логики не пишут в консоль, не выводят сообщения. Максимум - запись в логи идет
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;
}
}
}
}