From ab8360df4bf80e6b5de10a2389647fad7e054f07 Mon Sep 17 00:00:00 2001 From: kirin Date: Tue, 17 Oct 2023 13:49:31 +0400 Subject: [PATCH 1/2] ALL done --- .../DrawingElectricLocomotiv.cs | 10 +- lab1/{ => DrawingObjects}/DrawingLocomotiv.cs | 9 +- lab1/ElectricLocomotive.csproj | 12 ++ .../{ => Entities}/EntityElectricLocomotiv.cs | 2 +- lab1/{ => Entities}/EntityLocomotiv.cs | 8 +- ....Designer.cs => FormLocomotiv.Designer.cs} | 32 +++-- lab1/{MainForm.cs => FormLocomotiv.cs} | 84 ++++++------ lab1/{MainForm.resx => FormLocomotiv.resx} | 0 lab1/FormLocomotivCollection.Designer.cs | 121 ++++++++++++++++++ lab1/FormLocomotivCollection.cs | 42 ++++++ lab1/FormLocomotivCollection.resx | 120 +++++++++++++++++ lab1/Generics/LocosGenericCollection.cs | 76 +++++++++++ lab1/Generics/SetGeneric.cs | 52 ++++++++ .../AbstractStrategy.cs | 0 lab1/{ => MovementStrategy}/DirectionType.cs | 0 .../DrawingObjectLocomotiv.cs | 0 .../{ => MovementStrategy}/IMoveableObject.cs | 0 lab1/{ => MovementStrategy}/MoveToCenter.cs | 0 lab1/{ => MovementStrategy}/MoveToEdge.cs | 0 .../ObjectParameters.cs | 0 lab1/{ => MovementStrategy}/Status.cs | 0 lab1/Program.cs | 2 +- 22 files changed, 508 insertions(+), 62 deletions(-) rename lab1/{ => DrawingObjects}/DrawingElectricLocomotiv.cs (82%) rename lab1/{ => DrawingObjects}/DrawingLocomotiv.cs (92%) rename lab1/{ => Entities}/EntityElectricLocomotiv.cs (75%) rename lab1/{ => Entities}/EntityLocomotiv.cs (67%) rename lab1/{MainForm.Designer.cs => FormLocomotiv.Designer.cs} (87%) rename lab1/{MainForm.cs => FormLocomotiv.cs} (58%) rename lab1/{MainForm.resx => FormLocomotiv.resx} (100%) create mode 100644 lab1/FormLocomotivCollection.Designer.cs create mode 100644 lab1/FormLocomotivCollection.cs create mode 100644 lab1/FormLocomotivCollection.resx create mode 100644 lab1/Generics/LocosGenericCollection.cs create mode 100644 lab1/Generics/SetGeneric.cs rename lab1/{ => MovementStrategy}/AbstractStrategy.cs (100%) rename lab1/{ => MovementStrategy}/DirectionType.cs (100%) rename lab1/{ => MovementStrategy}/DrawingObjectLocomotiv.cs (100%) rename lab1/{ => MovementStrategy}/IMoveableObject.cs (100%) rename lab1/{ => MovementStrategy}/MoveToCenter.cs (100%) rename lab1/{ => MovementStrategy}/MoveToEdge.cs (100%) rename lab1/{ => MovementStrategy}/ObjectParameters.cs (100%) rename lab1/{ => MovementStrategy}/Status.cs (100%) diff --git a/lab1/DrawingElectricLocomotiv.cs b/lab1/DrawingObjects/DrawingElectricLocomotiv.cs similarity index 82% rename from lab1/DrawingElectricLocomotiv.cs rename to lab1/DrawingObjects/DrawingElectricLocomotiv.cs index 8782739..65935d8 100644 --- a/lab1/DrawingElectricLocomotiv.cs +++ b/lab1/DrawingObjects/DrawingElectricLocomotiv.cs @@ -1,13 +1,15 @@ -namespace ElectricLocomotive; +using System.Drawing.Drawing2D; + +namespace ElectricLocomotive; public class DrawingElectricLocomotiv : DrawingLocomotiv { - public DrawingElectricLocomotiv(int speed, double weight, int width, int height) : base(speed, weight, width, - height) + public DrawingElectricLocomotiv(int speed, double weight, int width, int height, Color mainColor, Color dopColor, Color batteryColor, Color rogaColor) : base(speed, weight, width, + height, mainColor, dopColor) { if (EntityLocomotiv != null) { - EntityLocomotiv = new EntityElectricLocomotiv(speed, weight, Color.Crimson, Color.Blue); + EntityLocomotiv = new EntityElectricLocomotiv(speed, weight, batteryColor, rogaColor, mainColor, dopColor); } } diff --git a/lab1/DrawingLocomotiv.cs b/lab1/DrawingObjects/DrawingLocomotiv.cs similarity index 92% rename from lab1/DrawingLocomotiv.cs rename to lab1/DrawingObjects/DrawingLocomotiv.cs index 7941506..049598d 100644 --- a/lab1/DrawingLocomotiv.cs +++ b/lab1/DrawingObjects/DrawingLocomotiv.cs @@ -21,24 +21,25 @@ namespace ElectricLocomotive protected int _startPosY; protected readonly int _vehicleWidth = 170; protected readonly int _vehicleHeight = 110; + public IMoveableObject GetMoveableObject => new DrawingObjectLocomotiv(this); - public DrawingLocomotiv(int speed, double weight,int width,int height) + public DrawingLocomotiv(int speed, double weight,int width,int height, Color mainColor, Color dopColor) { if (width <= _vehicleWidth || height <= _vehicleHeight) { return; } _pictureWidth = width; _pictureHeight = height; - EntityLocomotiv = new EntityLocomotiv(speed, weight); + EntityLocomotiv = new EntityLocomotiv(speed, weight, mainColor, dopColor); } - protected DrawingLocomotiv(int speed, double weight,int width,int height, int vehicleHeight, int vehicleWidth) + protected DrawingLocomotiv(int speed, double weight,int width,int height, int vehicleHeight, int vehicleWidth, Color mainColor, Color dopColor) { if (width <= _vehicleWidth || height <= _vehicleHeight) { return; } _pictureWidth = width; _pictureHeight = height; - EntityLocomotiv = new EntityLocomotiv(speed, weight); + EntityLocomotiv = new EntityLocomotiv(speed, weight, mainColor, dopColor); } public bool CanMove(DirectionType direction) { diff --git a/lab1/ElectricLocomotive.csproj b/lab1/ElectricLocomotive.csproj index b57c89e..7c4e5c4 100644 --- a/lab1/ElectricLocomotive.csproj +++ b/lab1/ElectricLocomotive.csproj @@ -8,4 +8,16 @@ enable + + + + + + + + + Form + + + \ No newline at end of file diff --git a/lab1/EntityElectricLocomotiv.cs b/lab1/Entities/EntityElectricLocomotiv.cs similarity index 75% rename from lab1/EntityElectricLocomotiv.cs rename to lab1/Entities/EntityElectricLocomotiv.cs index 9d034eb..5314e0f 100644 --- a/lab1/EntityElectricLocomotiv.cs +++ b/lab1/Entities/EntityElectricLocomotiv.cs @@ -4,7 +4,7 @@ public class EntityElectricLocomotiv : EntityLocomotiv { public Color BatteryColor { get; private set; } public Color RogaColor { get; private set; } - public EntityElectricLocomotiv(int speed,double weight, Color batteryColor, Color rogaColor) : base(speed, weight) + public EntityElectricLocomotiv(int speed,double weight, Color batteryColor, Color rogaColor, Color mainColor, Color dopColor) : base(speed, weight, mainColor, dopColor) { BatteryColor = batteryColor; RogaColor = rogaColor; diff --git a/lab1/EntityLocomotiv.cs b/lab1/Entities/EntityLocomotiv.cs similarity index 67% rename from lab1/EntityLocomotiv.cs rename to lab1/Entities/EntityLocomotiv.cs index c876dd6..5a32b76 100644 --- a/lab1/EntityLocomotiv.cs +++ b/lab1/Entities/EntityLocomotiv.cs @@ -12,14 +12,16 @@ namespace ElectricLocomotive public int Speed { get; private set; } public double Weight { get; private set; } public double Step => (double)Speed * 100 / Weight; - public readonly Color ColorBody = Color.Black; - public readonly Color ColorWindow = Color.Blue; + public Color ColorBody = Color.Black; + public Color ColorWindow = Color.Blue; public readonly Color ColorFillBody = Color.White; - public EntityLocomotiv(int speed,double weight) + public EntityLocomotiv(int speed,double weight, Color mainColor, Color dopColor) { Speed = speed; Weight = weight; + ColorBody = mainColor; + ColorWindow = dopColor; } } } diff --git a/lab1/MainForm.Designer.cs b/lab1/FormLocomotiv.Designer.cs similarity index 87% rename from lab1/MainForm.Designer.cs rename to lab1/FormLocomotiv.Designer.cs index 795c365..e2dcdbc 100644 --- a/lab1/MainForm.Designer.cs +++ b/lab1/FormLocomotiv.Designer.cs @@ -1,7 +1,5 @@ -namespace ElectricLocomotive -{ - partial class MainForm - { +namespace ElectricLocomotive { + partial class FormLocomotiv { /// /// Required designer variable. /// @@ -11,10 +9,8 @@ /// 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)) - { + protected override void Dispose(bool disposing) { + if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); @@ -26,8 +22,7 @@ /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// - private void InitializeComponent() - { + private void InitializeComponent() { locoBox = new PictureBox(); moveRightButton = new Button(); moveDownButton = new Button(); @@ -37,6 +32,7 @@ paintElectricLocoButton = new Button(); movesBox = new ComboBox(); moveButton = new Button(); + selectLocoButton = new Button(); ((System.ComponentModel.ISupportInitialize)locoBox).BeginInit(); SuspendLayout(); // @@ -133,11 +129,22 @@ moveButton.UseVisualStyleBackColor = true; moveButton.Click += MoveButton_Click; // - // MainForm + // selectLocoButton + // + selectLocoButton.Location = new Point(491, 647); + selectLocoButton.Name = "selectLocoButton"; + selectLocoButton.Size = new Size(198, 64); + selectLocoButton.TabIndex = 8; + selectLocoButton.Text = "Выбрать локомотив"; + selectLocoButton.UseVisualStyleBackColor = true; + selectLocoButton.Click += selectLocoButton_Click; + // + // FormLocomotiv // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(1265, 731); + Controls.Add(selectLocoButton); Controls.Add(moveButton); Controls.Add(movesBox); Controls.Add(paintElectricLocoButton); @@ -148,7 +155,7 @@ Controls.Add(paintLocoButton); Controls.Add(locoBox); FormBorderStyle = FormBorderStyle.FixedSingle; - Name = "MainForm"; + Name = "FormLocomotiv"; StartPosition = FormStartPosition.CenterScreen; Text = "Лабораторная работа 1"; ((System.ComponentModel.ISupportInitialize)locoBox).EndInit(); @@ -166,5 +173,6 @@ private Button paintElectricLocoButton; private ComboBox movesBox; private Button moveButton; + private Button selectLocoButton; } } \ No newline at end of file diff --git a/lab1/MainForm.cs b/lab1/FormLocomotiv.cs similarity index 58% rename from lab1/MainForm.cs rename to lab1/FormLocomotiv.cs index 46ec777..e872ea1 100644 --- a/lab1/MainForm.cs +++ b/lab1/FormLocomotiv.cs @@ -1,49 +1,60 @@ using ElectricLocomotive; -namespace ElectricLocomotive -{ - public partial class MainForm : Form - { +namespace ElectricLocomotive { + public partial class FormLocomotiv : Form { private DrawingLocomotiv? _drawingLocomotiv; - private AbstractStrategy _abstractStrategy; - public MainForm() - { + private AbstractStrategy? _abstractStrategy; + public DrawingLocomotiv? SelectedLocomotiv { get; private set; } + public FormLocomotiv() { InitializeComponent(); + _abstractStrategy = null; + SelectedLocomotiv = null; } - private void Draw() - { + private void Draw() { if (_drawingLocomotiv == null) return; Bitmap bmp = new(locoBox.Width, locoBox.Height); Graphics g = Graphics.FromImage(bmp); _drawingLocomotiv.DrawLoco(g); locoBox.Image = bmp; } - private void PaintLocoButton_click(object sender, EventArgs e) - { + private void PaintLocoButton_click(object sender, EventArgs e) { Random random = new(); - _drawingLocomotiv = new DrawingLocomotiv(random.Next(100, 300), random.Next(1000, 3000), locoBox.Width, locoBox.Height); + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + Color dopColor = 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; + _drawingLocomotiv = new DrawingLocomotiv(random.Next(100, 300), random.Next(1000, 3000), locoBox.Width, locoBox.Height, color, dopColor); + _drawingLocomotiv.SetPosition(random.Next(10, 100), random.Next(10, 100)); + + Draw(); + } + + private void PaintElectricLocoButton_click(object sender, EventArgs e) { + Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + Color batteryColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + Color rogaColor = 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; + if (dialog.ShowDialog() == DialogResult.OK) + dopColor = dialog.Color; + + _drawingLocomotiv = new DrawingElectricLocomotiv(random.Next(100, 300), random.Next(1000, 3000), locoBox.Width, locoBox.Height, color, dopColor, batteryColor, rogaColor); _drawingLocomotiv.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } - private void PaintElectricLocoButton_click(object sender, EventArgs e) - { - Random random = new(); - _drawingLocomotiv = new DrawingElectricLocomotiv(random.Next(100, 300), random.Next(1000, 3000), locoBox.Width, locoBox.Height); - _drawingLocomotiv.SetPosition(random.Next(10, 100), random.Next(10, 100)); - Draw(); - } - - private void MoveSideButton_Click(object sender, EventArgs e) - { + private void MoveSideButton_Click(object sender, EventArgs e) { if (_drawingLocomotiv == null) return; Control button = sender as Control; string name = button.Name; - switch (name) - { + switch (name) { case "moveUpButton": _drawingLocomotiv.MoveTransport(DirectionType.Up); break; @@ -60,21 +71,17 @@ namespace ElectricLocomotive Draw(); } - private void MoveButton_Click(object sender, EventArgs e) - { + private void MoveButton_Click(object sender, EventArgs e) { if (_drawingLocomotiv == null) return; - if (movesBox.Enabled) - { + if (movesBox.Enabled) { _abstractStrategy = movesBox.SelectedIndex - switch - { + switch { 0 => new MoveToEdge(), 1 => new MoveToCenter(), _ => null, - } ; - if (_abstractStrategy == null) - { + }; + if (_abstractStrategy == null) { return; } _abstractStrategy.SetData(new @@ -82,17 +89,20 @@ namespace ElectricLocomotive locoBox.Height); movesBox.Enabled = false; } - if (_abstractStrategy == null) - { + if (_abstractStrategy == null) { return; } _abstractStrategy.MakeStep(); Draw(); - if (_abstractStrategy.GetStatus() == Status.Finish) - { + if (_abstractStrategy.GetStatus() == Status.Finish) { movesBox.Enabled = true; _abstractStrategy = null; } } + + private void selectLocoButton_Click(object sender, EventArgs e) { + SelectedLocomotiv = _drawingLocomotiv; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/lab1/MainForm.resx b/lab1/FormLocomotiv.resx similarity index 100% rename from lab1/MainForm.resx rename to lab1/FormLocomotiv.resx diff --git a/lab1/FormLocomotivCollection.Designer.cs b/lab1/FormLocomotivCollection.Designer.cs new file mode 100644 index 0000000..a4898d1 --- /dev/null +++ b/lab1/FormLocomotivCollection.Designer.cs @@ -0,0 +1,121 @@ +using System.ComponentModel; + +namespace ElectricLocomotive; + +partial class FormLocomotivCollection { + /// + /// Required designer variable. + /// + private 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() { + toolsBox = new GroupBox(); + refreshCollection = new Button(); + deleteLoco = new Button(); + input = new TextBox(); + addLocomotiv = new Button(); + collectionPictureBox = new PictureBox(); + toolsBox.SuspendLayout(); + ((ISupportInitialize)collectionPictureBox).BeginInit(); + SuspendLayout(); + // + // toolsBox + // + toolsBox.Controls.Add(refreshCollection); + toolsBox.Controls.Add(deleteLoco); + toolsBox.Controls.Add(input); + toolsBox.Controls.Add(addLocomotiv); + toolsBox.Location = new Point(944, 12); + toolsBox.Name = "toolsBox"; + toolsBox.Size = new Size(309, 707); + toolsBox.TabIndex = 0; + toolsBox.TabStop = false; + toolsBox.Text = "Инструменты"; + // + // refreshCollection + // + refreshCollection.Location = new Point(19, 324); + refreshCollection.Name = "refreshCollection"; + refreshCollection.Size = new Size(271, 59); + refreshCollection.TabIndex = 3; + refreshCollection.Text = "Обновить коллекцию"; + refreshCollection.UseVisualStyleBackColor = true; + refreshCollection.Click += refreshCollection_Click; + // + // deleteLoco + // + deleteLoco.Location = new Point(21, 199); + deleteLoco.Name = "deleteLoco"; + deleteLoco.Size = new Size(271, 59); + deleteLoco.TabIndex = 2; + deleteLoco.Text = "Удалить электровоз"; + deleteLoco.UseVisualStyleBackColor = true; + deleteLoco.Click += deleteLoco_Click; + // + // input + // + input.Location = new Point(51, 152); + input.Name = "input"; + input.Size = new Size(214, 27); + input.TabIndex = 1; + // + // addLocomotiv + // + addLocomotiv.Location = new Point(21, 40); + addLocomotiv.Name = "addLocomotiv"; + addLocomotiv.Size = new Size(271, 59); + addLocomotiv.TabIndex = 0; + addLocomotiv.Text = "Добавить электровоз"; + addLocomotiv.UseVisualStyleBackColor = true; + addLocomotiv.Click += addLocomotiv_Click; + // + // collectionPictureBox + // + collectionPictureBox.Location = new Point(12, 12); + collectionPictureBox.Name = "collectionPictureBox"; + collectionPictureBox.Size = new Size(933, 707); + collectionPictureBox.TabIndex = 1; + collectionPictureBox.TabStop = false; + // + // FormLocomotivCollection + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1265, 731); + Controls.Add(collectionPictureBox); + Controls.Add(toolsBox); + Name = "FormLocomotivCollection"; + Text = "Набор локомотивов"; + toolsBox.ResumeLayout(false); + toolsBox.PerformLayout(); + ((ISupportInitialize)collectionPictureBox).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox toolsBox; + private TextBox input; + private Button addLocomotiv; + private PictureBox collectionPictureBox; + private Button refreshCollection; + private Button deleteLoco; +} \ No newline at end of file diff --git a/lab1/FormLocomotivCollection.cs b/lab1/FormLocomotivCollection.cs new file mode 100644 index 0000000..beb547b --- /dev/null +++ b/lab1/FormLocomotivCollection.cs @@ -0,0 +1,42 @@ +namespace ElectricLocomotive; + +public partial class FormLocomotivCollection : Form { + private readonly LocosGenericCollection _locos; + public FormLocomotivCollection() { + InitializeComponent(); + _locos = new(collectionPictureBox.Width, collectionPictureBox.Height); + } + + private void addLocomotiv_Click(object sender, EventArgs e) { + FormLocomotiv form = new(); + if (form.ShowDialog() == DialogResult.OK) { + if (_locos + form.SelectedLocomotiv != null) { + MessageBox.Show("Объект добавлен"); + collectionPictureBox.Image = _locos.ShowLocos(); + } + else { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + + private void deleteLoco_Click(object sender, EventArgs e) { + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { + return; + } + int pos = Convert.ToInt32(input.Text); + if (_locos - pos != null) { + MessageBox.Show("Объект удален"); + collectionPictureBox.Image = _locos.ShowLocos(); + } + else { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void refreshCollection_Click(object sender, EventArgs e) { + collectionPictureBox.Image = _locos.ShowLocos(); + + } + +} \ No newline at end of file diff --git a/lab1/FormLocomotivCollection.resx b/lab1/FormLocomotivCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/lab1/FormLocomotivCollection.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/lab1/Generics/LocosGenericCollection.cs b/lab1/Generics/LocosGenericCollection.cs new file mode 100644 index 0000000..9140655 --- /dev/null +++ b/lab1/Generics/LocosGenericCollection.cs @@ -0,0 +1,76 @@ +namespace ElectricLocomotive; + +public class LocosGenericCollection where T : DrawingLocomotiv where U : IMoveableObject +{ + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 170; + private readonly int _placeSizeHeight = 110; + private readonly SetGeneric _collection; + public LocosGenericCollection(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 +(LocosGenericCollection collect, T? + obj) + { + if (obj == null) return -1; + + return collect?._collection.Insert(obj) ?? -1; + } + + public static bool operator -(LocosGenericCollection collect, int + pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + return collect._collection.Remove(pos); + } + return false; + } + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + public Bitmap ShowLocos() + { + 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++) { + DrawingLocomotiv locomotiv = _collection.Get(i); + if (locomotiv != null) { + int inRow = _pictureWidth / _placeSizeWidth; + locomotiv.SetPosition(i % inRow * _placeSizeWidth, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight); + locomotiv.DrawLoco(g); + } + } + } + +} \ No newline at end of file diff --git a/lab1/Generics/SetGeneric.cs b/lab1/Generics/SetGeneric.cs new file mode 100644 index 0000000..5348a03 --- /dev/null +++ b/lab1/Generics/SetGeneric.cs @@ -0,0 +1,52 @@ +namespace ElectricLocomotive; + +public class SetGeneric where T : class where U : struct +{ + private readonly T?[] _places; + + public int Count => _places.Length; + + public SetGeneric(int count) + { + _places = new T?[count]; + } + public int Insert(T electricLocomotiv) + { + if (_places[Count - 1] != null) + return -1; + return Insert(electricLocomotiv, 0); + } + + public int Insert(T electricLocomotiv, 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] = electricLocomotiv; + 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]; + } + +} \ No newline at end of file diff --git a/lab1/AbstractStrategy.cs b/lab1/MovementStrategy/AbstractStrategy.cs similarity index 100% rename from lab1/AbstractStrategy.cs rename to lab1/MovementStrategy/AbstractStrategy.cs diff --git a/lab1/DirectionType.cs b/lab1/MovementStrategy/DirectionType.cs similarity index 100% rename from lab1/DirectionType.cs rename to lab1/MovementStrategy/DirectionType.cs diff --git a/lab1/DrawingObjectLocomotiv.cs b/lab1/MovementStrategy/DrawingObjectLocomotiv.cs similarity index 100% rename from lab1/DrawingObjectLocomotiv.cs rename to lab1/MovementStrategy/DrawingObjectLocomotiv.cs diff --git a/lab1/IMoveableObject.cs b/lab1/MovementStrategy/IMoveableObject.cs similarity index 100% rename from lab1/IMoveableObject.cs rename to lab1/MovementStrategy/IMoveableObject.cs diff --git a/lab1/MoveToCenter.cs b/lab1/MovementStrategy/MoveToCenter.cs similarity index 100% rename from lab1/MoveToCenter.cs rename to lab1/MovementStrategy/MoveToCenter.cs diff --git a/lab1/MoveToEdge.cs b/lab1/MovementStrategy/MoveToEdge.cs similarity index 100% rename from lab1/MoveToEdge.cs rename to lab1/MovementStrategy/MoveToEdge.cs diff --git a/lab1/ObjectParameters.cs b/lab1/MovementStrategy/ObjectParameters.cs similarity index 100% rename from lab1/ObjectParameters.cs rename to lab1/MovementStrategy/ObjectParameters.cs diff --git a/lab1/Status.cs b/lab1/MovementStrategy/Status.cs similarity index 100% rename from lab1/Status.cs rename to lab1/MovementStrategy/Status.cs diff --git a/lab1/Program.cs b/lab1/Program.cs index 47f0250..d457a45 100644 --- a/lab1/Program.cs +++ b/lab1/Program.cs @@ -11,7 +11,7 @@ namespace ElectricLocomotive // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new MainForm()); + Application.Run(new FormLocomotivCollection()); } } } \ No newline at end of file -- 2.25.1 From e787a6d765620c076b274f517553f46077b5d092 Mon Sep 17 00:00:00 2001 From: kirin Date: Tue, 31 Oct 2023 11:39:52 +0400 Subject: [PATCH 2/2] removed additional task --- lab1/Generics/SetGeneric.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lab1/Generics/SetGeneric.cs b/lab1/Generics/SetGeneric.cs index 5348a03..5da02e3 100644 --- a/lab1/Generics/SetGeneric.cs +++ b/lab1/Generics/SetGeneric.cs @@ -1,6 +1,6 @@ namespace ElectricLocomotive; -public class SetGeneric where T : class where U : struct +public class SetGeneric where T : class { private readonly T?[] _places; -- 2.25.1