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..d569a75 --- /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 != null ) + { + 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 != null) + { + 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 new file mode 100644 index 0000000..81bdbd8 --- /dev/null +++ b/Battleship/Battleship/MapWithSetBattleshipGeneric.cs @@ -0,0 +1,129 @@ +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 = 150; + private readonly int _placeSizeHeight = 45; + 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 int operator +(MapWithSetBattleshipGeneric map, T battleship) + { + return map._setBattleship.Insert(battleship); + } + public static T 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); + 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 g) + { + 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) + { + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight + 2, i * _placeSizeWidth + _placeSizeWidth / 2 + 40, j * _placeSizeHeight + 2); + } + 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 width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + for (int i = 0; i < _setBattleship.Count; i++) + { + 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 new file mode 100644 index 0000000..815ba49 --- /dev/null +++ b/Battleship/Battleship/SetBattleshipGeneric.cs @@ -0,0 +1,76 @@ +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 int Insert(T battleship) + { + return Insert(battleship, 0); + } + + public int Insert(T battleship, int position) + { + if (position >= _places.Length) + { + return -1; + } + if (_places[position] != null) + { + int indexNull = -1; + for (int i = position; i < _places.Length; i++) + { + if (_places[i] == null) + { + indexNull = i; + break; + } + } + 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 position; + } + public T Remove(int position) + { + if (position >= _places.Length) + { + return null; + } + T removedObject = _places[position]; + _places[position] = null; + return removedObject; + } + public T Get(int position) + { + if (position >= Count || position < 0) + return null; + + return _places[position]; + } + } +}