From b05fca00fe8fef04842ad66e7346f03214bb2361 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Tue, 24 Oct 2023 15:40:44 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D0=B0=D1=8F=20=D1=84=D0=BE?= =?UTF-8?q?=D1=80=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Battleship/FormBattleship.Designer.cs | 19 ++- Battleship/Battleship/FormBattleship.cs | 60 ++++++-- .../Battleship/FormShipCollection.Designer.cs | 129 ++++++++++++++++++ Battleship/Battleship/FormShipCollection.cs | 76 +++++++++++ Battleship/Battleship/FormShipCollection.resx | 60 ++++++++ Battleship/Battleship/Program.cs | 2 +- .../Battleship/ShipGenericCollection.cs | 6 +- 7 files changed, 331 insertions(+), 21 deletions(-) create mode 100644 Battleship/Battleship/FormShipCollection.Designer.cs create mode 100644 Battleship/Battleship/FormShipCollection.cs create mode 100644 Battleship/Battleship/FormShipCollection.resx diff --git a/Battleship/Battleship/FormBattleship.Designer.cs b/Battleship/Battleship/FormBattleship.Designer.cs index fc70172..ff3c486 100644 --- a/Battleship/Battleship/FormBattleship.Designer.cs +++ b/Battleship/Battleship/FormBattleship.Designer.cs @@ -37,6 +37,7 @@ this.buttonCreateAdd = new System.Windows.Forms.Button(); this.buttonStep = new System.Windows.Forms.Button(); this.comboBoxStrategy = new System.Windows.Forms.ComboBox(); + this.buttonSelectShip = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxBattleship)).BeginInit(); this.SuspendLayout(); // @@ -142,11 +143,23 @@ this.comboBoxStrategy.Size = new System.Drawing.Size(121, 23); this.comboBoxStrategy.TabIndex = 13; // - // Battleship + // buttonSelectShip + // + this.buttonSelectShip.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.buttonSelectShip.Location = new System.Drawing.Point(297, 415); + this.buttonSelectShip.Name = "buttonSelectShip"; + this.buttonSelectShip.Size = new System.Drawing.Size(118, 23); + this.buttonSelectShip.TabIndex = 14; + this.buttonSelectShip.Text = "Выбрать корабль"; + this.buttonSelectShip.UseVisualStyleBackColor = true; + this.buttonSelectShip.Click += new System.EventHandler(this.buttonSelectShip_Click); + // + // FormBattleship // 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.buttonSelectShip); this.Controls.Add(this.comboBoxStrategy); this.Controls.Add(this.buttonStep); this.Controls.Add(this.buttonCreateAdd); @@ -156,9 +169,8 @@ this.Controls.Add(this.buttonUp); this.Controls.Add(this.buttonCreate); this.Controls.Add(this.pictureBoxBattleship); - this.Name = "Battleship"; + this.Name = "FormBattleship"; this.Text = "Form1"; - this.Load += new System.EventHandler(this.Battleship_Load); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxBattleship)).EndInit(); this.ResumeLayout(false); @@ -174,5 +186,6 @@ private Button buttonCreateAdd; private Button buttonStep; private ComboBox comboBoxStrategy; + private Button buttonSelectShip; } } \ No newline at end of file diff --git a/Battleship/Battleship/FormBattleship.cs b/Battleship/Battleship/FormBattleship.cs index 2cbb10f..ec24490 100644 --- a/Battleship/Battleship/FormBattleship.cs +++ b/Battleship/Battleship/FormBattleship.cs @@ -1,5 +1,6 @@ using Battleship.DrawningObjects; using Battleship.MovementStrategy; +using System.Drawing; namespace Battleship { @@ -13,9 +14,15 @@ namespace Battleship /// /// private AbstractStrategy? _abstractStrategy; + /// + /// + /// + public DrawningShip? SelectedShip { get; private set; } public FormBattleship() { InitializeComponent(); + _abstractStrategy = null; + SelectedShip = null; } /// /// @@ -31,31 +38,56 @@ namespace Battleship _drawningShip.DrawTransport(gr); pictureBoxBattleship.Image = bmp; } - + /// + /// " " + /// + /// + /// private void buttonCreate_Click(object sender, EventArgs e) { Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } _drawningShip = new DrawningShip(random.Next(100, 300), - random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), + random.Next(1000, 3000), color, pictureBoxBattleship.Width, pictureBoxBattleship.Height); - _drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _drawningShip.SetPosition(random.Next(10, 100), random.Next(10, + 100)); Draw(); } + /// + /// " " + /// + /// + /// private void buttonCreateAdd_Click(object sender, EventArgs e) { Random random = new(); + Color bodyColor = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + bodyColor = dialog.Color; + } + Color addColor = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + if (dialog.ShowDialog() == DialogResult.OK) + { + addColor = dialog.Color; + } _drawningShip = new DrawningBattleship(random.Next(100, 300), - random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), + random.Next(1000, 3000), bodyColor, + addColor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxBattleship.Width, pictureBoxBattleship.Height); - _drawningShip.SetPosition(random.Next(10, 100), random.Next(10,100)); + _drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } @@ -120,10 +152,10 @@ namespace Battleship _abstractStrategy = null; } } - - private void Battleship_Load(object sender, EventArgs e) + private void buttonSelectShip_Click(object sender, EventArgs e) { - + SelectedShip = _drawningShip; + DialogResult = DialogResult.OK; } } } \ No newline at end of file diff --git a/Battleship/Battleship/FormShipCollection.Designer.cs b/Battleship/Battleship/FormShipCollection.Designer.cs new file mode 100644 index 0000000..f1d4f63 --- /dev/null +++ b/Battleship/Battleship/FormShipCollection.Designer.cs @@ -0,0 +1,129 @@ +namespace Battleship +{ + partial class FormShipCollection + { + /// + /// 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.groupBoxBattleShip = new System.Windows.Forms.GroupBox(); + this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); + this.buttonAddShip = new System.Windows.Forms.Button(); + this.buttonRemoveShip = new System.Windows.Forms.Button(); + this.buttonRefreshCollection = new System.Windows.Forms.Button(); + this.maskedTextBoxNumber = new System.Windows.Forms.TextBox(); + this.groupBoxBattleShip.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); + this.SuspendLayout(); + // + // groupBoxBattleShip + // + this.groupBoxBattleShip.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Right))); + this.groupBoxBattleShip.Controls.Add(this.maskedTextBoxNumber); + this.groupBoxBattleShip.Controls.Add(this.buttonRefreshCollection); + this.groupBoxBattleShip.Controls.Add(this.buttonRemoveShip); + this.groupBoxBattleShip.Controls.Add(this.buttonAddShip); + this.groupBoxBattleShip.Location = new System.Drawing.Point(618, 1); + this.groupBoxBattleShip.Name = "groupBoxBattleShip"; + this.groupBoxBattleShip.Size = new System.Drawing.Size(183, 450); + this.groupBoxBattleShip.TabIndex = 0; + this.groupBoxBattleShip.TabStop = false; + this.groupBoxBattleShip.Text = "Инструменты"; + // + // pictureBoxCollection + // + this.pictureBoxCollection.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.pictureBoxCollection.Location = new System.Drawing.Point(-1, 1); + this.pictureBoxCollection.Name = "pictureBoxCollection"; + this.pictureBoxCollection.Size = new System.Drawing.Size(613, 450); + this.pictureBoxCollection.TabIndex = 0; + this.pictureBoxCollection.TabStop = false; + // + // buttonAddShip + // + this.buttonAddShip.Location = new System.Drawing.Point(22, 22); + this.buttonAddShip.Name = "buttonAddShip"; + this.buttonAddShip.Size = new System.Drawing.Size(147, 33); + this.buttonAddShip.TabIndex = 0; + this.buttonAddShip.Text = "Добавить корабль"; + this.buttonAddShip.UseVisualStyleBackColor = true; + this.buttonAddShip.Click += new System.EventHandler(this.buttonAddShip_Click); + // + // buttonRemoveShip + // + this.buttonRemoveShip.Location = new System.Drawing.Point(22, 217); + this.buttonRemoveShip.Name = "buttonRemoveShip"; + this.buttonRemoveShip.Size = new System.Drawing.Size(147, 33); + this.buttonRemoveShip.TabIndex = 1; + this.buttonRemoveShip.Text = "Удалить корабль"; + this.buttonRemoveShip.UseVisualStyleBackColor = true; + this.buttonRemoveShip.Click += new System.EventHandler(this.buttonRemoveShip_Click); + // + // buttonRefreshCollection + // + this.buttonRefreshCollection.Location = new System.Drawing.Point(22, 265); + this.buttonRefreshCollection.Name = "buttonRefreshCollection"; + this.buttonRefreshCollection.Size = new System.Drawing.Size(147, 33); + this.buttonRefreshCollection.TabIndex = 2; + this.buttonRefreshCollection.Text = "Обновить коллекцию"; + this.buttonRefreshCollection.UseVisualStyleBackColor = true; + this.buttonRefreshCollection.Click += new System.EventHandler(this.buttonRefreshCollection_Click); + // + // maskedTextBoxNumber + // + this.maskedTextBoxNumber.Location = new System.Drawing.Point(22, 166); + this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + this.maskedTextBoxNumber.Size = new System.Drawing.Size(147, 23); + this.maskedTextBoxNumber.TabIndex = 3; + // + // FormShipCollection + // + 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.pictureBoxCollection); + this.Controls.Add(this.groupBoxBattleShip); + this.Name = "FormShipCollection"; + this.Text = "FormShipCollection"; + this.groupBoxBattleShip.ResumeLayout(false); + this.groupBoxBattleShip.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private GroupBox groupBoxBattleShip; + private TextBox maskedTextBoxNumber; + private Button buttonRefreshCollection; + private Button buttonRemoveShip; + private Button buttonAddShip; + private PictureBox pictureBoxCollection; + } +} \ No newline at end of file diff --git a/Battleship/Battleship/FormShipCollection.cs b/Battleship/Battleship/FormShipCollection.cs new file mode 100644 index 0000000..29c67fb --- /dev/null +++ b/Battleship/Battleship/FormShipCollection.cs @@ -0,0 +1,76 @@ +using Battleship.DrawningObjects; +using Battleship.Generics; +using Battleship.MovementStrategy; +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 FormShipCollection : Form + { + /// + /// Набор объектов + /// + private readonly ShipGenericCollection _ships; + /// + /// Конструктор + /// + public FormShipCollection() + { + InitializeComponent(); + _ships = new ShipGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + /// + /// Добавление объекта в набор + /// + /// + /// + private void buttonAddShip_Click(object sender, EventArgs e) + { + FormBattleship form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_ships + form.SelectedShip) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _ships.ShowShips(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + + private void buttonRemoveShip_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_ships - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _ships.ShowShips(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void buttonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _ships.ShowShips(); + } + } +} diff --git a/Battleship/Battleship/FormShipCollection.resx b/Battleship/Battleship/FormShipCollection.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Battleship/Battleship/FormShipCollection.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Battleship/Battleship/Program.cs b/Battleship/Battleship/Program.cs index 4d0e539..65edaed 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 FormBattleship()); + Application.Run(new FormShipCollection()); } } } \ No newline at end of file diff --git a/Battleship/Battleship/ShipGenericCollection.cs b/Battleship/Battleship/ShipGenericCollection.cs index a3071cd..4fdfe4c 100644 --- a/Battleship/Battleship/ShipGenericCollection.cs +++ b/Battleship/Battleship/ShipGenericCollection.cs @@ -36,11 +36,11 @@ namespace Battleship.Generics public static bool operator -(ShipGenericCollection? collect, int pos) { T? obj = collect?._collection.Get(pos); - if (obj != null) + if (obj != null && collect != null) { - collect._collection.Remove(pos); + return collect._collection.Remove(pos); } - return obj; + return false; } public U? GetU(int pos) {