From 068933cb5b1fa10114dce4f479e76cd16805e8b6 Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Sat, 1 Oct 2022 09:52:43 +0400 Subject: [PATCH 1/4] Generic classes --- .../MapWithSetWarshipsGeneric.cs | 176 ++++++++++++++++++ .../AircraftCarrier/SetWarshipsGeneric.cs | 95 ++++++++++ 2 files changed, 271 insertions(+) create mode 100644 AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs create mode 100644 AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs diff --git a/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs b/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs new file mode 100644 index 0000000..d0d7e83 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs @@ -0,0 +1,176 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftCarrier +{ + /// + /// Карта с набром объектов под нее + /// + /// + /// + internal class MapWithSetWarshipsGeneric + where T : class, IDrawingObject + where U : AbstractMap + { + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 210; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 90; + /// + /// Набор объектов + /// + private readonly SetWarshipsGeneric _setWarships; + /// + /// Карта + /// + private readonly U _map; + /// + /// Конструктор + /// + /// + /// + /// + public MapWithSetWarshipsGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setWarships = new SetWarshipsGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static bool operator +(MapWithSetWarshipsGeneric map, T warship) + { + return map._setWarships.Insert(warship); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(MapWithSetWarshipsGeneric map, int position) + { + return map._setWarships.Remove(position); + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawWarships(gr); + return bmp; + } + /// + /// Просмотр объекта на карте + /// + /// + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setWarships.Count; i++) + { + var warship = _setWarships.Get(i); + if (warship != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, warship); + } + } + 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 = _setWarships.Count - 1; + for (int i = 0; i < _setWarships.Count; i++) + { + if (_setWarships.Get(i) == null) + { + for (; j > i; j--) + { + var car = _setWarships.Get(j); + if (car != null) + { + _setWarships.Insert(car, i); + _setWarships.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 3); + 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 DrawWarships(Graphics g) + { + for (int i = 0; i < _setWarships.Count; i++) + { + // TODO установка позиции + + _setWarships.Get(i)?.DrawningObject(g); + } + } + } +} diff --git a/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs b/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs new file mode 100644 index 0000000..3c3e27f --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftCarrier +{ + /// + /// Параметризованный набор объектов + /// + /// + internal class SetWarshipsGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetWarshipsGeneric(int count) + { + _places = new T[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый военный корабль + /// + public bool Insert(T warship) + { + if(Count >= 5 ) return false; + for(int i = Count - 1; i >= 0; i--) + { + _places[i + 1] = _places[i]; + } + _places[0] = warship; + return true; + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый военный корабль + /// Позиция + /// + public bool Insert(T warship, int position) + { + if(position >= _places.Length) return false; + if (_places[position] != null) + { + for(int i = position + 1; i < _places.Length; i++) + { + if (_places[i] == null) + { + for(int j = i - 1; j >= position; j--) + { + _places[j + 1] = _places[j]; + } + break; + } + } + } + _places[position] = warship; + return true; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + if(position >= _places.Length) return false; + _places[position] = null; + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T Get(int position) + { + if (position >= _places.Length) return null; + return _places[position]; + } + } +} From d19cbf92bb3e172090a7bfa0cf1e069e433dfaf1 Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Sat, 1 Oct 2022 12:18:31 +0400 Subject: [PATCH 2/4] Changes forms --- AircraftCarrier/AircraftCarrier/Direction.cs | 2 +- .../AircraftCarrier/DrawingWarship.cs | 2 +- .../AircraftCarrier/EntityWarship.cs | 2 +- .../AircraftCarrier/FormMap.Designer.cs | 211 ----------------- AircraftCarrier/AircraftCarrier/FormMap.cs | 104 --------- .../FormMapWithSetWarships.Designer.cs | 220 ++++++++++++++++++ .../AircraftCarrier/FormMapWithSetWarships.cs | 133 +++++++++++ ...rmMap.resx => FormMapWithSetWarships.resx} | 3 - .../AircraftCarrier/FormWarship.Designer.cs | 13 ++ .../AircraftCarrier/FormWarship.cs | 10 + AircraftCarrier/AircraftCarrier/Program.cs | 2 +- 11 files changed, 380 insertions(+), 322 deletions(-) delete mode 100644 AircraftCarrier/AircraftCarrier/FormMap.Designer.cs delete mode 100644 AircraftCarrier/AircraftCarrier/FormMap.cs create mode 100644 AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.Designer.cs create mode 100644 AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs rename AircraftCarrier/AircraftCarrier/{FormMap.resx => FormMapWithSetWarships.resx} (93%) diff --git a/AircraftCarrier/AircraftCarrier/Direction.cs b/AircraftCarrier/AircraftCarrier/Direction.cs index 0f48c8f..36beb02 100644 --- a/AircraftCarrier/AircraftCarrier/Direction.cs +++ b/AircraftCarrier/AircraftCarrier/Direction.cs @@ -9,7 +9,7 @@ namespace AircraftCarrier /// /// Направление перемещения /// - internal enum Direction + public enum Direction { None = 0, Up = 1, diff --git a/AircraftCarrier/AircraftCarrier/DrawingWarship.cs b/AircraftCarrier/AircraftCarrier/DrawingWarship.cs index 0cf2699..93995c0 100644 --- a/AircraftCarrier/AircraftCarrier/DrawingWarship.cs +++ b/AircraftCarrier/AircraftCarrier/DrawingWarship.cs @@ -9,7 +9,7 @@ namespace AircraftCarrier /// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности /// - internal class DrawingWarship + public class DrawingWarship { /// /// Класс-сущность diff --git a/AircraftCarrier/AircraftCarrier/EntityWarship.cs b/AircraftCarrier/AircraftCarrier/EntityWarship.cs index b5d037c..a258479 100644 --- a/AircraftCarrier/AircraftCarrier/EntityWarship.cs +++ b/AircraftCarrier/AircraftCarrier/EntityWarship.cs @@ -9,7 +9,7 @@ namespace AircraftCarrier /// /// Класс - сущность "Военный корабль" /// - internal class EntityWarship + public class EntityWarship { /// /// Скорость diff --git a/AircraftCarrier/AircraftCarrier/FormMap.Designer.cs b/AircraftCarrier/AircraftCarrier/FormMap.Designer.cs deleted file mode 100644 index ee9bc8b..0000000 --- a/AircraftCarrier/AircraftCarrier/FormMap.Designer.cs +++ /dev/null @@ -1,211 +0,0 @@ -namespace AircraftCarrier -{ - 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.pictureBoxWarship = new System.Windows.Forms.PictureBox(); - this.statusStrip = 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.buttonCreate = new System.Windows.Forms.Button(); - this.buttonDown = new System.Windows.Forms.Button(); - this.buttonUp = new System.Windows.Forms.Button(); - this.buttonLeft = new System.Windows.Forms.Button(); - this.buttonRight = new System.Windows.Forms.Button(); - this.buttonCreateModif = new System.Windows.Forms.Button(); - this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxWarship)).BeginInit(); - this.statusStrip.SuspendLayout(); - this.SuspendLayout(); - // - // pictureBoxWarship - // - this.pictureBoxWarship.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBoxWarship.Location = new System.Drawing.Point(0, 0); - this.pictureBoxWarship.Name = "pictureBoxWarship"; - this.pictureBoxWarship.Size = new System.Drawing.Size(768, 426); - this.pictureBoxWarship.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; - this.pictureBoxWarship.TabIndex = 0; - this.pictureBoxWarship.TabStop = false; - // - // statusStrip - // - this.statusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripStatusLabelSpeed, - this.toolStripStatusLabelWeight, - this.toolStripStatusLabelBodyColor}); - this.statusStrip.Location = new System.Drawing.Point(0, 426); - this.statusStrip.Name = "statusStrip"; - this.statusStrip.Size = new System.Drawing.Size(768, 22); - this.statusStrip.TabIndex = 1; - // - // toolStripStatusLabelSpeed - // - this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; - this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(39, 17); - this.toolStripStatusLabelSpeed.Text = "Speed"; - // - // toolStripStatusLabelWeight - // - this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; - this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(45, 17); - this.toolStripStatusLabelWeight.Text = "Weight"; - // - // toolStripStatusLabelBodyColor - // - this.toolStripStatusLabelBodyColor.Name = "toolStripStatusLabelBodyColor"; - this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(36, 17); - this.toolStripStatusLabelBodyColor.Text = "Color"; - // - // buttonCreate - // - this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.buttonCreate.Location = new System.Drawing.Point(12, 393); - this.buttonCreate.Name = "buttonCreate"; - this.buttonCreate.Size = new System.Drawing.Size(75, 23); - this.buttonCreate.TabIndex = 2; - this.buttonCreate.Text = "Create"; - this.buttonCreate.UseVisualStyleBackColor = true; - this.buttonCreate.Click += new System.EventHandler(this.ButtonCreate_Click); - // - // buttonDown - // - this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonDown.BackgroundImage = global::AircraftCarrier.Properties.Resources.ArrowDown; - this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(686, 386); - this.buttonDown.Name = "buttonDown"; - this.buttonDown.Size = new System.Drawing.Size(30, 30); - this.buttonDown.TabIndex = 3; - this.buttonDown.Text = " "; - this.buttonDown.UseVisualStyleBackColor = true; - this.buttonDown.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::AircraftCarrier.Properties.Resources.ArrowUp; - this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(686, 350); - this.buttonUp.Name = "buttonUp"; - this.buttonUp.Size = new System.Drawing.Size(30, 30); - this.buttonUp.TabIndex = 4; - this.buttonUp.Text = " "; - this.buttonUp.UseVisualStyleBackColor = true; - this.buttonUp.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::AircraftCarrier.Properties.Resources.ArrowLeft; - this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(650, 386); - this.buttonLeft.Name = "buttonLeft"; - this.buttonLeft.Size = new System.Drawing.Size(30, 30); - this.buttonLeft.TabIndex = 5; - this.buttonLeft.Text = " "; - this.buttonLeft.UseVisualStyleBackColor = true; - this.buttonLeft.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::AircraftCarrier.Properties.Resources.ArrowRight; - this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(722, 386); - this.buttonRight.Name = "buttonRight"; - this.buttonRight.Size = new System.Drawing.Size(30, 30); - this.buttonRight.TabIndex = 6; - this.buttonRight.Text = " "; - this.buttonRight.UseVisualStyleBackColor = true; - this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); - // - // buttonCreateModif - // - this.buttonCreateModif.Location = new System.Drawing.Point(93, 393); - this.buttonCreateModif.Name = "buttonCreateModif"; - this.buttonCreateModif.Size = new System.Drawing.Size(92, 23); - this.buttonCreateModif.TabIndex = 7; - this.buttonCreateModif.Text = "Modification"; - this.buttonCreateModif.UseVisualStyleBackColor = true; - 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(12, 12); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 23); - this.comboBoxSelectorMap.TabIndex = 8; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.comboBoxSelectorMap_SelectedIndexChanged); - // - // FormMap - // - this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(768, 448); - this.Controls.Add(this.comboBoxSelectorMap); - this.Controls.Add(this.buttonCreateModif); - this.Controls.Add(this.buttonRight); - this.Controls.Add(this.buttonLeft); - this.Controls.Add(this.buttonUp); - this.Controls.Add(this.buttonDown); - this.Controls.Add(this.buttonCreate); - this.Controls.Add(this.pictureBoxWarship); - this.Controls.Add(this.statusStrip); - this.Name = "FormMap"; - this.Text = "Карта"; - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxWarship)).EndInit(); - this.statusStrip.ResumeLayout(false); - this.statusStrip.PerformLayout(); - this.ResumeLayout(false); - this.PerformLayout(); - - } - - #endregion - - private PictureBox pictureBoxWarship; - private StatusStrip statusStrip; - private ToolStripStatusLabel toolStripStatusLabelSpeed; - private ToolStripStatusLabel toolStripStatusLabelWeight; - private ToolStripStatusLabel toolStripStatusLabelBodyColor; - private Button buttonCreate; - private Button buttonDown; - private Button buttonUp; - private Button buttonLeft; - private Button buttonRight; - private Button buttonCreateModif; - private ComboBox comboBoxSelectorMap; - } -} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormMap.cs b/AircraftCarrier/AircraftCarrier/FormMap.cs deleted file mode 100644 index acd3cfe..0000000 --- a/AircraftCarrier/AircraftCarrier/FormMap.cs +++ /dev/null @@ -1,104 +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 AircraftCarrier -{ - public partial class FormMap : Form - { - private AbstractMap _abstractMap; - public FormMap() - { - InitializeComponent(); - _abstractMap = new SimpleMap(); - } - /// - /// Заполнение информации по объекту - /// - /// - private void SetData(DrawingWarship warship) - { - toolStripStatusLabelSpeed.Text = $"Скорость: {warship.Warship.Speed}"; - toolStripStatusLabelWeight.Text = $"Вес: {warship.Warship.Weight}"; - toolStripStatusLabelBodyColor.Text = $"Цвет: {warship.Warship.BodyColor.Name}"; - pictureBoxWarship.Image = _abstractMap.CreateMap(pictureBoxWarship.Width, pictureBoxWarship.Height, - new DrawingObjectWarship(warship)); - } - /// - /// Обработка нажатия кнопки "Create" - /// - /// - /// - private void ButtonCreate_Click(object sender, EventArgs e) - { - Random rnd = new(); - var warship = new DrawingWarship(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); - SetData(warship); - } - /// - /// Обработка нажатия стрелочек - /// - /// - /// - 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; - } - pictureBoxWarship.Image = _abstractMap?.MoveObject(dir); - } - - /// - /// Обработка нажатия кнопки "Modification" - /// - /// - /// - private void ButtonCreateModif_Click(object sender, EventArgs e) - { - Random rnd = new(); - var car = new DrawingAircraftCarrier(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)), Convert.ToBoolean(rnd.Next(0, 2))); - SetData(car); - } - /// - /// Смена карты - /// - /// - /// - private void comboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) - { - switch (comboBoxSelectorMap.Text) - { - case "Простая карта": - _abstractMap = new SimpleMap(); - break; - case "Преграды-линии": - _abstractMap = new LineMap(); - break; - } - } - } -} diff --git a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.Designer.cs b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.Designer.cs new file mode 100644 index 0000000..acef8a2 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.Designer.cs @@ -0,0 +1,220 @@ +namespace AircraftCarrier +{ + partial class FormMapWithSetWarships + { + /// + /// 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.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.pictureBox = new System.Windows.Forms.PictureBox(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + 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.buttonAddWarship = new System.Windows.Forms.Button(); + this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); + this.buttonRemoveWarship = new System.Windows.Forms.Button(); + this.buttonShowStorage = new System.Windows.Forms.Button(); + this.buttonShowOnMap = new System.Windows.Forms.Button(); + this.groupBoxTools.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.SuspendLayout(); + // + // groupBoxTools + // + this.groupBoxTools.Controls.Add(this.buttonShowOnMap); + this.groupBoxTools.Controls.Add(this.buttonShowStorage); + this.groupBoxTools.Controls.Add(this.buttonRemoveWarship); + this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); + this.groupBoxTools.Controls.Add(this.buttonAddWarship); + this.groupBoxTools.Controls.Add(this.buttonRight); + this.groupBoxTools.Controls.Add(this.buttonLeft); + this.groupBoxTools.Controls.Add(this.buttonUp); + this.groupBoxTools.Controls.Add(this.buttonDown); + this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap); + this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; + this.groupBoxTools.Location = new System.Drawing.Point(787, 0); + this.groupBoxTools.Name = "groupBoxTools"; + this.groupBoxTools.Size = new System.Drawing.Size(200, 593); + this.groupBoxTools.TabIndex = 0; + this.groupBoxTools.TabStop = false; + this.groupBoxTools.Text = "Tools"; + // + // 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(787, 593); + this.pictureBox.TabIndex = 1; + this.pictureBox.TabStop = false; + // + // 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(13, 22); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(175, 23); + this.comboBoxSelectorMap.TabIndex = 9; + // + // buttonRight + // + this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonRight.BackgroundImage = global::AircraftCarrier.Properties.Resources.ArrowRight; + this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonRight.Location = new System.Drawing.Point(121, 553); + this.buttonRight.Name = "buttonRight"; + this.buttonRight.Size = new System.Drawing.Size(30, 30); + this.buttonRight.TabIndex = 13; + this.buttonRight.Text = " "; + 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::AircraftCarrier.Properties.Resources.ArrowLeft; + this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonLeft.Location = new System.Drawing.Point(49, 553); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(30, 30); + this.buttonLeft.TabIndex = 12; + this.buttonLeft.Text = " "; + 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::AircraftCarrier.Properties.Resources.ArrowUp; + this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonUp.Location = new System.Drawing.Point(85, 517); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(30, 30); + this.buttonUp.TabIndex = 11; + this.buttonUp.Text = " "; + 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::AircraftCarrier.Properties.Resources.ArrowDown; + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonDown.Location = new System.Drawing.Point(85, 553); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(30, 30); + this.buttonDown.TabIndex = 10; + this.buttonDown.Text = " "; + this.buttonDown.UseVisualStyleBackColor = true; + this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonAddWarship + // + this.buttonAddWarship.Location = new System.Drawing.Point(13, 114); + this.buttonAddWarship.Name = "buttonAddWarship"; + this.buttonAddWarship.Size = new System.Drawing.Size(175, 35); + this.buttonAddWarship.TabIndex = 14; + this.buttonAddWarship.Text = "Add warship"; + this.buttonAddWarship.UseVisualStyleBackColor = true; + this.buttonAddWarship.Click += new System.EventHandler(this.ButtonAddWarship_Click); + // + // maskedTextBoxPosition + // + this.maskedTextBoxPosition.Location = new System.Drawing.Point(13, 177); + this.maskedTextBoxPosition.Mask = "00"; + this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23); + this.maskedTextBoxPosition.TabIndex = 15; + this.maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonRemoveWarship + // + this.buttonRemoveWarship.Location = new System.Drawing.Point(13, 208); + this.buttonRemoveWarship.Name = "buttonRemoveWarship"; + this.buttonRemoveWarship.Size = new System.Drawing.Size(175, 35); + this.buttonRemoveWarship.TabIndex = 16; + this.buttonRemoveWarship.Text = "Remove warship"; + this.buttonRemoveWarship.UseVisualStyleBackColor = true; + this.buttonRemoveWarship.Click += new System.EventHandler(this.ButtonRemoveWarship_Click); + // + // buttonShowStorage + // + this.buttonShowStorage.Location = new System.Drawing.Point(13, 323); + this.buttonShowStorage.Name = "buttonShowStorage"; + this.buttonShowStorage.Size = new System.Drawing.Size(175, 35); + this.buttonShowStorage.TabIndex = 17; + this.buttonShowStorage.Text = "Show storage"; + this.buttonShowStorage.UseVisualStyleBackColor = true; + this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); + // + // buttonShowOnMap + // + this.buttonShowOnMap.Location = new System.Drawing.Point(13, 431); + this.buttonShowOnMap.Name = "buttonShowOnMap"; + this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35); + this.buttonShowOnMap.TabIndex = 18; + this.buttonShowOnMap.Text = "Show map"; + this.buttonShowOnMap.UseVisualStyleBackColor = true; + this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); + // + // FormMapWithSetWarships + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(987, 593); + this.Controls.Add(this.pictureBox); + this.Controls.Add(this.groupBoxTools); + this.Name = "FormMapWithSetWarships"; + this.Text = "FormMapWithSetWarships"; + this.groupBoxTools.ResumeLayout(false); + this.groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private GroupBox groupBoxTools; + private PictureBox pictureBox; + private ComboBox comboBoxSelectorMap; + private Button buttonRight; + private Button buttonLeft; + private Button buttonUp; + private Button buttonDown; + private Button buttonShowOnMap; + private Button buttonShowStorage; + private Button buttonRemoveWarship; + private MaskedTextBox maskedTextBoxPosition; + private Button buttonAddWarship; + } +} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs new file mode 100644 index 0000000..6915f00 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs @@ -0,0 +1,133 @@ +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 AircraftCarrier +{ + public partial class FormMapWithSetWarships : Form + { + /// + /// Объект от класса карты с набором объектов + /// + private MapWithSetWarshipsGeneric _mapWarshipsCollectionGeneric; + /// + /// Конструктор + /// + public FormMapWithSetWarships() + { + InitializeComponent(); + } + /// + /// Добавление объекта + /// + /// + /// + private void ButtonAddWarship_Click(object sender, EventArgs e) + { + if (_mapWarshipsCollectionGeneric == null) + { + return; + } + FormWarship form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + DrawingObjectWarship warship = new(form.SelectedWarship); + if (_mapWarshipsCollectionGeneric + warship) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта + /// + /// + /// + private void ButtonRemoveWarship_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 (_mapWarshipsCollectionGeneric - pos) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Вывод набора + /// + /// + /// + private void ButtonShowStorage_Click(object sender, EventArgs e) + { + if (_mapWarshipsCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet(); + } + /// + /// Вывод карты + /// + /// + /// + private void ButtonShowOnMap_Click(object sender, EventArgs e) + { + //// + } + /// + /// Перемещение + /// + /// + /// + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_mapWarshipsCollectionGeneric == 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 = _mapWarshipsCollectionGeneric.MoveObject(dir); + } + } +} diff --git a/AircraftCarrier/AircraftCarrier/FormMap.resx b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.resx similarity index 93% rename from AircraftCarrier/AircraftCarrier/FormMap.resx rename to AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.resx index 2c0949d..f298a7b 100644 --- a/AircraftCarrier/AircraftCarrier/FormMap.resx +++ b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.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/AircraftCarrier/AircraftCarrier/FormWarship.Designer.cs b/AircraftCarrier/AircraftCarrier/FormWarship.Designer.cs index 854abe8..ff27171 100644 --- a/AircraftCarrier/AircraftCarrier/FormWarship.Designer.cs +++ b/AircraftCarrier/AircraftCarrier/FormWarship.Designer.cs @@ -39,6 +39,7 @@ this.buttonLeft = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonCreateModif = new System.Windows.Forms.Button(); + this.buttonSelectWarship = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxWarship)).BeginInit(); this.statusStrip.SuspendLayout(); this.SuspendLayout(); @@ -156,11 +157,22 @@ this.buttonCreateModif.UseVisualStyleBackColor = true; this.buttonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click); // + // buttonSelectWarship + // + this.buttonSelectWarship.Location = new System.Drawing.Point(530, 393); + this.buttonSelectWarship.Name = "buttonSelectWarship"; + this.buttonSelectWarship.Size = new System.Drawing.Size(75, 23); + this.buttonSelectWarship.TabIndex = 8; + this.buttonSelectWarship.Text = "Select"; + this.buttonSelectWarship.UseVisualStyleBackColor = true; + this.buttonSelectWarship.Click += new System.EventHandler(this.ButtonSelectWarship_Click); + // // FormWarship // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(768, 448); + this.Controls.Add(this.buttonSelectWarship); this.Controls.Add(this.buttonCreateModif); this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonLeft); @@ -192,5 +204,6 @@ private Button buttonLeft; private Button buttonRight; private Button buttonCreateModif; + private Button buttonSelectWarship; } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormWarship.cs b/AircraftCarrier/AircraftCarrier/FormWarship.cs index 1d0e4a3..d73d312 100644 --- a/AircraftCarrier/AircraftCarrier/FormWarship.cs +++ b/AircraftCarrier/AircraftCarrier/FormWarship.cs @@ -3,6 +3,10 @@ namespace AircraftCarrier public partial class FormWarship : Form { private DrawingWarship _warship; + /// + /// + /// + public DrawingWarship SelectedWarship { get; private set; } public FormWarship() { InitializeComponent(); @@ -91,5 +95,11 @@ namespace AircraftCarrier Draw(); } + + private void ButtonSelectWarship_Click(object sender, EventArgs e) + { + SelectedWarship = _warship; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/Program.cs b/AircraftCarrier/AircraftCarrier/Program.cs index 4080ebd..00b84f4 100644 --- a/AircraftCarrier/AircraftCarrier/Program.cs +++ b/AircraftCarrier/AircraftCarrier/Program.cs @@ -11,7 +11,7 @@ namespace AircraftCarrier // 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 FormMapWithSetWarships()); } } } \ No newline at end of file From 4b867003e2639d3bb8a1674666f63042538ee696 Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Sat, 1 Oct 2022 12:51:41 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=9D=D0=B5=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE?= =?UTF-8?q?=20=D0=B4=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetWarships.Designer.cs | 139 +++++++++--------- .../AircraftCarrier/FormMapWithSetWarships.cs | 22 +++ .../AircraftCarrier/FormWarship.cs | 22 ++- .../MapWithSetWarshipsGeneric.cs | 9 +- 4 files changed, 118 insertions(+), 74 deletions(-) diff --git a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.Designer.cs b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.Designer.cs index acef8a2..263bcbd 100644 --- a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.Designer.cs +++ b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.Designer.cs @@ -29,17 +29,17 @@ private void InitializeComponent() { this.groupBoxTools = new System.Windows.Forms.GroupBox(); - this.pictureBox = new System.Windows.Forms.PictureBox(); - this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.buttonShowOnMap = new System.Windows.Forms.Button(); + this.buttonShowStorage = new System.Windows.Forms.Button(); + this.buttonRemoveWarship = new System.Windows.Forms.Button(); + this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); + this.buttonAddWarship = 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.buttonAddWarship = new System.Windows.Forms.Button(); - this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); - this.buttonRemoveWarship = new System.Windows.Forms.Button(); - this.buttonShowStorage = new System.Windows.Forms.Button(); - this.buttonShowOnMap = new System.Windows.Forms.Button(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBoxTools.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); @@ -64,26 +64,54 @@ this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Tools"; // - // pictureBox + // buttonShowOnMap // - 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(787, 593); - this.pictureBox.TabIndex = 1; - this.pictureBox.TabStop = false; + this.buttonShowOnMap.Location = new System.Drawing.Point(13, 431); + this.buttonShowOnMap.Name = "buttonShowOnMap"; + this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35); + this.buttonShowOnMap.TabIndex = 18; + this.buttonShowOnMap.Text = "Show map"; + this.buttonShowOnMap.UseVisualStyleBackColor = true; + this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); // - // comboBoxSelectorMap + // buttonShowStorage // - 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(13, 22); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(175, 23); - this.comboBoxSelectorMap.TabIndex = 9; + this.buttonShowStorage.Location = new System.Drawing.Point(13, 323); + this.buttonShowStorage.Name = "buttonShowStorage"; + this.buttonShowStorage.Size = new System.Drawing.Size(175, 35); + this.buttonShowStorage.TabIndex = 17; + this.buttonShowStorage.Text = "Show storage"; + this.buttonShowStorage.UseVisualStyleBackColor = true; + this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); + // + // buttonRemoveWarship + // + this.buttonRemoveWarship.Location = new System.Drawing.Point(13, 208); + this.buttonRemoveWarship.Name = "buttonRemoveWarship"; + this.buttonRemoveWarship.Size = new System.Drawing.Size(175, 35); + this.buttonRemoveWarship.TabIndex = 16; + this.buttonRemoveWarship.Text = "Remove warship"; + this.buttonRemoveWarship.UseVisualStyleBackColor = true; + this.buttonRemoveWarship.Click += new System.EventHandler(this.ButtonRemoveWarship_Click); + // + // maskedTextBoxPosition + // + this.maskedTextBoxPosition.Location = new System.Drawing.Point(13, 177); + this.maskedTextBoxPosition.Mask = "00"; + this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23); + this.maskedTextBoxPosition.TabIndex = 15; + this.maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonAddWarship + // + this.buttonAddWarship.Location = new System.Drawing.Point(13, 114); + this.buttonAddWarship.Name = "buttonAddWarship"; + this.buttonAddWarship.Size = new System.Drawing.Size(175, 35); + this.buttonAddWarship.TabIndex = 14; + this.buttonAddWarship.Text = "Add warship"; + this.buttonAddWarship.UseVisualStyleBackColor = true; + this.buttonAddWarship.Click += new System.EventHandler(this.ButtonAddWarship_Click); // // buttonRight // @@ -137,54 +165,27 @@ this.buttonDown.UseVisualStyleBackColor = true; this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); // - // buttonAddWarship + // comboBoxSelectorMap // - this.buttonAddWarship.Location = new System.Drawing.Point(13, 114); - this.buttonAddWarship.Name = "buttonAddWarship"; - this.buttonAddWarship.Size = new System.Drawing.Size(175, 35); - this.buttonAddWarship.TabIndex = 14; - this.buttonAddWarship.Text = "Add warship"; - this.buttonAddWarship.UseVisualStyleBackColor = true; - this.buttonAddWarship.Click += new System.EventHandler(this.ButtonAddWarship_Click); + 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(13, 22); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(175, 23); + this.comboBoxSelectorMap.TabIndex = 9; + this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); // - // maskedTextBoxPosition + // pictureBox // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(13, 177); - this.maskedTextBoxPosition.Mask = "00"; - this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23); - this.maskedTextBoxPosition.TabIndex = 15; - this.maskedTextBoxPosition.ValidatingType = typeof(int); - // - // buttonRemoveWarship - // - this.buttonRemoveWarship.Location = new System.Drawing.Point(13, 208); - this.buttonRemoveWarship.Name = "buttonRemoveWarship"; - this.buttonRemoveWarship.Size = new System.Drawing.Size(175, 35); - this.buttonRemoveWarship.TabIndex = 16; - this.buttonRemoveWarship.Text = "Remove warship"; - this.buttonRemoveWarship.UseVisualStyleBackColor = true; - this.buttonRemoveWarship.Click += new System.EventHandler(this.ButtonRemoveWarship_Click); - // - // buttonShowStorage - // - this.buttonShowStorage.Location = new System.Drawing.Point(13, 323); - this.buttonShowStorage.Name = "buttonShowStorage"; - this.buttonShowStorage.Size = new System.Drawing.Size(175, 35); - this.buttonShowStorage.TabIndex = 17; - this.buttonShowStorage.Text = "Show storage"; - this.buttonShowStorage.UseVisualStyleBackColor = true; - this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); - // - // buttonShowOnMap - // - this.buttonShowOnMap.Location = new System.Drawing.Point(13, 431); - this.buttonShowOnMap.Name = "buttonShowOnMap"; - this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35); - this.buttonShowOnMap.TabIndex = 18; - this.buttonShowOnMap.Text = "Show map"; - this.buttonShowOnMap.UseVisualStyleBackColor = true; - this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); + 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(787, 593); + this.pictureBox.TabIndex = 1; + this.pictureBox.TabStop = false; // // FormMapWithSetWarships // diff --git a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs index 6915f00..96c5f9d 100644 --- a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs +++ b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs @@ -24,6 +24,28 @@ namespace AircraftCarrier { InitializeComponent(); } + private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + { + AbstractMap map = null; + switch (comboBoxSelectorMap.Text) + { + case "Простая карта": + map = new SimpleMap(); + break; + case "Преграды-линии": + map = new LineMap(); + break; + } + if (map != null) + { + _mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric( + pictureBox.Width, pictureBox.Height, map); + } + else + { + _mapWarshipsCollectionGeneric = null; + } + } /// /// Добавление объекта /// diff --git a/AircraftCarrier/AircraftCarrier/FormWarship.cs b/AircraftCarrier/AircraftCarrier/FormWarship.cs index d73d312..c6d7f9c 100644 --- a/AircraftCarrier/AircraftCarrier/FormWarship.cs +++ b/AircraftCarrier/AircraftCarrier/FormWarship.cs @@ -40,6 +40,12 @@ namespace AircraftCarrier private void ButtonCreate_Click(object sender, EventArgs e) { 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; + } _warship = new DrawingWarship(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); SetData(); Draw(); @@ -87,9 +93,19 @@ namespace AircraftCarrier private void ButtonCreateModif_Click(object sender, EventArgs e) { Random rnd = new(); - _warship = new DrawingAircraftCarrier(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)), + 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; + } + _warship = new DrawingAircraftCarrier(rnd.Next(100, 300), rnd.Next(1000, 2000), color, dopColor, Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2))); SetData(); Draw(); diff --git a/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs b/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs index d0d7e83..4caabe2 100644 --- a/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs @@ -167,8 +167,13 @@ namespace AircraftCarrier { for (int i = 0; i < _setWarships.Count; i++) { - // TODO установка позиции - + for (int k = 0; k < _pictureWidth / _placeSizeWidth; k++) + { + for (int l = 0; l < _pictureHeight / _placeSizeHeight + 1; ++l) + { + _setWarships.Get(i)?.SetObject(k * _placeSizeWidth, l * _placeSizeHeight, _pictureWidth, _pictureHeight); + } + } _setWarships.Get(i)?.DrawningObject(g); } } From e8e0bd932937391180282d6ae85babf63ca4c971 Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Tue, 4 Oct 2022 23:32:55 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D0=92=D0=BD=D0=B5=D1=81=D0=BB=D0=B0=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D1=8B=D0=B5=20=D0=B8?= =?UTF-8?q?=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AircraftCarrier/AbstractMap.cs | 21 +++++--- .../AircraftCarrier/FormMapWithSetWarships.cs | 10 ++-- .../AircraftCarrier/FormWarship.cs | 2 +- .../MapWithSetWarshipsGeneric.cs | 22 ++++---- .../AircraftCarrier/SetWarshipsGeneric.cs | 53 +++++++++++-------- 5 files changed, 61 insertions(+), 47 deletions(-) diff --git a/AircraftCarrier/AircraftCarrier/AbstractMap.cs b/AircraftCarrier/AircraftCarrier/AbstractMap.cs index 667881f..2193c03 100644 --- a/AircraftCarrier/AircraftCarrier/AbstractMap.cs +++ b/AircraftCarrier/AircraftCarrier/AbstractMap.cs @@ -32,17 +32,22 @@ namespace AircraftCarrier } public Bitmap MoveObject(Direction direction) { - if (true) + if (_drawingObject != null) { - _drawingObject.MoveObject(direction); - } - (float Left, float Right, float Top, float Bottom) = _drawingObject.GetCurrentPosition(); + if (true) + { + _drawingObject.MoveObject(direction); + } + (float Left, float Right, float Top, float Bottom) = _drawingObject.GetCurrentPosition(); - if (Check(Left, Right, Top, Bottom) != 0) - { - _drawingObject.MoveObject(GetOpositDirection(direction)); + if (Check(Left, Right, Top, Bottom) != 0) + { + _drawingObject.MoveObject(GetOpositDirection(direction)); + } + return DrawMapWithObject(); } - return DrawMapWithObject(); + return null; + } private Direction GetOpositDirection(Direction dir) diff --git a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs index 96c5f9d..ffd81ae 100644 --- a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs +++ b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs @@ -61,7 +61,7 @@ namespace AircraftCarrier if (form.ShowDialog() == DialogResult.OK) { DrawingObjectWarship warship = new(form.SelectedWarship); - if (_mapWarshipsCollectionGeneric + warship) + if (_mapWarshipsCollectionGeneric + warship >= 0) { MessageBox.Show("Объект добавлен"); pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet(); @@ -88,7 +88,7 @@ namespace AircraftCarrier return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapWarshipsCollectionGeneric - pos) + if (_mapWarshipsCollectionGeneric - pos != null) { MessageBox.Show("Объект удален"); pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet(); @@ -118,7 +118,11 @@ namespace AircraftCarrier /// private void ButtonShowOnMap_Click(object sender, EventArgs e) { - //// + if(_mapWarshipsCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapWarshipsCollectionGeneric.ShowOnMap(); } /// /// Перемещение diff --git a/AircraftCarrier/AircraftCarrier/FormWarship.cs b/AircraftCarrier/AircraftCarrier/FormWarship.cs index c6d7f9c..28646d5 100644 --- a/AircraftCarrier/AircraftCarrier/FormWarship.cs +++ b/AircraftCarrier/AircraftCarrier/FormWarship.cs @@ -46,7 +46,7 @@ namespace AircraftCarrier { color = dialog.Color; } - _warship = new DrawingWarship(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); + _warship = new DrawingWarship(rnd.Next(100, 300), rnd.Next(1000, 2000), color); SetData(); Draw(); } diff --git a/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs b/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs index 4caabe2..ed82a7b 100644 --- a/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs @@ -26,11 +26,11 @@ namespace AircraftCarrier /// /// Размер занимаемого объектом места (ширина) /// - private readonly int _placeSizeWidth = 210; + private readonly int _placeSizeWidth = 120; /// /// Размер занимаемого объектом места (высота) /// - private readonly int _placeSizeHeight = 90; + private readonly int _placeSizeHeight = 50; /// /// Набор объектов /// @@ -60,7 +60,7 @@ namespace AircraftCarrier /// /// /// - public static bool operator +(MapWithSetWarshipsGeneric map, T warship) + public static int operator +(MapWithSetWarshipsGeneric map, T warship) { return map._setWarships.Insert(warship); } @@ -70,7 +70,7 @@ namespace AircraftCarrier /// /// /// - public static bool operator -(MapWithSetWarshipsGeneric map, int position) + public static T operator -(MapWithSetWarshipsGeneric map, int position) { return map._setWarships.Remove(position); } @@ -149,7 +149,8 @@ namespace AircraftCarrier /// private void DrawBackground(Graphics g) { - Pen pen = new(Color.Black, 3); + Pen pen = new(Color.Black, 2); + 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) @@ -165,15 +166,12 @@ namespace AircraftCarrier /// private void DrawWarships(Graphics g) { + int countInLine = _pictureWidth / _placeSizeWidth; + int maxLeft = (countInLine - 1) * _placeSizeWidth; + //for (int i = 0; i < _setWarships.Count; i++) for (int i = 0; i < _setWarships.Count; i++) { - for (int k = 0; k < _pictureWidth / _placeSizeWidth; k++) - { - for (int l = 0; l < _pictureHeight / _placeSizeHeight + 1; ++l) - { - _setWarships.Get(i)?.SetObject(k * _placeSizeWidth, l * _placeSizeHeight, _pictureWidth, _pictureHeight); - } - } + _setWarships.Get(i)?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, i / countInLine * _placeSizeHeight + 3, _pictureWidth, _pictureHeight); _setWarships.Get(i)?.DrawningObject(g); } } diff --git a/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs b/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs index 3c3e27f..e2a38df 100644 --- a/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs @@ -34,15 +34,9 @@ namespace AircraftCarrier /// /// Добавляемый военный корабль /// - public bool Insert(T warship) + public int Insert(T warship) { - if(Count >= 5 ) return false; - for(int i = Count - 1; i >= 0; i--) - { - _places[i + 1] = _places[i]; - } - _places[0] = warship; - return true; + return Insert(warship, 0); } /// /// Добавление объекта в набор на конкретную позицию @@ -50,36 +44,49 @@ namespace AircraftCarrier /// Добавляемый военный корабль /// Позиция /// - public bool Insert(T warship, int position) + public int Insert(T warship, int position) { - if(position >= _places.Length) return false; - if (_places[position] != null) + int EmptyElement = -1; + if (position >= Count || position < 0) return -1; + + if (_places[position] == null) { - for(int i = position + 1; i < _places.Length; i++) - { + _places[position] = warship; + return 1; + } + + else if (_places[position] != null) + { + for (int i = position + 1; i < Count; i++) if (_places[i] == null) { - for(int j = i - 1; j >= position; j--) - { - _places[j + 1] = _places[j]; - } + EmptyElement = i; break; - } - } + } + + if (EmptyElement == -1) + return -1; + + for (int i = EmptyElement; i > position; i--) + _places[i] = _places[i - 1]; } + _places[position] = warship; - return true; + return 1; } /// /// Удаление объекта из набора с конкретной позиции /// /// /// - public bool Remove(int position) + public T Remove(int position) { - if(position >= _places.Length) return false; + if (position >= Count || position < 0 || _places[position] == null) + return null; + + T deleted = _places[position]; _places[position] = null; - return true; + return deleted; } /// /// Получение объекта из набора по позиции