From 296f2fdacda93f45c15c72f8312d3c1ed8c8d567 Mon Sep 17 00:00:00 2001 From: AnnZhimol Date: Fri, 30 Sep 2022 13:02:18 +0400 Subject: [PATCH 1/5] generic classes --- Warship/Warship/MapWithSetWarshipsGeneric.cs | 119 +++++++++++++++++++ Warship/Warship/SetWarshipsGeneric.cs | 57 +++++++++ 2 files changed, 176 insertions(+) create mode 100644 Warship/Warship/MapWithSetWarshipsGeneric.cs create mode 100644 Warship/Warship/SetWarshipsGeneric.cs diff --git a/Warship/Warship/MapWithSetWarshipsGeneric.cs b/Warship/Warship/MapWithSetWarshipsGeneric.cs new file mode 100644 index 0000000..180a80f --- /dev/null +++ b/Warship/Warship/MapWithSetWarshipsGeneric.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Warship +{ + internal class MapWithSetWarshipsGeneric + where T : class, IDrawingObject + where U : AbstractMap + { + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 120; + private readonly int _placeSizeHeight = 50; + private readonly SetWarshipsGeneric _setWarship; + private readonly U _map; + + public MapWithSetWarshipsGeneric(int picWidth,int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight/_placeSizeHeight; + _setWarship = new SetWarshipsGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public static bool operator +(MapWithSetWarshipsGeneric map, T warship) + { + return map._setWarship.Insert(warship); + } + + public static bool operator -(MapWithSetWarshipsGeneric map, int position) + { + return map._setWarship.Remove(position); + } + + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureWidth); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawWarship(gr, _placeSizeWidth,_placeSizeHeight,_pictureWidth,_pictureHeight); + return bmp; + } + + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setWarship.Count; i++) + { + var warship = _setWarship.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); + } + + public void Shaking() + { + int j = _setWarship.Count - 1; + for (int i = 0; i < _setWarship.Count; i++) + { + if (_setWarship.Get(i) == null) + { + for (; j > i; j--) + { + var warship = _setWarship.Get(j); + if (warship != null) + { + _setWarship.Insert(warship, i); + _setWarship.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 DrawWarship(Graphics gr, int x,int y,int width,int height) + { + for (int i = 0; i < _setWarship.Count; i++) + { + _setWarship.Get(i)?.SetObject(x, y,width,height); + _setWarship.Get(i)?.DrawingObject(gr); + } + } + } +} diff --git a/Warship/Warship/SetWarshipsGeneric.cs b/Warship/Warship/SetWarshipsGeneric.cs new file mode 100644 index 0000000..ccf9090 --- /dev/null +++ b/Warship/Warship/SetWarshipsGeneric.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Warship +{ + 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) + { + _places[0] = warship; + return true; + } + + public bool Insert(T warship, 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] = warship; + return true; + } + + public bool Remove(int position) + { + _places[position] = null; + return true; + } + + public T Get(int position) + { + return _places[position]; + } + } +} -- 2.25.1 From c9d1b9f133d7be30a445f8ea7825e85e2f681c16 Mon Sep 17 00:00:00 2001 From: AnnZhimol Date: Fri, 30 Sep 2022 14:23:18 +0400 Subject: [PATCH 2/5] changes forms --- Warship/Warship/Direction.cs | 2 +- Warship/Warship/DrawingWarship.cs | 2 +- Warship/Warship/EntityWarship.cs | 2 +- Warship/Warship/FormMap.Designer.cs | 211 ------------------ Warship/Warship/FormMap.cs | 78 ------- .../FormMapWithSetWarships.Designer.cs | 210 +++++++++++++++++ Warship/Warship/FormMapWithSetWarships.cs | 130 +++++++++++ ...rmMap.resx => FormMapWithSetWarships.resx} | 3 - Warship/Warship/FormWarship.Designer.cs | 13 ++ Warship/Warship/FormWarship.cs | 8 + Warship/Warship/MapWithSetWarshipsGeneric.cs | 12 +- Warship/Warship/Program.cs | 2 +- Warship/Warship/SetWarshipsGeneric.cs | 1 + 13 files changed, 375 insertions(+), 299 deletions(-) delete mode 100644 Warship/Warship/FormMap.Designer.cs delete mode 100644 Warship/Warship/FormMap.cs create mode 100644 Warship/Warship/FormMapWithSetWarships.Designer.cs create mode 100644 Warship/Warship/FormMapWithSetWarships.cs rename Warship/Warship/{FormMap.resx => FormMapWithSetWarships.resx} (93%) diff --git a/Warship/Warship/Direction.cs b/Warship/Warship/Direction.cs index cda5d03..ffed995 100644 --- a/Warship/Warship/Direction.cs +++ b/Warship/Warship/Direction.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Warship { - internal enum Direction + public enum Direction { None=0, Up=1, diff --git a/Warship/Warship/DrawingWarship.cs b/Warship/Warship/DrawingWarship.cs index 30f4f44..d985f58 100644 --- a/Warship/Warship/DrawingWarship.cs +++ b/Warship/Warship/DrawingWarship.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Warship { - internal class DrawingWarship + public class DrawingWarship { public EntityWarship Warship { get; protected set; } protected int _startPosX; diff --git a/Warship/Warship/EntityWarship.cs b/Warship/Warship/EntityWarship.cs index 288a008..b4d0daa 100644 --- a/Warship/Warship/EntityWarship.cs +++ b/Warship/Warship/EntityWarship.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Warship { - internal class EntityWarship + public class EntityWarship { public int Speed { get; private set; } public float Weight { get; private set; } diff --git a/Warship/Warship/FormMap.Designer.cs b/Warship/Warship/FormMap.Designer.cs deleted file mode 100644 index 5f3bf0f..0000000 --- a/Warship/Warship/FormMap.Designer.cs +++ /dev/null @@ -1,211 +0,0 @@ -namespace Warship -{ - 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.buttonRight = new System.Windows.Forms.Button(); - this.buttonLeft = 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(800, 428); - 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, 428); - this.statusStrip.Name = "statusStrip"; - this.statusStrip.Size = new System.Drawing.Size(800, 22); - this.statusStrip.TabIndex = 1; - // - // toolStripStatusLabelSpeed - // - this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; - this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(62, 17); - this.toolStripStatusLabelSpeed.Text = "Скорость:"; - // - // toolStripStatusLabelWeight - // - this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; - this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(29, 17); - this.toolStripStatusLabelWeight.Text = "Вес:"; - // - // toolStripStatusLabelBodyColor - // - this.toolStripStatusLabelBodyColor.Name = "toolStripStatusLabelBodyColor"; - this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(36, 17); - this.toolStripStatusLabelBodyColor.Text = "Цвет:"; - // - // 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 = "Создать"; - 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::Warship.Properties.Resources.arrowDown; - this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(716, 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::Warship.Properties.Resources.arrowUp; - this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(716, 314); - 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); - // - // buttonRight - // - this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonRight.BackgroundImage = global::Warship.Properties.Resources.arrowRight; - this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(752, 350); - this.buttonRight.Name = "buttonRight"; - this.buttonRight.Size = new System.Drawing.Size(30, 30); - this.buttonRight.TabIndex = 5; - 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::Warship.Properties.Resources.arrowLeft; - this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(680, 350); - this.buttonLeft.Name = "buttonLeft"; - this.buttonLeft.Size = new System.Drawing.Size(30, 30); - this.buttonLeft.TabIndex = 6; - this.buttonLeft.Text = " "; - this.buttonLeft.UseVisualStyleBackColor = true; - this.buttonLeft.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(110, 23); - this.buttonCreateModif.TabIndex = 7; - this.buttonCreateModif.Text = "Модификация"; - 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(667, 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(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.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 buttonRight; - private Button buttonLeft; - private Button buttonCreateModif; - private ComboBox comboBoxSelectorMap; - } -} \ No newline at end of file diff --git a/Warship/Warship/FormMap.cs b/Warship/Warship/FormMap.cs deleted file mode 100644 index 7279993..0000000 --- a/Warship/Warship/FormMap.cs +++ /dev/null @@ -1,78 +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 Warship -{ - public partial class FormMap : Form - { - private AbstractMap _abstractMap; - public FormMap() - { - InitializeComponent(); - _abstractMap = new SimpleMap(); - } - - private void SetData(DrawingWarship warship) - { - toolStripStatusLabelSpeed.Text = $"Cкорость: {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)); - } - - private void ButtonCreate_Click(object sender, EventArgs e) - { - Random rnd = new(); - var warship = new DrawingWarship(rnd.Next(10, 60), rnd.Next(20000, 25000), 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); - } - private void ButtonCreateModif_Click(object sender, EventArgs e) - { - Random rnd = new(); - var warship = new DrawingAdvancedWarship(rnd.Next(10, 60), rnd.Next(20000, 25000), 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(warship); - } - - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) - { - switch (comboBoxSelectorMap.Text) - { - case "Простая карта": - _abstractMap = new SimpleMap(); - break; - case "Вторая карта": - _abstractMap = new SecondMap(); - break; - } - } - } -} diff --git a/Warship/Warship/FormMapWithSetWarships.Designer.cs b/Warship/Warship/FormMapWithSetWarships.Designer.cs new file mode 100644 index 0000000..7b819cd --- /dev/null +++ b/Warship/Warship/FormMapWithSetWarships.Designer.cs @@ -0,0 +1,210 @@ +namespace Warship +{ + 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.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); + this.buttonRemoveWarship = new System.Windows.Forms.Button(); + this.buttonShowStorage = new System.Windows.Forms.Button(); + this.buttonDown = 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.buttonShowOnMap = new System.Windows.Forms.Button(); + this.buttonAddWarship = 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.maskedTextBoxPosition); + this.groupBoxTools.Controls.Add(this.buttonRemoveWarship); + this.groupBoxTools.Controls.Add(this.buttonShowStorage); + this.groupBoxTools.Controls.Add(this.buttonDown); + this.groupBoxTools.Controls.Add(this.buttonRight); + this.groupBoxTools.Controls.Add(this.buttonLeft); + this.groupBoxTools.Controls.Add(this.buttonUp); + this.groupBoxTools.Controls.Add(this.buttonShowOnMap); + this.groupBoxTools.Controls.Add(this.buttonAddWarship); + this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap); + this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; + this.groupBoxTools.Location = new System.Drawing.Point(811, 0); + this.groupBoxTools.Name = "groupBoxTools"; + this.groupBoxTools.Size = new System.Drawing.Size(204, 554); + this.groupBoxTools.TabIndex = 0; + this.groupBoxTools.TabStop = false; + this.groupBoxTools.Text = "Инструменты"; + // + // maskedTextBoxPosition + // + this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 166); + this.maskedTextBoxPosition.Mask = "00"; + this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23); + this.maskedTextBoxPosition.TabIndex = 2; + this.maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonRemoveWarship + // + this.buttonRemoveWarship.Location = new System.Drawing.Point(17, 195); + this.buttonRemoveWarship.Name = "buttonRemoveWarship"; + this.buttonRemoveWarship.Size = new System.Drawing.Size(175, 35); + this.buttonRemoveWarship.TabIndex = 3; + this.buttonRemoveWarship.Text = "Удалить корабль"; + this.buttonRemoveWarship.UseVisualStyleBackColor = true; + this.buttonRemoveWarship.Click += new System.EventHandler(this.ButtonRemoveWarship_Click); + // + // buttonShowStorage + // + this.buttonShowStorage.Location = new System.Drawing.Point(17, 287); + this.buttonShowStorage.Name = "buttonShowStorage"; + this.buttonShowStorage.Size = new System.Drawing.Size(175, 35); + this.buttonShowStorage.TabIndex = 4; + this.buttonShowStorage.Text = "Посмотреть хранилище"; + this.buttonShowStorage.UseVisualStyleBackColor = true; + this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); + // + // buttonDown + // + this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonDown.BackgroundImage = global::Warship.Properties.Resources.arrowDown; + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonDown.Location = new System.Drawing.Point(91, 504); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(30, 30); + this.buttonDown.TabIndex = 10; + this.buttonDown.UseVisualStyleBackColor = true; + // + // buttonRight + // + this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonRight.BackgroundImage = global::Warship.Properties.Resources.arrowRight; + this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonRight.Location = new System.Drawing.Point(127, 504); + this.buttonRight.Name = "buttonRight"; + this.buttonRight.Size = new System.Drawing.Size(30, 30); + this.buttonRight.TabIndex = 9; + this.buttonRight.UseVisualStyleBackColor = true; + // + // buttonLeft + // + this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonLeft.BackgroundImage = global::Warship.Properties.Resources.arrowLeft; + this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonLeft.Location = new System.Drawing.Point(55, 504); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(30, 30); + this.buttonLeft.TabIndex = 8; + this.buttonLeft.UseVisualStyleBackColor = true; + // + // buttonUp + // + this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonUp.BackgroundImage = global::Warship.Properties.Resources.arrowUp; + this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonUp.Location = new System.Drawing.Point(91, 468); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(30, 30); + this.buttonUp.TabIndex = 7; + this.buttonUp.UseVisualStyleBackColor = true; + // + // buttonShowOnMap + // + this.buttonShowOnMap.Location = new System.Drawing.Point(17, 391); + this.buttonShowOnMap.Name = "buttonShowOnMap"; + this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35); + this.buttonShowOnMap.TabIndex = 5; + this.buttonShowOnMap.Text = "Посмотреть карту"; + this.buttonShowOnMap.UseVisualStyleBackColor = true; + this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); + // + // buttonAddWarship + // + this.buttonAddWarship.Location = new System.Drawing.Point(17, 106); + this.buttonAddWarship.Name = "buttonAddWarship"; + this.buttonAddWarship.Size = new System.Drawing.Size(175, 35); + this.buttonAddWarship.TabIndex = 1; + this.buttonAddWarship.Text = "Добавить корабль"; + this.buttonAddWarship.UseVisualStyleBackColor = true; + this.buttonAddWarship.Click += new System.EventHandler(this.ButtonAddWarship_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(17, 32); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(175, 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(811, 554); + this.pictureBox.TabIndex = 1; + this.pictureBox.TabStop = false; + // + // FormMapWithSetWarships + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(1015, 554); + this.Controls.Add(this.pictureBox); + this.Controls.Add(this.groupBoxTools); + this.Name = "FormMapWithSetWarships"; + this.Text = "Карта с набором объектов"; + 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 buttonShowOnMap; + private Button buttonAddWarship; + private Button buttonDown; + private Button buttonRight; + private Button buttonLeft; + private Button buttonUp; + private Button buttonShowStorage; + private Button buttonRemoveWarship; + private MaskedTextBox maskedTextBoxPosition; + } +} \ No newline at end of file diff --git a/Warship/Warship/FormMapWithSetWarships.cs b/Warship/Warship/FormMapWithSetWarships.cs new file mode 100644 index 0000000..c5598d5 --- /dev/null +++ b/Warship/Warship/FormMapWithSetWarships.cs @@ -0,0 +1,130 @@ +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 Warship +{ + public partial class FormMapWithSetWarships : Form + { + private MapWithSetWarshipsGeneric _mapWarshipsCollectionGeneric; + + public FormMapWithSetWarships() + { + InitializeComponent(); + } + + private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + { + AbstractMap map = null; + switch (comboBoxSelectorMap.Text) + { + case "Простая карта": + map = new SimpleMap(); + break; + } + if (map != null) + { + _mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric( + pictureBox.Width, pictureBox.Height, map); + } + else + { + _mapWarshipsCollectionGeneric = null; + } + } + + 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) + { + if (_mapWarshipsCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapWarshipsCollectionGeneric.ShowOnMap(); + } + + 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/Warship/Warship/FormMap.resx b/Warship/Warship/FormMapWithSetWarships.resx similarity index 93% rename from Warship/Warship/FormMap.resx rename to Warship/Warship/FormMapWithSetWarships.resx index 2c0949d..f298a7b 100644 --- a/Warship/Warship/FormMap.resx +++ b/Warship/Warship/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/Warship/Warship/FormWarship.Designer.cs b/Warship/Warship/FormWarship.Designer.cs index cf33e56..6a8c482 100644 --- a/Warship/Warship/FormWarship.Designer.cs +++ b/Warship/Warship/FormWarship.Designer.cs @@ -39,6 +39,7 @@ this.buttonRight = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); this.buttonCreateModif = new System.Windows.Forms.Button(); + this.buttonSelect = 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); // + // buttonSelect + // + this.buttonSelect.Location = new System.Drawing.Point(551, 390); + this.buttonSelect.Name = "buttonSelect"; + this.buttonSelect.Size = new System.Drawing.Size(75, 23); + this.buttonSelect.TabIndex = 8; + this.buttonSelect.Text = "Выбрать"; + this.buttonSelect.UseVisualStyleBackColor = true; + this.buttonSelect.Click += new System.EventHandler(this.ButtonSelect_Click); + // // FormWarship // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.buttonSelect); this.Controls.Add(this.buttonCreateModif); this.Controls.Add(this.buttonLeft); this.Controls.Add(this.buttonRight); @@ -192,5 +204,6 @@ private Button buttonRight; private Button buttonLeft; private Button buttonCreateModif; + private Button buttonSelect; } } \ No newline at end of file diff --git a/Warship/Warship/FormWarship.cs b/Warship/Warship/FormWarship.cs index 9ae9161..430a9b6 100644 --- a/Warship/Warship/FormWarship.cs +++ b/Warship/Warship/FormWarship.cs @@ -3,6 +3,8 @@ namespace Warship public partial class FormWarship : Form { private DrawingWarship _warship; + + public DrawingWarship SelectedWarship { get; private set; } public FormWarship() { InitializeComponent(); @@ -65,5 +67,11 @@ namespace Warship SetData(); Draw(); } + + private void ButtonSelect_Click(object sender, EventArgs e) + { + SelectedWarship = _warship; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/Warship/Warship/MapWithSetWarshipsGeneric.cs b/Warship/Warship/MapWithSetWarshipsGeneric.cs index 180a80f..a84fb16 100644 --- a/Warship/Warship/MapWithSetWarshipsGeneric.cs +++ b/Warship/Warship/MapWithSetWarshipsGeneric.cs @@ -42,7 +42,7 @@ namespace Warship Bitmap bmp = new(_pictureWidth, _pictureWidth); Graphics gr = Graphics.FromImage(bmp); DrawBackground(gr); - DrawWarship(gr, _placeSizeWidth,_placeSizeHeight,_pictureWidth,_pictureHeight); + DrawWarship(gr); return bmp; } @@ -107,11 +107,17 @@ namespace Warship } } - private void DrawWarship(Graphics gr, int x,int y,int width,int height) + private void DrawWarship(Graphics gr) { for (int i = 0; i < _setWarship.Count; i++) { - _setWarship.Get(i)?.SetObject(x, y,width,height); + for (int i1 = 0; i1 < _pictureWidth / _placeSizeWidth; i1++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + _setWarship.Get(i)?.SetObject(i1 * _placeSizeWidth, j * _placeSizeHeight, _pictureWidth, _pictureHeight); + } + } _setWarship.Get(i)?.DrawingObject(gr); } } diff --git a/Warship/Warship/Program.cs b/Warship/Warship/Program.cs index 3ba6e5a..4c572c2 100644 --- a/Warship/Warship/Program.cs +++ b/Warship/Warship/Program.cs @@ -11,7 +11,7 @@ namespace Warship // 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 diff --git a/Warship/Warship/SetWarshipsGeneric.cs b/Warship/Warship/SetWarshipsGeneric.cs index ccf9090..4383872 100644 --- a/Warship/Warship/SetWarshipsGeneric.cs +++ b/Warship/Warship/SetWarshipsGeneric.cs @@ -39,6 +39,7 @@ namespace Warship for (int j = EmptyEl; j > position; j--) _places[j] = _places[j - 1]; } + _places[position] = warship; return true; } -- 2.25.1 From 7fb61aaaf1cbecc9dea454b639b8fda46b25ad44 Mon Sep 17 00:00:00 2001 From: AnnZhimol Date: Tue, 4 Oct 2022 14:49:13 +0400 Subject: [PATCH 3/5] =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=87=D0=B8=D0=B5?= =?UTF-8?q?=20TODO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetWarships.Designer.cs | 57 ++++++++++--------- Warship/Warship/FormMapWithSetWarships.cs | 6 +- Warship/Warship/MapWithSetWarshipsGeneric.cs | 19 ++++--- Warship/Warship/SetWarshipsGeneric.cs | 32 ++++++++--- 4 files changed, 70 insertions(+), 44 deletions(-) diff --git a/Warship/Warship/FormMapWithSetWarships.Designer.cs b/Warship/Warship/FormMapWithSetWarships.Designer.cs index 7b819cd..4c2c1c1 100644 --- a/Warship/Warship/FormMapWithSetWarships.Designer.cs +++ b/Warship/Warship/FormMapWithSetWarships.Designer.cs @@ -43,9 +43,9 @@ this.groupBoxTools.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); - // + // // groupBoxTools - // + // this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.buttonRemoveWarship); this.groupBoxTools.Controls.Add(this.buttonShowStorage); @@ -63,18 +63,18 @@ this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; - // + // // maskedTextBoxPosition - // + // this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 166); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23); this.maskedTextBoxPosition.TabIndex = 2; this.maskedTextBoxPosition.ValidatingType = typeof(int); - // + // // buttonRemoveWarship - // + // this.buttonRemoveWarship.Location = new System.Drawing.Point(17, 195); this.buttonRemoveWarship.Name = "buttonRemoveWarship"; this.buttonRemoveWarship.Size = new System.Drawing.Size(175, 35); @@ -82,9 +82,9 @@ this.buttonRemoveWarship.Text = "Удалить корабль"; this.buttonRemoveWarship.UseVisualStyleBackColor = true; this.buttonRemoveWarship.Click += new System.EventHandler(this.ButtonRemoveWarship_Click); - // + // // buttonShowStorage - // + // this.buttonShowStorage.Location = new System.Drawing.Point(17, 287); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(175, 35); @@ -92,9 +92,9 @@ this.buttonShowStorage.Text = "Посмотреть хранилище"; this.buttonShowStorage.UseVisualStyleBackColor = true; this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); - // + // // buttonDown - // + // this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::Warship.Properties.Resources.arrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; @@ -103,9 +103,9 @@ this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 10; this.buttonDown.UseVisualStyleBackColor = true; - // + // // buttonRight - // + // this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::Warship.Properties.Resources.arrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; @@ -114,9 +114,9 @@ this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 9; this.buttonRight.UseVisualStyleBackColor = true; - // + // // buttonLeft - // + // this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::Warship.Properties.Resources.arrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; @@ -125,9 +125,9 @@ this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; this.buttonLeft.UseVisualStyleBackColor = true; - // + // // buttonUp - // + // this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::Warship.Properties.Resources.arrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; @@ -136,9 +136,9 @@ this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 7; this.buttonUp.UseVisualStyleBackColor = true; - // + // // buttonShowOnMap - // + // this.buttonShowOnMap.Location = new System.Drawing.Point(17, 391); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35); @@ -146,9 +146,9 @@ this.buttonShowOnMap.Text = "Посмотреть карту"; this.buttonShowOnMap.UseVisualStyleBackColor = true; this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); - // + // // buttonAddWarship - // + // this.buttonAddWarship.Location = new System.Drawing.Point(17, 106); this.buttonAddWarship.Name = "buttonAddWarship"; this.buttonAddWarship.Size = new System.Drawing.Size(175, 35); @@ -156,29 +156,31 @@ this.buttonAddWarship.Text = "Добавить корабль"; this.buttonAddWarship.UseVisualStyleBackColor = true; this.buttonAddWarship.Click += new System.EventHandler(this.ButtonAddWarship_Click); - // + // // comboBoxSelectorMap - // + // this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxSelectorMap.FormattingEnabled = true; - this.comboBoxSelectorMap.Items.AddRange(new object[] {"Простая карта"}); + this.comboBoxSelectorMap.Items.AddRange(new object[] { + "Первая карта", + "Вторая карта"}); this.comboBoxSelectorMap.Location = new System.Drawing.Point(17, 32); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(175, 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(811, 554); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; - // + // // FormMapWithSetWarships - // + // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1015, 554); @@ -190,6 +192,7 @@ this.groupBoxTools.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); + } #endregion diff --git a/Warship/Warship/FormMapWithSetWarships.cs b/Warship/Warship/FormMapWithSetWarships.cs index c5598d5..477b2aa 100644 --- a/Warship/Warship/FormMapWithSetWarships.cs +++ b/Warship/Warship/FormMapWithSetWarships.cs @@ -22,11 +22,15 @@ namespace Warship private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) { AbstractMap map = null; + switch (comboBoxSelectorMap.Text) { - case "Простая карта": + case "Первая карта": map = new SimpleMap(); break; + case "Вторая карта": + map = new SecondMap(); + break; } if (map != null) { diff --git a/Warship/Warship/MapWithSetWarshipsGeneric.cs b/Warship/Warship/MapWithSetWarshipsGeneric.cs index a84fb16..64fa238 100644 --- a/Warship/Warship/MapWithSetWarshipsGeneric.cs +++ b/Warship/Warship/MapWithSetWarshipsGeneric.cs @@ -96,29 +96,30 @@ namespace Warship private void DrawBackground(Graphics gr) { - Pen pen = new(Color.Black, 3); + Pen pen = new(Color.Black, 5); 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+10, j * _placeSizeHeight+2, i * _placeSizeWidth + (int)(_placeSizeWidth*0.8), j * _placeSizeHeight+2); + gr.DrawLine(pen, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9)+10, j * _placeSizeHeight + 2+20); + gr.DrawLine(pen, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20); } - gr.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight/_placeSizeHeight) * _placeSizeHeight); } } private void DrawWarship(Graphics gr) { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + for (int i = 0; i < _setWarship.Count; i++) { - for (int i1 = 0; i1 < _pictureWidth / _placeSizeWidth; i1++) + if (_setWarship.Get(i) != null) { - for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) - { - _setWarship.Get(i)?.SetObject(i1 * _placeSizeWidth, j * _placeSizeHeight, _pictureWidth, _pictureHeight); - } + _setWarship.Get(i).SetObject(i % width * _placeSizeWidth + 10, (height - 1 - i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); + _setWarship.Get(i)?.DrawingObject(gr); } - _setWarship.Get(i)?.DrawingObject(gr); } } } diff --git a/Warship/Warship/SetWarshipsGeneric.cs b/Warship/Warship/SetWarshipsGeneric.cs index 4383872..c5c25b4 100644 --- a/Warship/Warship/SetWarshipsGeneric.cs +++ b/Warship/Warship/SetWarshipsGeneric.cs @@ -20,24 +20,36 @@ namespace Warship public bool Insert(T warship) { - _places[0] = warship; - return true; + return Insert(warship,0); } public bool Insert(T warship, 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] = warship; + 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] = warship; @@ -46,12 +58,18 @@ namespace Warship 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 26b48afac4c0365d851ce1ad9c032cd8d9a4f6f8 Mon Sep 17 00:00:00 2001 From: AnnZhimol Date: Tue, 4 Oct 2022 15:53:32 +0400 Subject: [PATCH 4/5] =?UTF-8?q?=D0=98=D1=82=D0=BE=D0=B3=D0=BE=D0=B2=D1=8B?= =?UTF-8?q?=D0=B9=20=D0=B2=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82=203=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=BE=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetWarships.Designer.cs | 4 ++++ Warship/Warship/FormMapWithSetWarships.cs | 4 ++-- Warship/Warship/MapWithSetWarshipsGeneric.cs | 13 +++++++------ Warship/Warship/SetWarshipsGeneric.cs | 19 ++++++++++--------- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/Warship/Warship/FormMapWithSetWarships.Designer.cs b/Warship/Warship/FormMapWithSetWarships.Designer.cs index 4c2c1c1..760c7de 100644 --- a/Warship/Warship/FormMapWithSetWarships.Designer.cs +++ b/Warship/Warship/FormMapWithSetWarships.Designer.cs @@ -103,6 +103,7 @@ this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 10; this.buttonDown.UseVisualStyleBackColor = true; + this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); // // buttonRight // @@ -114,6 +115,7 @@ this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 9; this.buttonRight.UseVisualStyleBackColor = true; + this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); // // buttonLeft // @@ -125,6 +127,7 @@ 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 // @@ -136,6 +139,7 @@ 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); // // buttonShowOnMap // diff --git a/Warship/Warship/FormMapWithSetWarships.cs b/Warship/Warship/FormMapWithSetWarships.cs index 477b2aa..b71de7e 100644 --- a/Warship/Warship/FormMapWithSetWarships.cs +++ b/Warship/Warship/FormMapWithSetWarships.cs @@ -53,7 +53,7 @@ namespace Warship if (form.ShowDialog() == DialogResult.OK) { DrawingObjectWarship warship = new(form.SelectedWarship); - if (_mapWarshipsCollectionGeneric + warship) + if (_mapWarshipsCollectionGeneric + warship>=0) { MessageBox.Show("Объект добавлен"); pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet(); @@ -76,7 +76,7 @@ namespace Warship return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapWarshipsCollectionGeneric - pos) + if (_mapWarshipsCollectionGeneric - pos !=null) { MessageBox.Show("Объект удален"); pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet(); diff --git a/Warship/Warship/MapWithSetWarshipsGeneric.cs b/Warship/Warship/MapWithSetWarshipsGeneric.cs index 64fa238..deec30f 100644 --- a/Warship/Warship/MapWithSetWarshipsGeneric.cs +++ b/Warship/Warship/MapWithSetWarshipsGeneric.cs @@ -27,12 +27,12 @@ namespace Warship _map = map; } - public static bool operator +(MapWithSetWarshipsGeneric map, T warship) + public static int operator +(MapWithSetWarshipsGeneric map, T warship) { return map._setWarship.Insert(warship); } - public static bool operator -(MapWithSetWarshipsGeneric map, int position) + public static T operator -(MapWithSetWarshipsGeneric map, int position) { return map._setWarship.Remove(position); } @@ -101,9 +101,10 @@ namespace Warship { for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) { - gr.DrawLine(pen, i * _placeSizeWidth+10, j * _placeSizeHeight+2, i * _placeSizeWidth + (int)(_placeSizeWidth*0.8), j * _placeSizeHeight+2); - gr.DrawLine(pen, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9)+10, j * _placeSizeHeight + 2+20); - gr.DrawLine(pen, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20); + gr.DrawLine(pen, i * _placeSizeWidth + 20, j * _placeSizeHeight+2, i * _placeSizeWidth + (int)(_placeSizeWidth*0.8), j * _placeSizeHeight+2); + gr.DrawLine(pen, i * _placeSizeWidth + 20, j * _placeSizeHeight + _placeSizeHeight/2+2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight/2+2); + gr.DrawLine(pen, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight/2); + gr.DrawLine(pen, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2, i * _placeSizeWidth+ (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight); } } } @@ -117,7 +118,7 @@ namespace Warship { if (_setWarship.Get(i) != null) { - _setWarship.Get(i).SetObject(i % width * _placeSizeWidth + 10, (height - 1 - i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); + _setWarship.Get(i).SetObject(i % width * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight, _pictureWidth, _pictureHeight); _setWarship.Get(i)?.DrawingObject(gr); } } diff --git a/Warship/Warship/SetWarshipsGeneric.cs b/Warship/Warship/SetWarshipsGeneric.cs index c5c25b4..ac9c126 100644 --- a/Warship/Warship/SetWarshipsGeneric.cs +++ b/Warship/Warship/SetWarshipsGeneric.cs @@ -18,22 +18,22 @@ namespace Warship _places = new T[count]; } - public bool Insert(T warship) + public int Insert(T warship) { return Insert(warship,0); } - public bool Insert(T warship, int position) + public int Insert(T warship, int position) { int EmptyEl=-1; if (position>=Count || position < 0) - return false; + return -1; if (_places[position] == null) { _places[position] = warship; - return true; + return 1; } else if (_places[position] != null) @@ -46,23 +46,24 @@ namespace Warship } if (EmptyEl == -1) - return false; + return -1; for (int i = EmptyEl; 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 >= Count || position < 0 || _places[position]==null) - return false; + return null; + T deleted =_places[position]; _places[position] = null; - return true; + return deleted; } public T Get(int position) -- 2.25.1 From f8520664ccd324320e7e52d3b5136ea1e16f6e8c Mon Sep 17 00:00:00 2001 From: AnnZhimol Date: Wed, 5 Oct 2022 10:27:49 +0400 Subject: [PATCH 5/5] =?UTF-8?q?=D0=A2=D0=BE=D1=87=D0=BD=D0=BE=20=D0=B8?= =?UTF-8?q?=D1=82=D0=BE=D0=B3=D0=BE=D0=B2=D1=8B=D0=B9=20=D0=B2=D0=B0=D1=80?= =?UTF-8?q?=D0=B8=D0=B0=D0=BD=D1=82=203=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80?= =?UTF-8?q?=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=BE=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Warship/Warship/FormWarship.cs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/Warship/Warship/FormWarship.cs b/Warship/Warship/FormWarship.cs index 430a9b6..07d6253 100644 --- a/Warship/Warship/FormWarship.cs +++ b/Warship/Warship/FormWarship.cs @@ -29,7 +29,13 @@ namespace Warship private void ButtonCreate_Click(object sender, EventArgs e) { Random rnd = new(); - _warship = new DrawingWarship(rnd.Next(10, 60), rnd.Next(20000, 25000), 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; + } + _warship = new DrawingWarship(rnd.Next(10, 60), rnd.Next(20000, 25000),color); SetData(); Draw(); } @@ -63,7 +69,19 @@ namespace Warship private void ButtonCreateModif_Click(object sender, EventArgs e) { Random rnd = new(); - _warship = new DrawingAdvancedWarship(rnd.Next(10, 60), rnd.Next(20000, 25000), 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))); + Color color1 = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + ColorDialog dialog1 = new(); + if (dialog1.ShowDialog() == DialogResult.OK) + { + color1 = dialog1.Color; + } + Color color2 = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + ColorDialog dialog2 = new(); + if (dialog2.ShowDialog() == DialogResult.OK) + { + color2 = dialog2.Color; + } + _warship = new DrawingAdvancedWarship(rnd.Next(10, 60), rnd.Next(20000, 25000), color1,color2, Convert.ToBoolean(rnd.Next(0,2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2))); SetData(); Draw(); } -- 2.25.1