From 83b916307230215c658687e8d71669cea7cfc906 Mon Sep 17 00:00:00 2001 From: ksenianeva <95441235+ksenianeva@users.noreply.github.com> Date: Tue, 18 Oct 2022 12:19:05 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=A2=D1=80=D0=B5=D1=82=D1=8C=D1=8F=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0.=20Generic?= =?UTF-8?q?=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D1=8B.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ContainerShip/MapWithSetShipsGeneric.cs | 170 ++++++++++++++++++ .../ContainerShip/SetShipsGeneric.cs | 109 +++++++++++ 2 files changed, 279 insertions(+) create mode 100644 ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs create mode 100644 ContainerShip/ContainerShip/SetShipsGeneric.cs diff --git a/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs b/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs new file mode 100644 index 0000000..4b69aa7 --- /dev/null +++ b/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs @@ -0,0 +1,170 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ContainerShip +{ + internal class MapWithSetShipsGeneric + where T : class, IDrawingObject + where U : AbstractMap + { + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 135; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 70; + /// + /// Набор объектов + /// + private readonly SetShipsGeneric _setShips; + /// + /// Карта + /// + private readonly U _map; + /// + /// Конструктор + /// + /// + /// + /// + public MapWithSetShipsGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setShips = new SetShipsGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static bool operator +(MapWithSetShipsGeneric map, T ship) + { + return map._setShips.Insert(ship); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(MapWithSetShipsGeneric map, int position) + { + return map._setShips.Remove(position); + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawShips(gr); + return bmp; + } + /// + /// Просмотр объекта на карте + /// + /// + 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); + } + /// + /// Перемещение объекта по крате + /// + /// + /// + public Bitmap MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new(_pictureWidth, _pictureHeight); + } + /// + /// "Взбалтываем" набор, чтобы все элементы оказались в начале + /// + 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; + } + } + } + } + /// + /// Метод отрисовки фона + /// + /// + 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); + } + } + /// + /// Метод прорисовки объектов + /// + /// + private void DrawShips(Graphics g) + { + for (int i = 0; i < _setShips.Count; i++) + { + //TODO установка позиции + } + } + } +} diff --git a/ContainerShip/ContainerShip/SetShipsGeneric.cs b/ContainerShip/ContainerShip/SetShipsGeneric.cs new file mode 100644 index 0000000..c299746 --- /dev/null +++ b/ContainerShip/ContainerShip/SetShipsGeneric.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ContainerShip +{ + internal class SetShipsGeneric + where T: class + { + /// + /// Массив объектов, которые храним + /// + private readonly T[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetShipsGeneric(int count) + { + _places = new T[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый автомобиль + /// + public bool Insert(T ship) + { + return Insert(ship, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый корабль + /// Позиция + /// + public bool Insert(T ship, int position) + { + if (position < 0 || position >= _places.Length) + { + return false; + } + 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 false; + } + 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 true; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + if (position < 0 || position >= _places.Length) + { + return false; + } + _places[position] = null; + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T Get(int position) + { + if (position < 0 || position >= _places.Length) + { + return null; + } + return _places[position]; + } + } +} -- 2.25.1 From 55300032aa2d175603ab2dfbcf6f0fc9a5bea3a1 Mon Sep 17 00:00:00 2001 From: ksenianeva <95441235+ksenianeva@users.noreply.github.com> Date: Tue, 18 Oct 2022 13:34:57 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=84=D0=BE=D1=80=D0=BC.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ContainerShip/ContainerShip/Direction.cs | 2 +- ContainerShip/ContainerShip/DrawingShip.cs | 2 +- ContainerShip/ContainerShip/EntityShip.cs | 2 +- .../ContainerShip/FormMap.Designer.cs | 212 ------------------ ContainerShip/ContainerShip/FormMap.cs | 102 --------- .../FormMapWithSetShips.Designer.cs | 206 +++++++++++++++++ .../ContainerShip/FormMapWithSetShips.cs | 164 ++++++++++++++ ...{FormMap.resx => FormMapWithSetShips.resx} | 3 - .../ContainerShip/FormShip.Designer.cs | 21 +- ContainerShip/ContainerShip/FormShip.cs | 9 + .../ContainerShip/MapWithSetShipsGeneric.cs | 3 +- ContainerShip/ContainerShip/Program.cs | 2 +- 12 files changed, 403 insertions(+), 325 deletions(-) delete mode 100644 ContainerShip/ContainerShip/FormMap.Designer.cs delete mode 100644 ContainerShip/ContainerShip/FormMap.cs create mode 100644 ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs create mode 100644 ContainerShip/ContainerShip/FormMapWithSetShips.cs rename ContainerShip/ContainerShip/{FormMap.resx => FormMapWithSetShips.resx} (93%) diff --git a/ContainerShip/ContainerShip/Direction.cs b/ContainerShip/ContainerShip/Direction.cs index d3661a5..aad9ec2 100644 --- a/ContainerShip/ContainerShip/Direction.cs +++ b/ContainerShip/ContainerShip/Direction.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ContainerShip { - internal enum Direction + public enum Direction { None = 0, Up = 1, diff --git a/ContainerShip/ContainerShip/DrawingShip.cs b/ContainerShip/ContainerShip/DrawingShip.cs index 75b76ad..bf9530e 100644 --- a/ContainerShip/ContainerShip/DrawingShip.cs +++ b/ContainerShip/ContainerShip/DrawingShip.cs @@ -7,7 +7,7 @@ using static ContainerShip.EntityShip; namespace ContainerShip { - internal class DrawingShip + public class DrawingShip { public EntityShip Ship { protected set; get; } protected float _startPosX; diff --git a/ContainerShip/ContainerShip/EntityShip.cs b/ContainerShip/ContainerShip/EntityShip.cs index df462f0..05fb955 100644 --- a/ContainerShip/ContainerShip/EntityShip.cs +++ b/ContainerShip/ContainerShip/EntityShip.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ContainerShip { - internal class EntityShip + public class EntityShip { /// /// Скорость diff --git a/ContainerShip/ContainerShip/FormMap.Designer.cs b/ContainerShip/ContainerShip/FormMap.Designer.cs deleted file mode 100644 index 8cdc83f..0000000 --- a/ContainerShip/ContainerShip/FormMap.Designer.cs +++ /dev/null @@ -1,212 +0,0 @@ -namespace ContainerShip -{ - partial class FormMap - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; - - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region Windows Form Designer generated code - - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - this.statusStrip1 = new System.Windows.Forms.StatusStrip(); - 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.ComboBoxSelectorMap = new System.Windows.Forms.ComboBox(); - this.statusStrip1.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).BeginInit(); - this.SuspendLayout(); - // - // statusStrip1 - // - this.statusStrip1.ImageScalingSize = new System.Drawing.Size(24, 24); - this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripStatusLabelSpeed, - this.toolStripStatusLabelWeight, - this.toolStripStatusLabelBodyColor}); - this.statusStrip1.Location = new System.Drawing.Point(0, 418); - this.statusStrip1.Name = "statusStrip1"; - this.statusStrip1.Size = new System.Drawing.Size(800, 32); - this.statusStrip1.TabIndex = 0; - this.statusStrip1.Text = "statusStrip1"; - // - // toolStripStatusLabelSpeed - // - this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; - this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(93, 25); - this.toolStripStatusLabelSpeed.Text = "Скорость:"; - // - // toolStripStatusLabelWeight - // - this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; - this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(43, 25); - this.toolStripStatusLabelWeight.Text = "Вес:"; - // - // 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, 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 - // - this.pictureBoxShip.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBoxShip.Location = new System.Drawing.Point(0, 0); - this.pictureBoxShip.Name = "pictureBoxShip"; - this.pictureBoxShip.Size = new System.Drawing.Size(800, 418); - this.pictureBoxShip.TabIndex = 7; - this.pictureBoxShip.TabStop = false; - // - // ButtonCreate - // - this.ButtonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.ButtonCreate.BackColor = System.Drawing.SystemColors.Window; - this.ButtonCreate.Location = new System.Drawing.Point(12, 381); - this.ButtonCreate.Name = "ButtonCreate"; - this.ButtonCreate.Size = new System.Drawing.Size(112, 34); - this.ButtonCreate.TabIndex = 8; - this.ButtonCreate.Text = "Создать"; - this.ButtonCreate.UseVisualStyleBackColor = false; - this.ButtonCreate.Click += new System.EventHandler(this.ButtonCreate_Click); - // - // ButtonCreateModif - // - this.ButtonCreateModif.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.ButtonCreateModif.BackColor = System.Drawing.SystemColors.Window; - this.ButtonCreateModif.Location = new System.Drawing.Point(130, 381); - this.ButtonCreateModif.Name = "ButtonCreateModif"; - this.ButtonCreateModif.Size = new System.Drawing.Size(143, 34); - this.ButtonCreateModif.TabIndex = 9; - this.ButtonCreateModif.Text = "Модификация"; - this.ButtonCreateModif.UseVisualStyleBackColor = false; - this.ButtonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click); - // - // 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(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 - // - this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Controls.Add(this.ComboBoxSelectorMap); - this.Controls.Add(this.ButtonCreateModif); - this.Controls.Add(this.ButtonCreate); - this.Controls.Add(this.buttonLeft); - this.Controls.Add(this.buttonDown); - this.Controls.Add(this.buttonUp); - this.Controls.Add(this.buttonRight); - 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.PerformLayout(); - - } - - #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; - private PictureBox pictureBoxShip; - private Button ButtonCreate; - private Button ButtonCreateModif; - private ComboBox ComboBoxSelectorMap; - } -} \ No newline at end of file diff --git a/ContainerShip/ContainerShip/FormMap.cs b/ContainerShip/ContainerShip/FormMap.cs deleted file mode 100644 index 0dedff7..0000000 --- a/ContainerShip/ContainerShip/FormMap.cs +++ /dev/null @@ -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(); - } - /// - /// Изменение размеров формы - /// - /// - /// - 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); - } - /// - /// Изменение размеров формы - /// - /// - /// - 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); - } - /// - /// Обработка нажатия кнопки "Модификация" - /// - /// - /// - 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); - } - /// - /// Смена карты - /// - /// - /// - - private void ComboBoxSelectorMap_SelectedIndexChanged_1(object sender, EventArgs e) - { - switch (ComboBoxSelectorMap.Text) - { - case "Простая карта": - _abstractMap = new SimpleMap(); - break; - case "Модифицированная карта": - _abstractMap = new ModifyMap(); - break; - } - } - } -} diff --git a/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs b/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs new file mode 100644 index 0000000..7143ba1 --- /dev/null +++ b/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs @@ -0,0 +1,206 @@ +namespace ContainerShip +{ + partial class FormMapWithSetShips + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.ButtonAddShip = new System.Windows.Forms.Button(); + this.ComboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.buttonUp = new System.Windows.Forms.Button(); + this.buttonRight = new System.Windows.Forms.Button(); + this.buttonDown = new System.Windows.Forms.Button(); + this.buttonLeft = new System.Windows.Forms.Button(); + this.button3 = new System.Windows.Forms.Button(); + this.ButtonShowStorage = new System.Windows.Forms.Button(); + this.ButtonRemoveShip = new System.Windows.Forms.Button(); + this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); + this.pictureBox = new System.Windows.Forms.PictureBox(); + this.groupBox1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.SuspendLayout(); + // + // groupBox1 + // + 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); + this.groupBox1.Controls.Add(this.buttonLeft); + this.groupBox1.Controls.Add(this.button3); + this.groupBox1.Controls.Add(this.ButtonShowStorage); + this.groupBox1.Controls.Add(this.ButtonRemoveShip); + 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 = "Инструменты"; + // + // ButtonAddShip + // + 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; + // + // ComboBoxSelectorMap + // + this.ComboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.ComboBoxSelectorMap.Items.AddRange(new object[] { + "Простая карта", + "Модифицированная карта"}); + this.ComboBoxSelectorMap.Location = new System.Drawing.Point(6, 35); + this.ComboBoxSelectorMap.Name = "ComboBoxSelectorMap"; + this.ComboBoxSelectorMap.Size = new System.Drawing.Size(288, 33); + this.ComboBoxSelectorMap.TabIndex = 10; + // + // 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.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(50, 50); + this.buttonUp.TabIndex = 9; + this.buttonUp.UseVisualStyleBackColor = true; + // + // 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; + // + // 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(131, 446); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(50, 50); + this.buttonDown.TabIndex = 7; + this.buttonDown.UseVisualStyleBackColor = true; + // + // 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(75, 446); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(50, 50); + this.buttonLeft.TabIndex = 6; + this.buttonLeft.UseVisualStyleBackColor = true; + // + // button3 + // + this.button3.Location = new System.Drawing.Point(8, 292); + this.button3.Name = "button3"; + this.button3.Size = new System.Drawing.Size(286, 34); + this.button3.TabIndex = 3; + this.button3.Text = "Посмотреть карту"; + this.button3.UseVisualStyleBackColor = true; + // + // ButtonShowStorage + // + this.ButtonShowStorage.Location = new System.Drawing.Point(6, 243); + this.ButtonShowStorage.Name = "ButtonShowStorage"; + this.ButtonShowStorage.Size = new System.Drawing.Size(288, 34); + this.ButtonShowStorage.TabIndex = 2; + this.ButtonShowStorage.Text = "Просмотреть хранилище"; + this.ButtonShowStorage.UseVisualStyleBackColor = true; + // + // ButtonRemoveShip + // + this.ButtonRemoveShip.Location = new System.Drawing.Point(6, 162); + this.ButtonRemoveShip.Name = "ButtonRemoveShip"; + this.ButtonRemoveShip.Size = new System.Drawing.Size(288, 41); + this.ButtonRemoveShip.TabIndex = 1; + this.ButtonRemoveShip.Text = "Удалить корабль"; + this.ButtonRemoveShip.UseVisualStyleBackColor = true; + // + // maskedTextBoxPosition + // + this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 125); + this.maskedTextBoxPosition.Mask = "00"; + this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + this.maskedTextBoxPosition.Size = new System.Drawing.Size(288, 31); + this.maskedTextBoxPosition.TabIndex = 0; + // + // 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.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(853, 508); + this.Controls.Add(this.pictureBox); + this.Controls.Add(this.groupBox1); + this.Name = "FormMapWithSetShips"; + this.Text = "Карта с набором объектов"; + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private GroupBox groupBox1; + private Button button3; + private Button ButtonShowStorage; + private Button ButtonRemoveShip; + private MaskedTextBox maskedTextBoxPosition; + private PictureBox pictureBox; + private Button buttonLeft; + private Button buttonDown; + private Button buttonUp; + private Button buttonRight; + private Button ButtonAddShip; + private ComboBox ComboBoxSelectorMap; + } +} \ No newline at end of file diff --git a/ContainerShip/ContainerShip/FormMapWithSetShips.cs b/ContainerShip/ContainerShip/FormMapWithSetShips.cs new file mode 100644 index 0000000..a2841b2 --- /dev/null +++ b/ContainerShip/ContainerShip/FormMapWithSetShips.cs @@ -0,0 +1,164 @@ +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 + { + /// + /// Объект от класса карты с набором объектов + /// + private MapWithSetShipsGeneric _mapShipsCollectionGeneric; + /// + /// Конструктор + /// + public FormMapWithSetShips() + { + InitializeComponent(); + } + /// + /// Выбор карты + /// + /// + /// + 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( + pictureBox.Width, pictureBox.Height, map); + } + else + { + _mapShipsCollectionGeneric = null; + } + } + /// + /// Добавление объекта + /// + /// + /// + private void ButtonAddShip_Click(object sender, EventArgs e) + { + if (_mapShipsCollectionGeneric == null) + { + return; + } + FormShip form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + DrawingObjectShip car = new(form.SelectedShip); + if (_mapShipsCollectionGeneric + car) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта + /// + /// + /// + 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) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Вывод набора + /// + /// + /// + private void ButtonShowStorage_Click(object sender, EventArgs e) + { + if (_mapShipsCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); + } + /// + /// Вывод карты + /// + /// + /// + private void ButtonShowOnMap_Click(object sender, EventArgs e) + { + if (_mapShipsCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapShipsCollectionGeneric.ShowOnMap(); + } + /// + /// Перемещение + /// + /// + /// + 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); + } + } +} diff --git a/ContainerShip/ContainerShip/FormMap.resx b/ContainerShip/ContainerShip/FormMapWithSetShips.resx similarity index 93% rename from ContainerShip/ContainerShip/FormMap.resx rename to ContainerShip/ContainerShip/FormMapWithSetShips.resx index 5cb320f..f298a7b 100644 --- a/ContainerShip/ContainerShip/FormMap.resx +++ b/ContainerShip/ContainerShip/FormMapWithSetShips.resx @@ -57,7 +57,4 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - 17, 17 - \ No newline at end of file diff --git a/ContainerShip/ContainerShip/FormShip.Designer.cs b/ContainerShip/ContainerShip/FormShip.Designer.cs index 7d6bb68..d6f56ff 100644 --- a/ContainerShip/ContainerShip/FormShip.Designer.cs +++ b/ContainerShip/ContainerShip/FormShip.Designer.cs @@ -39,6 +39,7 @@ 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.statusStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).BeginInit(); this.SuspendLayout(); @@ -79,7 +80,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(738, 353); + 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; @@ -103,7 +104,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(682, 352); + 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; @@ -115,7 +116,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(626, 353); + 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; @@ -155,11 +156,24 @@ this.ButtonCreateModif.UseVisualStyleBackColor = false; 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); + // // FormShip // this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.ButtonSelectShip); this.Controls.Add(this.ButtonCreateModif); this.Controls.Add(this.ButtonCreate); this.Controls.Add(this.buttonLeft); @@ -192,5 +206,6 @@ private PictureBox pictureBoxShip; private Button ButtonCreate; private Button ButtonCreateModif; + private Button ButtonSelectShip; } } \ No newline at end of file diff --git a/ContainerShip/ContainerShip/FormShip.cs b/ContainerShip/ContainerShip/FormShip.cs index 3fbc50b..f9850da 100644 --- a/ContainerShip/ContainerShip/FormShip.cs +++ b/ContainerShip/ContainerShip/FormShip.cs @@ -3,6 +3,10 @@ namespace ContainerShip public partial class FormShip : Form { private DrawingShip _ship; + /// + /// + /// + public DrawingShip SelectedShip { get; private set; } public FormShip() { InitializeComponent(); @@ -91,5 +95,10 @@ namespace ContainerShip Draw(); } + private void ButtonSelectShip_Click(object sender, EventArgs e) + { + SelectedShip = _ship; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs b/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs index 4b69aa7..c474137 100644 --- a/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs +++ b/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs @@ -163,7 +163,8 @@ namespace ContainerShip { for (int i = 0; i < _setShips.Count; i++) { - //TODO установка позиции + //TODO установка позиции + _setShips.Get(i)?.DrawingObject(g); } } } diff --git a/ContainerShip/ContainerShip/Program.cs b/ContainerShip/ContainerShip/Program.cs index 8f4b98f..d579063 100644 --- a/ContainerShip/ContainerShip/Program.cs +++ b/ContainerShip/ContainerShip/Program.cs @@ -11,7 +11,7 @@ namespace ContainerShip // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormMap()); + Application.Run(new FormMapWithSetShips()); } } } \ No newline at end of file -- 2.25.1 From 634434095086ca0e2556b6f7e91b4dead7395ad6 Mon Sep 17 00:00:00 2001 From: ksenianeva <95441235+ksenianeva@users.noreply.github.com> Date: Tue, 1 Nov 2022 12:19:04 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B3=D1=80=D1=83?= =?UTF-8?q?=D0=B7=D0=BA=D0=B0=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BE=D0=B2.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetShips.Designer.cs | 9 ++ .../ContainerShip/FormMapWithSetShips.cs | 22 ++-- .../ContainerShip/FormShip.Designer.cs | 116 +++++++++--------- ContainerShip/ContainerShip/FormShip.cs | 57 +++++++-- .../ContainerShip/MapWithSetShipsGeneric.cs | 44 +++---- .../ContainerShip/SetShipsGeneric.cs | 17 +-- 6 files changed, 155 insertions(+), 110 deletions(-) diff --git a/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs b/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs index 7143ba1..4e94da7 100644 --- a/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs +++ b/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs @@ -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 // diff --git a/ContainerShip/ContainerShip/FormMapWithSetShips.cs b/ContainerShip/ContainerShip/FormMapWithSetShips.cs index a2841b2..faa900b 100644 --- a/ContainerShip/ContainerShip/FormMapWithSetShips.cs +++ b/ContainerShip/ContainerShip/FormMapWithSetShips.cs @@ -25,10 +25,8 @@ namespace ContainerShip InitializeComponent(); } /// - /// Выбор карты + /// Добавление объекта /// - /// - /// private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) { AbstractMap map = null; @@ -43,17 +41,13 @@ namespace ContainerShip } if (map != null) { - _mapShipsCollectionGeneric = new MapWithSetShipsGeneric( - pictureBox.Width, pictureBox.Height, map); + _mapShipsCollectionGeneric = new MapWithSetShipsGeneric(pictureBox.Width, pictureBox.Height, map); } else - { - _mapShipsCollectionGeneric = null; - } +{ + _mapShipsCollectionGeneric = null; +} } - /// - /// Добавление объекта - /// /// /// 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(); diff --git a/ContainerShip/ContainerShip/FormShip.Designer.cs b/ContainerShip/ContainerShip/FormShip.Designer.cs index d6f56ff..b786c0b 100644 --- a/ContainerShip/ContainerShip/FormShip.Designer.cs +++ b/ContainerShip/ContainerShip/FormShip.Designer.cs @@ -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; } } \ No newline at end of file diff --git a/ContainerShip/ContainerShip/FormShip.cs b/ContainerShip/ContainerShip/FormShip.cs index f9850da..58377a5 100644 --- a/ContainerShip/ContainerShip/FormShip.cs +++ b/ContainerShip/ContainerShip/FormShip.cs @@ -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) + { + + } } } \ No newline at end of file diff --git a/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs b/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs index c474137..dfda657 100644 --- a/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs +++ b/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs @@ -50,26 +50,6 @@ namespace ContainerShip _map = map; } /// - /// Перегрузка оператора сложения - /// - /// - /// - /// - public static bool operator +(MapWithSetShipsGeneric map, T ship) - { - return map._setShips.Insert(ship); - } - /// - /// Перегрузка оператора вычитания - /// - /// - /// - /// - public static bool operator -(MapWithSetShipsGeneric map, int position) - { - return map._setShips.Remove(position); - } - /// /// Вывод всего набора объектов /// /// @@ -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); } } + /// + /// Перегрузка операторов + /// + /// + /// + /// + public static int operator +(MapWithSetShipsGeneric map, T ship) + { + return map._setShips.Insert(ship); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static T operator -(MapWithSetShipsGeneric map, int position) + { + return map._setShips.Remove(position); + } } } diff --git a/ContainerShip/ContainerShip/SetShipsGeneric.cs b/ContainerShip/ContainerShip/SetShipsGeneric.cs index c299746..883942d 100644 --- a/ContainerShip/ContainerShip/SetShipsGeneric.cs +++ b/ContainerShip/ContainerShip/SetShipsGeneric.cs @@ -30,7 +30,7 @@ namespace ContainerShip /// /// Добавляемый автомобиль /// - public bool Insert(T ship) + public int Insert(T ship) { return Insert(ship, 0); } @@ -40,11 +40,11 @@ namespace ContainerShip /// Добавляемый корабль /// Позиция /// - 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; } /// /// Удаление объекта из набора с конкретной позиции /// /// /// - 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; } /// /// Получение объекта из набора по позиции -- 2.25.1