Merge pull request 'Nevaeva K. A. LabWork03' (#3) from Lab_3 into Lab_2

Reviewed-on: http://student.git.athene.tech/ksenia_nevaeva/PIbd-23_Nevaeva_K.A._ContainerShip_Base/pulls/3
This commit is contained in:
Евгений Эгов 2022-11-07 10:55:09 +04:00
commit fbb0605bca
12 changed files with 699 additions and 297 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace ContainerShip namespace ContainerShip
{ {
internal enum Direction public enum Direction
{ {
None = 0, None = 0,
Up = 1, Up = 1,

View File

@ -7,7 +7,7 @@ using static ContainerShip.EntityShip;
namespace ContainerShip namespace ContainerShip
{ {
internal class DrawingShip public class DrawingShip
{ {
public EntityShip Ship { protected set; get; } public EntityShip Ship { protected set; get; }
protected float _startPosX; protected float _startPosX;

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace ContainerShip namespace ContainerShip
{ {
internal class EntityShip public class EntityShip
{ {
/// <summary> /// <summary>
/// Скорость /// Скорость

View File

@ -1,102 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ContainerShip
{
public partial class FormMap : Form
{
private DrawingShip _ship;
private AbstractMap _abstractMap;
public FormMap()
{
InitializeComponent();
_abstractMap = new SimpleMap();
}
/// <summary>
/// Изменение размеров формы
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SetData(DrawingShip ship)
{
toolStripStatusLabelSpeed.Text = $"Скорость: {ship.Ship.Speed}";
toolStripStatusLabelWeight.Text = $"Вес: {ship.Ship.Weight}";
toolStripStatusLabelBodyColor.Text = $"Цвет: {ship.Ship.BodyColor.Name}";
pictureBoxShip.Image = _abstractMap.CreateMap(pictureBoxShip.Width, pictureBoxShip.Height,
new DrawingObjectShip(ship));
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random rnd = new();
var ship = new DrawingShip(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
SetData(ship);
}
/// <summary>
/// Изменение размеров формы
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
//получаем имя кнопки
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (name)
{
case "buttonUp":
dir = Direction.Up;
break;
case "buttonDown":
dir = Direction.Down;
break;
case "buttonLeft":
dir = Direction.Left;
break;
case "buttonRight":
dir = Direction.Right;
break;
}
pictureBoxShip.Image = _abstractMap?.MoveObject(dir);
}
/// <summary>
/// Обработка нажатия кнопки "Модификация"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateModif_Click(object sender, EventArgs e)
{
Random rnd = new();
var ship = new DrawingContainerShip(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
SetData(ship);
}
/// <summary>
/// Смена карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBoxSelectorMap_SelectedIndexChanged_1(object sender, EventArgs e)
{
switch (ComboBoxSelectorMap.Text)
{
case "Простая карта":
_abstractMap = new SimpleMap();
break;
case "Модифицированная карта":
_abstractMap = new ModifyMap();
break;
}
}
}
}

View File

@ -1,6 +1,6 @@
namespace ContainerShip namespace ContainerShip
{ {
partial class FormMap partial class FormMapWithSetShips
{ {
/// <summary> /// <summary>
/// Required designer variable. /// Required designer variable.
@ -28,86 +28,97 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.statusStrip1 = new System.Windows.Forms.StatusStrip(); this.groupBox1 = new System.Windows.Forms.GroupBox();
this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel(); this.ButtonAddShip = new System.Windows.Forms.Button();
this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel(); this.ComboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.toolStripStatusLabelBodyColor = new System.Windows.Forms.ToolStripStatusLabel();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonUp = 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(); this.buttonDown = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button();
this.pictureBoxShip = new System.Windows.Forms.PictureBox(); this.button3 = new System.Windows.Forms.Button();
this.ButtonCreate = new System.Windows.Forms.Button(); this.ButtonShowStorage = new System.Windows.Forms.Button();
this.ButtonCreateModif = new System.Windows.Forms.Button(); this.ButtonRemoveShip = new System.Windows.Forms.Button();
this.ComboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.statusStrip1.SuspendLayout(); this.pictureBox = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).BeginInit(); this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
// statusStrip1 // groupBox1
// //
this.statusStrip1.ImageScalingSize = new System.Drawing.Size(24, 24); this.groupBox1.Controls.Add(this.ButtonAddShip);
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.groupBox1.Controls.Add(this.ComboBoxSelectorMap);
this.toolStripStatusLabelSpeed, this.groupBox1.Controls.Add(this.buttonUp);
this.toolStripStatusLabelWeight, this.groupBox1.Controls.Add(this.buttonRight);
this.toolStripStatusLabelBodyColor}); this.groupBox1.Controls.Add(this.buttonDown);
this.statusStrip1.Location = new System.Drawing.Point(0, 418); this.groupBox1.Controls.Add(this.buttonLeft);
this.statusStrip1.Name = "statusStrip1"; this.groupBox1.Controls.Add(this.button3);
this.statusStrip1.Size = new System.Drawing.Size(800, 32); this.groupBox1.Controls.Add(this.ButtonShowStorage);
this.statusStrip1.TabIndex = 0; this.groupBox1.Controls.Add(this.ButtonRemoveShip);
this.statusStrip1.Text = "statusStrip1"; this.groupBox1.Controls.Add(this.maskedTextBoxPosition);
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.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
// //
// toolStripStatusLabelSpeed // ButtonAddShip
// //
this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; this.ButtonAddShip.Location = new System.Drawing.Point(6, 74);
this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(93, 25); this.ButtonAddShip.Name = "ButtonAddShip";
this.toolStripStatusLabelSpeed.Text = "Скорость:"; 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);
// //
// toolStripStatusLabelWeight // ComboBoxSelectorMap
// //
this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; this.ComboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(43, 25); this.ComboBoxSelectorMap.Items.AddRange(new object[] {
this.toolStripStatusLabelWeight.Text = "Вес:"; "Простая карта",
// "Модифицированная карта"});
// toolStripStatusLabelBodyColor this.ComboBoxSelectorMap.Location = new System.Drawing.Point(6, 35);
// this.ComboBoxSelectorMap.Name = "ComboBoxSelectorMap";
this.toolStripStatusLabelBodyColor.Name = "toolStripStatusLabelBodyColor"; this.ComboBoxSelectorMap.Size = new System.Drawing.Size(288, 33);
this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(55, 25); this.ComboBoxSelectorMap.TabIndex = 10;
this.toolStripStatusLabelBodyColor.Text = "Цвет:"; this.ComboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
//
// buttonRight
//
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(738, 353);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(50, 50);
this.buttonRight.TabIndex = 2;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
// //
// buttonUp // buttonUp
// //
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); 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.BackgroundImage = global::ContainerShip.Properties.Resources.upArrow;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(682, 306); this.buttonUp.Location = new System.Drawing.Point(131, 390);
this.buttonUp.Name = "buttonUp"; this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(50, 50); this.buttonUp.Size = new System.Drawing.Size(50, 50);
this.buttonUp.TabIndex = 3; this.buttonUp.TabIndex = 9;
this.buttonUp.UseVisualStyleBackColor = true; this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click);
// //
// buttonRight
//
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.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(50, 50);
this.buttonRight.TabIndex = 8;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonDown // buttonDown
// //
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); 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.BackgroundImage = global::ContainerShip.Properties.Resources.DownArrow;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(682, 352); this.buttonDown.Location = new System.Drawing.Point(131, 446);
this.buttonDown.Name = "buttonDown"; this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(50, 50); this.buttonDown.Size = new System.Drawing.Size(50, 50);
this.buttonDown.TabIndex = 4; this.buttonDown.TabIndex = 7;
this.buttonDown.UseVisualStyleBackColor = true; this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
// //
@ -116,97 +127,89 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); 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.BackgroundImage = global::ContainerShip.Properties.Resources.RightArrow;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(626, 353); this.buttonLeft.Location = new System.Drawing.Point(75, 446);
this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(50, 50); this.buttonLeft.Size = new System.Drawing.Size(50, 50);
this.buttonLeft.TabIndex = 5; this.buttonLeft.TabIndex = 6;
this.buttonLeft.UseVisualStyleBackColor = true; this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
// //
// pictureBoxShip // button3
// //
this.pictureBoxShip.Dock = System.Windows.Forms.DockStyle.Fill; this.button3.Location = new System.Drawing.Point(8, 292);
this.pictureBoxShip.Location = new System.Drawing.Point(0, 0); this.button3.Name = "button3";
this.pictureBoxShip.Name = "pictureBoxShip"; this.button3.Size = new System.Drawing.Size(286, 34);
this.pictureBoxShip.Size = new System.Drawing.Size(800, 418); this.button3.TabIndex = 3;
this.pictureBoxShip.TabIndex = 7; this.button3.Text = "Посмотреть карту";
this.pictureBoxShip.TabStop = false; this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.ButtonShowOnMap_Click);
// //
// ButtonCreate // ButtonShowStorage
// //
this.ButtonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.ButtonShowStorage.Location = new System.Drawing.Point(6, 243);
this.ButtonCreate.BackColor = System.Drawing.SystemColors.Window; this.ButtonShowStorage.Name = "ButtonShowStorage";
this.ButtonCreate.Location = new System.Drawing.Point(12, 381); this.ButtonShowStorage.Size = new System.Drawing.Size(288, 34);
this.ButtonCreate.Name = "ButtonCreate"; this.ButtonShowStorage.TabIndex = 2;
this.ButtonCreate.Size = new System.Drawing.Size(112, 34); this.ButtonShowStorage.Text = "Просмотреть хранилище";
this.ButtonCreate.TabIndex = 8; this.ButtonShowStorage.UseVisualStyleBackColor = true;
this.ButtonCreate.Text = "Создать"; this.ButtonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click);
this.ButtonCreate.UseVisualStyleBackColor = false;
this.ButtonCreate.Click += new System.EventHandler(this.ButtonCreate_Click);
// //
// ButtonCreateModif // ButtonRemoveShip
// //
this.ButtonCreateModif.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.ButtonRemoveShip.Location = new System.Drawing.Point(6, 162);
this.ButtonCreateModif.BackColor = System.Drawing.SystemColors.Window; this.ButtonRemoveShip.Name = "ButtonRemoveShip";
this.ButtonCreateModif.Location = new System.Drawing.Point(130, 381); this.ButtonRemoveShip.Size = new System.Drawing.Size(288, 41);
this.ButtonCreateModif.Name = "ButtonCreateModif"; this.ButtonRemoveShip.TabIndex = 1;
this.ButtonCreateModif.Size = new System.Drawing.Size(143, 34); this.ButtonRemoveShip.Text = "Удалить корабль";
this.ButtonCreateModif.TabIndex = 9; this.ButtonRemoveShip.UseVisualStyleBackColor = true;
this.ButtonCreateModif.Text = "Модификация"; this.ButtonRemoveShip.Click += new System.EventHandler(this.ButtonRemoveShip_Click);
this.ButtonCreateModif.UseVisualStyleBackColor = false;
this.ButtonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click);
// //
// ComboBoxSelectorMap // maskedTextBoxPosition
// //
this.ComboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 125);
this.ComboBoxSelectorMap.FormattingEnabled = true; this.maskedTextBoxPosition.Mask = "00";
this.ComboBoxSelectorMap.Items.AddRange(new object[] { this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
"Простая карта", this.maskedTextBoxPosition.Size = new System.Drawing.Size(288, 31);
"Модифицированная карта"}); this.maskedTextBoxPosition.TabIndex = 0;
this.ComboBoxSelectorMap.Location = new System.Drawing.Point(279, 385);
this.ComboBoxSelectorMap.Name = "ComboBoxSelectorMap";
this.ComboBoxSelectorMap.Size = new System.Drawing.Size(182, 33);
this.ComboBoxSelectorMap.TabIndex = 10;
this.ComboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged_1);
// //
// FormMap // pictureBox
//
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.TabIndex = 1;
this.pictureBox.TabStop = false;
//
// FormMapWithSetShips
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450); this.ClientSize = new System.Drawing.Size(853, 508);
this.Controls.Add(this.ComboBoxSelectorMap); this.Controls.Add(this.pictureBox);
this.Controls.Add(this.ButtonCreateModif); this.Controls.Add(this.groupBox1);
this.Controls.Add(this.ButtonCreate); this.Name = "FormMapWithSetShips";
this.Controls.Add(this.buttonLeft); this.Text = "Карта с набором объектов";
this.Controls.Add(this.buttonDown); this.groupBox1.ResumeLayout(false);
this.Controls.Add(this.buttonUp); this.groupBox1.PerformLayout();
this.Controls.Add(this.buttonRight); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.Controls.Add(this.pictureBoxShip);
this.Controls.Add(this.statusStrip1);
this.Name = "FormMap";
this.Text = "Карта";
this.Click += new System.EventHandler(this.ButtonMove_Click);
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout();
} }
#endregion #endregion
private StatusStrip statusStrip1; private GroupBox groupBox1;
private Button buttonRight; private Button button3;
private Button buttonUp; private Button ButtonShowStorage;
private Button buttonDown; private Button ButtonRemoveShip;
private MaskedTextBox maskedTextBoxPosition;
private PictureBox pictureBox;
private Button buttonLeft; private Button buttonLeft;
private ToolStripStatusLabel toolStripStatusLabelSpeed; private Button buttonDown;
private ToolStripStatusLabel toolStripStatusLabelWeight; private Button buttonUp;
private ToolStripStatusLabel toolStripStatusLabelBodyColor; private Button buttonRight;
private PictureBox pictureBoxShip; private Button ButtonAddShip;
private Button ButtonCreate;
private Button ButtonCreateModif;
private ComboBox ComboBoxSelectorMap; private ComboBox ComboBoxSelectorMap;
} }
} }

