From 0a9220bf86cdcb2ef68221571ff886cb9fe83858 Mon Sep 17 00:00:00 2001 From: shoot Date: Sat, 16 Dec 2023 11:10:10 +0400 Subject: [PATCH] PIbd22_NikiforovaMV_lab3 --- ContainerShip/DrawingContainerShip.cs | 2 +- ContainerShip/DrawningObjectShip.cs | 16 +-- ContainerShip/DrawningShip.cs | 44 +++---- ContainerShip/FormShipCollection.Designer.cs | 127 +++++++++++++++++++ ContainerShip/FormShipCollection.cs | 72 +++++++++++ ContainerShip/FormShipCollection.resx | 120 ++++++++++++++++++ ContainerShip/FormShips.Designer.cs | 96 +++++++++----- ContainerShip/FormShips.cs | 58 ++++++--- ContainerShip/FormShips.resx | 2 +- ContainerShip/IMovableObject.cs | 3 +- ContainerShip/ObjectParameters.cs | 7 +- ContainerShip/Program.cs | 2 +- ContainerShip/SetGeneric.cs | 85 +++++++++++++ ContainerShip/ShipsGenericCollection.cs | 103 +++++++++++++++ 14 files changed, 635 insertions(+), 102 deletions(-) create mode 100644 ContainerShip/FormShipCollection.Designer.cs create mode 100644 ContainerShip/FormShipCollection.cs create mode 100644 ContainerShip/FormShipCollection.resx create mode 100644 ContainerShip/SetGeneric.cs create mode 100644 ContainerShip/ShipsGenericCollection.cs diff --git a/ContainerShip/DrawingContainerShip.cs b/ContainerShip/DrawingContainerShip.cs index 74aadad..68b3098 100644 --- a/ContainerShip/DrawingContainerShip.cs +++ b/ContainerShip/DrawingContainerShip.cs @@ -30,7 +30,7 @@ namespace ContainerShip.DrawningObjects SolidBrush(containerShip.AdditionalColor); if (containerShip.Load) { - g.FillRectangle(additionalBrush, _startPosX + 100, _startPosY + 32, 30, 20); //большой прямоугольник + g.FillRectangle(additionalBrush, _startPosX + 100, _startPosY + 32, 30, 20); } base.DrawTransport(g); if (containerShip.Crane) diff --git a/ContainerShip/DrawningObjectShip.cs b/ContainerShip/DrawningObjectShip.cs index b69595c..51360ed 100644 --- a/ContainerShip/DrawningObjectShip.cs +++ b/ContainerShip/DrawningObjectShip.cs @@ -9,24 +9,24 @@ namespace ContainerShip.MovementStrategy { public class DrawingObjectShip : IMoveableObject { - private readonly DrawningShip? _drawingBus = null; - public DrawingObjectShip(DrawningShip drawingBus) + private readonly DrawningShip? _drawingShip = null; + public DrawingObjectShip(DrawningShip drawingShip) { - _drawingBus = drawingBus; + _drawingShip = drawingShip; } public ObjectParameters? GetObjectPosition { get { - if (_drawingBus == null || _drawingBus.EntityShip == null) + if (_drawingShip == null || _drawingShip.EntityShip == null) { return null; } - return new ObjectParameters(_drawingBus.GetPosX, _drawingBus.GetPosY, _drawingBus.GetWidth, _drawingBus.GetHeight); + return new ObjectParameters(_drawingShip.GetPosX, _drawingShip.GetPosY, _drawingShip.GetWidth, _drawingShip.GetHeight); } } - public int GetStep => (int)(_drawingBus?.EntityShip?.Step ?? 0); - public bool CheckCanMove(DirectionType direction) => _drawingBus?.CanMove(direction) ?? false; - public void MoveObject(DirectionType direction) => _drawingBus?.MoveTransport(direction); + public int GetStep => (int)(_drawingShip?.EntityShip?.Step ?? 0); + public bool CheckCanMove(DirectionType direction) => _drawingShip?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) => _drawingShip?.MoveTransport(direction); } } diff --git a/ContainerShip/DrawningShip.cs b/ContainerShip/DrawningShip.cs index f36bebd..806c072 100644 --- a/ContainerShip/DrawningShip.cs +++ b/ContainerShip/DrawningShip.cs @@ -5,32 +5,26 @@ using System.Text; using System.Threading.Tasks; using ContainerShip.Entities; +using ContainerShip.MovementStrategy; namespace ContainerShip.DrawningObjects { - public class DrawningShip { public EntityShip? EntityShip { get; protected set; } - private int _pictureWidth; private int _pictureHeight; protected int _startPosX; - protected int _startPosY; - - protected readonly int _busWidth = 150; - - protected readonly int _busHeight = 65; - + protected readonly int _shipWidth = 150; + protected readonly int _shipHeight = 65; public int GetPosX => _startPosX; public int GetPosY => _startPosY; - public int GetWidth => _busWidth; - public int GetHeight => _busHeight; - + public int GetWidth => _shipWidth; + public int GetHeight => _shipHeight; public DrawningShip(int speed, double weight, Color bodyColor, int width, int height) { - if (width < _busWidth || height < _busHeight) + if (width < _shipWidth || height < _shipHeight) { return; } @@ -38,16 +32,16 @@ namespace ContainerShip.DrawningObjects _pictureHeight = height; EntityShip = new EntityShip(speed, weight, bodyColor); } - protected DrawningShip(int speed, double weight, Color bodyColor, int width, int height, int busWidth, int busHeight) + protected DrawningShip(int speed, double weight, Color bodyColor, int width, int height, int shipWidth, int shipHeight) { - if (width < _busWidth || height < _busHeight) + if (width < _shipWidth || height < _shipHeight) { return; } _pictureWidth = width; _pictureHeight = height; - _busWidth = busWidth; - _busHeight = busHeight; + _shipWidth = shipWidth; + _shipHeight = shipHeight; EntityShip = new EntityShip(speed, weight, bodyColor); } public void SetPosition(int x, int y) @@ -67,37 +61,32 @@ namespace ContainerShip.DrawningObjects } switch (direction) { - case DirectionType.Left: if (_startPosX - EntityShip.Step > 0) { _startPosX -= (int)EntityShip.Step; } break; - case DirectionType.Up: if (_startPosY - EntityShip.Step > 0) { _startPosY -= (int)EntityShip.Step; } break; - case DirectionType.Right: - if (_startPosX + _busWidth + EntityShip.Step < _pictureWidth) + if (_startPosX + _shipWidth + EntityShip.Step < _pictureWidth) { _startPosX += (int)EntityShip.Step; } break; - case DirectionType.Down: - if (_startPosY + _busHeight + EntityShip.Step < _pictureHeight) + if (_startPosY + _shipHeight + EntityShip.Step < _pictureHeight) { _startPosY += (int)EntityShip.Step; } break; } } - public virtual void DrawTransport(Graphics g) { Pen pen = new(Color.Black); @@ -109,11 +98,10 @@ namespace ContainerShip.DrawningObjects Point[] p = { point1, point2, point3, point4 }; - g.FillPolygon(bodyBrush, p); + g.FillPolygon(bodyBrush, p); g.FillRectangle(bodyBrush, _startPosX + 15, _startPosY + 32, 80, 20); } - public bool CanMove(DirectionType direction) { if (EntityShip == null) @@ -124,10 +112,12 @@ namespace ContainerShip.DrawningObjects { DirectionType.Left => _startPosX - EntityShip.Step > 0, DirectionType.Up => _startPosY - EntityShip.Step > 0, - DirectionType.Right => _startPosX + _busWidth + EntityShip.Step < _pictureWidth, - DirectionType.Down => _startPosY + _busHeight + EntityShip.Step < _pictureHeight, + DirectionType.Right => _startPosX + _shipWidth + EntityShip.Step < _pictureWidth, + DirectionType.Down => _startPosY + _shipHeight + EntityShip.Step < _pictureHeight, _ => false, }; } + public IMoveableObject GetMoveableObject => new DrawingObjectShip(this); + } } diff --git a/ContainerShip/FormShipCollection.Designer.cs b/ContainerShip/FormShipCollection.Designer.cs new file mode 100644 index 0000000..c95156d --- /dev/null +++ b/ContainerShip/FormShipCollection.Designer.cs @@ -0,0 +1,127 @@ +namespace ContainerShip +{ + 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() + { + buttonAdd = new Button(); + buttonDelete = new Button(); + buttonUpdate = new Button(); + label1 = new Label(); + maskedTextBoxNumber = new TextBox(); + pictureBoxCollection = new PictureBox(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + SuspendLayout(); + // + // buttonAdd + // + buttonAdd.Location = new Point(736, 80); + buttonAdd.Margin = new Padding(3, 4, 3, 4); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(165, 32); + buttonAdd.TabIndex = 0; + buttonAdd.Text = "Добавить корабль"; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += buttonAdd_Click; + // + // buttonDelete + // + buttonDelete.Location = new Point(738, 217); + buttonDelete.Margin = new Padding(3, 4, 3, 4); + buttonDelete.Name = "buttonDelete"; + buttonDelete.Size = new Size(163, 32); + buttonDelete.TabIndex = 1; + buttonDelete.Text = "Удалить корабль"; + buttonDelete.UseVisualStyleBackColor = true; + buttonDelete.Click += buttonDelete_Click; + // + // buttonUpdate + // + buttonUpdate.Location = new Point(738, 292); + buttonUpdate.Margin = new Padding(3, 4, 3, 4); + buttonUpdate.Name = "buttonUpdate"; + buttonUpdate.Size = new Size(163, 32); + buttonUpdate.TabIndex = 2; + buttonUpdate.Text = "Обновить коллекцию"; + buttonUpdate.UseVisualStyleBackColor = true; + buttonUpdate.Click += buttonUpdate_Click; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(774, 12); + label1.Name = "label1"; + label1.Size = new Size(103, 20); + label1.TabIndex = 3; + label1.Text = "Инструменты"; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new Point(738, 149); + maskedTextBoxNumber.Margin = new Padding(3, 4, 3, 4); + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(162, 27); + maskedTextBoxNumber.TabIndex = 4; + // + // pictureBoxCollection + // + pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Margin = new Padding(3, 4, 3, 4); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(731, 531); + pictureBoxCollection.TabIndex = 5; + pictureBoxCollection.TabStop = false; + // + // FormShipCollection + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(914, 529); + Controls.Add(pictureBoxCollection); + Controls.Add(maskedTextBoxNumber); + Controls.Add(label1); + Controls.Add(buttonUpdate); + Controls.Add(buttonDelete); + Controls.Add(buttonAdd); + Margin = new Padding(3, 4, 3, 4); + Name = "FormShipCollection"; + Text = "Набор кораблей"; + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Button buttonAdd; + private Button buttonDelete; + private Button buttonUpdate; + private Label label1; + private TextBox maskedTextBoxNumber; + private PictureBox pictureBoxCollection; + } +} \ No newline at end of file diff --git a/ContainerShip/FormShipCollection.cs b/ContainerShip/FormShipCollection.cs new file mode 100644 index 0000000..85ebbba --- /dev/null +++ b/ContainerShip/FormShipCollection.cs @@ -0,0 +1,72 @@ +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 ContainerShip.MovementStrategy; +using ContainerShip.DrawningObjects; +using ContainerShip.Generic; + +namespace ContainerShip +{ + public partial class FormShipCollection : Form + { + private readonly ShipsGenericCollection _ship; + + public FormShipCollection() + { + InitializeComponent(); + _ship = new ShipsGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + private void buttonAdd_Click(object sender, EventArgs e) + { + FormShips form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_ship + form.SelectedShip != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _ship.ShowShips(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + private void buttonDelete_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = 0; + try + { + pos = Convert.ToInt32(maskedTextBoxNumber.Text); + } + catch + { + MessageBox.Show("Ошибка ввода данных"); + return; + } + if (_ship - pos) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _ship.ShowShips(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + private void buttonUpdate_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _ship.ShowShips(); + } + } +} diff --git a/ContainerShip/FormShipCollection.resx b/ContainerShip/FormShipCollection.resx new file mode 100644 index 0000000..a395bff --- /dev/null +++ b/ContainerShip/FormShipCollection.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/ContainerShip/FormShips.Designer.cs b/ContainerShip/FormShips.Designer.cs index f1f589e..b237e7b 100644 --- a/ContainerShip/FormShips.Designer.cs +++ b/ContainerShip/FormShips.Designer.cs @@ -31,13 +31,14 @@ System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormShips)); pictureBoxShips = new PictureBox(); buttonCreateContainerShip = new Button(); + buttonCreateShip = new Button(); buttonRight = new Button(); buttonDown = new Button(); buttonLeft = new Button(); buttonUp = new Button(); - buttonCreateShip = new Button(); - comboBoxStrategy = new ComboBox(); buttonStep = new Button(); + comboBoxStrategy = new ComboBox(); + button1 = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxShips).BeginInit(); SuspendLayout(); // @@ -45,8 +46,9 @@ // pictureBoxShips.Dock = DockStyle.Fill; pictureBoxShips.Location = new Point(0, 0); + pictureBoxShips.Margin = new Padding(3, 2, 3, 2); pictureBoxShips.Name = "pictureBoxShips"; - pictureBoxShips.Size = new Size(882, 453); + pictureBoxShips.Size = new Size(772, 340); pictureBoxShips.SizeMode = PictureBoxSizeMode.AutoSize; pictureBoxShips.TabIndex = 0; pictureBoxShips.TabStop = false; @@ -54,22 +56,36 @@ // buttonCreateContainerShip // buttonCreateContainerShip.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateContainerShip.Location = new Point(12, 364); + buttonCreateContainerShip.Location = new Point(10, 273); + buttonCreateContainerShip.Margin = new Padding(3, 2, 3, 2); buttonCreateContainerShip.Name = "buttonCreateContainerShip"; - buttonCreateContainerShip.Size = new Size(131, 77); + buttonCreateContainerShip.Size = new Size(115, 58); buttonCreateContainerShip.TabIndex = 1; buttonCreateContainerShip.Text = "Создать контейнеровоз"; buttonCreateContainerShip.UseVisualStyleBackColor = true; buttonCreateContainerShip.Click += ButtonCreateContainerShip_Click; // + // buttonCreateShip + // + buttonCreateShip.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateShip.Location = new Point(130, 273); + buttonCreateShip.Margin = new Padding(3, 2, 3, 2); + buttonCreateShip.Name = "buttonCreateShip"; + buttonCreateShip.Size = new Size(115, 58); + buttonCreateShip.TabIndex = 6; + buttonCreateShip.Text = "Создать корабль"; + buttonCreateShip.UseVisualStyleBackColor = true; + buttonCreateShip.Click += ButtonCreateShip_Click; + // // buttonRight // buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonRight.BackgroundImage = (Image)resources.GetObject("buttonRight.BackgroundImage"); buttonRight.BackgroundImageLayout = ImageLayout.Zoom; - buttonRight.Location = new Point(840, 411); + buttonRight.Location = new Point(735, 308); + buttonRight.Margin = new Padding(3, 2, 3, 2); buttonRight.Name = "buttonRight"; - buttonRight.Size = new Size(30, 30); + buttonRight.Size = new Size(26, 22); buttonRight.TabIndex = 2; buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += buttonMove_Click; @@ -79,9 +95,10 @@ buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonDown.BackgroundImage = (Image)resources.GetObject("buttonDown.BackgroundImage"); buttonDown.BackgroundImageLayout = ImageLayout.Zoom; - buttonDown.Location = new Point(804, 411); + buttonDown.Location = new Point(704, 308); + buttonDown.Margin = new Padding(3, 2, 3, 2); buttonDown.Name = "buttonDown"; - buttonDown.Size = new Size(30, 30); + buttonDown.Size = new Size(26, 22); buttonDown.TabIndex = 3; buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += buttonMove_Click; @@ -91,9 +108,10 @@ buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonLeft.BackgroundImage = (Image)resources.GetObject("buttonLeft.BackgroundImage"); buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; - buttonLeft.Location = new Point(768, 411); + buttonLeft.Location = new Point(672, 308); + buttonLeft.Margin = new Padding(3, 2, 3, 2); buttonLeft.Name = "buttonLeft"; - buttonLeft.Size = new Size(30, 30); + buttonLeft.Size = new Size(26, 22); buttonLeft.TabIndex = 4; buttonLeft.UseVisualStyleBackColor = true; buttonLeft.Click += buttonMove_Click; @@ -103,48 +121,54 @@ buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonUp.BackgroundImage = (Image)resources.GetObject("buttonUp.BackgroundImage"); buttonUp.BackgroundImageLayout = ImageLayout.Zoom; - buttonUp.Location = new Point(804, 375); + buttonUp.Location = new Point(704, 281); + buttonUp.Margin = new Padding(3, 2, 3, 2); buttonUp.Name = "buttonUp"; - buttonUp.Size = new Size(30, 30); + buttonUp.Size = new Size(26, 22); buttonUp.TabIndex = 5; buttonUp.UseVisualStyleBackColor = true; buttonUp.Click += buttonMove_Click; // - // buttonCreateShip + // buttonStep // - buttonCreateShip.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateShip.Location = new Point(149, 364); - buttonCreateShip.Name = "buttonCreateShip"; - buttonCreateShip.Size = new Size(131, 77); - buttonCreateShip.TabIndex = 6; - buttonCreateShip.Text = "Создать корабль"; - buttonCreateShip.UseVisualStyleBackColor = true; + buttonStep.Location = new Point(638, 34); + buttonStep.Margin = new Padding(3, 2, 3, 2); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(112, 23); + buttonStep.TabIndex = 8; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += ButtonStep_Click; // // comboBoxStrategy // comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxStrategy.FormattingEnabled = true; comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К правому нижнему краю" }); - comboBoxStrategy.Location = new Point(711, 12); + comboBoxStrategy.Location = new Point(622, 9); + comboBoxStrategy.Margin = new Padding(3, 2, 3, 2); comboBoxStrategy.Name = "comboBoxStrategy"; - comboBoxStrategy.Size = new Size(159, 28); + comboBoxStrategy.Size = new Size(140, 23); comboBoxStrategy.TabIndex = 7; // - // buttonStep + // button1 // - buttonStep.Location = new Point(729, 46); - buttonStep.Name = "buttonStep"; - buttonStep.Size = new Size(128, 31); - buttonStep.TabIndex = 8; - buttonStep.Text = "Шаг"; - buttonStep.UseVisualStyleBackColor = true; - buttonStep.Click += ButtonStep_Click; + button1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + button1.Location = new Point(251, 273); + button1.Margin = new Padding(3, 2, 3, 2); + button1.Name = "button1"; + button1.Size = new Size(115, 58); + button1.TabIndex = 9; + button1.Text = "Выбрать корабль"; + button1.UseVisualStyleBackColor = true; + button1.Click += buttonSelectedContainerShip_Click; // // FormShips // - AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(882, 453); + ClientSize = new Size(772, 340); + Controls.Add(button1); Controls.Add(buttonStep); Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateShip); @@ -154,6 +178,7 @@ Controls.Add(buttonRight); Controls.Add(buttonCreateContainerShip); Controls.Add(pictureBoxShips); + Margin = new Padding(3, 2, 3, 2); Name = "FormShips"; StartPosition = FormStartPosition.CenterScreen; Text = "ContainerShip"; @@ -166,12 +191,13 @@ private PictureBox pictureBoxShips; private Button buttonCreateContainerShip; + private Button buttonCreateShip; private Button buttonRight; private Button buttonDown; private Button buttonLeft; private Button buttonUp; - private Button buttonCreateShip; - private ComboBox comboBoxStrategy; private Button buttonStep; + private ComboBox comboBoxStrategy; + private Button button1; } } \ No newline at end of file diff --git a/ContainerShip/FormShips.cs b/ContainerShip/FormShips.cs index 9c3a652..7f52e1d 100644 --- a/ContainerShip/FormShips.cs +++ b/ContainerShip/FormShips.cs @@ -6,12 +6,14 @@ namespace ContainerShip public partial class FormShips : Form { private DrawningShip? _drawningShip; - private AbstractStrategy? _abstractStrategy; + public DrawningShip? SelectedShip { get; private set; } public FormShips() { InitializeComponent(); + _abstractStrategy = null; + SelectedShip = null; } private void Draw() { @@ -25,34 +27,42 @@ namespace ContainerShip _drawningShip.DrawTransport(gr); pictureBoxShips.Image = bmp; } - private void ButtonCreateContainerShip_Click(object sender, EventArgs e) { - Random random = new(); - _drawningShip = new DrawingContainerShip(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)), - Convert.ToBoolean(random.Next(0, 2)), - Convert.ToBoolean(random.Next(0, 2)), - pictureBoxShips.Width, pictureBoxShips.Height); - _drawningShip.SetPosition(random.Next(10, 100), random.Next(10, - 100)); + Random random = new Random(); + 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; + } + + Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + if (dialog.ShowDialog() == DialogResult.OK) + { + dopColor = dialog.Color; + } + + _drawningShip = new DrawingContainerShip(random.Next(200, 400), random.Next(1000, 3000), + color, dopColor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), + pictureBoxShips.Width, pictureBoxShips.Height); + + _drawningShip.SetPosition(random.Next(10, 200), random.Next(10, 200)); Draw(); } - private void ButtonCreateShip_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, pictureBoxShips.Width, pictureBoxShips.Height); - _drawningShip.SetPosition(random.Next(10, 100), random.Next(10, - 100)); + _drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } @@ -80,7 +90,6 @@ namespace ContainerShip } Draw(); } - private void ButtonStep_Click(object sender, EventArgs e) { if (_drawningShip == null) @@ -117,6 +126,13 @@ namespace ContainerShip _abstractStrategy = null; } } + + private void buttonSelectedContainerShip_Click(object sender, EventArgs e) + { + SelectedShip = _drawningShip; + DialogResult = DialogResult.OK; + } + } } diff --git a/ContainerShip/FormShips.resx b/ContainerShip/FormShips.resx index 0128bc7..e36444c 100644 --- a/ContainerShip/FormShips.resx +++ b/ContainerShip/FormShips.resx @@ -18,7 +18,7 @@ System.Resources.ResXResourceReader, System.Windows.Forms, ... System.Resources.ResXResourceWriter, System.Windows.Forms, ... this is my long stringthis is a comment - Blue + Blue [base64 mime encoded serialized .NET Framework object] diff --git a/ContainerShip/IMovableObject.cs b/ContainerShip/IMovableObject.cs index 88317d1..10f55ef 100644 --- a/ContainerShip/IMovableObject.cs +++ b/ContainerShip/IMovableObject.cs @@ -9,12 +9,11 @@ namespace ContainerShip.MovementStrategy { public interface IMoveableObject { + ObjectParameters? GetObjectPosition { get; } int GetStep { get; } - bool CheckCanMove(DirectionType direction); - void MoveObject(DirectionType direction); } } diff --git a/ContainerShip/ObjectParameters.cs b/ContainerShip/ObjectParameters.cs index 93f6557..4058be0 100644 --- a/ContainerShip/ObjectParameters.cs +++ b/ContainerShip/ObjectParameters.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; namespace ContainerShip.MovementStrategy { + public class ObjectParameters { private readonly int _x; @@ -13,17 +14,11 @@ namespace ContainerShip.MovementStrategy private readonly int _width; private readonly int _height; public int LeftBorder => _x; - public int TopBorder => _y; - public int RightBorder => _x + _width; - public int DownBorder => _y + _height; - public int ObjectMiddleHorizontal => _x + _width / 2; - public int ObjectMiddleVertical => _y + _height / 2; - public ObjectParameters(int x, int y, int width, int height) { _x = x; diff --git a/ContainerShip/Program.cs b/ContainerShip/Program.cs index e01b451..a13bbc1 100644 --- a/ContainerShip/Program.cs +++ b/ContainerShip/Program.cs @@ -11,7 +11,7 @@ namespace ContainerShip // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormShips()); + Application.Run(new FormShipCollection()); } } } \ No newline at end of file diff --git a/ContainerShip/SetGeneric.cs b/ContainerShip/SetGeneric.cs new file mode 100644 index 0000000..2b2aaa6 --- /dev/null +++ b/ContainerShip/SetGeneric.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ContainerShip.Generic +{ + internal class SetGeneric + where T : class + { + private readonly T?[] _places; + public int Count => _places.Length - 1; + public SetGeneric(int count) + { + _places = new T?[count]; + } + public int Insert(T ship) + { + int pos = Count - 1; + if (_places[Count] != null) + { + + for (int i = pos; i >= 0; --i) + { + if (_places[i] == null) + { + pos = i; + break; + } + } + for (int i = pos + 1; i <= Count; ++i) + { + _places[i - 1] = _places[i]; + } + } + _places[Count] = ship; + return pos; + } + + public bool Insert(T ship, int position) + { + if (position < 0 || position > Count) + { + return false; + } + if (_places[Count] != null) + { + int pos = Count; + for (int i = Count; i > 0; --i) + { + + if (_places[i] == null) + { + pos = i; + break; + } + } + for (int i = Count; i >= pos; --i) + { + _places[i - 1] = _places[i]; + } + } + _places[Count] = ship; + return true; + } + public bool Remove(int position) + { + if (position < 0 || position > Count) + { + 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/ContainerShip/ShipsGenericCollection.cs b/ContainerShip/ShipsGenericCollection.cs new file mode 100644 index 0000000..08815ba --- /dev/null +++ b/ContainerShip/ShipsGenericCollection.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ContainerShip.Generic; +using ContainerShip.MovementStrategy; +using ContainerShip.DrawningObjects; + +namespace ContainerShip.Generic +{ + internal class ShipsGenericCollection + where T : DrawningShip + where U : IMoveableObject + { + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 210; + private readonly int _placeSizeHeight = 90; + private readonly SetGeneric _collection; + public ShipsGenericCollection(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 +(ShipsGenericCollection collect, T? obj) + { + if (obj != null) + { + return collect._collection.Insert(obj); + } + return -1; + } + public static bool operator -(ShipsGenericCollection collect, int pos) + { + if (collect._collection.Get(pos) == null) + { + return false; + } + return collect?._collection.Remove(pos) ?? 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) + { + int x; + int y; + int index = -1; + for (int i = 0; i <= _collection.Count; ++i) + { + DrawningShip _ship = _collection.Get(i); + x = 0; + y = _pictureHeight / _placeSizeHeight - 1; + if (_ship != null) + { + index = _collection.Count - i; + while ((index - _pictureWidth / _placeSizeWidth) >= 0) + { + y--; + index -= _pictureWidth / _placeSizeWidth; + } + if (index > 0) + { + x += index; + } + x = _pictureWidth / _placeSizeWidth - 1 - x; + _ship.SetPosition(_placeSizeWidth * x, _placeSizeHeight * y); + + _ship.DrawTransport(g); + } + } + } + } + +} -- 2.25.1