Merge pull request 'Nevaeva K. A. LabWork04' (#4) from Lab_4 into Lab_3

Reviewed-on: http://student.git.athene.tech/ksenia_nevaeva/PIbd-23_Nevaeva_K.A._ContainerShip_Base/pulls/4
This commit is contained in:
Евгений Эгов 2022-11-07 10:55:58 +04:00
commit 00b7817460
5 changed files with 319 additions and 118 deletions

View File

@ -29,8 +29,13 @@
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.ButtonAddShip = new System.Windows.Forms.Button();
this.groupBoxMaps = new System.Windows.Forms.GroupBox();
this.ButtonDeleteMap = new System.Windows.Forms.Button();
this.listBoxMaps = new System.Windows.Forms.ListBox();
this.textBoxNewMapName = new System.Windows.Forms.TextBox();
this.ButtonAddMap = new System.Windows.Forms.Button();
this.ComboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.ButtonAddShip = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
@ -41,13 +46,14 @@
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBox1.SuspendLayout();
this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.groupBoxMaps);
this.groupBox1.Controls.Add(this.ButtonAddShip);
this.groupBox1.Controls.Add(this.ComboBoxSelectorMap);
this.groupBox1.Controls.Add(this.buttonUp);
this.groupBox1.Controls.Add(this.buttonRight);
this.groupBox1.Controls.Add(this.buttonDown);
@ -59,20 +65,61 @@
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox1.Location = new System.Drawing.Point(553, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(300, 508);
this.groupBox1.Size = new System.Drawing.Size(300, 687);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
//
// ButtonAddShip
// groupBoxMaps
//
this.ButtonAddShip.Location = new System.Drawing.Point(6, 74);
this.ButtonAddShip.Name = "ButtonAddShip";
this.ButtonAddShip.Size = new System.Drawing.Size(288, 34);
this.ButtonAddShip.TabIndex = 11;
this.ButtonAddShip.Text = "Добавить корабль";
this.ButtonAddShip.UseVisualStyleBackColor = true;
this.ButtonAddShip.Click += new System.EventHandler(this.ButtonAddShip_Click);
this.groupBoxMaps.Controls.Add(this.ButtonDeleteMap);
this.groupBoxMaps.Controls.Add(this.listBoxMaps);
this.groupBoxMaps.Controls.Add(this.textBoxNewMapName);
this.groupBoxMaps.Controls.Add(this.ButtonAddMap);
this.groupBoxMaps.Controls.Add(this.ComboBoxSelectorMap);
this.groupBoxMaps.Location = new System.Drawing.Point(10, 32);
this.groupBoxMaps.Name = "groupBoxMaps";
this.groupBoxMaps.Size = new System.Drawing.Size(284, 327);
this.groupBoxMaps.TabIndex = 12;
this.groupBoxMaps.TabStop = false;
this.groupBoxMaps.Text = "Карты";
//
// ButtonDeleteMap
//
this.ButtonDeleteMap.Location = new System.Drawing.Point(6, 277);
this.ButtonDeleteMap.Name = "ButtonDeleteMap";
this.ButtonDeleteMap.Size = new System.Drawing.Size(272, 44);
this.ButtonDeleteMap.TabIndex = 15;
this.ButtonDeleteMap.Text = "Удалить карту";
this.ButtonDeleteMap.UseVisualStyleBackColor = true;
this.ButtonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click);
//
// listBoxMaps
//
this.listBoxMaps.FormattingEnabled = true;
this.listBoxMaps.ItemHeight = 25;
this.listBoxMaps.Location = new System.Drawing.Point(6, 167);
this.listBoxMaps.Name = "listBoxMaps";
this.listBoxMaps.Size = new System.Drawing.Size(272, 104);
this.listBoxMaps.TabIndex = 14;
this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged);
//
// textBoxNewMapName
//
this.textBoxNewMapName.Location = new System.Drawing.Point(6, 39);
this.textBoxNewMapName.Name = "textBoxNewMapName";
this.textBoxNewMapName.Size = new System.Drawing.Size(272, 31);
this.textBoxNewMapName.TabIndex = 13;
//
// ButtonAddMap
//
this.ButtonAddMap.Location = new System.Drawing.Point(6, 115);
this.ButtonAddMap.Name = "ButtonAddMap";
this.ButtonAddMap.Size = new System.Drawing.Size(272, 44);
this.ButtonAddMap.TabIndex = 12;
this.ButtonAddMap.Text = "Добавить карту";
this.ButtonAddMap.UseVisualStyleBackColor = true;
this.ButtonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click);
//
// ComboBoxSelectorMap
//
@ -80,18 +127,27 @@
this.ComboBoxSelectorMap.Items.AddRange(new object[] {
"Простая карта",
"Модифицированная карта"});
this.ComboBoxSelectorMap.Location = new System.Drawing.Point(6, 35);
this.ComboBoxSelectorMap.Location = new System.Drawing.Point(6, 76);
this.ComboBoxSelectorMap.Name = "ComboBoxSelectorMap";
this.ComboBoxSelectorMap.Size = new System.Drawing.Size(288, 33);
this.ComboBoxSelectorMap.Size = new System.Drawing.Size(272, 33);
this.ComboBoxSelectorMap.TabIndex = 10;
this.ComboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
//
// ButtonAddShip
//
this.ButtonAddShip.Location = new System.Drawing.Point(6, 365);
this.ButtonAddShip.Name = "ButtonAddShip";
this.ButtonAddShip.Size = new System.Drawing.Size(288, 34);
this.ButtonAddShip.TabIndex = 11;
this.ButtonAddShip.Text = "Добавить корабль";
this.ButtonAddShip.UseVisualStyleBackColor = true;
this.ButtonAddShip.Click += new System.EventHandler(this.ButtonAddShip_Click);
//
// buttonUp
//
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::ContainerShip.Properties.Resources.upArrow;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(131, 390);
this.buttonUp.Location = new System.Drawing.Point(131, 569);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(50, 50);
this.buttonUp.TabIndex = 9;
@ -103,7 +159,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::ContainerShip.Properties.Resources.LeftArrow;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(187, 446);
this.buttonRight.Location = new System.Drawing.Point(187, 625);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(50, 50);
this.buttonRight.TabIndex = 8;
@ -115,7 +171,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::ContainerShip.Properties.Resources.DownArrow;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(131, 446);
this.buttonDown.Location = new System.Drawing.Point(131, 625);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(50, 50);
this.buttonDown.TabIndex = 7;
@ -127,7 +183,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::ContainerShip.Properties.Resources.RightArrow;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(75, 446);
this.buttonLeft.Location = new System.Drawing.Point(75, 625);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(50, 50);
this.buttonLeft.TabIndex = 6;
@ -136,9 +192,9 @@
//
// button3
//
this.button3.Location = new System.Drawing.Point(8, 292);
this.button3.Location = new System.Drawing.Point(6, 529);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(286, 34);
this.button3.Size = new System.Drawing.Size(288, 34);
this.button3.TabIndex = 3;
this.button3.Text = "Посмотреть карту";
this.button3.UseVisualStyleBackColor = true;
@ -146,7 +202,7 @@
//
// ButtonShowStorage
//
this.ButtonShowStorage.Location = new System.Drawing.Point(6, 243);
this.ButtonShowStorage.Location = new System.Drawing.Point(6, 489);
this.ButtonShowStorage.Name = "ButtonShowStorage";
this.ButtonShowStorage.Size = new System.Drawing.Size(288, 34);
this.ButtonShowStorage.TabIndex = 2;
@ -156,7 +212,7 @@
//
// ButtonRemoveShip
//
this.ButtonRemoveShip.Location = new System.Drawing.Point(6, 162);
this.ButtonRemoveShip.Location = new System.Drawing.Point(6, 442);
this.ButtonRemoveShip.Name = "ButtonRemoveShip";
this.ButtonRemoveShip.Size = new System.Drawing.Size(288, 41);
this.ButtonRemoveShip.TabIndex = 1;
@ -166,7 +222,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 125);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 405);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(288, 31);
@ -177,7 +233,7 @@
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(553, 508);
this.pictureBox.Size = new System.Drawing.Size(553, 687);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -185,13 +241,15 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(853, 508);
this.ClientSize = new System.Drawing.Size(853, 687);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox1);
this.Name = "FormMapWithSetShips";
this.Text = "Карта с набором объектов";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBoxMaps.ResumeLayout(false);
this.groupBoxMaps.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
@ -211,5 +269,10 @@
private Button buttonRight;
private Button ButtonAddShip;
private ComboBox ComboBoxSelectorMap;
private ListBox listBoxMaps;
private TextBox textBoxNewMapName;
private Button ButtonAddMap;
private GroupBox groupBoxMaps;
private Button ButtonDeleteMap;
}
}

