generic classes
This commit is contained in:
parent
98e2a363e5
commit
544a3d3006
124
Battleship/Battleship/MapWithSetBattleshipGeneric.cs
Normal file
124
Battleship/Battleship/MapWithSetBattleshipGeneric.cs
Normal file
@ -0,0 +1,124 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Battleship
|
||||
{
|
||||
/// <summary>
|
||||
/// Карта с набром объектов под нее
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <typeparam name="U"></typeparam>
|
||||
internal class MapWithSetBattleshipGeneric<T, U>
|
||||
where T : class, IDrawningObject
|
||||
where U : AbstractMap
|
||||
{
|
||||
private readonly int _pictureWidth;
|
||||
private readonly int _pictureHeight;
|
||||
private readonly int _placeSizeWidth = 130;
|
||||
private readonly int _placeSizeHeight = 40;
|
||||
private readonly SetBattleshipGeneric<T> _setBattleship;
|
||||
private readonly U _map;
|
||||
|
||||
public MapWithSetBattleshipGeneric(int picWidth, int picHeight, U map)
|
||||
{
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
int height = picHeight / _placeSizeHeight;
|
||||
_setBattleship = new SetBattleshipGeneric<T>(width * height);
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
_map = map;
|
||||
}
|
||||
|
||||
public static bool operator +(MapWithSetBattleshipGeneric<T, U> map, T battleship)
|
||||
{
|
||||
return map._setBattleship.Insert(battleship);
|
||||
}
|
||||
|
||||
public static bool operator -(MapWithSetBattleshipGeneric<T, U> map, int position)
|
||||
{
|
||||
return map._setBattleship.Remove(position);
|
||||
}
|
||||
|
||||
public Bitmap ShowSet()
|
||||
{
|
||||
Bitmap bmp = new(_pictureWidth, _pictureWidth);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
DrawBackground(gr);
|
||||
DrawBattleship(gr, _placeSizeWidth, _placeSizeHeight, _pictureWidth, _pictureHeight);
|
||||
return bmp;
|
||||
}
|
||||
|
||||
public Bitmap ShowOnMap()
|
||||
{
|
||||
Shaking();
|
||||
for (int i = 0; i < _setBattleship.Count; i++)
|
||||
{
|
||||
var battleship = _setBattleship.Get(i);
|
||||
if (battleship != null)
|
||||
{
|
||||
return _map.CreateMap(_pictureWidth, _pictureHeight, battleship);
|
||||
}
|
||||
}
|
||||
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 = _setBattleship.Count - 1;
|
||||
for (int i = 0; i < _setBattleship.Count; i++)
|
||||
{
|
||||
if (_setBattleship.Get(i) == null)
|
||||
{
|
||||
for (; j > i; j--)
|
||||
{
|
||||
var battleship = _setBattleship.Get(j);
|
||||
if (battleship != null)
|
||||
{
|
||||
_setBattleship.Insert(battleship, i);
|
||||
_setBattleship.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 DrawBattleship(Graphics gr, int x, int y, int width, int height)
|
||||
{
|
||||
for (int i = 0; i < _setBattleship.Count; i++)
|
||||
{
|
||||
_setBattleship.Get(i)?.SetObject(x, y, width, height);
|
||||
_setBattleship.Get(i)?.DrawningObject(gr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
61
Battleship/Battleship/SetBattleshipGeneric.cs
Normal file
61
Battleship/Battleship/SetBattleshipGeneric.cs
Normal file
@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Battleship
|
||||
{
|
||||
/// <summary>
|
||||
/// Параметризованный набор объектов
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
internal class SetBattleshipGeneric<T>
|
||||
where T : class
|
||||
{
|
||||
private readonly T[] _places;
|
||||
|
||||
public int Count => _places.Length;
|
||||
|
||||
public SetBattleshipGeneric(int count)
|
||||
{
|
||||
_places = new T[count];
|
||||
}
|
||||
|
||||
public bool Insert(T battleship)
|
||||
{
|
||||
_places[0] = battleship;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Insert(T battleship, 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] = battleship;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Remove(int position)
|
||||
{
|
||||
_places[position] = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
public T Get(int position)
|
||||
{
|
||||
return _places[position];
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user