From 99de878d24faaeaffcf14d2789520e36ebe82d48 Mon Sep 17 00:00:00 2001 From: frog24 Date: Fri, 17 Nov 2023 22:36:23 +0400 Subject: [PATCH] Lab3 WarmlyShip Barsukov --- .../ProjectWarmlyShip/AbstractStrategy.cs | 3 - .../ProjectWarmlyShip/DrawingShip.cs | 4 +- .../FormShipCollection.Designer.cs | 123 ++++++++++++++++++ .../ProjectWarmlyShip/FormShipCollection.cs | 67 ++++++++++ .../ProjectWarmlyShip/FormShipCollection.resx | 60 +++++++++ .../ProjectWarmlyShip/Frame.Designer.cs | 22 +++- ProjectWarmlyShip/ProjectWarmlyShip/Frame.cs | 32 ++++- .../ProjectWarmlyShip/Program.cs | 2 +- .../ProjectWarmlyShip/SetGeneric.cs | 93 +++++++++++++ .../ShipsGenericCollection.cs | 89 +++++++++++++ 10 files changed, 482 insertions(+), 13 deletions(-) create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/AbstractStrategy.cs b/ProjectWarmlyShip/ProjectWarmlyShip/AbstractStrategy.cs index 2698dfa..d758d06 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/AbstractStrategy.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/AbstractStrategy.cs @@ -26,7 +26,6 @@ namespace ProjectWarmlyShip.MovementStrategy FieldWidth = width; FieldHeight = height; } - public void MakeStep() { if (_state != Status.InProgress) @@ -44,9 +43,7 @@ namespace ProjectWarmlyShip.MovementStrategy protected bool MoveRight() => MoveTo(DirectionType.Right); protected bool MoveUp() => MoveTo(DirectionType.Up); protected bool MoveDown() => MoveTo(DirectionType.Down); - protected ObjectParametrs? GetObjectParametrs => _movebleObject?.GetObjectPosition; - protected int? GetStep() { if (_state != Status.InProgress) diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/DrawingShip.cs b/ProjectWarmlyShip/ProjectWarmlyShip/DrawingShip.cs index afde554..e4cd5c2 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/DrawingShip.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/DrawingShip.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using ProjectWarmlyShip.Entities; +using ProjectWarmlyShip.MovementStrategy; namespace ProjectWarmlyShip.DrawingObjects { @@ -16,7 +17,7 @@ namespace ProjectWarmlyShip.DrawingObjects protected int _startPosY; protected readonly int _shipWidth = 100; protected readonly int _shipHeight = 30; - + public IMoveableObject GetMoveableObject => new DrawingObjectShip(this); public DrawingShip(int speed, double weight, Color mainColor, int width, int heigth) { if (width <= _shipWidth || heigth <= _shipHeight) @@ -70,7 +71,6 @@ namespace ProjectWarmlyShip.DrawingObjects _ => false, }; } - public void MoveTransport(DirectionType direction) { if (!CanMove(direction) || EntityShip == null) diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs new file mode 100644 index 0000000..4edb544 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs @@ -0,0 +1,123 @@ +namespace ProjectWarmlyShip +{ + 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() + { + pictureBoxCollection = new PictureBox(); + Tools = new GroupBox(); + maskedTextBoxNumber = new TextBox(); + buttonRefresh = new Button(); + buttonDeleteShip = new Button(); + buttonAddShip = new Button(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + Tools.SuspendLayout(); + SuspendLayout(); + // + // pictureBoxCollection + // + pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(700, 460); + pictureBoxCollection.TabIndex = 0; + pictureBoxCollection.TabStop = false; + // + // Tools + // + Tools.Controls.Add(maskedTextBoxNumber); + Tools.Controls.Add(buttonRefresh); + Tools.Controls.Add(buttonDeleteShip); + Tools.Controls.Add(buttonAddShip); + Tools.Location = new Point(700, 0); + Tools.Name = "Tools"; + Tools.Size = new Size(180, 460); + Tools.TabIndex = 1; + Tools.TabStop = false; + Tools.Text = "Tools"; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new Point(10, 120); + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(165, 23); + maskedTextBoxNumber.TabIndex = 4; + // + // buttonRefresh + // + buttonRefresh.Location = new Point(10, 205); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(165, 50); + buttonRefresh.TabIndex = 2; + buttonRefresh.Text = "Refresh"; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += ButtonRefreshCollection; + // + // buttonDeleteShip + // + buttonDeleteShip.Location = new Point(10, 150); + buttonDeleteShip.Name = "buttonDeleteShip"; + buttonDeleteShip.Size = new Size(165, 50); + buttonDeleteShip.TabIndex = 1; + buttonDeleteShip.Text = "Delete Ship"; + buttonDeleteShip.UseVisualStyleBackColor = true; + buttonDeleteShip.Click += ButtonRemoveShip_Click; + // + // buttonAddShip + // + buttonAddShip.Location = new Point(10, 20); + buttonAddShip.Name = "buttonAddShip"; + buttonAddShip.Size = new Size(165, 50); + buttonAddShip.TabIndex = 0; + buttonAddShip.Text = "Add Ship"; + buttonAddShip.UseVisualStyleBackColor = true; + buttonAddShip.Click += ButtonAddShip_Click; + // + // FormShipCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(884, 461); + Controls.Add(Tools); + Controls.Add(pictureBoxCollection); + Name = "FormShipCollection"; + Text = "FormShipCollection"; + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + Tools.ResumeLayout(false); + Tools.PerformLayout(); + ResumeLayout(false); + } + + #endregion + + private PictureBox pictureBoxCollection; + private GroupBox Tools; + private TextBox maskedTextBoxNumber; + private Button buttonRefresh; + private Button buttonDeleteShip; + private Button buttonAddShip; + } +} \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs new file mode 100644 index 0000000..b4284e3 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs @@ -0,0 +1,67 @@ +using ProjectWarmlyShip.DrawingObjects; +using ProjectWarmlyShip.Generics; +using ProjectWarmlyShip.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; +using ProjectWarmlyShip.DrawingObjects; +using ProjectWarmlyShip.Generics; +using ProjectWarmlyShip.MovementStrategy; + +namespace ProjectWarmlyShip +{ + public partial class FormShipCollection : Form + { + private readonly ShipsGenericCollection _ships; + public FormShipCollection() + { + InitializeComponent(); + _ships = new ShipsGenericCollection + (pictureBoxCollection.Width, pictureBoxCollection.Height); + } + private void ButtonAddShip_Click(object sender, EventArgs e) + { + WarmlyShipForm form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_ships + form.SelectedShip != -1) + { + MessageBox.Show("Object Inserted"); + pictureBoxCollection.Image = _ships.ShowShips(); + } + else + { + MessageBox.Show("Object Not Inserted"); + } + } + } + private void ButtonRemoveShip_Click(Object sender, EventArgs e) + { + if (MessageBox.Show("Delete Object?", "Delete", MessageBoxButtons.YesNo, + MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_ships - pos != false) + { + MessageBox.Show("Object Deleted"); + pictureBoxCollection.Image = _ships.ShowShips(); + } + else + { + MessageBox.Show("Object Not Deleted"); + } + } + private void ButtonRefreshCollection(object sender, EventArgs e) + { + pictureBoxCollection.Image = _ships.ShowShips(); + } + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/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/ProjectWarmlyShip/ProjectWarmlyShip/Frame.Designer.cs b/ProjectWarmlyShip/ProjectWarmlyShip/Frame.Designer.cs index 8ed3ab7..0ed6e2a 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/Frame.Designer.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/Frame.Designer.cs @@ -37,6 +37,7 @@ button1 = new Button(); comboBoxStrategy = new ComboBox(); buttonStep = new Button(); + buttonSelectShip = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxWarmlyShip).BeginInit(); SuspendLayout(); // @@ -53,9 +54,9 @@ // buttonCreate // buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 426); + buttonCreate.Location = new Point(10, 425); buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(75, 23); + buttonCreate.Size = new Size(75, 25); buttonCreate.TabIndex = 1; buttonCreate.Text = "Create Ship"; buttonCreate.UseVisualStyleBackColor = true; @@ -112,9 +113,9 @@ // button1 // button1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - button1.Location = new Point(93, 426); + button1.Location = new Point(90, 425); button1.Name = "button1"; - button1.Size = new Size(120, 23); + button1.Size = new Size(120, 25); button1.TabIndex = 6; button1.Text = "Create Warmly Ship"; button1.UseVisualStyleBackColor = true; @@ -141,11 +142,23 @@ buttonStep.UseVisualStyleBackColor = true; buttonStep.Click += ButtonStep_Click; // + // buttonSelectShip + // + buttonSelectShip.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonSelectShip.Location = new Point(215, 425); + buttonSelectShip.Name = "buttonSelectShip"; + buttonSelectShip.Size = new Size(75, 25); + buttonSelectShip.TabIndex = 10; + buttonSelectShip.Text = "Select"; + buttonSelectShip.UseVisualStyleBackColor = true; + buttonSelectShip.Click += ButtonSelectShip_Click; + // // WarmlyShipForm // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(884, 461); + Controls.Add(buttonSelectShip); Controls.Add(buttonStep); Controls.Add(comboBoxStrategy); Controls.Add(button1); @@ -174,5 +187,6 @@ private Button button1; private ComboBox comboBoxStrategy; private Button buttonStep; + private Button buttonSelectShip; } } \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/Frame.cs b/ProjectWarmlyShip/ProjectWarmlyShip/Frame.cs index c9d8c8c..a2f6b27 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/Frame.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/Frame.cs @@ -7,9 +7,12 @@ namespace ProjectWarmlyShip { private DrawingShip? _drawingShip; private AbstractStrategy? _abstractStrategy; + public DrawingShip SelectedShip { get; set; } public WarmlyShipForm() { InitializeComponent(); + _abstractStrategy = null; + SelectedShip = null; } private void Draw() { @@ -26,10 +29,21 @@ namespace ProjectWarmlyShip private void ButtonCreateWarmlyShip_Click(object sender, EventArgs e) { Random random = new Random(); + Color color = Color.FromArgb(random.Next(0, 256)); + ColorDialog dialog = new ColorDialog(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + Color opt_color = Color.FromArgb(random.Next(0, 256)); + if (dialog.ShowDialog() == DialogResult.OK) + { + opt_color = dialog.Color; + } _drawingShip = new DrawingWarmlyShip( 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)), + color, + opt_color, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxWarmlyShip.Width, @@ -40,10 +54,17 @@ namespace ProjectWarmlyShip private void ButtonCreateShip_Click(object sender, EventArgs e) { Random random = new Random(); + Color color = Color.FromArgb(random.Next(0, 256)); + ColorDialog dialog = new ColorDialog(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + _drawingShip = new DrawingShip( random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + color, pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); _drawingShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); @@ -110,5 +131,10 @@ namespace ProjectWarmlyShip _abstractStrategy = null; } } + private void ButtonSelectShip_Click(object sender, EventArgs e) + { + SelectedShip = _drawingShip; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/Program.cs b/ProjectWarmlyShip/ProjectWarmlyShip/Program.cs index edcb85c..2c7e667 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/Program.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/Program.cs @@ -11,7 +11,7 @@ namespace ProjectWarmlyShip // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new WarmlyShipForm()); + Application.Run(new FormShipCollection()); } } } \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs b/ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs new file mode 100644 index 0000000..cb98c71 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.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[0] == null) + { + _places[0] = ship; + } + else + { + int nullIndex = 0; + for (int i = 0; i < _places.Length; i++) + { + if (_places[i] == null) + { + nullIndex = i; + break; + } + } + if (nullIndex == 0) + { + return -1; + } + for (int i = nullIndex; i > 0; i--) + { + _places[i] = _places[i - 1]; + } + _places[0] = ship; + } + return 0; + } + public int Insert(T ship, int position) + { + if (position >= Count) + { + return -1; + } + if (_places[position] == null) + { + _places[position] = ship; + } + else + { + int nullIndex = position; + for (int i = position; i < _places.Length; i++) + { + if (_places[i] == null) + { + nullIndex = i; + break; + } + } + if (nullIndex == position) + { + return -1; + } + for (int i = nullIndex; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = ship; + } + return position; + } + public bool Remove(int position) + { + if (position >= Count) + { + return false; + } + _places[position] = null; + return true; + } + public T? Get(int position) + { + return _places[position]; + } + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs new file mode 100644 index 0000000..d1d9b7f --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectWarmlyShip.DrawingObjects; +using ProjectWarmlyShip.MovementStrategy; + +namespace ProjectWarmlyShip.Generics +{ + internal class ShipsGenericCollection + where T : DrawingShip + where U : IMoveableObject + { + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 140; + private readonly int _placeSizeHeight = 80; + 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 -1; + } + return collect?._collection.Insert(obj); + } + public static bool operator -(ShipsGenericCollection collect, int pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + collect._collection.Remove(pos); + return true; + } + 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); + DrawBackgroud(gr); + DrawObjects(gr); + return bmp; + } + private void DrawBackgroud(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 width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + for (int i = 0; i < _collection.Count; i++) + { + DrawingShip? ship = _collection.Get(i); + if (ship != null) + { + ship.SetPosition((width - (i % width) - 1) * _placeSizeWidth, (i / height) * _placeSizeHeight + 15); + ship.DrawTrasport(g); + } + } + } + } +} -- 2.25.1