View File

@ -0,0 +1,158 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.DataFormats;
namespace ContainerShip
{
public partial class FormMapWithSetShips : Form
{
/// <summary>
/// Объект от класса карты с набором объектов
/// </summary>
private MapWithSetShipsGeneric<DrawingObjectShip, AbstractMap> _mapShipsCollectionGeneric;
/// <summary>
/// Конструктор
/// </summary>
public FormMapWithSetShips()
{
InitializeComponent();
}
/// <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)
{
return;
}
FormShip form = new();
if (form.ShowDialog() == DialogResult.OK)
{
DrawingObjectShip ship = new(form.SelectedShip);
if (_mapShipsCollectionGeneric + ship != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapShipsCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
/// <summary>
/// Удаление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemoveShip_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapShipsCollectionGeneric - pos != null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapShipsCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Вывод набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonShowStorage_Click(object sender, EventArgs e)
{
if (_mapShipsCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapShipsCollectionGeneric.ShowSet();
}
/// <summary>
/// Вывод карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonShowOnMap_Click(object sender, EventArgs e)
{
if (_mapShipsCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapShipsCollectionGeneric.ShowOnMap();
}
/// <summary>
/// Перемещение
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_mapShipsCollectionGeneric == null)
{
return;
}
//получаем имя кнопки
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (name)
{
case "buttonUp":
dir = Direction.Up;
break;
case "buttonDown":
dir = Direction.Down;
break;
case "buttonLeft":
dir = Direction.Left;
break;
case "buttonRight":
dir = Direction.Right;
break;
}
pictureBox.Image = _mapShipsCollectionGeneric.MoveObject(dir);
}
}
}

View File

@ -57,7 +57,4 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<metadata name="statusStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root> </root>

View File

@ -32,13 +32,14 @@
this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelBodyColor = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripStatusLabelBodyColor = new System.Windows.Forms.ToolStripStatusLabel();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
this.pictureBoxShip = new System.Windows.Forms.PictureBox(); this.pictureBoxShip = new System.Windows.Forms.PictureBox();
this.ButtonCreate = new System.Windows.Forms.Button(); this.ButtonCreate = new System.Windows.Forms.Button();
this.ButtonCreateModif = new System.Windows.Forms.Button(); this.ButtonCreateModif = new System.Windows.Forms.Button();
this.ButtonSelectShip = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.statusStrip1.SuspendLayout(); this.statusStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
@ -61,66 +62,21 @@
this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed";
this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(93, 25); this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(93, 25);
this.toolStripStatusLabelSpeed.Text = "Скорость:"; this.toolStripStatusLabelSpeed.Text = "Скорость:";
this.toolStripStatusLabelSpeed.Click += new System.EventHandler(this.toolStripStatusLabelSpeed_Click);
// //
// toolStripStatusLabelWeight // toolStripStatusLabelWeight
// //
this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight";
this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(43, 25); this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(43, 25);
this.toolStripStatusLabelWeight.Text = "Вес:"; this.toolStripStatusLabelWeight.Text = "Вес:";
this.toolStripStatusLabelWeight.Click += new System.EventHandler(this.toolStripStatusLabelWeight_Click);
// //
// toolStripStatusLabelBodyColor // toolStripStatusLabelBodyColor
// //
this.toolStripStatusLabelBodyColor.Name = "toolStripStatusLabelBodyColor"; this.toolStripStatusLabelBodyColor.Name = "toolStripStatusLabelBodyColor";
this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(55, 25); this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(55, 25);
this.toolStripStatusLabelBodyColor.Text = "Цвет:"; this.toolStripStatusLabelBodyColor.Text = "Цвет:";
// this.toolStripStatusLabelBodyColor.Click += new System.EventHandler(this.toolStripStatusLabelBodyColor_Click);
// buttonRight
//
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(738, 353);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(50, 50);
this.buttonRight.TabIndex = 2;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_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(682, 306);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(50, 50);
this.buttonUp.TabIndex = 3;
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonDown
//
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(682, 352);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(50, 50);
this.buttonDown.TabIndex = 4;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonLeft
//
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(626, 353);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(50, 50);
this.buttonLeft.TabIndex = 5;
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
// //
// pictureBoxShip // pictureBoxShip
// //
@ -130,6 +86,7 @@
this.pictureBoxShip.Size = new System.Drawing.Size(800, 418); this.pictureBoxShip.Size = new System.Drawing.Size(800, 418);
this.pictureBoxShip.TabIndex = 7; this.pictureBoxShip.TabIndex = 7;
this.pictureBoxShip.TabStop = false; this.pictureBoxShip.TabStop = false;
this.pictureBoxShip.Click += new System.EventHandler(this.pictureBoxShip_Click);
// //
// ButtonCreate // ButtonCreate
// //
@ -155,11 +112,72 @@
this.ButtonCreateModif.UseVisualStyleBackColor = false; this.ButtonCreateModif.UseVisualStyleBackColor = false;
this.ButtonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click); this.ButtonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click);
// //
// ButtonSelectShip
//
this.ButtonSelectShip.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.ButtonSelectShip.BackColor = System.Drawing.SystemColors.Window;
this.ButtonSelectShip.Location = new System.Drawing.Point(479, 381);
this.ButtonSelectShip.Name = "ButtonSelectShip";
this.ButtonSelectShip.Size = new System.Drawing.Size(112, 34);
this.ButtonSelectShip.TabIndex = 10;
this.ButtonSelectShip.Text = "Выбрать";
this.ButtonSelectShip.UseVisualStyleBackColor = false;
this.ButtonSelectShip.Click += new System.EventHandler(this.ButtonSelectShip_Click);
//
// buttonRight
//
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(738, 362);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(50, 50);
this.buttonRight.TabIndex = 2;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonLeft
//
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(626, 362);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(50, 50);
this.buttonLeft.TabIndex = 5;
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_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(682, 306);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(50, 50);
this.buttonUp.TabIndex = 3;
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonDown
//
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(682, 362);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(50, 50);
this.buttonDown.TabIndex = 4;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
//
// FormShip // FormShip
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450); this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.ButtonSelectShip);
this.Controls.Add(this.ButtonCreateModif); this.Controls.Add(this.ButtonCreateModif);
this.Controls.Add(this.ButtonCreate); this.Controls.Add(this.ButtonCreate);
this.Controls.Add(this.buttonLeft); this.Controls.Add(this.buttonLeft);
@ -182,15 +200,16 @@
#endregion #endregion
private StatusStrip statusStrip1; private StatusStrip statusStrip1;
private Button buttonRight;
private Button buttonUp;
private Button buttonDown;
private Button buttonLeft;
private ToolStripStatusLabel toolStripStatusLabelSpeed; private ToolStripStatusLabel toolStripStatusLabelSpeed;
private ToolStripStatusLabel toolStripStatusLabelWeight; private ToolStripStatusLabel toolStripStatusLabelWeight;
private ToolStripStatusLabel toolStripStatusLabelBodyColor; private ToolStripStatusLabel toolStripStatusLabelBodyColor;
private PictureBox pictureBoxShip; private PictureBox pictureBoxShip;
private Button ButtonCreate; private Button ButtonCreate;
private Button ButtonCreateModif; private Button ButtonCreateModif;
private Button ButtonSelectShip;
private Button buttonRight;
private Button buttonLeft;
private Button buttonUp;
private Button buttonDown;
} }
} }

