generic classes

This commit is contained in:
AnnZhimol 2022-09-30 13:02:18 +04:00
parent ab43211760
commit 296f2fdacd
2 changed files with 176 additions and 0 deletions

View File

@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Warship
{
internal class MapWithSetWarshipsGeneric<T,U>
where T : class, IDrawingObject
where U : AbstractMap
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 120;
private readonly int _placeSizeHeight = 50;
private readonly SetWarshipsGeneric<T> _setWarship;
private readonly U _map;
public MapWithSetWarshipsGeneric(int picWidth,int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight/_placeSizeHeight;
_setWarship = new SetWarshipsGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public static bool operator +(MapWithSetWarshipsGeneric<T,U> map, T warship)
{
return map._setWarship.Insert(warship);
}
public static bool operator -(MapWithSetWarshipsGeneric<T,U> map, int position)
{
return map._setWarship.Remove(position);
}
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureWidth);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawWarship(gr, _placeSizeWidth,_placeSizeHeight,_pictureWidth,_pictureHeight);
return bmp;
}
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setWarship.Count; i++)
{
var warship = _setWarship.Get(i);
if (warship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, warship);
}
}
return new(_pictureWidth, _pictureHeight);
}
public Bitmap MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new(_pictureWidth, _pictureHeight);
}
public void Shaking()
{
int j = _setWarship.Count - 1;
for (int i = 0; i < _setWarship.Count; i++)
{
if (_setWarship.Get(i) == null)
{
for (; j > i; j--)
{
var warship = _setWarship.Get(j);
if (warship != null)
{
_setWarship.Insert(warship, i);
_setWarship.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics gr)
{
Pen pen = new(Color.Black, 3);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
gr.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
}
gr.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight/_placeSizeHeight) * _placeSizeHeight);
}
}
private void DrawWarship(Graphics gr, int x,int y,int width,int height)
{
for (int i = 0; i < _setWarship.Count; i++)
{
_setWarship.Get(i)?.SetObject(x, y,width,height);
_setWarship.Get(i)?.DrawingObject(gr);
}
}
}
}

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Warship
{
internal class SetWarshipsGeneric<T>
where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
public SetWarshipsGeneric(int count)
{
_places = new T[count];
}
public bool Insert(T warship)
{
_places[0] = warship;
return true;
}
public bool Insert(T warship, int position)
{
int EmptyEl=position;
if (_places[position]!=null)
{
for (int i = position; i < Count; i++)
if (_places[i] == null)
{
EmptyEl = i;
break;
}
for (int j = EmptyEl; j > position; j--)
_places[j] = _places[j - 1];
}
_places[position] = warship;
return true;
}
public bool Remove(int position)
{
_places[position] = null;
return true;
}
public T Get(int position)
{
return _places[position];
}
}
}