From 544a3d3006b7e111f546019704b8d1c9dc06d995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=B9=D0=B4=D0=B0=D1=80?= Date: Wed, 26 Oct 2022 17:09:12 +0400 Subject: [PATCH 1/4] generic classes --- .../Battleship/MapWithSetBattleshipGeneric.cs | 124 ++++++++++++++++++ Battleship/Battleship/SetBattleshipGeneric.cs | 61 +++++++++ 2 files changed, 185 insertions(+) create mode 100644 Battleship/Battleship/MapWithSetBattleshipGeneric.cs create mode 100644 Battleship/Battleship/SetBattleshipGeneric.cs diff --git a/Battleship/Battleship/MapWithSetBattleshipGeneric.cs b/Battleship/Battleship/MapWithSetBattleshipGeneric.cs new file mode 100644 index 0000000..3582ddd --- /dev/null +++ b/Battleship/Battleship/MapWithSetBattleshipGeneric.cs @@ -0,0 +1,124 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Battleship +{ + /// + /// Карта с набром объектов под нее + /// + /// + /// + internal class MapWithSetBattleshipGeneric + where T : class, IDrawningObject + where U : AbstractMap + { + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 130; + private readonly int _placeSizeHeight = 40; + private readonly SetBattleshipGeneric _setBattleship; + private readonly U _map; + + public MapWithSetBattleshipGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setBattleship = new SetBattleshipGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public static bool operator +(MapWithSetBattleshipGeneric map, T battleship) + { + return map._setBattleship.Insert(battleship); + } + + public static bool operator -(MapWithSetBattleshipGeneric map, int position) + { + return map._setBattleship.Remove(position); + } + + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureWidth); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawBattleship(gr, _placeSizeWidth, _placeSizeHeight, _pictureWidth, _pictureHeight); + return bmp; + } + + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setBattleship.Count; i++) + { + var battleship = _setBattleship.Get(i); + if (battleship != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, battleship); + } + } + return new(_pictureWidth, _pictureHeight); + } + + public Bitmap MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new(_pictureWidth, _pictureHeight); + } + + public void Shaking() + { + int j = _setBattleship.Count - 1; + for (int i = 0; i < _setBattleship.Count; i++) + { + if (_setBattleship.Get(i) == null) + { + for (; j > i; j--) + { + var battleship = _setBattleship.Get(j); + if (battleship != null) + { + _setBattleship.Insert(battleship, i); + _setBattleship.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + private void DrawBackground(Graphics gr) + { + Pen pen = new(Color.Black, 3); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + gr.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + } + gr.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + } + + private void DrawBattleship(Graphics gr, int x, int y, int width, int height) + { + for (int i = 0; i < _setBattleship.Count; i++) + { + _setBattleship.Get(i)?.SetObject(x, y, width, height); + _setBattleship.Get(i)?.DrawningObject(gr); + } + } + } +} diff --git a/Battleship/Battleship/SetBattleshipGeneric.cs b/Battleship/Battleship/SetBattleshipGeneric.cs new file mode 100644 index 0000000..70f68f7 --- /dev/null +++ b/Battleship/Battleship/SetBattleshipGeneric.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Battleship +{ + /// + /// Параметризованный набор объектов + /// + /// + internal class SetBattleshipGeneric + where T : class + { + private readonly T[] _places; + + public int Count => _places.Length; + + public SetBattleshipGeneric(int count) + { + _places = new T[count]; + } + + public bool Insert(T battleship) + { + _places[0] = battleship; + return true; + } + + public bool Insert(T battleship, int position) + { + int EmptyEl = position; + + if (_places[position] != null) + { + for (int i = position; i < Count; i++) + if (_places[i] == null) + { + EmptyEl = i; + break; + } + for (int j = EmptyEl; j > position; j--) + _places[j] = _places[j - 1]; + } + _places[position] = battleship; + return true; + } + + public bool Remove(int position) + { + _places[position] = null; + return true; + } + + public T Get(int position) + { + return _places[position]; + } + } +} -- 2.25.1 From bed8d7d922ea1ff2ae169fb688de1a7d2bb5fad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=B9=D0=B4=D0=B0=D1=80?= Date: Wed, 26 Oct 2022 18:47:48 +0400 Subject: [PATCH 2/4] changes forms --- Battleship/Battleship/Direction.cs | 2 +- Battleship/Battleship/DrawningBattleship.cs | 2 +- Battleship/Battleship/EntityBattleship.cs | 2 +- .../Battleship/FormBattleship.Designer.cs | 13 ++ Battleship/Battleship/FormBattleship.cs | 35 ++- Battleship/Battleship/FormMap.Designer.cs | 205 ----------------- Battleship/Battleship/FormMap.cs | 104 --------- .../FormMapWithSetBattleship.Designer.cs | 217 ++++++++++++++++++ .../Battleship/FormMapWithSetBattleship.cs | 168 ++++++++++++++ ...Map.resx => FormMapWithSetBattleship.resx} | 3 - .../Battleship/MapWithSetBattleshipGeneric.cs | 35 +-- Battleship/Battleship/Program.cs | 2 +- Battleship/Battleship/SetBattleshipGeneric.cs | 33 ++- 13 files changed, 480 insertions(+), 341 deletions(-) delete mode 100644 Battleship/Battleship/FormMap.Designer.cs delete mode 100644 Battleship/Battleship/FormMap.cs create mode 100644 Battleship/Battleship/FormMapWithSetBattleship.Designer.cs create mode 100644 Battleship/Battleship/FormMapWithSetBattleship.cs rename Battleship/Battleship/{FormMap.resx => FormMapWithSetBattleship.resx} (92%) diff --git a/Battleship/Battleship/Direction.cs b/Battleship/Battleship/Direction.cs index 048f8e8..55b4ca0 100644 --- a/Battleship/Battleship/Direction.cs +++ b/Battleship/Battleship/Direction.cs @@ -9,7 +9,7 @@ namespace Battleship /// /// Направление перемещения /// - internal enum Direction + public enum Direction { None = 0, Up = 1, diff --git a/Battleship/Battleship/DrawningBattleship.cs b/Battleship/Battleship/DrawningBattleship.cs index 6341f46..fa41b00 100644 --- a/Battleship/Battleship/DrawningBattleship.cs +++ b/Battleship/Battleship/DrawningBattleship.cs @@ -9,7 +9,7 @@ namespace Battleship /// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности /// - internal class DrawningBattleship + public class DrawningBattleship { /// /// Класс-сущность diff --git a/Battleship/Battleship/EntityBattleship.cs b/Battleship/Battleship/EntityBattleship.cs index 00a13de..bb54fa5 100644 --- a/Battleship/Battleship/EntityBattleship.cs +++ b/Battleship/Battleship/EntityBattleship.cs @@ -9,7 +9,7 @@ namespace Battleship /// /// Класс-сущность "Боевой корабль" /// - internal class EntityBattleship + public class EntityBattleship { /// /// Скорость diff --git a/Battleship/Battleship/FormBattleship.Designer.cs b/Battleship/Battleship/FormBattleship.Designer.cs index fe452e7..b1b1a09 100644 --- a/Battleship/Battleship/FormBattleship.Designer.cs +++ b/Battleship/Battleship/FormBattleship.Designer.cs @@ -39,6 +39,7 @@ this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripStatusLabeBodylColor = new System.Windows.Forms.ToolStripStatusLabel(); this.statusStripBattleship = new System.Windows.Forms.StatusStrip(); + this.buttonSelectBatlleship = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxBattleship)).BeginInit(); this.statusStripBattleship.SuspendLayout(); this.SuspendLayout(); @@ -149,12 +150,23 @@ this.statusStripBattleship.TabIndex = 1; this.statusStripBattleship.Text = "statusStrip1"; // + // buttonSelectBatlleship + // + this.buttonSelectBatlleship.Location = new System.Drawing.Point(614, 397); + this.buttonSelectBatlleship.Name = "buttonSelectBatlleship"; + this.buttonSelectBatlleship.Size = new System.Drawing.Size(75, 23); + this.buttonSelectBatlleship.TabIndex = 8; + this.buttonSelectBatlleship.Text = "Выбрать"; + this.buttonSelectBatlleship.UseVisualStyleBackColor = true; + this.buttonSelectBatlleship.Click += new System.EventHandler(this.ButtonSelectBatlleship_Click); + // // FormBattleship // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoSize = true; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.buttonSelectBatlleship); this.Controls.Add(this.buttonCreateModif); this.Controls.Add(this.buttonLeft); this.Controls.Add(this.buttonRight); @@ -186,5 +198,6 @@ private ToolStripStatusLabel toolStripStatusLabelWeight; private ToolStripStatusLabel toolStripStatusLabeBodylColor; private StatusStrip statusStripBattleship; + private Button buttonSelectBatlleship; } } \ No newline at end of file diff --git a/Battleship/Battleship/FormBattleship.cs b/Battleship/Battleship/FormBattleship.cs index 534d5d6..61d77eb 100644 --- a/Battleship/Battleship/FormBattleship.cs +++ b/Battleship/Battleship/FormBattleship.cs @@ -3,6 +3,10 @@ namespace Battleship public partial class FormBattleship : Form { private DrawningBattleship _battleship; + /// + /// + /// + public DrawningBattleship SelectedBattleship { get; private set; } public FormBattleship() { @@ -38,7 +42,13 @@ namespace Battleship private void ButtonCreate_Click(object sender, EventArgs e) { Random rnd = new(); - _battleship = new DrawningBattleship(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); + Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + _battleship = new DrawningBattleship(rnd.Next(100, 300), rnd.Next(1000, 2000), color); SetData(); Draw(); } @@ -86,12 +96,29 @@ namespace Battleship private void ButtonCreateModif_Click(object sender, EventArgs e) { Random rnd = new(); - _battleship = new DrawningGunBattleship(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; + } + _battleship = new DrawningGunBattleship(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(); } + + private void ButtonSelectBatlleship_Click(object sender, EventArgs e) + { + SelectedBattleship = _battleship; + DialogResult = DialogResult.OK; + + } } } \ No newline at end of file diff --git a/Battleship/Battleship/FormMap.Designer.cs b/Battleship/Battleship/FormMap.Designer.cs deleted file mode 100644 index 2cb8b9a..0000000 --- a/Battleship/Battleship/FormMap.Designer.cs +++ /dev/null @@ -1,205 +0,0 @@ -namespace Battleship -{ - 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.pictureBoxBattleship = new System.Windows.Forms.PictureBox(); - this.buttonCreate = new System.Windows.Forms.Button(); - this.buttonDown = new System.Windows.Forms.Button(); - this.buttonUp = new System.Windows.Forms.Button(); - this.buttonRight = new System.Windows.Forms.Button(); - this.buttonLeft = new System.Windows.Forms.Button(); - this.buttonCreateModif = new System.Windows.Forms.Button(); - this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel(); - this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel(); - this.toolStripStatusLabeBodylColor = new System.Windows.Forms.ToolStripStatusLabel(); - this.statusStripBattleship = new System.Windows.Forms.StatusStrip(); - this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxBattleship)).BeginInit(); - this.statusStripBattleship.SuspendLayout(); - this.SuspendLayout(); - // - // pictureBoxBattleship - // - this.pictureBoxBattleship.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBoxBattleship.Location = new System.Drawing.Point(0, 0); - this.pictureBoxBattleship.Name = "pictureBoxBattleship"; - this.pictureBoxBattleship.Size = new System.Drawing.Size(800, 450); - this.pictureBoxBattleship.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; - this.pictureBoxBattleship.TabIndex = 0; - this.pictureBoxBattleship.TabStop = false; - // - // 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, 397); - this.buttonCreate.Name = "buttonCreate"; - this.buttonCreate.Size = new System.Drawing.Size(75, 23); - this.buttonCreate.TabIndex = 2; - this.buttonCreate.Text = "Создание"; - 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::Battleship.Properties.Resources.BatteleshipDown; - this.buttonDown.Location = new System.Drawing.Point(731, 390); - this.buttonDown.Name = "buttonDown"; - this.buttonDown.Size = new System.Drawing.Size(30, 30); - this.buttonDown.TabIndex = 3; - 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::Battleship.Properties.Resources.BattleshipUp; - this.buttonUp.Location = new System.Drawing.Point(732, 361); - this.buttonUp.Name = "buttonUp"; - this.buttonUp.Size = new System.Drawing.Size(30, 30); - this.buttonUp.TabIndex = 4; - this.buttonUp.UseVisualStyleBackColor = true; - this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); - // - // buttonRight - // - this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonRight.BackgroundImage = global::Battleship.Properties.Resources.BattleshipRight; - this.buttonRight.Location = new System.Drawing.Point(758, 390); - this.buttonRight.Name = "buttonRight"; - this.buttonRight.Size = new System.Drawing.Size(30, 30); - this.buttonRight.TabIndex = 5; - 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::Battleship.Properties.Resources.BattleshipLeft; - this.buttonLeft.Location = new System.Drawing.Point(706, 390); - this.buttonLeft.Name = "buttonLeft"; - this.buttonLeft.Size = new System.Drawing.Size(30, 30); - this.buttonLeft.TabIndex = 6; - this.buttonLeft.UseVisualStyleBackColor = true; - this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); - // - // buttonCreateModif - // - this.buttonCreateModif.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.buttonCreateModif.Location = new System.Drawing.Point(93, 397); - this.buttonCreateModif.Name = "buttonCreateModif"; - this.buttonCreateModif.Size = new System.Drawing.Size(110, 23); - this.buttonCreateModif.TabIndex = 7; - this.buttonCreateModif.Text = "Модификация"; - this.buttonCreateModif.UseVisualStyleBackColor = true; - this.buttonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click); - // - // toolStripStatusLabelSpeed - // - this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; - this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(65, 17); - this.toolStripStatusLabelSpeed.Text = "Скорость: "; - // - // toolStripStatusLabelWeight - // - this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; - this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(32, 17); - this.toolStripStatusLabelWeight.Text = "Вес: "; - // - // toolStripStatusLabeBodylColor - // - this.toolStripStatusLabeBodylColor.Name = "toolStripStatusLabeBodylColor"; - this.toolStripStatusLabeBodylColor.Size = new System.Drawing.Size(36, 17); - this.toolStripStatusLabeBodylColor.Text = "Цвет:"; - // - // statusStripBattleship - // - this.statusStripBattleship.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripStatusLabelSpeed, - this.toolStripStatusLabelWeight, - this.toolStripStatusLabeBodylColor}); - this.statusStripBattleship.Location = new System.Drawing.Point(0, 428); - this.statusStripBattleship.Name = "statusStripBattleship"; - this.statusStripBattleship.Size = new System.Drawing.Size(800, 22); - this.statusStripBattleship.TabIndex = 1; - this.statusStripBattleship.Text = "statusStrip1"; - // - // comboBoxSelectorMap - // - this.comboBoxSelectorMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - 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(209, 397); - 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.AutoSize = true; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Controls.Add(this.comboBoxSelectorMap); - this.Controls.Add(this.buttonCreateModif); - this.Controls.Add(this.buttonLeft); - this.Controls.Add(this.buttonRight); - this.Controls.Add(this.buttonUp); - this.Controls.Add(this.buttonDown); - this.Controls.Add(this.buttonCreate); - this.Controls.Add(this.statusStripBattleship); - this.Controls.Add(this.pictureBoxBattleship); - this.Name = "FormMap"; - this.Text = "Военный корабль "; - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxBattleship)).EndInit(); - this.statusStripBattleship.ResumeLayout(false); - this.statusStripBattleship.PerformLayout(); - this.ResumeLayout(false); - this.PerformLayout(); - - } - #endregion - private PictureBox pictureBoxBattleship; - private Button buttonCreate; - private Button buttonDown; - private Button buttonUp; - private Button buttonRight; - private Button buttonLeft; - private Button buttonCreateModif; - private ToolStripStatusLabel toolStripStatusLabelSpeed; - private ToolStripStatusLabel toolStripStatusLabelWeight; - private ToolStripStatusLabel toolStripStatusLabeBodylColor; - private StatusStrip statusStripBattleship; - private ComboBox comboBoxSelectorMap; - } -} \ No newline at end of file diff --git a/Battleship/Battleship/FormMap.cs b/Battleship/Battleship/FormMap.cs deleted file mode 100644 index f0a2a89..0000000 --- a/Battleship/Battleship/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 Battleship -{ - public partial class FormMap : Form - { - private AbstractMap _abstractMap; - - public FormMap() - { - InitializeComponent(); - _abstractMap = new SimpleMap(); - } - /// - /// Заполнение информации по объекту - /// - /// - private void SetData(DrawningBattleship battleship) - { - toolStripStatusLabelSpeed.Text = $"Скорость: {battleship.Battleship.Speed}"; - toolStripStatusLabelWeight.Text = $"Вес: {battleship.Battleship.Weight}"; - toolStripStatusLabeBodylColor.Text = $"Цвет: {battleship.Battleship.BodyColor.Name}"; - pictureBoxBattleship.Image = _abstractMap.CreateMap(pictureBoxBattleship.Width, pictureBoxBattleship.Height, - new DrawningObjectBattleship(battleship)); - } - /// - /// Обработка нажатия кнопки "Создать" - /// - /// - /// - private void ButtonCreate_Click(object sender, EventArgs e) - { - Random rnd = new(); - var car = new DrawningBattleship(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); - SetData(car); - } - /// - /// Изменение размеров формы - /// - /// - /// - 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; - } - pictureBoxBattleship.Image = _abstractMap?.MoveObject(dir); - } - /// - /// Обработка нажатия кнопки "Модификация" - /// - /// - /// - private void ButtonCreateModif_Click(object sender, EventArgs e) - { - Random rnd = new(); - var car = new DrawningGunBattleship(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 WaterMap(); - break; - } - } - } -} diff --git a/Battleship/Battleship/FormMapWithSetBattleship.Designer.cs b/Battleship/Battleship/FormMapWithSetBattleship.Designer.cs new file mode 100644 index 0000000..3d0a147 --- /dev/null +++ b/Battleship/Battleship/FormMapWithSetBattleship.Designer.cs @@ -0,0 +1,217 @@ +namespace Battleship +{ + partial class FormMapWithSetBattleship + { + /// + /// 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.buttonDown = new System.Windows.Forms.Button(); + this.buttonLeft = new System.Windows.Forms.Button(); + this.buttonUp = new System.Windows.Forms.Button(); + this.buttonRight = new System.Windows.Forms.Button(); + this.buttonShowOnMap = new System.Windows.Forms.Button(); + this.buttonShowStorage = new System.Windows.Forms.Button(); + this.buttonRemoveBattleship = new System.Windows.Forms.Button(); + this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); + this.buttonAddBattleship = 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(); + // + // groupBoxTools + // + this.groupBoxTools.Controls.Add(this.buttonDown); + this.groupBoxTools.Controls.Add(this.buttonLeft); + this.groupBoxTools.Controls.Add(this.buttonUp); + this.groupBoxTools.Controls.Add(this.buttonRight); + this.groupBoxTools.Controls.Add(this.buttonShowOnMap); + this.groupBoxTools.Controls.Add(this.buttonShowStorage); + this.groupBoxTools.Controls.Add(this.buttonRemoveBattleship); + this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); + this.groupBoxTools.Controls.Add(this.buttonAddBattleship); + this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap); + this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; + this.groupBoxTools.Location = new System.Drawing.Point(632, 0); + this.groupBoxTools.Name = "groupBoxTools"; + this.groupBoxTools.Size = new System.Drawing.Size(200, 493); + this.groupBoxTools.TabIndex = 0; + this.groupBoxTools.TabStop = false; + this.groupBoxTools.Text = "Инструменты"; + // + // buttonDown + // + this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonDown.BackgroundImage = global::Battleship.Properties.Resources.BatteleshipDown; + this.buttonDown.Location = new System.Drawing.Point(77, 449); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(30, 30); + this.buttonDown.TabIndex = 9; + 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::Battleship.Properties.Resources.BattleshipLeft; + this.buttonLeft.Location = new System.Drawing.Point(41, 449); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(30, 30); + this.buttonLeft.TabIndex = 8; + 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::Battleship.Properties.Resources.BattleshipUp; + this.buttonUp.Location = new System.Drawing.Point(77, 413); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(30, 30); + this.buttonUp.TabIndex = 7; + this.buttonUp.UseVisualStyleBackColor = true; + this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonRight + // + this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonRight.BackgroundImage = global::Battleship.Properties.Resources.BattleshipRight; + this.buttonRight.Location = new System.Drawing.Point(113, 449); + this.buttonRight.Name = "buttonRight"; + this.buttonRight.Size = new System.Drawing.Size(30, 30); + this.buttonRight.TabIndex = 6; + this.buttonRight.UseVisualStyleBackColor = true; + this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonShowOnMap + // + this.buttonShowOnMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonShowOnMap.Location = new System.Drawing.Point(6, 367); + this.buttonShowOnMap.Name = "buttonShowOnMap"; + this.buttonShowOnMap.Size = new System.Drawing.Size(188, 30); + this.buttonShowOnMap.TabIndex = 5; + this.buttonShowOnMap.Text = "Посмотреть карту"; + this.buttonShowOnMap.UseVisualStyleBackColor = true; + this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); + // + // buttonShowStorage + // + this.buttonShowStorage.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonShowStorage.Location = new System.Drawing.Point(6, 290); + this.buttonShowStorage.Name = "buttonShowStorage"; + this.buttonShowStorage.Size = new System.Drawing.Size(188, 30); + this.buttonShowStorage.TabIndex = 4; + this.buttonShowStorage.Text = "Посмотреть хранилище"; + this.buttonShowStorage.UseVisualStyleBackColor = true; + this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); + // + // buttonRemoveBattleship + // + this.buttonRemoveBattleship.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonRemoveBattleship.Location = new System.Drawing.Point(6, 195); + this.buttonRemoveBattleship.Name = "buttonRemoveBattleship"; + this.buttonRemoveBattleship.Size = new System.Drawing.Size(188, 29); + this.buttonRemoveBattleship.TabIndex = 3; + this.buttonRemoveBattleship.Text = "Удалить корабль"; + this.buttonRemoveBattleship.UseVisualStyleBackColor = true; + this.buttonRemoveBattleship.Click += new System.EventHandler(this.ButtonRemoveBattleship_Click); + // + // maskedTextBoxPosition + // + this.maskedTextBoxPosition.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 166); + this.maskedTextBoxPosition.Mask = "00"; + this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + this.maskedTextBoxPosition.Size = new System.Drawing.Size(188, 23); + this.maskedTextBoxPosition.TabIndex = 2; + // + // buttonAddBattleship + // + this.buttonAddBattleship.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonAddBattleship.Location = new System.Drawing.Point(6, 112); + this.buttonAddBattleship.Name = "buttonAddBattleship"; + this.buttonAddBattleship.Size = new System.Drawing.Size(188, 29); + this.buttonAddBattleship.TabIndex = 1; + this.buttonAddBattleship.Text = "Добавить корабль"; + this.buttonAddBattleship.UseVisualStyleBackColor = true; + this.buttonAddBattleship.Click += new System.EventHandler(this.ButtonAddBattleship_Click); + // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.comboBoxSelectorMap.FormattingEnabled = true; + this.comboBoxSelectorMap.Items.AddRange(new object[] { + "Простая карта", + "Водная карта"}); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 32); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(188, 23); + this.comboBoxSelectorMap.TabIndex = 0; + this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + // + // 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(632, 493); + this.pictureBox.TabIndex = 0; + this.pictureBox.TabStop = false; + // + // FormMapWithSetBattleship + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(832, 493); + this.Controls.Add(this.pictureBox); + this.Controls.Add(this.groupBoxTools); + this.Name = "FormMapWithSetBattleship"; + this.Text = "FormMapWithSetBattleship"; + this.groupBoxTools.ResumeLayout(false); + this.groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private GroupBox groupBoxTools; + private Button buttonDown; + private Button buttonLeft; + private Button buttonUp; + private Button buttonRight; + private Button buttonShowOnMap; + private Button buttonShowStorage; + private Button buttonRemoveBattleship; + private MaskedTextBox maskedTextBoxPosition; + private Button buttonAddBattleship; + private ComboBox comboBoxSelectorMap; + private PictureBox pictureBox; + } +} \ No newline at end of file diff --git a/Battleship/Battleship/FormMapWithSetBattleship.cs b/Battleship/Battleship/FormMapWithSetBattleship.cs new file mode 100644 index 0000000..1e90bfe --- /dev/null +++ b/Battleship/Battleship/FormMapWithSetBattleship.cs @@ -0,0 +1,168 @@ +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 Battleship +{ + public partial class FormMapWithSetBattleship : Form + { + /// + /// Объект от класса карты с набором объектов + /// + private MapWithSetBattleshipGeneric _mapBattleshipCollectionGeneric; + /// + /// Конструктор + /// + public FormMapWithSetBattleship() + { + InitializeComponent(); + } + /// + /// Выбор карты + /// + /// + /// + private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + { + AbstractMap map = null; + switch (comboBoxSelectorMap.Text) + { + case "Простая карта": + map = new SimpleMap(); + break; + } + switch (comboBoxSelectorMap.Text) + { + case "Водная карта": + map = new WaterMap(); + break; + } + if (map != null) + { + _mapBattleshipCollectionGeneric = new MapWithSetBattleshipGeneric( + pictureBox.Width, pictureBox.Height, map); + } + else + { + _mapBattleshipCollectionGeneric = null; + } + } + /// + /// Добавление объекта + /// + /// + /// + private void ButtonAddBattleship_Click(object sender, EventArgs e) + { + if (_mapBattleshipCollectionGeneric == null) + { + return; + } + FormBattleship form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + DrawningObjectBattleship battleship = new(form.SelectedBattleship); + if (_mapBattleshipCollectionGeneric + battleship) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _mapBattleshipCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта + /// + /// + /// + private void ButtonRemoveBattleship_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 (_mapBattleshipCollectionGeneric - pos) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _mapBattleshipCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Вывод набора + /// + /// + /// + private void ButtonShowStorage_Click(object sender, EventArgs e) + { + if (_mapBattleshipCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapBattleshipCollectionGeneric.ShowSet(); + } + /// + /// Вывод карты + /// + /// + /// + private void ButtonShowOnMap_Click(object sender, EventArgs e) + { + if (_mapBattleshipCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapBattleshipCollectionGeneric.ShowOnMap(); + } + /// + /// Перемещение + /// + /// + /// + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_mapBattleshipCollectionGeneric == 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 = _mapBattleshipCollectionGeneric.MoveObject(dir); + } + } +} + diff --git a/Battleship/Battleship/FormMap.resx b/Battleship/Battleship/FormMapWithSetBattleship.resx similarity index 92% rename from Battleship/Battleship/FormMap.resx rename to Battleship/Battleship/FormMapWithSetBattleship.resx index dd36fec..f298a7b 100644 --- a/Battleship/Battleship/FormMap.resx +++ b/Battleship/Battleship/FormMapWithSetBattleship.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/Battleship/Battleship/MapWithSetBattleshipGeneric.cs b/Battleship/Battleship/MapWithSetBattleshipGeneric.cs index 3582ddd..f4d2163 100644 --- a/Battleship/Battleship/MapWithSetBattleshipGeneric.cs +++ b/Battleship/Battleship/MapWithSetBattleshipGeneric.cs @@ -17,8 +17,8 @@ namespace Battleship { private readonly int _pictureWidth; private readonly int _pictureHeight; - private readonly int _placeSizeWidth = 130; - private readonly int _placeSizeHeight = 40; + private readonly int _placeSizeWidth = 150; + private readonly int _placeSizeHeight = 45; private readonly SetBattleshipGeneric _setBattleship; private readonly U _map; @@ -32,9 +32,9 @@ namespace Battleship _map = map; } - public static bool operator +(MapWithSetBattleshipGeneric map, T battleship) + public static bool operator +(MapWithSetBattleshipGeneric map, T warship) { - return map._setBattleship.Insert(battleship); + return map._setBattleship.Insert(warship); } public static bool operator -(MapWithSetBattleshipGeneric map, int position) @@ -47,7 +47,7 @@ namespace Battleship Bitmap bmp = new(_pictureWidth, _pictureWidth); Graphics gr = Graphics.FromImage(bmp); DrawBackground(gr); - DrawBattleship(gr, _placeSizeWidth, _placeSizeHeight, _pictureWidth, _pictureHeight); + DrawWarship(gr); return bmp; } @@ -98,26 +98,33 @@ namespace Battleship } } } - - private void DrawBackground(Graphics gr) + private void DrawBackground(Graphics g) { - Pen pen = new(Color.Black, 3); + Pen pen = new(Color.LightSlateGray, 3); + Brush brush = new SolidBrush(Color.SkyBlue); + g.FillRectangle(brush, 0, 0, _pictureWidth, _pictureHeight); for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) { - gr.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight + 2, i * _placeSizeWidth + _placeSizeWidth / 2 + 40, j * _placeSizeHeight + 2); } - gr.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + g.DrawLine(pen, i * _placeSizeWidth + 7, 2, i * _placeSizeWidth + 7, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + g.DrawLine(pen, i * _placeSizeWidth + 3, 2, i * _placeSizeWidth + 3, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); } } - - private void DrawBattleship(Graphics gr, int x, int y, int width, int height) + private void DrawWarship(Graphics gr) { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + for (int i = 0; i < _setBattleship.Count; i++) { - _setBattleship.Get(i)?.SetObject(x, y, width, height); - _setBattleship.Get(i)?.DrawningObject(gr); + if (_setBattleship.Get(i) != null) + { + _setBattleship.Get(i).SetObject(i % width * _placeSizeWidth + 15, (height - 1 - i / width) * _placeSizeHeight + 8, _pictureWidth, _pictureHeight); + _setBattleship.Get(i)?.DrawningObject(gr); + } } } } diff --git a/Battleship/Battleship/Program.cs b/Battleship/Battleship/Program.cs index 14fd6f5..d328611 100644 --- a/Battleship/Battleship/Program.cs +++ b/Battleship/Battleship/Program.cs @@ -11,7 +11,7 @@ namespace Battleship // 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 FormMapWithSetBattleship()); } } } \ No newline at end of file diff --git a/Battleship/Battleship/SetBattleshipGeneric.cs b/Battleship/Battleship/SetBattleshipGeneric.cs index 70f68f7..9733c69 100644 --- a/Battleship/Battleship/SetBattleshipGeneric.cs +++ b/Battleship/Battleship/SetBattleshipGeneric.cs @@ -24,37 +24,56 @@ namespace Battleship public bool Insert(T battleship) { - _places[0] = battleship; - return true; + return Insert(battleship, 0); } public bool Insert(T battleship, int position) { - int EmptyEl = position; + int EmptyEl = -1; - if (_places[position] != null) + if (position >= Count || position < 0) + return false; + + if (_places[position] == null) { - for (int i = position; i < Count; i++) + _places[position] = battleship; + return true; + } + + else if (_places[position] != null) + { + for (int i = position + 1; i < Count; i++) if (_places[i] == null) { EmptyEl = i; break; } - for (int j = EmptyEl; j > position; j--) - _places[j] = _places[j - 1]; + + if (EmptyEl == -1) + return false; + + for (int i = EmptyEl; i > position; i--) + _places[i] = _places[i - 1]; } + _places[position] = battleship; return true; } public bool Remove(int position) { + if (position >= Count || position < 0 || _places[position] == null) + return false; + _places[position] = null; return true; } public T Get(int position) { + if (position >= Count || position < 0) + return null; + return _places[position]; } } -- 2.25.1 From a678395c8b08eb3eb1c700a423bcc27ac61a488a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=B9=D0=B4=D0=B0=D1=80?= Date: Sun, 30 Oct 2022 15:40:58 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=203.=20=D0=A4?= =?UTF-8?q?=D0=BE=D1=80=D0=BC=D0=B0=20=D0=B8=20=D0=B7=D0=B0=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20bool.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Battleship/FormMapWithSetBattleship.cs | 4 +- .../Battleship/MapWithSetBattleshipGeneric.cs | 8 ++- Battleship/Battleship/SetBattleshipGeneric.cs | 52 +++++++++---------- 3 files changed, 29 insertions(+), 35 deletions(-) diff --git a/Battleship/Battleship/FormMapWithSetBattleship.cs b/Battleship/Battleship/FormMapWithSetBattleship.cs index 1e90bfe..d569a75 100644 --- a/Battleship/Battleship/FormMapWithSetBattleship.cs +++ b/Battleship/Battleship/FormMapWithSetBattleship.cs @@ -69,7 +69,7 @@ namespace Battleship if (form.ShowDialog() == DialogResult.OK) { DrawningObjectBattleship battleship = new(form.SelectedBattleship); - if (_mapBattleshipCollectionGeneric + battleship) + if (_mapBattleshipCollectionGeneric + battleship != null ) { MessageBox.Show("Объект добавлен"); pictureBox.Image = _mapBattleshipCollectionGeneric.ShowSet(); @@ -96,7 +96,7 @@ namespace Battleship return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapBattleshipCollectionGeneric - pos) + if (_mapBattleshipCollectionGeneric - pos != null) { MessageBox.Show("Объект удален"); pictureBox.Image = _mapBattleshipCollectionGeneric.ShowSet(); diff --git a/Battleship/Battleship/MapWithSetBattleshipGeneric.cs b/Battleship/Battleship/MapWithSetBattleshipGeneric.cs index f4d2163..a458549 100644 --- a/Battleship/Battleship/MapWithSetBattleshipGeneric.cs +++ b/Battleship/Battleship/MapWithSetBattleshipGeneric.cs @@ -31,13 +31,11 @@ namespace Battleship _pictureHeight = picHeight; _map = map; } - - public static bool operator +(MapWithSetBattleshipGeneric map, T warship) + public static int operator +(MapWithSetBattleshipGeneric map, T battleship) { - return map._setBattleship.Insert(warship); + return map._setBattleship.Insert(battleship); } - - public static bool operator -(MapWithSetBattleshipGeneric map, int position) + public static T operator -(MapWithSetBattleshipGeneric map, int position) { return map._setBattleship.Remove(position); } diff --git a/Battleship/Battleship/SetBattleshipGeneric.cs b/Battleship/Battleship/SetBattleshipGeneric.cs index 9733c69..815ba49 100644 --- a/Battleship/Battleship/SetBattleshipGeneric.cs +++ b/Battleship/Battleship/SetBattleshipGeneric.cs @@ -22,53 +22,49 @@ namespace Battleship _places = new T[count]; } - public bool Insert(T battleship) + public int Insert(T battleship) { return Insert(battleship, 0); } - public bool Insert(T battleship, int position) + public int Insert(T battleship, int position) { - int EmptyEl = -1; - - if (position >= Count || position < 0) - return false; - - if (_places[position] == null) + if (position >= _places.Length) { - _places[position] = battleship; - return true; + return -1; } - - else if (_places[position] != null) + if (_places[position] != null) { - for (int i = position + 1; i < Count; i++) + int indexNull = -1; + for (int i = position; i < _places.Length; i++) + { if (_places[i] == null) { - EmptyEl = i; + indexNull = i; break; } - - if (EmptyEl == -1) - return false; - - for (int i = EmptyEl; i > position; i--) + } + if (indexNull == -1) return -1; + for (int i = indexNull; i > position; i--) + { + T tmp = _places[i]; _places[i] = _places[i - 1]; + _places[i - 1] = tmp; + } } - _places[position] = battleship; - return true; + return position; } - - public bool Remove(int position) + public T Remove(int position) { - if (position >= Count || position < 0 || _places[position] == null) - return false; - + if (position >= _places.Length) + { + return null; + } + T removedObject = _places[position]; _places[position] = null; - return true; + return removedObject; } - public T Get(int position) { if (position >= Count || position < 0) -- 2.25.1 From 1429feb1a04f2449144161f67b75668056b242e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=B9=D0=B4=D0=B0=D1=80?= Date: Sun, 30 Oct 2022 15:40:58 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=203.=20=D0=A4?= =?UTF-8?q?=D0=BE=D1=80=D0=BC=D0=B0=20=D0=B8=20=D0=B7=D0=B0=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20bool.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Battleship/Battleship/MapWithSetBattleshipGeneric.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Battleship/Battleship/MapWithSetBattleshipGeneric.cs b/Battleship/Battleship/MapWithSetBattleshipGeneric.cs index a458549..81bdbd8 100644 --- a/Battleship/Battleship/MapWithSetBattleshipGeneric.cs +++ b/Battleship/Battleship/MapWithSetBattleshipGeneric.cs @@ -45,7 +45,7 @@ namespace Battleship Bitmap bmp = new(_pictureWidth, _pictureWidth); Graphics gr = Graphics.FromImage(bmp); DrawBackground(gr); - DrawWarship(gr); + DrawBattleship(gr); return bmp; } @@ -111,7 +111,7 @@ namespace Battleship g.DrawLine(pen, i * _placeSizeWidth + 3, 2, i * _placeSizeWidth + 3, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); } } - private void DrawWarship(Graphics gr) + private void DrawBattleship(Graphics gr) { int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; -- 2.25.1