View File

@ -1,8 +1,14 @@
using System.Drawing;
namespace ContainerShip namespace ContainerShip
{ {
public partial class FormShip : Form public partial class FormShip : Form
{ {
private DrawingShip _ship; private DrawingShip _ship;
/// <summary>
/// âűáđŕííűé îáőĺęň
/// </summary>
public DrawingShip SelectedShip { get; private set; }
public FormShip() public FormShip()
{ {
InitializeComponent(); InitializeComponent();
@ -71,25 +77,63 @@ namespace ContainerShip
} }
private void ButtonCreate_Click(object sender, EventArgs e) private void ButtonCreate_Click(object sender, EventArgs e)
{ {
Random rnd = new(); Random rnd = new();
_ship = new DrawingShip(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
_ship.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxShip.Width, pictureBoxShip.Height); ColorDialog dialog = new();
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_ship.Ship.Speed}"; if (dialog.ShowDialog() == DialogResult.OK)
toolStripStatusLabelWeight.Text = $"Âåñ: {_ship.Ship.Weight}"; {
toolStripStatusLabelBodyColor.Text = $"Öâåò: {_ship.Ship.BodyColor.Name}"; color = dialog.Color;
Draw(); }
_ship = new DrawingShip(rnd.Next(100, 300), rnd.Next(1000, 2000), color);
SetData();
Draw();
} }
private void ButtonCreateModif_Click(object sender, EventArgs e) private void ButtonCreateModif_Click(object sender, EventArgs e)
{ {
Random rnd = new(); Random rnd = new();
_ship = new DrawingContainerShip(rnd.Next(100, 300), rnd.Next(1000, 2000), Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
Color.FromArgb(rnd.Next(0,256), rnd.Next(0, 256), rnd.Next(0, 256)), ColorDialog dialog = new();
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), if (dialog.ShowDialog() == DialogResult.OK)
Convert.ToBoolean(rnd.Next(0,2)), Convert.ToBoolean(rnd.Next(0,2))); {
color = dialog.Color;
}
Color dopColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialogDop = new();
if (dialogDop.ShowDialog() == DialogResult.OK)
{
dopColor = dialogDop.Color;
}
_ship = new DrawingContainerShip(rnd.Next(100, 300), rnd.Next(1000, 2000), color, dopColor,
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
SetData(); SetData();
Draw(); Draw();
} }
private void ButtonSelectShip_Click(object sender, EventArgs e)
{
SelectedShip = _ship;
DialogResult = DialogResult.OK;
}
private void pictureBoxShip_Click(object sender, EventArgs e)
{
}
private void toolStripStatusLabelBodyColor_Click(object sender, EventArgs e)
{
}
private void toolStripStatusLabelWeight_Click(object sender, EventArgs e)
{
}
private void toolStripStatusLabelSpeed_Click(object sender, EventArgs e)
{
}
} }
} }

