Compare commits

..

No commits in common. "8b6f9e2177ed3068e99391c90fa90373f6dced6e" and "e2969fef09ea601987efb096a73aa613cbdf1f5a" have entirely different histories.

5 changed files with 126 additions and 290 deletions

View File

@ -29,118 +29,39 @@
private void InitializeComponent() private void InitializeComponent()
{ {
this.groupBoxTools = new System.Windows.Forms.GroupBox(); this.groupBoxTools = new System.Windows.Forms.GroupBox();
this.groupBoxMaps = new System.Windows.Forms.GroupBox();
this.buttonDeleteMap = new System.Windows.Forms.Button();
this.buttonAddMap = new System.Windows.Forms.Button();
this.listBoxMaps = new System.Windows.Forms.ListBox();
this.textBoxMap = new System.Windows.Forms.TextBox();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.buttonAddWarship = new System.Windows.Forms.Button();
this.buttonShowOnMap = new System.Windows.Forms.Button(); this.buttonShowOnMap = new System.Windows.Forms.Button();
this.buttonRemoveWarship = new System.Windows.Forms.Button();
this.buttonShowStorage = new System.Windows.Forms.Button(); this.buttonShowStorage = new System.Windows.Forms.Button();
this.buttonRemoveWarship = new System.Windows.Forms.Button();
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonAddWarship = new System.Windows.Forms.Button();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.pictureBox = new System.Windows.Forms.PictureBox(); this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBoxTools.SuspendLayout(); this.groupBoxTools.SuspendLayout();
this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
// groupBoxTools // groupBoxTools
// //
this.groupBoxTools.Controls.Add(this.groupBoxMaps);
this.groupBoxTools.Controls.Add(this.buttonAddWarship);
this.groupBoxTools.Controls.Add(this.buttonShowOnMap); this.groupBoxTools.Controls.Add(this.buttonShowOnMap);
this.groupBoxTools.Controls.Add(this.buttonRemoveWarship);
this.groupBoxTools.Controls.Add(this.buttonShowStorage); this.groupBoxTools.Controls.Add(this.buttonShowStorage);
this.groupBoxTools.Controls.Add(this.buttonRemoveWarship);
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.buttonAddWarship);
this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap);
this.groupBoxTools.Controls.Add(this.pictureBox1); this.groupBoxTools.Controls.Add(this.pictureBox1);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(911, 0); this.groupBoxTools.Location = new System.Drawing.Point(600, 0);
this.groupBoxTools.Name = "groupBoxTools"; this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(200, 597); this.groupBoxTools.Size = new System.Drawing.Size(200, 450);
this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false; this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты"; this.groupBoxTools.Text = "Инструменты";
// //
// groupBoxMaps
//
this.groupBoxMaps.Controls.Add(this.buttonDeleteMap);
this.groupBoxMaps.Controls.Add(this.buttonAddMap);
this.groupBoxMaps.Controls.Add(this.listBoxMaps);
this.groupBoxMaps.Controls.Add(this.textBoxMap);
this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap);
this.groupBoxMaps.Location = new System.Drawing.Point(15, 32);
this.groupBoxMaps.Name = "groupBoxMaps";
this.groupBoxMaps.Size = new System.Drawing.Size(173, 250);
this.groupBoxMaps.TabIndex = 2;
this.groupBoxMaps.TabStop = false;
this.groupBoxMaps.Text = "Карты";
//
// buttonDeleteMap
//
this.buttonDeleteMap.Location = new System.Drawing.Point(6, 214);
this.buttonDeleteMap.Name = "buttonDeleteMap";
this.buttonDeleteMap.Size = new System.Drawing.Size(152, 23);
this.buttonDeleteMap.TabIndex = 9;
this.buttonDeleteMap.Text = "Удалить карту";
this.buttonDeleteMap.UseVisualStyleBackColor = true;
this.buttonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click);
//
// buttonAddMap
//
this.buttonAddMap.Location = new System.Drawing.Point(3, 77);
this.buttonAddMap.Name = "buttonAddMap";
this.buttonAddMap.Size = new System.Drawing.Size(155, 31);
this.buttonAddMap.TabIndex = 7;
this.buttonAddMap.Text = "Добавить карту";
this.buttonAddMap.UseVisualStyleBackColor = true;
this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click);
//
// listBoxMaps
//
this.listBoxMaps.FormattingEnabled = true;
this.listBoxMaps.ItemHeight = 15;
this.listBoxMaps.Location = new System.Drawing.Point(3, 114);
this.listBoxMaps.Name = "listBoxMaps";
this.listBoxMaps.Size = new System.Drawing.Size(158, 94);
this.listBoxMaps.TabIndex = 8;
this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged);
//
// textBoxMap
//
this.textBoxMap.Location = new System.Drawing.Point(3, 19);
this.textBoxMap.Name = "textBoxMap";
this.textBoxMap.Size = new System.Drawing.Size(164, 23);
this.textBoxMap.TabIndex = 6;
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Первая карта",
"Вторая карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(4, 48);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(157, 23);
this.comboBoxSelectorMap.TabIndex = 0;
//
// buttonAddWarship
//
this.buttonAddWarship.Location = new System.Drawing.Point(18, 344);
this.buttonAddWarship.Name = "buttonAddWarship";
this.buttonAddWarship.Size = new System.Drawing.Size(163, 23);
this.buttonAddWarship.TabIndex = 3;
this.buttonAddWarship.Text = "Добавить корабль";
this.buttonAddWarship.UseVisualStyleBackColor = true;
this.buttonAddWarship.Click += new System.EventHandler(this.ButtonAddWarship_Click);
//
// buttonShowOnMap // buttonShowOnMap
// //
this.buttonShowOnMap.Location = new System.Drawing.Point(15, 509); this.buttonShowOnMap.Location = new System.Drawing.Point(25, 317);
this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(163, 23); this.buttonShowOnMap.Size = new System.Drawing.Size(163, 23);
this.buttonShowOnMap.TabIndex = 7; this.buttonShowOnMap.TabIndex = 7;
@ -148,19 +69,9 @@
this.buttonShowOnMap.UseVisualStyleBackColor = true; this.buttonShowOnMap.UseVisualStyleBackColor = true;
this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click);
// //
// buttonRemoveWarship
//
this.buttonRemoveWarship.Location = new System.Drawing.Point(15, 402);
this.buttonRemoveWarship.Name = "buttonRemoveWarship";
this.buttonRemoveWarship.Size = new System.Drawing.Size(163, 23);
this.buttonRemoveWarship.TabIndex = 5;
this.buttonRemoveWarship.Text = "Удалить корабль";
this.buttonRemoveWarship.UseVisualStyleBackColor = true;
this.buttonRemoveWarship.Click += new System.EventHandler(this.ButtonRemoveWarship_Click);
//
// buttonShowStorage // buttonShowStorage
// //
this.buttonShowStorage.Location = new System.Drawing.Point(18, 480); this.buttonShowStorage.Location = new System.Drawing.Point(25, 253);
this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(163, 23); this.buttonShowStorage.Size = new System.Drawing.Size(163, 23);
this.buttonShowStorage.TabIndex = 6; this.buttonShowStorage.TabIndex = 6;
@ -168,19 +79,51 @@
this.buttonShowStorage.UseVisualStyleBackColor = true; this.buttonShowStorage.UseVisualStyleBackColor = true;
this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click);
// //
// buttonRemoveWarship
//
this.buttonRemoveWarship.Location = new System.Drawing.Point(25, 188);
this.buttonRemoveWarship.Name = "buttonRemoveWarship";
this.buttonRemoveWarship.Size = new System.Drawing.Size(163, 23);
this.buttonRemoveWarship.TabIndex = 5;
this.buttonRemoveWarship.Text = "Удалить корабль";
this.buttonRemoveWarship.UseVisualStyleBackColor = true;
this.buttonRemoveWarship.Click += new System.EventHandler(this.ButtonRemoveWarship_Click);
//
// maskedTextBoxPosition // maskedTextBoxPosition
// //
this.maskedTextBoxPosition.Location = new System.Drawing.Point(19, 373); this.maskedTextBoxPosition.Location = new System.Drawing.Point(25, 123);
this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(163, 23); this.maskedTextBoxPosition.Size = new System.Drawing.Size(163, 23);
this.maskedTextBoxPosition.TabIndex = 4; this.maskedTextBoxPosition.TabIndex = 4;
// //
// buttonAddWarship
//
this.buttonAddWarship.Location = new System.Drawing.Point(25, 62);
this.buttonAddWarship.Name = "buttonAddWarship";
this.buttonAddWarship.Size = new System.Drawing.Size(163, 23);
this.buttonAddWarship.TabIndex = 3;
this.buttonAddWarship.Text = "Добавить корабль";
this.buttonAddWarship.UseVisualStyleBackColor = true;
this.buttonAddWarship.Click += new System.EventHandler(this.ButtonAddWarship_Click);
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Первая карта",
"Вторая карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(25, 22);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(163, 23);
this.comboBoxSelectorMap.TabIndex = 2;
this.comboBoxSelectorMap.Click += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
//
// pictureBox1 // pictureBox1
// //
this.pictureBox1.Location = new System.Drawing.Point(3, 12); this.pictureBox1.Location = new System.Drawing.Point(3, 12);
this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(197, 546); this.pictureBox1.Size = new System.Drawing.Size(197, 438);
this.pictureBox1.TabIndex = 0; this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false; this.pictureBox1.TabStop = false;
// //
@ -189,7 +132,7 @@
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0); this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Name = "pictureBox"; this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(911, 597); this.pictureBox.Size = new System.Drawing.Size(600, 450);
this.pictureBox.TabIndex = 1; this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false; this.pictureBox.TabStop = false;
// //
@ -197,15 +140,13 @@
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1111, 597); this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.pictureBox); this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.groupBoxTools);
this.Name = "FormMapWithSetWarships"; this.Name = "FormMapWithSetWarships";
this.Text = "FormMapWithSetWarships"; this.Text = "FormMapWithSetWarships";
this.groupBoxTools.ResumeLayout(false); this.groupBoxTools.ResumeLayout(false);
this.groupBoxTools.PerformLayout(); this.groupBoxTools.PerformLayout();
this.groupBoxMaps.ResumeLayout(false);
this.groupBoxMaps.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
@ -223,10 +164,5 @@
private Button buttonShowStorage; private Button buttonShowStorage;
private Button buttonRemoveWarship; private Button buttonRemoveWarship;
private MaskedTextBox maskedTextBoxPosition; private MaskedTextBox maskedTextBoxPosition;
private GroupBox groupBoxMaps;
private TextBox textBoxMap;
private Button buttonAddMap;
private ListBox listBoxMaps;
private Button buttonDeleteMap;
} }
} }

