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

This commit is contained in:
Кашин Максим 2022-10-04 15:55:12 +04:00
parent 1c8fb269b1
commit 7dda1f708a
4 changed files with 75 additions and 29 deletions

View File

@ -82,6 +82,7 @@
this.keyRight.Size = new System.Drawing.Size(30, 30); this.keyRight.Size = new System.Drawing.Size(30, 30);
this.keyRight.TabIndex = 10; this.keyRight.TabIndex = 10;
this.keyRight.UseVisualStyleBackColor = true; this.keyRight.UseVisualStyleBackColor = true;
this.keyRight.Click += new System.EventHandler(this.ButtonMove_Click);
// //
// keyLeft // keyLeft
// //
@ -93,6 +94,7 @@
this.keyLeft.Size = new System.Drawing.Size(30, 30); this.keyLeft.Size = new System.Drawing.Size(30, 30);
this.keyLeft.TabIndex = 9; this.keyLeft.TabIndex = 9;
this.keyLeft.UseVisualStyleBackColor = true; this.keyLeft.UseVisualStyleBackColor = true;
this.keyLeft.Click += new System.EventHandler(this.ButtonMove_Click);
// //
// keyUp // keyUp
// //
@ -104,6 +106,7 @@
this.keyUp.Size = new System.Drawing.Size(30, 30); this.keyUp.Size = new System.Drawing.Size(30, 30);
this.keyUp.TabIndex = 8; this.keyUp.TabIndex = 8;
this.keyUp.UseVisualStyleBackColor = true; this.keyUp.UseVisualStyleBackColor = true;
this.keyUp.Click += new System.EventHandler(this.ButtonMove_Click);
// //
// keyDown // keyDown
// //
@ -115,6 +118,7 @@
this.keyDown.Size = new System.Drawing.Size(30, 30); this.keyDown.Size = new System.Drawing.Size(30, 30);
this.keyDown.TabIndex = 7; this.keyDown.TabIndex = 7;
this.keyDown.UseVisualStyleBackColor = true; this.keyDown.UseVisualStyleBackColor = true;
this.keyDown.Click += new System.EventHandler(this.ButtonMove_Click);
// //
// buttonShowOnMap // buttonShowOnMap
// //

View File

@ -29,7 +29,7 @@ namespace GasolineTanker
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
DrawingObjectGasolineTanker GasolineTanker = new(form.SelectedGasolineTanker); DrawingObjectGasolineTanker GasolineTanker = new(form.SelectedGasolineTanker);
if (_mapGasolineTankerCollectionGeneric + GasolineTanker) if (_mapGasolineTankerCollectionGeneric + GasolineTanker >= 0)
{ {
MessageBox.Show("Object added"); MessageBox.Show("Object added");
pictureBox.Image = _mapGasolineTankerCollectionGeneric.ShowSet(); pictureBox.Image = _mapGasolineTankerCollectionGeneric.ShowSet();
@ -52,7 +52,7 @@ namespace GasolineTanker
return; return;
} }
int pos = Convert.ToInt32(maskedTextBoxPosition.Text); int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapGasolineTankerCollectionGeneric - pos) if (_mapGasolineTankerCollectionGeneric - pos != null)
{ {
MessageBox.Show("Object deleted"); MessageBox.Show("Object deleted");
pictureBox.Image = _mapGasolineTankerCollectionGeneric.ShowSet(); pictureBox.Image = _mapGasolineTankerCollectionGeneric.ShowSet();
@ -91,16 +91,16 @@ namespace GasolineTanker
Direction dir = Direction.None; Direction dir = Direction.None;
switch (name) switch (name)
{ {
case "buttonUp": case "keyUp":
dir = Direction.Up; dir = Direction.Up;
break; break;
case "buttonDown": case "keyDown":
dir = Direction.Down; dir = Direction.Down;
break; break;
case "buttonLeft": case "keyLeft":
dir = Direction.Left; dir = Direction.Left;
break; break;
case "buttonRight": case "keyRight":
dir = Direction.Right; dir = Direction.Right;
break; break;
} }

View File

@ -26,12 +26,12 @@ namespace GasolineTanker
_map = map; _map = map;
} }
public static bool operator +(MapWithSetGasolienTankerGeneric<T, U> map, T GasolineTanker) public static int operator +(MapWithSetGasolienTankerGeneric<T, U> map, T GasolineTanker)
{ {
return map._setGasolineTanker.Insert(GasolineTanker); return map._setGasolineTanker.Insert(GasolineTanker);
} }
public static bool operator -(MapWithSetGasolienTankerGeneric<T, U> map, int position) public static T operator -(MapWithSetGasolienTankerGeneric<T, U> map, int position)
{ {
return map._setGasolineTanker.Remove(position); return map._setGasolineTanker.Remove(position);
} }
@ -95,24 +95,31 @@ namespace GasolineTanker
private void DrawBackground(Graphics g) private void DrawBackground(Graphics g)
{ {
Pen pen = new(Color.Black, 3); Pen pen = new(Color.Black, 5);
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 * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); g.DrawLine(pen, i * _placeSizeWidth + 10, j * _placeSizeHeight + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2);
g.DrawLine(pen, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20);
g.DrawLine(pen, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20);
} }
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
} }
} }
private void DrawGasolineTanker(Graphics g) private void DrawGasolineTanker(Graphics g)
{ {
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _setGasolineTanker.Count; i++) for (int i = 0; i < _setGasolineTanker.Count; i++)
{ {
// TODO установка позиции if (_setGasolineTanker.Get(i) != null)
{
_setGasolineTanker.Get(i).SetObject(i % width * _placeSizeWidth + 10, (height - 1 - i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
_setGasolineTanker.Get(i)?.DrawningObject(g); _setGasolineTanker.Get(i)?.DrawningObject(g);
} }
} }
} }
}
} }

View File

@ -15,30 +15,65 @@ namespace GasolineTanker
{ {
_places = new T[count]; _places = new T[count];
} }
public bool Insert(T GasolineTanker) public int Insert(T GasolineTanker)
{ {
// TODO вставка в начало набора if (_places[Count - 1] == null)
return true; {
for (int i = Count - 1; i > 0; i--)
{
_places[i] = _places[i - 1];
} }
public bool Insert(T GasolineTanker, int position) _places[0] = GasolineTanker;
return 0;
}
return -1;
}
public int Insert(T GasolineTanker, int position)
{
if (position < 0 || position >= Count) return -1;
if (_places[position] == null)
{ {
// TODO проверка позиции
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
// проверка, что после вставляемого элемента в массиве есть пустой элемент
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
// TODO вставка по позиции
_places[position] = GasolineTanker; _places[position] = GasolineTanker;
return true; return position;
} }
public bool Remove(int position) else
{ {
// TODO проверка позиции if (_places[Count - 1] == null)
// TODO удаление объекта из массива, присовив элементу массива значение null {
return true; for (int i = Count - 1; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = GasolineTanker;
return position;
}
return -1;
}
}
public T Remove(int position)
{
if (position < 0 || position >= Count)
return null;
if (_places[position] != null)
{
T removed = _places[position];
_places[position] = null;
if (position < Count - 1)
{
for (int k = position; k < Count - 1; k++)
{
_places[k] = _places[k + 1];
}
}
return removed;
}
return null;
} }
public T Get(int position) public T Get(int position)
{ {
// TODO проверка позиции if (position >= Count || position < 0)
return null;
return _places[position]; return _places[position];
} }
} }