From fa9a16f4f450214e9afdb9b09d75a35de704f776 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Tue, 24 Oct 2023 14:43:44 +0400 Subject: [PATCH 1/5] =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D1=8B=D0=B5=202=20=D0=BA?= =?UTF-8?q?=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20=D0=BF=D0=BE=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B8=D0=BC=D0=B5=D1=80=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Battleship/Battleship/DrawningShip.cs | 8 ++ ...Designer.cs => FormBattleship.Designer.cs} | 3 +- .../{Battleship.cs => FormBattleship.cs} | 9 +- .../{Battleship.resx => FormBattleship.resx} | 0 Battleship/Battleship/Program.cs | 2 +- Battleship/Battleship/SetGeneric.cs | 80 ++++++++++++++++++ .../Battleship/ShipGenericCollection.cs | 84 +++++++++++++++++++ 7 files changed, 182 insertions(+), 4 deletions(-) rename Battleship/Battleship/{Battleship.Designer.cs => FormBattleship.Designer.cs} (98%) rename Battleship/Battleship/{Battleship.cs => FormBattleship.cs} (96%) rename Battleship/Battleship/{Battleship.resx => FormBattleship.resx} (100%) create mode 100644 Battleship/Battleship/SetGeneric.cs create mode 100644 Battleship/Battleship/ShipGenericCollection.cs diff --git a/Battleship/Battleship/DrawningShip.cs b/Battleship/Battleship/DrawningShip.cs index d6c291b..019988f 100644 --- a/Battleship/Battleship/DrawningShip.cs +++ b/Battleship/Battleship/DrawningShip.cs @@ -4,9 +4,12 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Battleship.Entities; +using Battleship.MovementStrategy; namespace Battleship.DrawningObjects { + + /// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности /// @@ -51,6 +54,11 @@ namespace Battleship.DrawningObjects /// true - объект создан, false - проверка не пройдена, ///нельзя создать объект в этих размерах + /// + /// Получение объекта IMoveableObject из объекта DrawningCar + /// + public IMoveableObject GetMoveableObject => new DrawningObjectShip(this); + public DrawningShip(int speed, double weight, Color bodyColor, int width, int height) { if (width < _buttleshipWidth || height < _buttleshipHeight) diff --git a/Battleship/Battleship/Battleship.Designer.cs b/Battleship/Battleship/FormBattleship.Designer.cs similarity index 98% rename from Battleship/Battleship/Battleship.Designer.cs rename to Battleship/Battleship/FormBattleship.Designer.cs index 0808631..fc70172 100644 --- a/Battleship/Battleship/Battleship.Designer.cs +++ b/Battleship/Battleship/FormBattleship.Designer.cs @@ -1,6 +1,6 @@ namespace Battleship { - partial class Battleship + partial class FormBattleship { /// /// Required designer variable. @@ -158,6 +158,7 @@ this.Controls.Add(this.pictureBoxBattleship); this.Name = "Battleship"; this.Text = "Form1"; + this.Load += new System.EventHandler(this.Battleship_Load); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxBattleship)).EndInit(); this.ResumeLayout(false); diff --git a/Battleship/Battleship/Battleship.cs b/Battleship/Battleship/FormBattleship.cs similarity index 96% rename from Battleship/Battleship/Battleship.cs rename to Battleship/Battleship/FormBattleship.cs index c2458df..2cbb10f 100644 --- a/Battleship/Battleship/Battleship.cs +++ b/Battleship/Battleship/FormBattleship.cs @@ -3,7 +3,7 @@ using Battleship.MovementStrategy; namespace Battleship { - public partial class Battleship : Form + public partial class FormBattleship : Form { /// /// - @@ -13,7 +13,7 @@ namespace Battleship /// /// private AbstractStrategy? _abstractStrategy; - public Battleship() + public FormBattleship() { InitializeComponent(); } @@ -120,5 +120,10 @@ namespace Battleship _abstractStrategy = null; } } + + private void Battleship_Load(object sender, EventArgs e) + { + + } } } \ No newline at end of file diff --git a/Battleship/Battleship/Battleship.resx b/Battleship/Battleship/FormBattleship.resx similarity index 100% rename from Battleship/Battleship/Battleship.resx rename to Battleship/Battleship/FormBattleship.resx diff --git a/Battleship/Battleship/Program.cs b/Battleship/Battleship/Program.cs index ea61019..4d0e539 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 Battleship()); + Application.Run(new FormBattleship()); } } } \ No newline at end of file diff --git a/Battleship/Battleship/SetGeneric.cs b/Battleship/Battleship/SetGeneric.cs new file mode 100644 index 0000000..a62348d --- /dev/null +++ b/Battleship/Battleship/SetGeneric.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Battleship.Generics +{ + /// + /// Параметризованный набор объектов + /// + /// + internal class SetGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T?[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetGeneric(int count) + { + _places = new T?[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый автомобиль + /// + public bool Insert(T car) + { + // TODO вставка в начало набора + return true; + } + /// + /// Добавление объекта в набор на конкретную позицию + /// /// + /// Добавляемый автомобиль + /// Позиция + /// + public bool Insert(T car, int position) + { + // TODO проверка позиции + // TODO проверка, что элемент массива по этой позиции пустой, если нет, то + // проверка, что после вставляемого элемента в массиве есть пустой элемент + // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + // TODO вставка по позиции_places[position] = car; + return true; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + // TODO проверка позиции + // TODO удаление объекта из массива, присвоив элементу массива значение null + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + // TODO проверка позиции + return _places[position]; + } + } +} + diff --git a/Battleship/Battleship/ShipGenericCollection.cs b/Battleship/Battleship/ShipGenericCollection.cs new file mode 100644 index 0000000..a3071cd --- /dev/null +++ b/Battleship/Battleship/ShipGenericCollection.cs @@ -0,0 +1,84 @@ +using Battleship.DrawningObjects; +using Battleship.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Battleship.Generics +{ + internal class ShipGenericCollection + where T : DrawningShip + where U : IMoveableObject + { + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 175; + private readonly int _placeSizeHeight = 80; + private readonly SetGeneric _collection; + public ShipGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + public static bool operator +(ShipGenericCollection? collect, T? obj) + { + if (obj == null) + { + return false; + } + return collect?._collection.Insert(obj) ?? false; + } + public static bool operator -(ShipGenericCollection? collect, int pos) + { + T? obj = collect?._collection.Get(pos); + if (obj != null) + { + collect._collection.Remove(pos); + } + return obj; + } + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + public Bitmap ShowShips() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 3); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + + 1; ++j) + { + g.DrawLine(pen, i * _placeSizeWidth, j * + _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * + _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * + _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + private void DrawObjects(Graphics g) + { + for (int i = 0; i < _collection.Count; i++) + { + // TODO получение объекта + // TODO установка позиции + // TODO прорисовка объекта + } + } + } +} + -- 2.25.1 From b05fca00fe8fef04842ad66e7346f03214bb2361 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Tue, 24 Oct 2023 15:40:44 +0400 Subject: [PATCH 2/5] =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D0=B0=D1=8F=20=D1=84?= =?UTF-8?q?=D0=BE=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) { -- 2.25.1 From 1fdfd6ec664379c612be1c51cbfabd0fdeb95f69 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Wed, 25 Oct 2023 07:54:01 +0400 Subject: [PATCH 3/5] =?UTF-8?q?3=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Battleship/Battleship/DrawningBattleship.cs | 9 +-- .../Battleship/FormShipCollection.Designer.cs | 78 +++++++++---------- Battleship/Battleship/FormShipCollection.cs | 5 +- Battleship/Battleship/SetGeneric.cs | 30 +++++-- .../Battleship/ShipGenericCollection.cs | 24 +++--- 5 files changed, 83 insertions(+), 63 deletions(-) diff --git a/Battleship/Battleship/DrawningBattleship.cs b/Battleship/Battleship/DrawningBattleship.cs index f2b792d..c0c8d7a 100644 --- a/Battleship/Battleship/DrawningBattleship.cs +++ b/Battleship/Battleship/DrawningBattleship.cs @@ -33,8 +33,7 @@ namespace Battleship.DrawningObjects //орудийная башня if (battleShip.Tower) { - Brush baseBrush = new SolidBrush(Color.White); - g.FillRectangle(baseBrush, _startPosX + 108, _startPosY + 28, 15, 15); + g.FillRectangle(additionalBrush, _startPosX + 108, _startPosY + 28, 15, 15); g.DrawRectangle(pen, _startPosX + 108, _startPosY + 28, 15, 15); Brush gunBrush = new SolidBrush(Color.Black); g.FillRectangle(gunBrush, _startPosX + 123, _startPosY + 32, 47, 6); @@ -43,11 +42,9 @@ namespace Battleship.DrawningObjects //отсеки под ракеты if (battleShip.Section) { - Brush sectionBrush = new - SolidBrush(Color.Gray); - g.FillRectangle(sectionBrush, _startPosX + 20, _startPosY + 70, 40, 10); + g.FillRectangle(additionalBrush, _startPosX + 20, _startPosY + 70, 40, 10); g.DrawRectangle(pen, _startPosX + 20, _startPosY + 70, 40, 10); - g.FillRectangle(sectionBrush, _startPosX + 75, _startPosY + 70, 40, 10); + g.FillRectangle(additionalBrush, _startPosX + 75, _startPosY + 70, 40, 10); g.DrawRectangle(pen, _startPosX + 75, _startPosY + 70, 40, 10); } } diff --git a/Battleship/Battleship/FormShipCollection.Designer.cs b/Battleship/Battleship/FormShipCollection.Designer.cs index f1d4f63..8da44c5 100644 --- a/Battleship/Battleship/FormShipCollection.Designer.cs +++ b/Battleship/Battleship/FormShipCollection.Designer.cs @@ -29,11 +29,11 @@ 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.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); this.buttonRefreshCollection = new System.Windows.Forms.Button(); - this.maskedTextBoxNumber = new System.Windows.Forms.TextBox(); + this.buttonRemoveShip = new System.Windows.Forms.Button(); + this.buttonAddShip = new System.Windows.Forms.Button(); + this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.groupBoxBattleShip.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.SuspendLayout(); @@ -53,36 +53,12 @@ this.groupBoxBattleShip.TabStop = false; this.groupBoxBattleShip.Text = "Инструменты"; // - // pictureBoxCollection + // maskedTextBoxNumber // - 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); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(22, 152); + this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + this.maskedTextBoxNumber.Size = new System.Drawing.Size(147, 23); + this.maskedTextBoxNumber.TabIndex = 3; // // buttonRefreshCollection // @@ -94,12 +70,36 @@ this.buttonRefreshCollection.UseVisualStyleBackColor = true; this.buttonRefreshCollection.Click += new System.EventHandler(this.buttonRefreshCollection_Click); // - // maskedTextBoxNumber + // buttonRemoveShip // - 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; + 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); + // + // 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); + // + // 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; // // FormShipCollection // @@ -120,10 +120,10 @@ #endregion private GroupBox groupBoxBattleShip; - private TextBox maskedTextBoxNumber; private Button buttonRefreshCollection; private Button buttonRemoveShip; private Button buttonAddShip; private PictureBox pictureBoxCollection; + private MaskedTextBox maskedTextBoxNumber; } } \ No newline at end of file diff --git a/Battleship/Battleship/FormShipCollection.cs b/Battleship/Battleship/FormShipCollection.cs index 29c67fb..27c8eb4 100644 --- a/Battleship/Battleship/FormShipCollection.cs +++ b/Battleship/Battleship/FormShipCollection.cs @@ -37,7 +37,7 @@ namespace Battleship FormBattleship form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_ships + form.SelectedShip) + if (_ships + form.SelectedShip != -1) { MessageBox.Show("Объект добавлен"); pictureBoxCollection.Image = _ships.ShowShips(); @@ -52,7 +52,7 @@ namespace Battleship private void buttonRemoveShip_Click(object sender, EventArgs e) { if (MessageBox.Show("Удалить объект?", "Удаление", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } @@ -72,5 +72,6 @@ namespace Battleship { pictureBoxCollection.Image = _ships.ShowShips(); } + } } diff --git a/Battleship/Battleship/SetGeneric.cs b/Battleship/Battleship/SetGeneric.cs index a62348d..8047a61 100644 --- a/Battleship/Battleship/SetGeneric.cs +++ b/Battleship/Battleship/SetGeneric.cs @@ -34,10 +34,12 @@ namespace Battleship.Generics /// /// Добавляемый автомобиль /// - public bool Insert(T car) + public int Insert(T ship) { // TODO вставка в начало набора - return true; + if (_places[Count - 1] != null) + return -1; + return Insert(ship,0); } /// /// Добавление объекта в набор на конкретную позицию @@ -45,14 +47,27 @@ namespace Battleship.Generics /// Добавляемый автомобиль /// Позиция /// - public bool Insert(T car, int position) + public int Insert(T ship, int position) { // TODO проверка позиции // TODO проверка, что элемент массива по этой позиции пустой, если нет, то // проверка, что после вставляемого элемента в массиве есть пустой элемент // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента // TODO вставка по позиции_places[position] = car; - return true; + if (!(position >= 0 && position < Count)) + return -1; + if (_places[position] != null) + { + int ind = position; + while (ind < Count && _places[ind] != null) + ind++; + if (ind == Count) + return -1; + for (int i = ind - 1; i >= position; i--) + _places[i + 1] = _places[i]; + } + _places[position] = ship; + return position; } /// /// Удаление объекта из набора с конкретной позиции @@ -63,7 +78,10 @@ namespace Battleship.Generics { // TODO проверка позиции // TODO удаление объекта из массива, присвоив элементу массива значение null - return true; + if (!(position >= 0 && position < Count) || _places[position] == null) + return false; + _places[position] = null; + return true; } /// /// Получение объекта из набора по позиции @@ -73,6 +91,8 @@ namespace Battleship.Generics public T? Get(int position) { // TODO проверка позиции + if (!(position >= 0 && position < Count)) + return null; return _places[position]; } } diff --git a/Battleship/Battleship/ShipGenericCollection.cs b/Battleship/Battleship/ShipGenericCollection.cs index 4fdfe4c..b2575b9 100644 --- a/Battleship/Battleship/ShipGenericCollection.cs +++ b/Battleship/Battleship/ShipGenericCollection.cs @@ -14,8 +14,8 @@ namespace Battleship.Generics { private readonly int _pictureWidth; private readonly int _pictureHeight; - private readonly int _placeSizeWidth = 175; - private readonly int _placeSizeHeight = 80; + private readonly int _placeSizeWidth = 15; + private readonly int _placeSizeHeight = 95; private readonly SetGeneric _collection; public ShipGenericCollection(int picWidth, int picHeight) { @@ -25,13 +25,11 @@ namespace Battleship.Generics _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } - public static bool operator +(ShipGenericCollection? collect, T? obj) + public static int operator +(ShipGenericCollection? collect, T? obj) { - if (obj == null) - { - return false; - } - return collect?._collection.Insert(obj) ?? false; + if (obj != null && collect != null) + return collect._collection.Insert(obj); + return -1; } public static bool operator -(ShipGenericCollection? collect, int pos) { @@ -74,9 +72,13 @@ namespace Battleship.Generics { for (int i = 0; i < _collection.Count; i++) { - // TODO получение объекта - // TODO установка позиции - // TODO прорисовка объекта + DrawningShip? ship = _collection.Get(i); + if(ship != null) + { + int inRow = _pictureWidth / _placeSizeWidth; + ship.SetPosition(i % inRow * _placeSizeWidth, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight + 5); + ship.DrawTransport(g); + } } } } -- 2.25.1 From 7f35155da40f1ab622a27f3193a7ad6cde0efb9e Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Wed, 25 Oct 2023 07:56:00 +0400 Subject: [PATCH 4/5] =?UTF-8?q?3=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Battleship/Battleship/ShipGenericCollection.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Battleship/Battleship/ShipGenericCollection.cs b/Battleship/Battleship/ShipGenericCollection.cs index b2575b9..9a0ab6d 100644 --- a/Battleship/Battleship/ShipGenericCollection.cs +++ b/Battleship/Battleship/ShipGenericCollection.cs @@ -14,8 +14,8 @@ namespace Battleship.Generics { private readonly int _pictureWidth; private readonly int _pictureHeight; - private readonly int _placeSizeWidth = 15; - private readonly int _placeSizeHeight = 95; + private readonly int _placeSizeWidth = 185; + private readonly int _placeSizeHeight = 90; private readonly SetGeneric _collection; public ShipGenericCollection(int picWidth, int picHeight) { -- 2.25.1 From 504798d4c4853a3db22a25ff6cf613a36640f1c3 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Wed, 25 Oct 2023 09:55:10 +0400 Subject: [PATCH 5/5] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Battleship/Battleship/SetGeneric.cs | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/Battleship/Battleship/SetGeneric.cs b/Battleship/Battleship/SetGeneric.cs index 8047a61..29da7ea 100644 --- a/Battleship/Battleship/SetGeneric.cs +++ b/Battleship/Battleship/SetGeneric.cs @@ -6,10 +6,7 @@ using System.Threading.Tasks; namespace Battleship.Generics { - /// - /// Параметризованный набор объектов - /// - /// + internal class SetGeneric where T : class { @@ -36,7 +33,6 @@ namespace Battleship.Generics /// public int Insert(T ship) { - // TODO вставка в начало набора if (_places[Count - 1] != null) return -1; return Insert(ship,0); @@ -49,11 +45,6 @@ namespace Battleship.Generics /// public int Insert(T ship, int position) { - // TODO проверка позиции - // TODO проверка, что элемент массива по этой позиции пустой, если нет, то - // проверка, что после вставляемого элемента в массиве есть пустой элемент - // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - // TODO вставка по позиции_places[position] = car; if (!(position >= 0 && position < Count)) return -1; if (_places[position] != null) @@ -76,8 +67,6 @@ namespace Battleship.Generics /// public bool Remove(int position) { - // TODO проверка позиции - // TODO удаление объекта из массива, присвоив элементу массива значение null if (!(position >= 0 && position < Count) || _places[position] == null) return false; _places[position] = null; @@ -90,7 +79,6 @@ namespace Battleship.Generics /// public T? Get(int position) { - // TODO проверка позиции if (!(position >= 0 && position < Count)) return null; return _places[position]; -- 2.25.1