generic classes
This commit is contained in:
parent
3c80bd3686
commit
64d37b837f
122
Warship/Warship/MapWithSetWarshipsGeneric.cs
Normal file
122
Warship/Warship/MapWithSetWarshipsGeneric.cs
Normal file
@ -0,0 +1,122 @@
|
||||
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 int operator +(MapWithSetWarshipsGeneric<T, U> map, T warship)
|
||||
{
|
||||
return map._setWarship.Insert(warship);
|
||||
}
|
||||
|
||||
public static T 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);
|
||||
return bmp;
|
||||
}
|
||||
|
||||
public Bitmap ShowOnMap()
|
||||
{
|
||||
Shaking();
|
||||
foreach (var warship in _setWarship.GetWarships())
|
||||
{
|
||||
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[i] == null)
|
||||
{
|
||||
for (; j > i; j--)
|
||||
{
|
||||
var warship = _setWarship[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, 5);
|
||||
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
||||
{
|
||||
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
||||
{
|
||||
gr.DrawLine(pen, i * _placeSizeWidth + 20, j * _placeSizeHeight + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2);
|
||||
gr.DrawLine(pen, i * _placeSizeWidth + 20, j * _placeSizeHeight + _placeSizeHeight / 2 + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight / 2 + 2);
|
||||
gr.DrawLine(pen, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2);
|
||||
gr.DrawLine(pen, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawWarship(Graphics gr)
|
||||
{
|
||||
int width = _pictureWidth / _placeSizeWidth;
|
||||
int height = _pictureHeight / _placeSizeHeight;
|
||||
int i = 0;
|
||||
|
||||
foreach (var warship in _setWarship.GetWarships())
|
||||
{
|
||||
warship.SetObject(i % width * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight, _pictureWidth, _pictureHeight);
|
||||
warship.DrawingObject(gr);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
81
Warship/Warship/SetWarshipsGeneric.cs
Normal file
81
Warship/Warship/SetWarshipsGeneric.cs
Normal file
@ -0,0 +1,81 @@
|
||||
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 List<T> _places;
|
||||
|
||||
public int Count => _places.Count;
|
||||
|
||||
private readonly int _maxCount;
|
||||
|
||||
public SetWarshipsGeneric(int count)
|
||||
{
|
||||
_maxCount = count;
|
||||
_places = new List<T>();
|
||||
}
|
||||
|
||||
public int Insert(T warship)
|
||||
{
|
||||
if (_places.Count + 1 >= _maxCount)
|
||||
return -1;
|
||||
_places.Insert(0, warship);
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int Insert(T warship, int position)
|
||||
{
|
||||
if (position >= _maxCount || position < 0)
|
||||
return -1;
|
||||
if (_places.Count + 1 >= _maxCount)
|
||||
return -1;
|
||||
_places.Insert(position, warship);
|
||||
return position;
|
||||
}
|
||||
|
||||
public T Remove(int position)
|
||||
{
|
||||
if (position >= _maxCount || position < 0)
|
||||
return null;
|
||||
|
||||
T deleted = _places[position];
|
||||
_places.RemoveAt(position);
|
||||
return deleted;
|
||||
}
|
||||
|
||||
public T this[int position]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (position < 0 || position >= _maxCount)
|
||||
return null;
|
||||
return _places[position];
|
||||
}
|
||||
set
|
||||
{
|
||||
if (position < 0 || position >= _maxCount)
|
||||
Insert(value, position);
|
||||
}
|
||||
}
|
||||
public IEnumerable<T> GetWarships()
|
||||
{
|
||||
foreach (var warship in _places)
|
||||
{
|
||||
if (warship != null)
|
||||
{
|
||||
yield return warship;
|
||||
}
|
||||
else
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user