diff --git a/speed_Boat/speed_Boat/BoatsGenericCollection.cs b/speed_Boat/speed_Boat/BoatsGenericCollection.cs new file mode 100644 index 0000000..3a63d73 --- /dev/null +++ b/speed_Boat/speed_Boat/BoatsGenericCollection.cs @@ -0,0 +1,140 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SpeedBoatLab.Drawings; +using speed_Boat.MovementStrategy; +using System.Drawing; +using System.IO; + +namespace speed_Boat.Generics +{ + internal class BoatsGenericCollection + where T : DrawingBoat + where U : IMovementObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 180; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 90; + /// + /// Набор объектов + /// + public readonly GenericClass _collection; + /// + /// Конструктор + /// + public BoatsGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new GenericClass(width * height); + } + /// + /// Перегрузка оператора сложения + /// + /// + public static bool operator + (BoatsGenericCollection collect, T? obj) + { + if (obj == null) + { + return false; + } + return collect?._collection.Insert(obj) ?? false; + } + /// + /// Перегрузка оператора вычитания + /// + public static bool? operator - (BoatsGenericCollection collect, int pos)//bool?? + { + T? obj = collect._collection.Get(pos); + if (obj == null) + { + return false; + } + return collect?._collection.Remove(pos) ?? false; + } + /// + /// Получение объекта IMoveableObject + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowBoats() + { + 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++) + { + int col = 0; + int width_Col = _pictureWidth / _placeSizeWidth;//количество колонок в окне прорисовки + for (int j = 0; j < _collection.Count; j++) + { + DrawingBoat? boat = _collection.Get(j); + if (boat == null) + { + col++; + if (col > 2) + col = 0; + continue; + } + boat.SetPosition(col * _placeSizeWidth, j / width_Col * _placeSizeHeight); + col++; + if (col > 2) + col = 0; + boat.DrawTransport(g); + } + } + } + } +} diff --git a/speed_Boat/speed_Boat/DrawingObjectBoat.cs b/speed_Boat/speed_Boat/DrawingObjectBoat.cs index ff455d6..041b925 100644 --- a/speed_Boat/speed_Boat/DrawingObjectBoat.cs +++ b/speed_Boat/speed_Boat/DrawingObjectBoat.cs @@ -34,5 +34,6 @@ namespace speed_Boat.MovementStrategy public bool CheckCanMove(DirectionType direction) => _drawingBoat?.CanMove(direction) ?? false; public void MoveObject(DirectionType direction) => _drawingBoat?.MoveBoat(direction); + } } diff --git a/speed_Boat/speed_Boat/DrawingSpeedBoat.cs b/speed_Boat/speed_Boat/DrawingSpeedBoat.cs index dc93f62..26a4120 100644 --- a/speed_Boat/speed_Boat/DrawingSpeedBoat.cs +++ b/speed_Boat/speed_Boat/DrawingSpeedBoat.cs @@ -18,6 +18,10 @@ namespace SpeedBoatLab.Drawings _entityBoat = new EntitySpeedboat(speed, weight, mainColor, secondColor, _isMotor, _isProtectedGlass); } } + /// + /// перегружаемый метод DrawTransport + /// + /// public override void DrawTransport(Graphics g) { if (_entityBoat is not EntitySpeedboat speedBoat) diff --git a/speed_Boat/speed_Boat/FormBoatCollection.Designer.cs b/speed_Boat/speed_Boat/FormBoatCollection.Designer.cs new file mode 100644 index 0000000..5a22003 --- /dev/null +++ b/speed_Boat/speed_Boat/FormBoatCollection.Designer.cs @@ -0,0 +1,136 @@ + +namespace SpeedBoatLab +{ + partial class FormBoatCollection + { + /// + /// 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() + { + groupBox1 = new System.Windows.Forms.GroupBox(); + label1 = new System.Windows.Forms.Label(); + maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); + UpdateCollectionButton = new System.Windows.Forms.Button(); + DeleteBoatButton = new System.Windows.Forms.Button(); + AddBoatButton = new System.Windows.Forms.Button(); + pictureBoxCollection = new System.Windows.Forms.PictureBox(); + groupBox1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + SuspendLayout(); + // + // groupBox1 + // + groupBox1.Controls.Add(label1); + groupBox1.Controls.Add(maskedTextBoxNumber); + groupBox1.Controls.Add(UpdateCollectionButton); + groupBox1.Controls.Add(DeleteBoatButton); + groupBox1.Controls.Add(AddBoatButton); + groupBox1.Location = new System.Drawing.Point(581, 12); + groupBox1.Name = "groupBox1"; + groupBox1.Size = new System.Drawing.Size(205, 426); + groupBox1.TabIndex = 0; + groupBox1.TabStop = false; + groupBox1.Text = "Настройки"; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new System.Drawing.Point(6, 85); + label1.Name = "label1"; + label1.Size = new System.Drawing.Size(135, 20); + label1.TabIndex = 4; + label1.Text = "введите позицию:"; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new System.Drawing.Point(6, 108); + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new System.Drawing.Size(193, 27); + maskedTextBoxNumber.TabIndex = 3; + // + // UpdateCollectionButton + // + UpdateCollectionButton.Location = new System.Drawing.Point(6, 194); + UpdateCollectionButton.Name = "UpdateCollectionButton"; + UpdateCollectionButton.Size = new System.Drawing.Size(193, 47); + UpdateCollectionButton.TabIndex = 2; + UpdateCollectionButton.Text = "Обновить коллекцию"; + UpdateCollectionButton.UseVisualStyleBackColor = true; + UpdateCollectionButton.Click += ButtonRefreshCollection_Click; + // + // DeleteBoatButton + // + DeleteBoatButton.Location = new System.Drawing.Point(6, 141); + DeleteBoatButton.Name = "DeleteBoatButton"; + DeleteBoatButton.Size = new System.Drawing.Size(193, 47); + DeleteBoatButton.TabIndex = 1; + DeleteBoatButton.Text = "Удалить Катер"; + DeleteBoatButton.UseVisualStyleBackColor = true; + DeleteBoatButton.Click += ButtonRemoveBoat_Click; + // + // AddBoatButton + // + AddBoatButton.Location = new System.Drawing.Point(6, 26); + AddBoatButton.Name = "AddBoatButton"; + AddBoatButton.Size = new System.Drawing.Size(193, 47); + AddBoatButton.TabIndex = 0; + AddBoatButton.Text = "Добавить Катер"; + AddBoatButton.UseVisualStyleBackColor = true; + AddBoatButton.Click += ButtonAddBoat_Click; + // + // pictureBoxCollection + // + pictureBoxCollection.Location = new System.Drawing.Point(12, 12); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new System.Drawing.Size(554, 425); + pictureBoxCollection.TabIndex = 1; + pictureBoxCollection.TabStop = false; + // + // FormBoatCollection + // + AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + ClientSize = new System.Drawing.Size(800, 450); + Controls.Add(pictureBoxCollection); + Controls.Add(groupBox1); + Name = "FormBoatCollection"; + Text = "Коллекция катеров"; + groupBox1.ResumeLayout(false); + groupBox1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + ResumeLayout(false); + } + + #endregion + + private System.Windows.Forms.GroupBox groupBox1; + private System.Windows.Forms.Button UpdateCollectionButton; + private System.Windows.Forms.Button DeleteBoatButton; + private System.Windows.Forms.Button AddBoatButton; + private System.Windows.Forms.PictureBox pictureBoxCollection; + private System.Windows.Forms.MaskedTextBox maskedTextBoxNumber; + private System.Windows.Forms.Label label1; + } +} \ No newline at end of file diff --git a/speed_Boat/speed_Boat/FormBoatCollection.cs b/speed_Boat/speed_Boat/FormBoatCollection.cs new file mode 100644 index 0000000..b08c0e0 --- /dev/null +++ b/speed_Boat/speed_Boat/FormBoatCollection.cs @@ -0,0 +1,98 @@ +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 SpeedBoatLab.Drawings; +using speed_Boat.Generics; +using speed_Boat.MovementStrategy; + + +namespace SpeedBoatLab +{ + public partial class FormBoatCollection : Form + { + /// + /// Набор объектов + /// + private readonly BoatsGenericCollection _boats; + /// + /// Конструктор + /// + public FormBoatCollection() + { + InitializeComponent(); + _boats = new BoatsGenericCollection + (pictureBoxCollection.Width, pictureBoxCollection.Height); + } + /// + /// Добавление объекта в набор + /// + /// + /// + private void ButtonAddBoat_Click(object sender, EventArgs e) + { + FormSpeedBoat form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_boats + form.SelectedBoat != false) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _boats.ShowBoats(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта из набора + /// + /// + /// + private void ButtonRemoveBoat_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + string insertPosition = maskedTextBoxNumber.Text; + int pos = -1; + if (insertPosition != string.Empty) + { + int.TryParse(insertPosition, out pos); + if (pos < 0 || pos > _boats._collection.Count - 1) + MessageBox.Show("Неверный формат позиции"); + + if (_boats - pos != false) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _boats.ShowBoats(); + } + } + else if (insertPosition == string.Empty) + { + MessageBox.Show("Неверный формат позиции"); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Обновление рисунка по набору + /// + /// + /// + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _boats.ShowBoats(); + } + } +} diff --git a/speed_Boat/speed_Boat/FormBoatCollection.resx b/speed_Boat/speed_Boat/FormBoatCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/speed_Boat/speed_Boat/FormBoatCollection.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/speed_Boat/speed_Boat/FormSpeedBoat.Designer.cs b/speed_Boat/speed_Boat/FormSpeedBoat.Designer.cs index 71530d4..c90a97d 100644 --- a/speed_Boat/speed_Boat/FormSpeedBoat.Designer.cs +++ b/speed_Boat/speed_Boat/FormSpeedBoat.Designer.cs @@ -29,149 +29,163 @@ namespace SpeedBoatLab /// private void InitializeComponent() { - this.pictureBoxSpeedBoat = new System.Windows.Forms.PictureBox(); - this.buttonCreate = new System.Windows.Forms.Button(); - this.buttonUp = new System.Windows.Forms.Button(); - this.buttonLeft = new System.Windows.Forms.Button(); - this.buttonDown = new System.Windows.Forms.Button(); - this.buttonRight = new System.Windows.Forms.Button(); - this.comboBox1 = new System.Windows.Forms.ComboBox(); - this.StepButton = new System.Windows.Forms.Button(); - this.buttonCreateSpeedBoat = new System.Windows.Forms.Button(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxSpeedBoat)).BeginInit(); - this.SuspendLayout(); + pictureBoxSpeedBoat = new System.Windows.Forms.PictureBox(); + buttonCreate = new System.Windows.Forms.Button(); + buttonUp = new System.Windows.Forms.Button(); + buttonLeft = new System.Windows.Forms.Button(); + buttonDown = new System.Windows.Forms.Button(); + buttonRight = new System.Windows.Forms.Button(); + comboBox1 = new System.Windows.Forms.ComboBox(); + StepButton = new System.Windows.Forms.Button(); + buttonCreateSpeedBoat = new System.Windows.Forms.Button(); + mainColorDialog = new System.Windows.Forms.ColorDialog(); + additionalColorDialog = new System.Windows.Forms.ColorDialog(); + BoatSelectButton = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)pictureBoxSpeedBoat).BeginInit(); + SuspendLayout(); // // pictureBoxSpeedBoat // - this.pictureBoxSpeedBoat.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBoxSpeedBoat.Location = new System.Drawing.Point(0, 0); - this.pictureBoxSpeedBoat.Name = "pictureBoxSpeedBoat"; - this.pictureBoxSpeedBoat.Size = new System.Drawing.Size(800, 450); - this.pictureBoxSpeedBoat.TabIndex = 0; - this.pictureBoxSpeedBoat.TabStop = false; + pictureBoxSpeedBoat.Dock = System.Windows.Forms.DockStyle.Fill; + pictureBoxSpeedBoat.Location = new System.Drawing.Point(0, 0); + pictureBoxSpeedBoat.Name = "pictureBoxSpeedBoat"; + pictureBoxSpeedBoat.Size = new System.Drawing.Size(800, 450); + pictureBoxSpeedBoat.TabIndex = 0; + pictureBoxSpeedBoat.TabStop = false; // // buttonCreate // - this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.buttonCreate.BackColor = System.Drawing.Color.White; - this.buttonCreate.FlatStyle = System.Windows.Forms.FlatStyle.Flat; - this.buttonCreate.Location = new System.Drawing.Point(12, 387); - this.buttonCreate.Name = "buttonCreate"; - this.buttonCreate.Size = new System.Drawing.Size(103, 51); - this.buttonCreate.TabIndex = 2; - this.buttonCreate.Text = "Создать катер"; - this.buttonCreate.UseVisualStyleBackColor = false; - this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); + buttonCreate.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left; + buttonCreate.BackColor = System.Drawing.Color.White; + buttonCreate.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + buttonCreate.Location = new System.Drawing.Point(12, 387); + buttonCreate.Name = "buttonCreate"; + buttonCreate.Size = new System.Drawing.Size(103, 51); + buttonCreate.TabIndex = 2; + buttonCreate.Text = "Создать катер"; + buttonCreate.UseVisualStyleBackColor = false; + buttonCreate.Click += buttonCreate_Click; // // buttonUp // - this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonUp.BackgroundImage = global::speed_Boat.Properties.Resources.UP; - this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonUp.FlatStyle = System.Windows.Forms.FlatStyle.Flat; - this.buttonUp.Location = new System.Drawing.Point(716, 372); - this.buttonUp.Name = "buttonUp"; - this.buttonUp.Size = new System.Drawing.Size(30, 30); - this.buttonUp.TabIndex = 10; - this.buttonUp.UseVisualStyleBackColor = true; - this.buttonUp.Click += new System.EventHandler(this.buttonMove_Click); + buttonUp.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right; + buttonUp.BackgroundImage = speed_Boat.Properties.Resources.UP; + buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + buttonUp.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + buttonUp.Location = new System.Drawing.Point(716, 372); + buttonUp.Name = "buttonUp"; + buttonUp.Size = new System.Drawing.Size(30, 30); + buttonUp.TabIndex = 10; + buttonUp.UseVisualStyleBackColor = true; + buttonUp.Click += buttonMove_Click; // // buttonLeft // - this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonLeft.BackgroundImage = global::speed_Boat.Properties.Resources.LEFT; - this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonLeft.FlatStyle = System.Windows.Forms.FlatStyle.Flat; - this.buttonLeft.Location = new System.Drawing.Point(680, 408); - this.buttonLeft.Name = "buttonLeft"; - this.buttonLeft.Size = new System.Drawing.Size(30, 30); - this.buttonLeft.TabIndex = 9; - this.buttonLeft.UseVisualStyleBackColor = true; - this.buttonLeft.Click += new System.EventHandler(this.buttonMove_Click); + buttonLeft.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right; + buttonLeft.BackgroundImage = speed_Boat.Properties.Resources.LEFT; + buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + buttonLeft.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + buttonLeft.Location = new System.Drawing.Point(680, 408); + buttonLeft.Name = "buttonLeft"; + buttonLeft.Size = new System.Drawing.Size(30, 30); + buttonLeft.TabIndex = 9; + buttonLeft.UseVisualStyleBackColor = true; + buttonLeft.Click += buttonMove_Click; // // buttonDown // - this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonDown.BackgroundImage = global::speed_Boat.Properties.Resources.DOWN; - this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonDown.FlatStyle = System.Windows.Forms.FlatStyle.Flat; - this.buttonDown.Location = new System.Drawing.Point(716, 408); - this.buttonDown.Name = "buttonDown"; - this.buttonDown.Size = new System.Drawing.Size(30, 30); - this.buttonDown.TabIndex = 8; - this.buttonDown.UseVisualStyleBackColor = true; - this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click); + buttonDown.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right; + buttonDown.BackgroundImage = speed_Boat.Properties.Resources.DOWN; + buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + buttonDown.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + buttonDown.Location = new System.Drawing.Point(716, 408); + buttonDown.Name = "buttonDown"; + buttonDown.Size = new System.Drawing.Size(30, 30); + buttonDown.TabIndex = 8; + buttonDown.UseVisualStyleBackColor = true; + buttonDown.Click += buttonMove_Click; // // buttonRight // - this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonRight.BackgroundImage = global::speed_Boat.Properties.Resources.RIGHT; - this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonRight.FlatStyle = System.Windows.Forms.FlatStyle.Flat; - this.buttonRight.Location = new System.Drawing.Point(752, 408); - this.buttonRight.Name = "buttonRight"; - this.buttonRight.Size = new System.Drawing.Size(30, 30); - this.buttonRight.TabIndex = 7; - this.buttonRight.UseVisualStyleBackColor = true; - this.buttonRight.Click += new System.EventHandler(this.buttonMove_Click); + buttonRight.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right; + buttonRight.BackgroundImage = speed_Boat.Properties.Resources.RIGHT; + buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + buttonRight.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + buttonRight.Location = new System.Drawing.Point(752, 408); + buttonRight.Name = "buttonRight"; + buttonRight.Size = new System.Drawing.Size(30, 30); + buttonRight.TabIndex = 7; + buttonRight.UseVisualStyleBackColor = true; + buttonRight.Click += buttonMove_Click; // // comboBox1 // - this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; - this.comboBox1.FormattingEnabled = true; - this.comboBox1.Items.AddRange(new object[] { - "MoveToCenter", - "MoveToBorder"}); - this.comboBox1.Location = new System.Drawing.Point(637, 12); - this.comboBox1.Name = "comboBox1"; - this.comboBox1.Size = new System.Drawing.Size(151, 28); - this.comboBox1.TabIndex = 11; + comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + comboBox1.FormattingEnabled = true; + comboBox1.Items.AddRange(new object[] { "MoveToCenter", "MoveToBorder" }); + comboBox1.Location = new System.Drawing.Point(637, 12); + comboBox1.Name = "comboBox1"; + comboBox1.Size = new System.Drawing.Size(151, 28); + comboBox1.TabIndex = 11; // // StepButton // - this.StepButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.StepButton.BackColor = System.Drawing.Color.White; - this.StepButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; - this.StepButton.Location = new System.Drawing.Point(731, 46); - this.StepButton.Name = "StepButton"; - this.StepButton.Size = new System.Drawing.Size(57, 33); - this.StepButton.TabIndex = 12; - this.StepButton.Text = "Шаг"; - this.StepButton.UseVisualStyleBackColor = false; - this.StepButton.Click += new System.EventHandler(this.StepButton_Click); + StepButton.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left; + StepButton.BackColor = System.Drawing.Color.White; + StepButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + StepButton.Location = new System.Drawing.Point(731, 46); + StepButton.Name = "StepButton"; + StepButton.Size = new System.Drawing.Size(57, 33); + StepButton.TabIndex = 12; + StepButton.Text = "Шаг"; + StepButton.UseVisualStyleBackColor = false; + StepButton.Click += StepButton_Click; // // buttonCreateSpeedBoat // - this.buttonCreateSpeedBoat.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.buttonCreateSpeedBoat.BackColor = System.Drawing.Color.White; - this.buttonCreateSpeedBoat.FlatStyle = System.Windows.Forms.FlatStyle.Flat; - this.buttonCreateSpeedBoat.Location = new System.Drawing.Point(121, 387); - this.buttonCreateSpeedBoat.Name = "buttonCreateSpeedBoat"; - this.buttonCreateSpeedBoat.Size = new System.Drawing.Size(143, 51); - this.buttonCreateSpeedBoat.TabIndex = 13; - this.buttonCreateSpeedBoat.Text = "Создать скоростной катер"; - this.buttonCreateSpeedBoat.UseVisualStyleBackColor = false; - this.buttonCreateSpeedBoat.Click += new System.EventHandler(this.buttonCreateSpeedBoat_Click); + buttonCreateSpeedBoat.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left; + buttonCreateSpeedBoat.BackColor = System.Drawing.Color.White; + buttonCreateSpeedBoat.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + buttonCreateSpeedBoat.Location = new System.Drawing.Point(121, 387); + buttonCreateSpeedBoat.Name = "buttonCreateSpeedBoat"; + buttonCreateSpeedBoat.Size = new System.Drawing.Size(143, 51); + buttonCreateSpeedBoat.TabIndex = 13; + buttonCreateSpeedBoat.Text = "Создать скоростной катер"; + buttonCreateSpeedBoat.UseVisualStyleBackColor = false; + buttonCreateSpeedBoat.Click += buttonCreateSpeedBoat_Click; + // + // BoatSelectButton + // + BoatSelectButton.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left; + BoatSelectButton.BackColor = System.Drawing.Color.White; + BoatSelectButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + BoatSelectButton.Location = new System.Drawing.Point(637, 294); + BoatSelectButton.Name = "BoatSelectButton"; + BoatSelectButton.Size = new System.Drawing.Size(151, 41); + BoatSelectButton.TabIndex = 18; + BoatSelectButton.Text = "Выбор катера"; + BoatSelectButton.UseVisualStyleBackColor = false; + BoatSelectButton.Click += BoatSelectButton_Click; // // FormSpeedBoat // - this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Controls.Add(this.buttonCreateSpeedBoat); - this.Controls.Add(this.StepButton); - this.Controls.Add(this.comboBox1); - this.Controls.Add(this.buttonUp); - this.Controls.Add(this.buttonLeft); - this.Controls.Add(this.buttonDown); - this.Controls.Add(this.buttonRight); - this.Controls.Add(this.buttonCreate); - this.Controls.Add(this.pictureBoxSpeedBoat); - this.Name = "FormSpeedBoat"; - this.Text = "Движение катера"; - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxSpeedBoat)).EndInit(); - this.ResumeLayout(false); - + AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + ClientSize = new System.Drawing.Size(800, 450); + Controls.Add(BoatSelectButton); + Controls.Add(buttonCreateSpeedBoat); + Controls.Add(StepButton); + Controls.Add(comboBox1); + Controls.Add(buttonUp); + Controls.Add(buttonLeft); + Controls.Add(buttonDown); + Controls.Add(buttonRight); + Controls.Add(buttonCreate); + Controls.Add(pictureBoxSpeedBoat); + Name = "FormSpeedBoat"; + Text = "Движение катера"; + ((System.ComponentModel.ISupportInitialize)pictureBoxSpeedBoat).EndInit(); + ResumeLayout(false); } #endregion @@ -185,6 +199,9 @@ namespace SpeedBoatLab private System.Windows.Forms.ComboBox comboBox1; private System.Windows.Forms.Button StepButton; private System.Windows.Forms.Button buttonCreateSpeedBoat; + private System.Windows.Forms.ColorDialog mainColorDialog; + private System.Windows.Forms.ColorDialog additionalColorDialog; + private System.Windows.Forms.Button BoatSelectButton; } } diff --git a/speed_Boat/speed_Boat/FormSpeedBoat.cs b/speed_Boat/speed_Boat/FormSpeedBoat.cs index 9cd4248..22f17b7 100644 --- a/speed_Boat/speed_Boat/FormSpeedBoat.cs +++ b/speed_Boat/speed_Boat/FormSpeedBoat.cs @@ -15,6 +15,8 @@ namespace SpeedBoatLab { public partial class FormSpeedBoat : Form { + Color mainColor = Color.White; + Color addColor = Color.White; /// /// Поле-объект для прорисовки объекта /// @@ -23,10 +25,16 @@ namespace SpeedBoatLab /// Стратегия перемещения /// private AbstractStrategy? _abstractStrategy; + /// + /// Выбранный автомобиль + /// + public DrawingBoat? SelectedBoat { get; private set; } public FormSpeedBoat() { InitializeComponent(); + _abstractStrategy = null; + SelectedBoat = null; } /// @@ -50,13 +58,20 @@ namespace SpeedBoatLab /// private void buttonCreate_Click(object sender, EventArgs e) { + mainColorDialog.ShowDialog(); + mainColor = mainColorDialog.Color; + Random random = new(); + if (mainColor == Color.White) + { + mainColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + } _boatMovement = new DrawingBoat(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + mainColor, pictureBoxSpeedBoat.Width, pictureBoxSpeedBoat.Height); - //startXCoord and startYCoord + ///startXCoord and startYCoord _boatMovement.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -67,16 +82,28 @@ namespace SpeedBoatLab /// private void buttonCreateSpeedBoat_Click(object sender, EventArgs e) { + mainColorDialog.ShowDialog(); + mainColor = mainColorDialog.Color; + + additionalColorDialog.ShowDialog(); + addColor = additionalColorDialog.Color; + Random random = new(); + if (mainColor == Color.White || addColor == Color.White) + { + mainColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + addColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + } + _boatMovement = new DrawingSpeedBoat(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)), + mainColor, + addColor, + true, Convert.ToBoolean(random.Next(0, 2)), pictureBoxSpeedBoat.Width, pictureBoxSpeedBoat.Height); - //startXCoord and startYCoord + ///startXCoord and startYCoord _boatMovement.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -115,19 +142,19 @@ namespace SpeedBoatLab /// private void StepButton_Click(object sender, EventArgs e) { - if(_boatMovement == null) + if (_boatMovement == null) { return; } - if(comboBox1.Enabled) - { + if (comboBox1.Enabled) + { _abstractStrategy = comboBox1.SelectedIndex switch { 0 => new MoveToCenter(), 1 => new MoveToBorder(), _ => null }; - if(_abstractStrategy == null) + if (_abstractStrategy == null) { return; } @@ -140,11 +167,17 @@ namespace SpeedBoatLab } _abstractStrategy.MakeStep(); Draw(); - if(_abstractStrategy.GetStatus() == Status.Finish) + if (_abstractStrategy.GetStatus() == Status.Finish) { comboBox1.Enabled = true; _abstractStrategy = null; } } + + private void BoatSelectButton_Click(object sender, EventArgs e) + { + SelectedBoat = _boatMovement; + DialogResult = DialogResult.OK; + } } } diff --git a/speed_Boat/speed_Boat/FormSpeedBoat.resx b/speed_Boat/speed_Boat/FormSpeedBoat.resx index f298a7b..7b4f12e 100644 --- a/speed_Boat/speed_Boat/FormSpeedBoat.resx +++ b/speed_Boat/speed_Boat/FormSpeedBoat.resx @@ -1,4 +1,64 @@ - + + + @@ -57,4 +117,10 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 161, 17 + \ No newline at end of file diff --git a/speed_Boat/speed_Boat/GenericClass.cs b/speed_Boat/speed_Boat/GenericClass.cs new file mode 100644 index 0000000..88764e1 --- /dev/null +++ b/speed_Boat/speed_Boat/GenericClass.cs @@ -0,0 +1,123 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace speed_Boat.Generics +{ + internal class GenericClass where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T?[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + + /// + /// Конструктор + /// + public GenericClass(int count) + { + _places = new T?[count]; + } + /// + /// Добавление объекта в набор + /// + public bool Insert(T boat) + { + int currentI = -1; + for(int i = 0; i < _places.Length; i++) + { + if (_places[i] == null) + { + currentI = i; + break; + } + } + + if (currentI < 0) + { + return false; + } + + for(int i = currentI; i > 0; i--) + { + _places[i] = _places[i - 1]; + } + + _places[0] = boat; + return true; + } + /// + /// Добавление объекта в набор на конкретную позицию. + /// Если позиция пуста, то производится вставка. + /// Если позиция занята, то происходит сдвиг элементов + /// вправо(при возможности) на одну позицию. + /// + public bool Insert(T boat, int position) + { + if (position < 0 || position >= Count) + return false; + + if (_places[position] == null) + { + _places[position] = boat; + return true; + } + + int currentI = -1; + for (int i = position; i < Count; i++) + { + if (_places[i] == null) + { + currentI = i; + break; + } + } + + if (currentI < 0) + return false; + + for (int i = currentI + 1; i > 0; i--) + _places[i] = _places[i - 1]; + + + _places[currentI] = boat; + 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; + } + else + { + return _places[position]; + } + + } + } +} diff --git a/speed_Boat/speed_Boat/MoveToBorder.cs b/speed_Boat/speed_Boat/MoveToBorder.cs index 1d56658..717cd1e 100644 --- a/speed_Boat/speed_Boat/MoveToBorder.cs +++ b/speed_Boat/speed_Boat/MoveToBorder.cs @@ -15,7 +15,6 @@ namespace speed_Boat.MovementStrategy { return false; } - //return objParams.RightBorder == FieldWidth && objParams.DownBorder == FieldHeight; return objParams.RightBorder <= FieldWidth && objParams.RightBorder + GetStep() >= FieldWidth&& objParams.DownBorder <= FieldHeight && diff --git a/speed_Boat/speed_Boat/Program.cs b/speed_Boat/speed_Boat/Program.cs index 63c0040..5c3bf4d 100644 --- a/speed_Boat/speed_Boat/Program.cs +++ b/speed_Boat/speed_Boat/Program.cs @@ -17,7 +17,7 @@ namespace SpeedBoatLab Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new FormSpeedBoat()); + Application.Run(new FormBoatCollection()); } } } diff --git a/speed_Boat/speed_Boat/SpeedBoatMovement.cs b/speed_Boat/speed_Boat/SpeedBoatMovement.cs index 9cf2918..7059236 100644 --- a/speed_Boat/speed_Boat/SpeedBoatMovement.cs +++ b/speed_Boat/speed_Boat/SpeedBoatMovement.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using SpeedBoatLab; using System.Drawing; using SpeedBoatLab.Entity; +using speed_Boat.MovementStrategy; namespace SpeedBoatLab.Drawings @@ -62,6 +63,11 @@ namespace SpeedBoatLab.Drawings /// public int GetHeight => heightBoat; + /// + /// Получение объекта IMoveableObject из объекта DrawningBoat + /// + public IMovementObject GetMoveableObject => new DrawingObjectBoat(this); + public bool CanMove(DirectionType direction) { if (_entityBoat == null) @@ -104,9 +110,6 @@ namespace SpeedBoatLab.Drawings break; } } - - - /// /// конструктор ///