View File

@ -13,46 +13,60 @@ namespace ContainerShip
{
public partial class FormMapWithSetShips : Form
{
/// <summary>
/// Словарь для выпадающего списка
/// </summary>
private readonly Dictionary<string, AbstractMap> _mapsDict = new()
{
{ "Простая карта", new SimpleMap() }
};
/// <summary>
/// Объект от класса карты с набором объектов
/// </summary>
private MapWithSetShipsGeneric<DrawingObjectShip, AbstractMap> _mapShipsCollectionGeneric;
private readonly MapsCollection _mapsCollection;
/// <summary>
/// Конструктор
/// </summary>
public FormMapWithSetShips()
{
InitializeComponent();
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
ComboBoxSelectorMap.Items.Clear();
foreach (var elem in _mapsDict)
{
ComboBoxSelectorMap.Items.Add(elem.Key);
}
}
/// <summary>
/// Заполнение listBoxMaps
/// </summary>
private void ReloadMaps()
{
int index = listBoxMaps.SelectedIndex;
listBoxMaps.Items.Clear();
for (int i = 0; i < _mapsCollection.Keys.Count; i++)
{
listBoxMaps.Items.Add(_mapsCollection.Keys[i]);
}
if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count))
{
listBoxMaps.SelectedIndex = 0;
}
else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count)
{
listBoxMaps.SelectedIndex = index;
}
}
/// <summary>
/// Добавление объекта
/// </summary>
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{
AbstractMap map = null;
switch (ComboBoxSelectorMap.Text)
{
case "Простая карта":
map = new SimpleMap();
break;
case "Модифицированная карта":
map = new ModifyMap();
break;
}
if (map != null)
{
_mapShipsCollectionGeneric = new MapWithSetShipsGeneric<DrawingObjectShip, AbstractMap>(pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapShipsCollectionGeneric = null;
}
}
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddShip_Click(object sender, EventArgs e)
{
if (_mapShipsCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
@ -60,10 +74,10 @@ namespace ContainerShip
if (form.ShowDialog() == DialogResult.OK)
{
DrawingObjectShip ship = new(form.SelectedShip);
if (_mapShipsCollectionGeneric + ship != -1)
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + ship != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapShipsCollectionGeneric.ShowSet();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
@ -78,6 +92,10 @@ namespace ContainerShip
/// <param name="e"></param>
private void ButtonRemoveShip_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;
@ -87,10 +105,10 @@ namespace ContainerShip
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapShipsCollectionGeneric - pos != null)
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapShipsCollectionGeneric.ShowSet();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
@ -104,11 +122,11 @@ namespace ContainerShip
/// <param name="e"></param>
private void ButtonShowStorage_Click(object sender, EventArgs e)
{
if (_mapShipsCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
pictureBox.Image = _mapShipsCollectionGeneric.ShowSet();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
/// <summary>
/// Вывод карты
@ -117,11 +135,11 @@ namespace ContainerShip
/// <param name="e"></param>
private void ButtonShowOnMap_Click(object sender, EventArgs e)
{
if (_mapShipsCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
pictureBox.Image = _mapShipsCollectionGeneric.ShowOnMap();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap();
}
/// <summary>
/// Перемещение
@ -130,7 +148,7 @@ namespace ContainerShip
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_mapShipsCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
@ -152,7 +170,50 @@ namespace ContainerShip
dir = Direction.Right;
break;
}
pictureBox.Image = _mapShipsCollectionGeneric.MoveObject(dir);
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
}
private void ButtonAddMap_Click(object sender, EventArgs e)
{
if (ComboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!_mapsDict.ContainsKey(ComboBoxSelectorMap.Text))
{
MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[ComboBoxSelectorMap.Text]);
ReloadMaps();
}
/// <summary>
/// Выбор карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
/// <summary>
/// Удаление карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonDeleteMap_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
ReloadMaps();
}
}
}
}

