начало работы над лабой 4

This commit is contained in:
Полина Чубыкина 2023-11-04 20:53:12 +04:00
parent 605e3cf2e5
commit 39ac5fc6aa
3 changed files with 131 additions and 23 deletions

View File

@ -6,9 +6,6 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Sailboat.DrawingObjects;
using Sailboat.MovementStrategy;
namespace Sailboat.Generics namespace Sailboat.Generics
{ {
/// <summary> /// <summary>
@ -59,8 +56,7 @@ namespace Sailboat.Generics
/// <param name="collect"></param> /// <param name="collect"></param>
/// <param name="obj"></param> /// <param name="obj"></param>
/// <returns></returns> /// <returns></returns>
public static int operator +(BoatsGenericCollection<T, U> collect, T? public static int operator +(BoatsGenericCollection<T, U> collect, T? obj)
obj)
{ {
if (obj == null) if (obj == null)
{ {
@ -74,16 +70,16 @@ namespace Sailboat.Generics
/// <param name="collect"></param> /// <param name="collect"></param>
/// <param name="pos"></param> /// <param name="pos"></param>
/// <returns></returns> /// <returns></returns>
public static bool operator -(BoatsGenericCollection<T, U> collect, int public static T? operator -(BoatsGenericCollection<T, U> collect, int pos)
pos)
{ {
T? obj = collect._collection.Get(pos); T? obj = collect._collection[pos];
if (obj != null) if (obj != null)
{ {
collect._collection.Remove(pos); collect._collection.Remove(pos);
} }
return false; return obj;
} }
/// <summary> /// <summary>
/// Получение объекта IMoveableObject /// Получение объекта IMoveableObject
/// </summary> /// </summary>
@ -91,7 +87,7 @@ namespace Sailboat.Generics
/// <returns></returns> /// <returns></returns>
public U? GetU(int pos) public U? GetU(int pos)
{ {
return (U?)_collection.Get(pos)?.GetMoveableObject; return (U?)_collection[pos]?.GetMoveableObject;
} }
/// <summary> /// <summary>
/// Вывод всего набора объектов /// Вывод всего набора объектов
@ -131,18 +127,17 @@ namespace Sailboat.Generics
/// <param name="g"></param> /// <param name="g"></param>
private void DrawObjects(Graphics g) private void DrawObjects(Graphics g)
{ {
for (int i = 0; i < _collection.Count; i++) int i = 0;
foreach (var boat in _collection.GetBoats())
{ {
DrawingBoat boat = _collection.Get(i);
if (boat != null) if (boat != null)
{ {
int width = _pictureWidth / _placeSizeWidth; int width = _pictureWidth / _placeSizeWidth;
boat.SetPosition(i % width * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); boat.SetPosition(i % width * _placeSizeWidth, _pictureHeight - _pictureHeight % _placeSizeHeight - (i / width + 1) * _placeSizeHeight);
boat.DrawTransport(g); boat.DrawTransport(g);
} }
i++;
} }
} }
} }
} }

View File

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sailboat.DrawingObjects;
using Sailboat.MovementStrategy;
namespace Sailboat.Generics
{
internal class BoatsGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, BoatsGenericCollection<DrawingBoat, DrawingObjectBoat>> _boatStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _boatStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public BoatsGenericStorage(int pictureWidth, int pictureHeight)
{
_boatStorages = new Dictionary<string,
BoatsGenericCollection<DrawingBoat, DrawingObjectBoat>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (_boatStorages.ContainsKey(name))
{
return;
}
_boatStorages[name] = new BoatsGenericCollection<DrawingBoat, DrawingObjectBoat>(_pictureWidth, _pictureHeight);
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (!_boatStorages.ContainsKey(name))
{
return;
}
_boatStorages.Remove(name);
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public BoatsGenericCollection<DrawingBoat, DrawingObjectBoat>?
this[string ind]
{
get
{
if (_boatStorages.ContainsKey(ind))
{
return _boatStorages[ind];
}
return null;
}
}
}
}

View File

@ -4,25 +4,31 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
//пофиксить чота
namespace Sailboat.Generics namespace Sailboat.Generics
{ {
internal class SetGeneric<T> where T : class internal class SetGeneric<T> where T : class
{ {
/// <summary> /// <summary>
/// Массив объектов, которые храним /// Список объектов, которые храним
/// </summary> /// </summary>
private readonly T?[] _places; private readonly List <T?> _places;
/// <summary> /// <summary>
/// Количество объектов в массиве /// Количество объектов в массиве
/// </summary> /// </summary>
public int Count => _places.Length; public int Count => _places.Count;
/// <summary>
/// Максимальное количество объектов в списке
/// </summary>
private readonly int _maxCount;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
/// <param name="count"></param> /// <param name="count"></param>
public SetGeneric(int count) public SetGeneric(int count)
{ {
_places = new T?[count]; _maxCount = count;
_places = new List<T?>(count);
} }
/// <summary> /// <summary>
/// Добавление объекта в набор /// Добавление объекта в набор
@ -90,13 +96,36 @@ namespace Sailboat.Generics
/// </summary> /// </summary>
/// <param name="position"></param> /// <param name="position"></param>
/// <returns></returns> /// <returns></returns>
public T? Get(int position) public T? this[int position]
{ {
if (position < 0 || position >= Count) get
{ {
return null; if (!(position >= 0 && position < Count))
return null;
return _places[position];
}
set
{
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
return;
_places.Insert(position, value);
return;
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetBoats(int? maxBoats = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxBoats.HasValue && i == maxBoats.Value)
{
yield break;
}
} }
return _places[position];
} }
} }
} }