View File

@ -12,46 +12,40 @@ namespace Warship
{ {
public partial class FormMapWithSetWarships : Form public partial class FormMapWithSetWarships : Form
{ {
private MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap> _mapWarshipsCollectionGeneric;
private readonly Dictionary<string, AbstractMap> _mapDict = new()
{
{"Первая карта",new SimpleMap() },
{"Вторая карта",new SecondMap() }
};
private readonly MapsCollection _mapsCollection;
public FormMapWithSetWarships() public FormMapWithSetWarships()
{ {
InitializeComponent(); InitializeComponent();
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
comboBoxSelectorMap.Items.Clear();
foreach (var elem in _mapDict)
{
comboBoxSelectorMap.Items.Add(elem.Key);
}
} }
private void ReloadMaps() private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{ {
int index = listBoxMaps.SelectedIndex; AbstractMap map = null;
for (int i = 0; i < _mapsCollection.Keys.Count; i++)
switch (comboBoxSelectorMap.Text)
{ {
listBoxMaps.Items.Add(_mapsCollection.Keys[i]); case "Первая карта":
map = new SimpleMap();
break;
case "Вторая карта":
map = new SecondMap();
break;
} }
if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count)) if (map != null)
{ {
listBoxMaps.SelectedIndex = 0; _mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
} }
else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count) else
{ {
listBoxMaps.SelectedIndex = index; _mapWarshipsCollectionGeneric = null;
} }
} }
private void ButtonAddWarship_Click(object sender, EventArgs e) private void ButtonAddWarship_Click(object sender, EventArgs e)
{ {
if (listBoxMaps.SelectedIndex == -1) if (_mapWarshipsCollectionGeneric == null)
{ {
return; return;
} }
@ -59,10 +53,10 @@ namespace Warship
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
DrawingObjectWarship warship = new(form.SelectedWarship); DrawingObjectWarship warship = new(form.SelectedWarship);
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + warship != -1) if (_mapWarshipsCollectionGeneric + warship >= 0)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet();
} }
else else
{ {
@ -73,10 +67,6 @@ namespace Warship
private void ButtonRemoveWarship_Click(object sender, EventArgs e) private void ButtonRemoveWarship_Click(object sender, EventArgs e)
{ {
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{ {
return; return;
@ -86,10 +76,10 @@ namespace Warship
return; return;
} }
int pos = Convert.ToInt32(maskedTextBoxPosition.Text); int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) if (_mapWarshipsCollectionGeneric - pos != null)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet();
} }
else else
{ {
@ -99,25 +89,25 @@ namespace Warship
private void ButtonShowStorage_Click(object sender, EventArgs e) private void ButtonShowStorage_Click(object sender, EventArgs e)
{ {
if (listBoxMaps.SelectedIndex == -1) if (_mapWarshipsCollectionGeneric == null)
{ {
return; return;
} }
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet();
} }
private void ButtonShowOnMap_Click(object sender, EventArgs e) private void ButtonShowOnMap_Click(object sender, EventArgs e)
{ {
if (listBoxMaps.SelectedIndex == -1) if (_mapWarshipsCollectionGeneric == null)
{ {
return; return;
} }
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); pictureBox.Image = _mapWarshipsCollectionGeneric.ShowOnMap();
} }
private void ButtonMove_Click(object sender, EventArgs e) private void ButtonMove_Click(object sender, EventArgs e)
{ {
if (listBoxMaps.SelectedIndex == -1) if (_mapWarshipsCollectionGeneric == null)
{ {
return; return;
} }
@ -138,43 +128,7 @@ namespace Warship
dir = Direction.Right; dir = Direction.Right;
break; break;
} }
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); pictureBox.Image = _mapWarshipsCollectionGeneric.MoveObject(dir);
}
private void ButtonAddMap_Click(object sender, EventArgs e)
{
if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxMap.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!_mapDict.ContainsKey(comboBoxSelectorMap.Text))
{
MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
listBoxMaps.Items.Clear();
_mapsCollection.AddMap(textBoxMap.Text, _mapDict[comboBoxSelectorMap.Text]);
ReloadMaps();
}
private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
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);
listBoxMaps.Items.Clear();
ReloadMaps();
}
} }
} }
} }