View File

@ -44,7 +44,7 @@ namespace ContainerShip
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setShips = new SetShipsGeneric<T>(width * height);
_setShips = new SetShipsGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
@ -68,13 +68,9 @@ namespace ContainerShip
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setShips.Count; i++)
foreach (var ship in _setShips.GetShips())
{
var ship = _setShips.Get(i);
if (ship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, ship);
}
return _map.CreateMap(_pictureWidth, _pictureHeight, ship);
}
return new(_pictureWidth, _pictureHeight);
}
@ -99,11 +95,11 @@ namespace ContainerShip
int j = _setShips.Count - 1;
for (int i = 0; i < _setShips.Count; i++)
{
if (_setShips.Get(i) == null)
if (_setShips[i] == null)
{
for (; j > i; j--)
{
var ship = _setShips.Get(j);
var ship = _setShips[j];
if (ship != null)
{
_setShips.Insert(ship, i);
@ -141,12 +137,14 @@ namespace ContainerShip
/// <param name="g"></param>
private void DrawShips(Graphics g)
{
for (int i = 0; i < _setShips.Count; i++)
int i = 0;
int countOfShipsInLine = _pictureWidth / _placeSizeWidth;
int countOfShipsInColumn = _pictureHeight / _placeSizeHeight;
foreach (var ship in _setShips.GetShips())
{
int countOfShipsInLine = _pictureWidth / _placeSizeWidth;
int countOfShipsInColumn = _pictureHeight / _placeSizeHeight;
_setShips.Get(i)?.SetObject((countOfShipsInLine - (i % countOfShipsInLine) - 1) * _placeSizeWidth, (countOfShipsInColumn - (i / countOfShipsInLine) - 1) * _placeSizeHeight, _pictureWidth, _pictureHeight);
_setShips.Get(i)?.DrawingObject(g);
ship.SetObject((countOfShipsInLine - (i % countOfShipsInLine) - 1) * _placeSizeWidth, (countOfShipsInColumn - (i / countOfShipsInLine) - 1) * _placeSizeHeight, _pictureWidth, _pictureHeight);
ship.DrawingObject(g);
i++;
}
}
/// <summary>

View File

@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContainerShip
{
internal class MapsCollection
{
/// <summary>
/// Словарь (хранилище) с картами
/// </summary>
readonly Dictionary<string, MapWithSetShipsGeneric<DrawingObjectShip, AbstractMap>> _mapStorages;
/// <summary>
/// Возвращение списка названий карт
/// </summary>
public List<string> Keys => _mapStorages.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 MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string, MapWithSetShipsGeneric<DrawingObjectShip, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление карты
/// </summary>
/// <param name="name">Название карты</param>
/// <param name="map">Карта</param>
public void AddMap(string name, AbstractMap map)
{
// TODO Прописать логику для добавления
if (_mapStorages.ContainsKey(name)) return; //уникальное имя
else
{
_mapStorages.Add(name, new MapWithSetShipsGeneric<DrawingObjectShip, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
}
/// <summary>
/// Удаление карты
/// </summary>
/// <param name="name">Название карты</param>
public void DelMap(string name)
{
// TODO Прописать логику для удаления
if (_mapStorages.ContainsKey(name))
{
_mapStorages.Remove(name);
}
}
/// <summary>
/// Доступ к парковке
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public MapWithSetShipsGeneric<DrawingObjectShip, AbstractMap> this[string ind]
{
get
{
//TODO логика получения объекта
if (_mapStorages.ContainsKey(ind)) return _mapStorages[ind];
return null;
}
}
}
}

View File

@ -10,20 +10,22 @@ namespace ContainerShip
where T: class
{
/// <summary>
/// Массив объектов, которые храним
/// Список объектов, которые храним
/// </summary>
private readonly T[] _places;
private readonly List<T> _places;
/// <summary>
/// Количество объектов в массиве
/// Количество объектов в списке
/// </summary>
public int Count => _places.Length;
public int Count => _places.Count;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
private readonly int _maxCount;
public SetShipsGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
/// <summary>
/// Добавление объекта в набор
@ -41,41 +43,10 @@ namespace ContainerShip
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T ship, int position)
{
if (position < 0 || position >= _places.Length)
{
return -1;
}
if (_places[position] == null)
{
_places[position] = ship;
}
else
{
int freePlace = -1;
for (int i = position; i < _places.Length; i++)
{
if (_places[i] == null)
{
freePlace = i;
}
}
if (freePlace == -1)
{
return -1;
}
else
{
for (int i = freePlace; i > position; i--)
{
T buf = _places[i];
_places[i] = _places[i - 1];
_places[i - 1] = buf;
}
_places[position] = ship;
}
}
{
if (position < 0 || position >= _maxCount) return -1;
if (_places.Count + 1 >= _maxCount) return -1;
_places.Add(ship);
return position;
}
/// <summary>
@ -85,12 +56,12 @@ namespace ContainerShip
/// <returns></returns>
public T Remove(int position)
{
if (position < 0 || position >= _places.Length)
if (position < 0 || position >= _maxCount)
{
return null;
}
T ship = _places[position];
_places[position] = null;
_places.RemoveAt(position);
return ship;
}
/// <summary>
@ -98,13 +69,41 @@ namespace ContainerShip
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position)
public T this[int position]
{
if (position < 0 || position >= _places.Length)
get
{
return null;
// TODO проверка позиции
if (position < 0 || position > _maxCount) return null;
return _places[position];
}
set
{
if (position >= 0 || position < _maxCount)
{
Insert(value, position);
}
// TODO проверка позиции
// TODO вставка в список по позиции
}
}
/// <summary>
/// Проход по набору до первого пустого
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetShips()
{
foreach (var ship in _places)
{
if (ship != null)
{
yield return ship;
}
else
{
yield break;
}
}
return _places[position];
}
}
}