Перегрузка операторов.

This commit is contained in:
ksenianeva 2022-11-01 12:19:04 +04:00
parent 55300032aa
commit 6344340950
6 changed files with 155 additions and 110 deletions

View File

@ -72,6 +72,7 @@
this.ButtonAddShip.TabIndex = 11;
this.ButtonAddShip.Text = "Добавить корабль";
this.ButtonAddShip.UseVisualStyleBackColor = true;
this.ButtonAddShip.Click += new System.EventHandler(this.ButtonAddShip_Click);
//
// ComboBoxSelectorMap
//
@ -83,6 +84,7 @@
this.ComboBoxSelectorMap.Name = "ComboBoxSelectorMap";
this.ComboBoxSelectorMap.Size = new System.Drawing.Size(288, 33);
this.ComboBoxSelectorMap.TabIndex = 10;
this.ComboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
//
// buttonUp
//
@ -94,6 +96,7 @@
this.buttonUp.Size = new System.Drawing.Size(50, 50);
this.buttonUp.TabIndex = 9;
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonRight
//
@ -105,6 +108,7 @@
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
//
@ -116,6 +120,7 @@
this.buttonDown.Size = new System.Drawing.Size(50, 50);
this.buttonDown.TabIndex = 7;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonLeft
//
@ -127,6 +132,7 @@
this.buttonLeft.Size = new System.Drawing.Size(50, 50);
this.buttonLeft.TabIndex = 6;
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
//
// button3
//
@ -136,6 +142,7 @@
this.button3.TabIndex = 3;
this.button3.Text = "Посмотреть карту";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.ButtonShowOnMap_Click);
//
// ButtonShowStorage
//
@ -145,6 +152,7 @@
this.ButtonShowStorage.TabIndex = 2;
this.ButtonShowStorage.Text = "Просмотреть хранилище";
this.ButtonShowStorage.UseVisualStyleBackColor = true;
this.ButtonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click);
//
// ButtonRemoveShip
//
@ -154,6 +162,7 @@
this.ButtonRemoveShip.TabIndex = 1;
this.ButtonRemoveShip.Text = "Удалить корабль";
this.ButtonRemoveShip.UseVisualStyleBackColor = true;
this.ButtonRemoveShip.Click += new System.EventHandler(this.ButtonRemoveShip_Click);
//
// maskedTextBoxPosition
//

View File

