лаб - 3
This commit is contained in:
parent
274bfc284a
commit
b0b97b53e0
@ -0,0 +1,190 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AircraftCarrier
|
||||||
|
{
|
||||||
|
internal class MapWithSetAircraftCarriersGeneric<T, U>
|
||||||
|
where T : class, IDrawningObject
|
||||||
|
where U : AbstractMap
|
||||||
|
{
|
||||||
|
/// Ширина окна отрисовки
|
||||||
|
/// </summary>
|
||||||
|
private readonly int _pictureWidth;
|
||||||
|
/// <summary>
|
||||||
|
/// Высота окна отрисовки
|
||||||
|
/// </summary>
|
||||||
|
private readonly int _pictureHeight;
|
||||||
|
/// <summary>
|
||||||
|
/// Размер занимаемого объектом места (ширина)
|
||||||
|
/// </summary>
|
||||||
|
private readonly int _placeSizeWidth = 250;
|
||||||
|
/// <summary>
|
||||||
|
/// Размер занимаемого объектом места (высота)
|
||||||
|
/// </summary>
|
||||||
|
private readonly int _placeSizeHeight = 100;
|
||||||
|
/// <summary>
|
||||||
|
/// Набор объектов
|
||||||
|
/// </summary>
|
||||||
|
private readonly SetAircraftCarriersGeneric<T> _setAircraftCarriers;
|
||||||
|
/// <summary>
|
||||||
|
/// Карта
|
||||||
|
/// </summary>
|
||||||
|
private readonly U _map;
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="picWidth"></param>
|
||||||
|
/// <param name="picHeight"></param>
|
||||||
|
/// <param name="map"></param>
|
||||||
|
public MapWithSetAircraftCarriersGeneric(int picWidth, int picHeight, U map)
|
||||||
|
{
|
||||||
|
int width = picWidth / _placeSizeWidth;
|
||||||
|
int height = picHeight / _placeSizeHeight;
|
||||||
|
_setAircraftCarriers = new SetAircraftCarriersGeneric<T>(width * height);
|
||||||
|
_pictureWidth = picWidth;
|
||||||
|
_pictureHeight = picHeight;
|
||||||
|
_map = map;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Перегрузка оператора сложения
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="map"></param>
|
||||||
|
/// <param name="car"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static int operator +(MapWithSetAircraftCarriersGeneric<T, U> map, T AircraftCarrier)
|
||||||
|
{
|
||||||
|
return map._setAircraftCarriers.Insert(AircraftCarrier);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Перегрузка оператора вычитания
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="map"></param>
|
||||||
|
/// <param name="position"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static T operator -(MapWithSetAircraftCarriersGeneric<T, U> map, int
|
||||||
|
position)
|
||||||
|
{
|
||||||
|
return map._setAircraftCarriers.Remove(position);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Вывод всего набора объектов
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public Bitmap ShowSet()
|
||||||
|
{
|
||||||
|
Bitmap bmp = new(_pictureWidth, _pictureHeight);
|
||||||
|
Graphics gr = Graphics.FromImage(bmp);
|
||||||
|
DrawBackground(gr);
|
||||||
|
DrawAircraftCarriers(gr);
|
||||||
|
return bmp;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Просмотр объекта на карте
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public Bitmap ShowOnMap()
|
||||||
|
{
|
||||||
|
Shaking();
|
||||||
|
for (int i = 0; i < _setAircraftCarriers.Count; i++)
|
||||||
|
{
|
||||||
|
var aircraftcarrier = _setAircraftCarriers.Get(i);
|
||||||
|
if (aircraftcarrier != null)
|
||||||
|
{
|
||||||
|
return _map.CreateMap(_pictureWidth, _pictureHeight, aircraftcarrier);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new(_pictureWidth, _pictureHeight);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Перемещение объекта по крате
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="direction"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public Bitmap MoveObject(Direction direction)
|
||||||
|
{
|
||||||
|
if (_map != null)
|
||||||
|
{
|
||||||
|
return _map.MoveObject(direction);
|
||||||
|
}
|
||||||
|
return new(_pictureWidth, _pictureHeight);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
|
||||||
|
/// </summary>
|
||||||
|
private void Shaking()
|
||||||
|
{
|
||||||
|
int j = _setAircraftCarriers.Count - 1;
|
||||||
|
for (int i = 0; i < _setAircraftCarriers.Count; i++)
|
||||||
|
{
|
||||||
|
if (_setAircraftCarriers.Get(i) == null)
|
||||||
|
{
|
||||||
|
for (; j > i; j--)
|
||||||
|
{
|
||||||
|
var aircraftcarrier = _setAircraftCarriers.Get(j);
|
||||||
|
if (aircraftcarrier != null)
|
||||||
|
{
|
||||||
|
_setAircraftCarriers.Insert(aircraftcarrier, i);
|
||||||
|
_setAircraftCarriers.Remove(j);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (j <= i)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Метод отрисовки фона
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="g"></param>
|
||||||
|
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)
|
||||||
|
{//линия рамзетки места
|
||||||
|
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i *
|
||||||
|
_placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
||||||
|
}
|
||||||
|
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth,
|
||||||
|
(_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Метод прорисовки объектов
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="g"></param>
|
||||||
|
private void DrawAircraftCarriers(Graphics g)
|
||||||
|
{
|
||||||
|
int yNumOfPlaces = _pictureHeight / _placeSizeHeight;
|
||||||
|
int xNumOfPlaces = _pictureWidth / _placeSizeWidth;
|
||||||
|
int rowNum = yNumOfPlaces - 1;
|
||||||
|
int columnNum = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < _setAircraftCarriers.Count; i++)
|
||||||
|
{
|
||||||
|
if (_setAircraftCarriers.Get(i) != null)
|
||||||
|
{
|
||||||
|
(float Left, float Top, float Right, float Bottom) = _setAircraftCarriers.Get(i).GetCurrentPosition();
|
||||||
|
_setAircraftCarriers.Get(i).SetObject(columnNum * _placeSizeWidth, rowNum * _placeSizeHeight + (_placeSizeHeight - (int)(Bottom - Top)), _pictureWidth, _pictureHeight);
|
||||||
|
_setAircraftCarriers.Get(i).DrawningObject(g);
|
||||||
|
}
|
||||||
|
if (columnNum == xNumOfPlaces - 1)
|
||||||
|
{
|
||||||
|
columnNum = 0;
|
||||||
|
rowNum--;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
columnNum++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -11,7 +11,7 @@ namespace AircraftCarrier
|
|||||||
// To customize application configuration such as set high DPI settings or default font,
|
// To customize application configuration such as set high DPI settings or default font,
|
||||||
// see https://aka.ms/applicationconfiguration.
|
// see https://aka.ms/applicationconfiguration.
|
||||||
ApplicationConfiguration.Initialize();
|
ApplicationConfiguration.Initialize();
|
||||||
Application.Run(new FormMap());
|
Application.Run(new FormMapWithSetAircraftCarriers());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,99 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AircraftCarrier
|
||||||
|
{
|
||||||
|
internal class SetAircraftCarriersGeneric<T>
|
||||||
|
where T : class
|
||||||
|
{
|
||||||
|
private readonly T[] _places;
|
||||||
|
/// <summary>
|
||||||
|
/// Количество объектов в массиве
|
||||||
|
/// </summary>
|
||||||
|
public int Count => _places.Length;
|
||||||
|
/// <summary>
|
||||||
|
/// Конструктор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="count"></param>
|
||||||
|
public SetAircraftCarriersGeneric(int count)
|
||||||
|
{
|
||||||
|
_places = new T[count];
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Добавление объекта в набор
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="car">Добавляемый автомобиль</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public int Insert(T AircraftCarrier)
|
||||||
|
{
|
||||||
|
return Insert(AircraftCarrier, 0);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Добавление объекта в набор на конкретную позицию
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="car">Добавляемый автомобиль</param>
|
||||||
|
/// <param name="position">Позиция</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public int Insert(T AircraftCarrier, int position)
|
||||||
|
{
|
||||||
|
// TODO проверка позиции
|
||||||
|
if (position >= _places.Length || position < 0)
|
||||||
|
return -1;
|
||||||
|
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
|
||||||
|
if (_places[position] == null)
|
||||||
|
{
|
||||||
|
_places[position] = AircraftCarrier;
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
// проверка, что после вставляемого элемента в массиве есть пустой элемент
|
||||||
|
int findEmptyPos = -1;
|
||||||
|
|
||||||
|
for (int i = position + 1; i < Count; i++)
|
||||||
|
{
|
||||||
|
if (_places[i] == null)
|
||||||
|
{
|
||||||
|
findEmptyPos = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (findEmptyPos < 0) return -1;
|
||||||
|
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
|
||||||
|
for (int i = findEmptyPos; i > position; i--)
|
||||||
|
{
|
||||||
|
_places[i] = _places[i - 1];
|
||||||
|
}
|
||||||
|
// TODO вставка по позиции
|
||||||
|
_places[position] = AircraftCarrier;
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Удаление объекта из набора с конкретной позиции
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="position"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public T Remove(int position)
|
||||||
|
{
|
||||||
|
// TODO проверка позиции
|
||||||
|
if (position >= _places.Length || position < 0) return null;
|
||||||
|
// TODO удаление объекта из массива, присовив элементу массива значение null
|
||||||
|
T temp = _places[position];
|
||||||
|
_places[position] = null;
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Получение объекта из набора по позиции
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="position"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public T Get(int position)
|
||||||
|
{
|
||||||
|
if (position >= _places.Length || position < 0)
|
||||||
|
return null;
|
||||||
|
return _places[position];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -36,7 +36,7 @@ namespace AircraftCarrier
|
|||||||
_map[i, j] = _freeRoad;
|
_map[i, j] = _freeRoad;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while (counter < 10)
|
while (counter < 5)
|
||||||
{
|
{
|
||||||
int x = _random.Next(0, 100);
|
int x = _random.Next(0, 100);
|
||||||
int y = _random.Next(0, 100);
|
int y = _random.Next(0, 100);
|
||||||
@ -47,5 +47,6 @@ namespace AircraftCarrier
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user