View File

@ -49,10 +49,14 @@ namespace Warship
public Bitmap ShowOnMap() public Bitmap ShowOnMap()
{ {
Shaking(); Shaking();
foreach (var warship in _setWarship.GetWarships()) for (int i = 0; i < _setWarship.Count; i++)
{
var warship = _setWarship.Get(i);
if (warship != null)
{ {
return _map.CreateMap(_pictureWidth, _pictureHeight, warship); return _map.CreateMap(_pictureWidth, _pictureHeight, warship);
} }
}
return new(_pictureWidth, _pictureHeight); return new(_pictureWidth, _pictureHeight);
} }
@ -70,11 +74,11 @@ namespace Warship
int j = _setWarship.Count - 1; int j = _setWarship.Count - 1;
for (int i = 0; i < _setWarship.Count; i++) for (int i = 0; i < _setWarship.Count; i++)
{ {
if (_setWarship[i] == null) if (_setWarship.Get(i) == null)
{ {
for (; j > i; j--) for (; j > i; j--)
{ {
var warship = _setWarship[j]; var warship = _setWarship.Get(j);
if (warship != null) if (warship != null)
{ {
_setWarship.Insert(warship, i); _setWarship.Insert(warship, i);
@ -109,13 +113,14 @@ namespace Warship
{ {
int width = _pictureWidth / _placeSizeWidth; int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight; int height = _pictureHeight / _placeSizeHeight;
int i = 0;
foreach (var warship in _setWarship.GetWarships()) for (int i = 0; i < _setWarship.Count; i++)
{ {
warship.SetObject(i % width * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight, _pictureWidth, _pictureHeight); if (_setWarship.Get(i) != null)
warship.DrawingObject(gr); {
i++; _setWarship.Get(i).SetObject(i % width * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight, _pictureWidth, _pictureHeight);
_setWarship.Get(i)?.DrawingObject(gr);
}
} }
} }
} }

View File

@ -1,55 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Warship
{
internal class MapsCollection
{
readonly Dictionary<string, MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap>> _mapStorages;
public List<string> Keys => _mapStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string, MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddMap(string name, AbstractMap map)
{
if (_mapStorages.ContainsKey(name))
{
MessageBox.Show("Карта уже существует");
return;
}
else
{
_mapStorages.Add(name, new MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
}
public void DelMap(string name)
{
_mapStorages.Remove(name);
}
public MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap> this[string ind]
{
get
{
if (_mapStorages.ContainsKey(ind))
return _mapStorages[ind];
return null;
}
}
}
}

View File

@ -9,73 +9,69 @@ namespace Warship
internal class SetWarshipsGeneric<T> internal class SetWarshipsGeneric<T>
where T : class where T : class
{ {
private readonly List<T> _places; private readonly T[] _places;
public int Count => _places.Count; public int Count => _places.Length;
private readonly int _maxCount;
public SetWarshipsGeneric(int count) public SetWarshipsGeneric(int count)
{ {
_maxCount = count; _places = new T[count];
_places = new List<T>();
} }
public int Insert(T warship) public int Insert(T warship)
{ {
if (_places.Count + 1 >= _maxCount) return Insert(warship, 0);
return -1;
_places.Insert(0, warship);
return 0;
} }
public int Insert(T warship, int position) public int Insert(T warship, int position)
{ {
if (position >= _maxCount || position < 0) int EmptyEl = -1;
if (position >= Count || position < 0)
return -1; return -1;
if (_places.Count + 1 >= _maxCount)
if (_places[position] == null)
{
_places[position] = warship;
return 1;
}
else if (_places[position] != null)
{
for (int i = position + 1; i < Count; i++)
if (_places[i] == null)
{
EmptyEl = i;
break;
}
if (EmptyEl == -1)
return -1; return -1;
_places.Insert(position, warship);
return position; for (int i = EmptyEl; i > position; i--)
_places[i] = _places[i - 1];
}
_places[position] = warship;
return 1;
} }
public T Remove(int position) public T Remove(int position)
{ {
if (position >= _maxCount || position < 0) if (position >= Count || position < 0 || _places[position] == null)
return null; return null;
T deleted = _places[position]; T deleted = _places[position];
_places.RemoveAt(position); _places[position] = null;
return deleted; return deleted;
} }
public T this[int position] public T Get(int position)
{ {
get if (position >= Count || position < 0)
{
if (position < 0 || position >= _maxCount)
return null; return null;
return _places[position]; 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;
}
}
}
} }
} }