From 1fdfd6ec664379c612be1c51cbfabd0fdeb95f69 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Wed, 25 Oct 2023 07:54:01 +0400 Subject: [PATCH] =?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); + } } } }