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/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 90% rename from Battleship/Battleship/Battleship.Designer.cs rename to Battleship/Battleship/FormBattleship.Designer.cs index 0808631..ff3c486 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. @@ -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,7 +169,7 @@ this.Controls.Add(this.buttonUp); this.Controls.Add(this.buttonCreate); this.Controls.Add(this.pictureBoxBattleship); - this.Name = "Battleship"; + this.Name = "FormBattleship"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this.pictureBoxBattleship)).EndInit(); this.ResumeLayout(false); @@ -173,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/Battleship.cs b/Battleship/Battleship/FormBattleship.cs similarity index 66% rename from Battleship/Battleship/Battleship.cs rename to Battleship/Battleship/FormBattleship.cs index c2458df..ec24490 100644 --- a/Battleship/Battleship/Battleship.cs +++ b/Battleship/Battleship/FormBattleship.cs @@ -1,9 +1,10 @@ using Battleship.DrawningObjects; using Battleship.MovementStrategy; +using System.Drawing; namespace Battleship { - public partial class Battleship : Form + public partial class FormBattleship : Form { /// /// - @@ -13,9 +14,15 @@ namespace Battleship /// /// private AbstractStrategy? _abstractStrategy; - public Battleship() + /// + /// + /// + 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,5 +152,10 @@ namespace Battleship _abstractStrategy = null; } } + private void buttonSelectShip_Click(object sender, EventArgs e) + { + SelectedShip = _drawningShip; + DialogResult = DialogResult.OK; + } } } \ 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/FormShipCollection.Designer.cs b/Battleship/Battleship/FormShipCollection.Designer.cs new file mode 100644 index 0000000..8da44c5 --- /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.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); + this.buttonRefreshCollection = new System.Windows.Forms.Button(); + 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(); + // + // 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 = "Инструменты"; + // + // maskedTextBoxNumber + // + 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 + // + 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); + // + // 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); + // + // 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 + // + 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 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 new file mode 100644 index 0000000..27c8eb4 --- /dev/null +++ b/Battleship/Battleship/FormShipCollection.cs @@ -0,0 +1,77 @@ +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 != -1) + { + 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 ea61019..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 Battleship()); + Application.Run(new FormShipCollection()); } } } \ No newline at end of file diff --git a/Battleship/Battleship/SetGeneric.cs b/Battleship/Battleship/SetGeneric.cs new file mode 100644 index 0000000..29da7ea --- /dev/null +++ b/Battleship/Battleship/SetGeneric.cs @@ -0,0 +1,88 @@ +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 int Insert(T ship) + { + if (_places[Count - 1] != null) + return -1; + return Insert(ship,0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// /// + /// Добавляемый автомобиль + /// Позиция + /// + public int Insert(T ship, int position) + { + 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; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + if (!(position >= 0 && position < Count) || _places[position] == null) + return false; + _places[position] = null; + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + if (!(position >= 0 && position < Count)) + return null; + return _places[position]; + } + } +} + diff --git a/Battleship/Battleship/ShipGenericCollection.cs b/Battleship/Battleship/ShipGenericCollection.cs new file mode 100644 index 0000000..9a0ab6d --- /dev/null +++ b/Battleship/Battleship/ShipGenericCollection.cs @@ -0,0 +1,86 @@ +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 = 185; + private readonly int _placeSizeHeight = 90; + 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 int operator +(ShipGenericCollection? collect, T? obj) + { + if (obj != null && collect != null) + return collect._collection.Insert(obj); + return -1; + } + public static bool operator -(ShipGenericCollection? collect, int pos) + { + T? obj = collect?._collection.Get(pos); + if (obj != null && collect != null) + { + return collect._collection.Remove(pos); + } + return false; + } + 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++) + { + 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); + } + } + } + } +} +