@ -25,10 +25,8 @@ namespace ContainerShip
InitializeComponent();
}
/// <summary>
/// Выбор карты
/// Добавление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{
AbstractMap map = null;
@ -43,17 +41,13 @@ namespace ContainerShip
}
if (map != null)
{
_mapShipsCollectionGeneric = new MapWithSetShipsGeneric<DrawingObjectShip, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
_mapShipsCollectionGeneric = new MapWithSetShipsGeneric<DrawingObjectShip, AbstractMap>(pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapShipsCollectionGeneric = null;
}
{
_mapShipsCollectionGeneric = null;
}
}
/// <summary>
/// Добавление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddShip_Click(object sender, EventArgs e)
@ -65,8 +59,8 @@ namespace ContainerShip
FormShip form = new();
if (form.ShowDialog() == DialogResult.OK)
{
DrawingObjectShip car = new(form.SelectedShip);
if (_mapShipsCollectionGeneric + car)
DrawingObjectShip ship = new(form.SelectedShip);
if (_mapShipsCollectionGeneric + ship != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapShipsCollectionGeneric.ShowSet();
@ -93,7 +87,7 @@ namespace ContainerShip
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapShipsCollectionGeneric - pos)
if (_mapShipsCollectionGeneric - pos != null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapShipsCollectionGeneric.ShowSet();

View File

@ -32,14 +32,14 @@
this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelWeight = 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.ButtonCreate = 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();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).BeginInit();
this.SuspendLayout();
@ -62,66 +62,21 @@
this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed";
this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(93, 25);
this.toolStripStatusLabelSpeed.Text = "Скорость:";
this.toolStripStatusLabelSpeed.Click += new System.EventHandler(this.toolStripStatusLabelSpeed_Click);
//
// toolStripStatusLabelWeight
//
this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight";
this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(43, 25);
this.toolStripStatusLabelWeight.Text = "Вес:";
this.toolStripStatusLabelWeight.Click += new System.EventHandler(this.toolStripStatusLabelWeight_Click);
//
// toolStripStatusLabelBodyColor
//
this.toolStripStatusLabelBodyColor.Name = "toolStripStatusLabelBodyColor";
this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(55, 25);
this.toolStripStatusLabelBodyColor.Text = "Цвет:";
//
// 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);
//
// 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);
//
// 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);
this.toolStripStatusLabelBodyColor.Click += new System.EventHandler(this.toolStripStatusLabelBodyColor_Click);
//
// pictureBoxShip
//
@ -131,6 +86,7 @@
this.pictureBoxShip.Size = new System.Drawing.Size(800, 418);
this.pictureBoxShip.TabIndex = 7;
this.pictureBoxShip.TabStop = false;
this.pictureBoxShip.Click += new System.EventHandler(this.pictureBoxShip_Click);
//
// ButtonCreate
//
@ -168,6 +124,54 @@
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
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
@ -196,10 +200,6 @@
#endregion
private StatusStrip statusStrip1;
private Button buttonRight;
private Button buttonUp;
private Button buttonDown;
private Button buttonLeft;
private ToolStripStatusLabel toolStripStatusLabelSpeed;
private ToolStripStatusLabel toolStripStatusLabelWeight;
private ToolStripStatusLabel toolStripStatusLabelBodyColor;
@ -207,5 +207,9 @@
private Button ButtonCreate;
private Button ButtonCreateModif;
private Button ButtonSelectShip;
private Button buttonRight;
private Button buttonLeft;
private Button buttonUp;
private Button buttonDown;
}
}

View File

@ -1,3 +1,5 @@
using System.Drawing;
namespace ContainerShip
{
public partial class FormShip : Form
@ -75,22 +77,35 @@ namespace ContainerShip
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
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)));
_ship.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxShip.Width, pictureBoxShip.Height);
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_ship.Ship.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ: {_ship.Ship.Weight}";
toolStripStatusLabelBodyColor.Text = $"Öâåò: {_ship.Ship.BodyColor.Name}";
Draw();
Random rnd = new();
Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
_ship = new DrawingShip(rnd.Next(100, 300), rnd.Next(1000, 2000), color);
SetData();
Draw();
}
private void ButtonCreateModif_Click(object sender, EventArgs e)
{
Random rnd = new();
_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)));
Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
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();
Draw();
}
@ -100,5 +115,25 @@ namespace ContainerShip
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

@ -50,26 +50,6 @@ namespace ContainerShip
_map = map;
}
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
/// <param name="map"></param>
/// <param name="car"></param>
/// <returns></returns>
public static bool 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 bool operator -(MapWithSetShipsGeneric<T, U> map, int position)
{
return map._setShips.Remove(position);
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
@ -163,9 +143,31 @@ namespace ContainerShip
{
for (int i = 0; i < _setShips.Count; i++)
{
//TODO установка позиции
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

@ -30,7 +30,7 @@ namespace ContainerShip
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <returns></returns>
public bool Insert(T ship)
public int Insert(T ship)
{
return Insert(ship, 0);
}
@ -40,11 +40,11 @@ namespace ContainerShip
/// <param name="ship">Добавляемый корабль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public bool Insert(T ship, int position)
public int Insert(T ship, int position)
{
if (position < 0 || position >= _places.Length)
{
return false;
return -1;
}
if (_places[position] == null)
{
@ -63,7 +63,7 @@ namespace ContainerShip
}
if (freePlace == -1)
{
return false;
return -1;
}
else
{
@ -76,21 +76,22 @@ namespace ContainerShip
_places[position] = ship;
}
}
return true;
return position;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
public T Remove(int position)
{
if (position < 0 || position >= _places.Length)
{
return false;
return null;
}
T ship = _places[position];
_places[position] = null;
return true;
return ship;
}
/// <summary>
/// Получение объекта из набора по позиции