View File

@ -0,0 +1,173 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContainerShip
{
internal class MapWithSetShipsGeneric<T, U>
where T : class, IDrawingObject
where U : AbstractMap
{
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Размер занимаемого объектом места (ширина)
/// </summary>
private readonly int _placeSizeWidth = 135;
/// <summary>
/// Размер занимаемого объектом места (высота)
/// </summary>
private readonly int _placeSizeHeight = 70;
/// <summary>
/// Набор объектов
/// </summary>
private readonly SetShipsGeneric<T> _setShips;
/// <summary>
/// Карта
/// </summary>
private readonly U _map;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
/// <param name="map"></param>
public MapWithSetShipsGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setShips = new SetShipsGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawShips(gr);
return bmp;
}
/// <summary>
/// Просмотр объекта на карте
/// </summary>
/// <returns></returns>
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setShips.Count; i++)
{
var ship = _setShips.Get(i);
if (ship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, ship);
}
}
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 = _setShips.Count - 1;
for (int i = 0; i < _setShips.Count; i++)
{
if (_setShips.Get(i) == null)
{
for (; j > i; j--)
{
var ship = _setShips.Get(j);
if (ship != null)
{
_setShips.Insert(ship, i);
_setShips.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
/// <summary>
/// Метод отрисовки фона
/// </summary>
/// <param name="g"></param>
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Brown, 3);
g.FillRectangle(Brushes.Aqua, 0, 0, _pictureWidth, _pictureHeight);
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 DrawShips(Graphics g)
{
for (int i = 0; i < _setShips.Count; i++)
{
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);
}
}
/// <summary>
/// Перегрузка операторов
/// </summary>
/// <param name="map"></param>
/// <param name="ship"></param>
/// <returns></returns>
public static int operator +(MapWithSetShipsGeneric<T, U> map, T ship)
{
return map._setShips.Insert(ship);
}
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="map"></param>
/// <param name="position"></param>
/// <returns></returns>
public static T operator -(MapWithSetShipsGeneric<T, U> map, int position)
{
return map._setShips.Remove(position);
}
}
}

View File

@ -11,7 +11,7 @@ namespace ContainerShip
// 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 FormMapWithSetShips());
} }
} }
} }

View File

@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContainerShip
{
internal class SetShipsGeneric<T>
where T: class
{
/// <summary>
/// Массив объектов, которые храним
/// </summary>
private readonly T[] _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetShipsGeneric(int count)
{
_places = new T[count];
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <returns></returns>
public int Insert(T ship)
{
return Insert(ship, 0);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="ship">Добавляемый корабль</param>
/// <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;
}
}
return position;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Remove(int position)
{
if (position < 0 || position >= _places.Length)
{
return null;
}
T ship = _places[position];
_places[position] = null;
return ship;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position)
{
if (position < 0 || position >= _places.Length)
{
return null;
}
return _places[position];
}
}
}