Polevoy S.V Lab_work3 #3

Merged
eegov merged 11 commits from LabWork03 into LabWork02 2022-10-07 09:57:40 +04:00
2 changed files with 132 additions and 2 deletions
Showing only changes of commit d8c763c582 - Show all commits

View File

@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Artilleries
{
internal class MapWithArtilleriesGeneric<T, U>
where T : class, IDrawingObject
where U : AbstractMap
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 210;
private readonly int _placeSizeHeight = 90;
private readonly SetArtilleriesGeneric<T> _setArtilleries;
private readonly U _map;
public MapWithArtilleriesGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setArtilleries = new SetArtilleriesGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public static bool operator +(MapWithArtilleriesGeneric<T, U> map, T artillery)
{
return map._setArtilleries.Insert(artillery); // TODO
}
public static bool operator -(MapWithArtilleriesGeneric<T, U> map, int position)
{
return map._setArtilleries.Remove(position); // TODO
}
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setArtilleries.Count; i++)
{
var car = _setArtilleries.Get(i);
if (car != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
}
}
return new(_pictureWidth, _pictureHeight);
}
public Bitmap MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new(_pictureWidth, _pictureHeight);
}
private void Shaking()
{
int j = _setArtilleries.Count - 1;
for (int i = 0; i < _setArtilleries.Count; i++)
{
if (_setArtilleries.Get(i) == null)
{
for (; j > i; j--)
{
var car = _setArtilleries.Get(j);
if (car != null)
{
_setArtilleries.Insert(car, i);
_setArtilleries.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 3);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{// TODO
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i *
_placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth,
(_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
}
private void DrawArtilleries(Graphics g)
{
for (int i = 0; i < _setArtilleries.Count; i++)
{
// TODO установка позиции
_setArtilleries.Get(i)?.DrawingObject(g);
}
}
}
}

View File

@ -19,22 +19,40 @@ namespace Artilleries
public bool Insert(T artillery)
{
return true; // TODO
return Insert(artillery, 0);
}
public bool Insert(T artillery, int position)
{
if (position < 0 || position >= Count)
{
return false;
}
_places[position] = artillery;
return true; // TODO
}
public bool Remove(int position)
{
return true; // TODO
if (position < 0 || position >= Count || _places[position] == null)
{
return false;
}
_places[position] = null;
return true;
}
public T Get(int position)
{
if (position < 0 || position >= Count)
{
return null;
}
return _places[position];
}
}