From a17318de927bde44e56fe1371f7e2a4b36db4e06 Mon Sep 17 00:00:00 2001 From: "nikbel2004@outlook.com" Date: Sat, 21 Oct 2023 14:19:42 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tank/Tank.sln | 14 +- Tank/Tank/CollectionsFrame.Designer.cs | 135 ++++++++++++++++++ Tank/Tank/CollectionsFrame.cs | 72 ++++++++++ Tank/Tank/CollectionsFrame.resx | 120 ++++++++++++++++ Tank/Tank/DrawArmoVehicle.cs | 9 +- Tank/Tank/DrawTank.cs | 6 +- .../{EntityBase.cs => EntityArmoVehicle.cs} | 4 +- Tank/Tank/{EntityAddBase.cs => EntityTank.cs} | 4 +- Tank/Tank/FormTank.Designer.cs | 63 ++++---- Tank/Tank/FormTank.cs | 45 +++++- Tank/Tank/FormTank.resx | 2 +- Tank/Tank/Program.cs | 2 +- Tank/Tank/SetGeneric.cs | 84 +++++++++++ Tank/Tank/TanksGenericCollection.cs | 96 +++++++++++++ 14 files changed, 602 insertions(+), 54 deletions(-) create mode 100644 Tank/Tank/CollectionsFrame.Designer.cs create mode 100644 Tank/Tank/CollectionsFrame.cs create mode 100644 Tank/Tank/CollectionsFrame.resx rename Tank/Tank/{EntityBase.cs => EntityArmoVehicle.cs} (81%) rename Tank/Tank/{EntityAddBase.cs => EntityTank.cs} (71%) create mode 100644 Tank/Tank/SetGeneric.cs create mode 100644 Tank/Tank/TanksGenericCollection.cs diff --git a/Tank/Tank.sln b/Tank/Tank.sln index a226ebb..736023a 100644 --- a/Tank/Tank.sln +++ b/Tank/Tank.sln @@ -1,9 +1,9 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 -VisualStudioVersion = 17.6.33801.468 +VisualStudioVersion = 17.7.34031.279 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tank", "Tank\Tank.csproj", "{19BA0FA1-372B-490A-AA7F-757B0B8F322B}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tank", "Tank\Tank.csproj", "{5C602C58-ECEC-4FAB-BA10-FB671582D227}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -11,15 +11,15 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {19BA0FA1-372B-490A-AA7F-757B0B8F322B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {19BA0FA1-372B-490A-AA7F-757B0B8F322B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {19BA0FA1-372B-490A-AA7F-757B0B8F322B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {19BA0FA1-372B-490A-AA7F-757B0B8F322B}.Release|Any CPU.Build.0 = Release|Any CPU + {5C602C58-ECEC-4FAB-BA10-FB671582D227}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5C602C58-ECEC-4FAB-BA10-FB671582D227}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5C602C58-ECEC-4FAB-BA10-FB671582D227}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5C602C58-ECEC-4FAB-BA10-FB671582D227}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {5332F2FC-D1C2-4B42-B32E-53ECC094BBB7} + SolutionGuid = {73BF3262-E499-401F-BDBD-BD2BC7D3C324} EndGlobalSection EndGlobal diff --git a/Tank/Tank/CollectionsFrame.Designer.cs b/Tank/Tank/CollectionsFrame.Designer.cs new file mode 100644 index 0000000..fe910b7 --- /dev/null +++ b/Tank/Tank/CollectionsFrame.Designer.cs @@ -0,0 +1,135 @@ +namespace Tank +{ + partial class CollectionsFrame + { + /// + /// 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() + { + panel1 = new Panel(); + UpdateButton = new Button(); + DeleteButton = new Button(); + AddButton = new Button(); + InputNum = new TextBox(); + label1 = new Label(); + DrawTank = new PictureBox(); + panel1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)DrawTank).BeginInit(); + SuspendLayout(); + // + // panel1 + // + panel1.Controls.Add(UpdateButton); + panel1.Controls.Add(DeleteButton); + panel1.Controls.Add(AddButton); + panel1.Controls.Add(InputNum); + panel1.Controls.Add(label1); + panel1.Dock = DockStyle.Right; + panel1.Location = new Point(550, 0); + panel1.Name = "panel1"; + panel1.Size = new Size(250, 451); + panel1.TabIndex = 0; + // + // UpdateButton + // + UpdateButton.Location = new Point(10, 241); + UpdateButton.Name = "UpdateButton"; + UpdateButton.Size = new Size(229, 37); + UpdateButton.TabIndex = 4; + UpdateButton.Text = "Обновить коллекцию"; + UpdateButton.UseVisualStyleBackColor = true; + UpdateButton.Click += ButtonRefreshCollection_Click; + // + // DeleteButton + // + DeleteButton.Location = new Point(10, 176); + DeleteButton.Name = "DeleteButton"; + DeleteButton.Size = new Size(229, 37); + DeleteButton.TabIndex = 3; + DeleteButton.Text = "Удалить технику"; + DeleteButton.UseVisualStyleBackColor = true; + DeleteButton.Click += ButtonRemoveCar_Click; + // + // AddButton + // + AddButton.Location = new Point(10, 53); + AddButton.Name = "AddButton"; + AddButton.Size = new Size(229, 37); + AddButton.TabIndex = 2; + AddButton.Text = "Добавить технику"; + AddButton.UseVisualStyleBackColor = true; + AddButton.Click += ButtonAddTank_Click; + // + // InputNum + // + InputNum.Location = new Point(10, 133); + InputNum.Name = "InputNum"; + InputNum.Size = new Size(228, 27); + InputNum.TabIndex = 1; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(2, 3); + label1.Name = "label1"; + label1.Size = new Size(103, 20); + label1.TabIndex = 0; + label1.Text = "Инструменты"; + // + // DrawTank + // + DrawTank.Dock = DockStyle.Fill; + DrawTank.Location = new Point(0, 0); + DrawTank.Name = "DrawTank"; + DrawTank.Size = new Size(550, 451); + DrawTank.TabIndex = 1; + DrawTank.TabStop = false; + // + // CollectionsFrame + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 451); + Controls.Add(DrawTank); + Controls.Add(panel1); + Name = "CollectionsFrame"; + Text = "Form1"; + panel1.ResumeLayout(false); + panel1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)DrawTank).EndInit(); + ResumeLayout(false); + } + + #endregion + + private Panel panel1; + private Button UpdateButton; + private Button DeleteButton; + private Button AddButton; + private TextBox InputNum; + private Label label1; + private PictureBox DrawTank; + } +} \ No newline at end of file diff --git a/Tank/Tank/CollectionsFrame.cs b/Tank/Tank/CollectionsFrame.cs new file mode 100644 index 0000000..c458979 --- /dev/null +++ b/Tank/Tank/CollectionsFrame.cs @@ -0,0 +1,72 @@ +using Tank.DrawningObjects; +using Tank.Generics; +using Tank.MovementStrategy; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Tank +{ + public partial class CollectionsFrame : Form + { + private readonly TanksGenericCollection _tanks; + + public CollectionsFrame() + { + InitializeComponent(); + _tanks = new TanksGenericCollection(DrawTank.Width, DrawTank.Height); + } + + private void ButtonAddTank_Click(object sender, EventArgs e) + { + FormTank form = new FormTank(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_tanks + form.SelectedCar != -1) + { + MessageBox.Show("Объект добавлен"); + DrawTank.Image = _tanks.ShowCars(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + private void ButtonRemoveCar_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = -1; + try + { + pos = Convert.ToInt32(InputNum.Text); + } + catch (Exception ex) { } + if (_tanks - pos) + { + MessageBox.Show("Объект удален"); + DrawTank.Image = _tanks.ShowCars(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + DrawTank.Image = _tanks.ShowCars(); + } + + } +} diff --git a/Tank/Tank/CollectionsFrame.resx b/Tank/Tank/CollectionsFrame.resx new file mode 100644 index 0000000..a395bff --- /dev/null +++ b/Tank/Tank/CollectionsFrame.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/Tank/Tank/DrawArmoVehicle.cs b/Tank/Tank/DrawArmoVehicle.cs index aa389df..9036694 100644 --- a/Tank/Tank/DrawArmoVehicle.cs +++ b/Tank/Tank/DrawArmoVehicle.cs @@ -5,12 +5,13 @@ using System.Security.Cryptography.Pkcs; using System.Text; using System.Threading.Tasks; using Tank.Entities; +using Tank.MovementStrategy; namespace Tank { public class DrawArmoVehicle { - public EntityBase? Tank { get; protected set; } + public EntityArmoVehicle? Tank { get; protected set; } protected int _pictureWidth; protected int _pictureHeight; protected int _startPosX; @@ -29,7 +30,7 @@ namespace Tank _pictureWidth = width; if (_pictureHeight < _Height || _pictureWidth < _Width) return; - Tank = new EntityBase(speed, weight, bodyColor); + Tank = new EntityArmoVehicle(speed, weight, bodyColor); } protected DrawArmoVehicle(int speed, double weight, Color bodyColor, int width, int height, int tankWidth, int tankHeight) @@ -40,7 +41,7 @@ namespace Tank _Width = tankWidth; if (_pictureHeight < _Height || _pictureWidth < _Width) return; - Tank = new EntityBase(speed, weight, bodyColor); + Tank = new EntityArmoVehicle(speed, weight, bodyColor); } public bool CanMove(Direction direction) @@ -124,5 +125,7 @@ namespace Tank g.FillEllipse(ColorBlack, 85 + _startPosX, 42 + _startPosY, 20, 20); g.FillEllipse(ColorBlack, 110 + _startPosX, 42 + _startPosY, 20, 20); } + + public IMoveableObject GetMoveableObject => new DrawingObjectTank(this); } } diff --git a/Tank/Tank/DrawTank.cs b/Tank/Tank/DrawTank.cs index f473bf4..e270a72 100644 --- a/Tank/Tank/DrawTank.cs +++ b/Tank/Tank/DrawTank.cs @@ -14,13 +14,13 @@ namespace Tank.DrawningObjects { if (Tank != null) { - Tank = new EntityAddBase(speed, weight, bodyColor, additionalColor, bodyKit, caterpillar, tower); + Tank = new EntityTank(speed, weight, bodyColor, additionalColor, bodyKit, caterpillar, tower); } } public override void DrawTransport(Graphics g) { - if (Tank is not EntityAddBase ArmoVehicle) + if (Tank is not EntityTank ArmoVehicle) return; base.DrawTransport(g); if (ArmoVehicle.BodyKit) @@ -49,7 +49,7 @@ namespace Tank.DrawningObjects { // Орудие Brush bodyBrush = new SolidBrush(ArmoVehicle.AdditionalColor); - g.FillRectangle(bodyBrush, _startPosX + 111, _startPosY + 17, 70, 5); + g.FillRectangle(bodyBrush, _startPosX + 111, _startPosY + 17, 60, 5); // Зенитное орудие Point[] pointgun = { new Point(_startPosX + 44, _startPosY + 13), new Point(_startPosX + 45, _startPosY + 12), new Point(_startPosX + 41, _startPosY + 8), new Point(_startPosX + 41, _startPosY + 7), diff --git a/Tank/Tank/EntityBase.cs b/Tank/Tank/EntityArmoVehicle.cs similarity index 81% rename from Tank/Tank/EntityBase.cs rename to Tank/Tank/EntityArmoVehicle.cs index 2c23395..8263ed4 100644 --- a/Tank/Tank/EntityBase.cs +++ b/Tank/Tank/EntityArmoVehicle.cs @@ -6,13 +6,13 @@ using System.Threading.Tasks; namespace Tank.Entities { - public class EntityBase + public class EntityArmoVehicle { public int Speed { get; private set; } public double Weight { get; private set; } public Color BodyColor { get; private set; } public double Step => (double)Speed * 100 / Weight; - public EntityBase(int speed, double weight, Color bodyColor) + public EntityArmoVehicle(int speed, double weight, Color bodyColor) { Speed = speed; Weight = weight; diff --git a/Tank/Tank/EntityAddBase.cs b/Tank/Tank/EntityTank.cs similarity index 71% rename from Tank/Tank/EntityAddBase.cs rename to Tank/Tank/EntityTank.cs index 1527343..04d5f7f 100644 --- a/Tank/Tank/EntityAddBase.cs +++ b/Tank/Tank/EntityTank.cs @@ -6,13 +6,13 @@ using System.Threading.Tasks; namespace Tank.Entities { - public class EntityAddBase : EntityBase + public class EntityTank : EntityArmoVehicle { public Color AdditionalColor { get; private set; } public bool BodyKit { get; private set; } public bool Caterpillar { get; private set; } public bool Tower { get; private set; } - public EntityAddBase(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool caterpillar, bool tower) : base(speed, weight, bodyColor) + public EntityTank(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool caterpillar, bool tower) : base(speed, weight, bodyColor) { AdditionalColor = additionalColor; BodyKit = bodyKit; diff --git a/Tank/Tank/FormTank.Designer.cs b/Tank/Tank/FormTank.Designer.cs index 70578cb..9aef839 100644 --- a/Tank/Tank/FormTank.Designer.cs +++ b/Tank/Tank/FormTank.Designer.cs @@ -37,24 +37,23 @@ buttonArmVechicle = new Button(); buttonStep = new Button(); comboBoxStrategy = new ComboBox(); + ChooseCar = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxTank).BeginInit(); SuspendLayout(); // // pictureBoxTank // pictureBoxTank.Location = new Point(1, 0); - pictureBoxTank.Margin = new Padding(3, 4, 3, 4); pictureBoxTank.Name = "pictureBoxTank"; - pictureBoxTank.Size = new Size(913, 599); + pictureBoxTank.Size = new Size(799, 428); pictureBoxTank.TabIndex = 0; pictureBoxTank.TabStop = false; // // ButtonCreate // - ButtonCreate.Location = new Point(14, 500); - ButtonCreate.Margin = new Padding(3, 4, 3, 4); + ButtonCreate.Location = new Point(12, 375); ButtonCreate.Name = "ButtonCreate"; - ButtonCreate.Size = new Size(145, 53); + ButtonCreate.Size = new Size(127, 40); ButtonCreate.TabIndex = 1; ButtonCreate.Text = "Создание танка"; ButtonCreate.UseVisualStyleBackColor = true; @@ -65,10 +64,9 @@ keyDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; keyDown.BackgroundImage = Properties.Resources.KeyDown; keyDown.BackgroundImageLayout = ImageLayout.Stretch; - keyDown.Location = new Point(821, 515); - keyDown.Margin = new Padding(3, 4, 3, 4); + keyDown.Location = new Point(718, 386); keyDown.Name = "keyDown"; - keyDown.Size = new Size(34, 40); + keyDown.Size = new Size(30, 30); keyDown.TabIndex = 11; keyDown.UseVisualStyleBackColor = true; keyDown.Click += ButtonMove_Click; @@ -78,10 +76,9 @@ keyUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; keyUp.BackgroundImage = Properties.Resources.KeyUp; keyUp.BackgroundImageLayout = ImageLayout.Stretch; - keyUp.Location = new Point(821, 467); - keyUp.Margin = new Padding(3, 4, 3, 4); + keyUp.Location = new Point(718, 350); keyUp.Name = "keyUp"; - keyUp.Size = new Size(34, 40); + keyUp.Size = new Size(30, 30); keyUp.TabIndex = 12; keyUp.UseVisualStyleBackColor = true; keyUp.Click += ButtonMove_Click; @@ -91,10 +88,9 @@ keyLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; keyLeft.BackgroundImage = Properties.Resources.KeyLeft; keyLeft.BackgroundImageLayout = ImageLayout.Stretch; - keyLeft.Location = new Point(779, 515); - keyLeft.Margin = new Padding(3, 4, 3, 4); + keyLeft.Location = new Point(682, 386); keyLeft.Name = "keyLeft"; - keyLeft.Size = new Size(34, 40); + keyLeft.Size = new Size(30, 30); keyLeft.TabIndex = 13; keyLeft.UseVisualStyleBackColor = true; keyLeft.Click += ButtonMove_Click; @@ -104,20 +100,18 @@ keyRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; keyRight.BackgroundImage = Properties.Resources.KeyRight; keyRight.BackgroundImageLayout = ImageLayout.Stretch; - keyRight.Location = new Point(861, 515); - keyRight.Margin = new Padding(3, 4, 3, 4); + keyRight.Location = new Point(753, 386); keyRight.Name = "keyRight"; - keyRight.Size = new Size(34, 40); + keyRight.Size = new Size(30, 30); keyRight.TabIndex = 14; keyRight.UseVisualStyleBackColor = true; keyRight.Click += ButtonMove_Click; // // buttonArmVechicle // - buttonArmVechicle.Location = new Point(176, 501); - buttonArmVechicle.Margin = new Padding(3, 4, 3, 4); + buttonArmVechicle.Location = new Point(154, 376); buttonArmVechicle.Name = "buttonArmVechicle"; - buttonArmVechicle.Size = new Size(145, 53); + buttonArmVechicle.Size = new Size(127, 40); buttonArmVechicle.TabIndex = 15; buttonArmVechicle.Text = "Создание бронеавтомобиля"; buttonArmVechicle.UseVisualStyleBackColor = true; @@ -125,10 +119,9 @@ // // buttonStep // - buttonStep.Location = new Point(815, 71); - buttonStep.Margin = new Padding(3, 4, 3, 4); + buttonStep.Location = new Point(713, 53); buttonStep.Name = "buttonStep"; - buttonStep.Size = new Size(86, 31); + buttonStep.Size = new Size(75, 23); buttonStep.TabIndex = 17; buttonStep.Text = "Шаг"; buttonStep.UseVisualStyleBackColor = true; @@ -138,17 +131,30 @@ // comboBoxStrategy.FormattingEnabled = true; comboBoxStrategy.Items.AddRange(new object[] { "0", "1" }); - comboBoxStrategy.Location = new Point(749, 24); + comboBoxStrategy.Location = new Point(655, 18); + comboBoxStrategy.Margin = new Padding(3, 2, 3, 2); comboBoxStrategy.Name = "comboBoxStrategy"; - comboBoxStrategy.Size = new Size(151, 28); + comboBoxStrategy.Size = new Size(133, 23); comboBoxStrategy.TabIndex = 19; // + // ChooseCar + // + ChooseCar.Location = new Point(706, 90); + ChooseCar.Margin = new Padding(3, 2, 3, 2); + ChooseCar.Name = "ChooseCar"; + ChooseCar.Size = new Size(82, 39); + ChooseCar.TabIndex = 20; + ChooseCar.Text = "Выбрать технику"; + ChooseCar.UseVisualStyleBackColor = true; + ChooseCar.Click += ButtonSelectTank_Click; + // // FormTank // - AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; BackColor = SystemColors.ButtonHighlight; - ClientSize = new Size(914, 568); + ClientSize = new Size(800, 426); + Controls.Add(ChooseCar); Controls.Add(comboBoxStrategy); Controls.Add(buttonStep); Controls.Add(buttonArmVechicle); @@ -158,9 +164,9 @@ Controls.Add(keyDown); Controls.Add(ButtonCreate); Controls.Add(pictureBoxTank); - Margin = new Padding(3, 4, 3, 4); Name = "FormTank"; Text = "FormTank"; + Load += FormTank_Load; ((System.ComponentModel.ISupportInitialize)pictureBoxTank).EndInit(); ResumeLayout(false); } @@ -176,5 +182,6 @@ private Button buttonArmVechicle; private Button buttonStep; private ComboBox comboBoxStrategy; + private Button ChooseCar; } } \ No newline at end of file diff --git a/Tank/Tank/FormTank.cs b/Tank/Tank/FormTank.cs index d43d757..702e5c8 100644 --- a/Tank/Tank/FormTank.cs +++ b/Tank/Tank/FormTank.cs @@ -7,10 +7,13 @@ namespace Tank { private DrawArmoVehicle? _Tank; private AbstractStrategy? _abstractStrategy; + public DrawArmoVehicle? SelectedCar { get; private set; } public FormTank() { InitializeComponent(); + _abstractStrategy = null; + SelectedCar = null; } private void Draw() @@ -18,19 +21,30 @@ namespace Tank if (_Tank == null) return; - Bitmap bmp = new(pictureBoxTank.Width, pictureBoxTank.Height); - Graphics gr = Graphics.FromImage(bmp); + Bitmap bitmap = new(pictureBoxTank.Width, pictureBoxTank.Height); + Graphics gr = Graphics.FromImage(bitmap); _Tank?.DrawTransport(gr); - pictureBoxTank.Image = bmp; + pictureBoxTank.Image = bitmap; } private void ButtonCreateTank_Click(object sender, EventArgs e) { Random rnd = new(); + Color mainColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + mainColor = dialog.Color; + } + Color addColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + if (dialog.ShowDialog() == DialogResult.OK) + { + addColor = dialog.Color; + } + _Tank = new DrawTank(rnd.Next(100, 200), rnd.Next(2000, 4000), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), + mainColor, addColor, Convert.ToBoolean(rnd.Next(1, 2)), Convert.ToBoolean(rnd.Next(1, 2)), Convert.ToBoolean(rnd.Next(1, 2)), pictureBoxTank.Width, pictureBoxTank.Height); _Tank.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100)); @@ -66,9 +80,15 @@ namespace Tank private void CreateButtonArmoVehicle_Click(object sender, EventArgs e) { Random rnd = new(); + Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + _Tank = new DrawArmoVehicle(rnd.Next(100, 200), rnd.Next(2000, 4000), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), - pictureBoxTank.Width, pictureBoxTank.Height); + color, pictureBoxTank.Width, pictureBoxTank.Height); _Tank.SetPosition(rnd.Next(10, 50), rnd.Next(30, 70)); Draw(); @@ -102,5 +122,16 @@ namespace Tank _abstractStrategy = null; } } + + private void ButtonSelectTank_Click(object sender, EventArgs e) + { + SelectedCar = _Tank; + DialogResult = DialogResult.OK; + } + + private void FormTank_Load(object sender, EventArgs e) + { + + } } } \ No newline at end of file diff --git a/Tank/Tank/FormTank.resx b/Tank/Tank/FormTank.resx index a395bff..af32865 100644 --- a/Tank/Tank/FormTank.resx +++ b/Tank/Tank/FormTank.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/Tank/Tank/Program.cs b/Tank/Tank/Program.cs index 3fbc0e1..be056fe 100644 --- a/Tank/Tank/Program.cs +++ b/Tank/Tank/Program.cs @@ -11,7 +11,7 @@ namespace Tank // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormTank()); + Application.Run(new CollectionsFrame()); } } } \ No newline at end of file diff --git a/Tank/Tank/SetGeneric.cs b/Tank/Tank/SetGeneric.cs new file mode 100644 index 0000000..e4be151 --- /dev/null +++ b/Tank/Tank/SetGeneric.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tank +{ + 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 car) + { + int index = -1; + for(int i = 0; i < _places.Length; i++) + { + if (_places[i] == null) + { + index = i; break; + } + } + if (index < 0) + { + return -1; + } + for(int i = index; i > 0; i--) + { + _places[i] = _places[i - 1]; + } + _places[0] = car; + return 0; + } + + public int Insert(T car, int position) + { + if (position < 0 || position >= Count) + return -1; + if (_places[position] == null) + { + _places[position] = car; + return position; + } + int index = -1; + for(int i = position; i < Count; i++) + { + if (_places[i] == null) + { + index = i; break; + } + } + if (index < 0) + return -1; + for(int i = index; index > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = car; + return position; + } + + 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/Tank/Tank/TanksGenericCollection.cs b/Tank/Tank/TanksGenericCollection.cs new file mode 100644 index 0000000..851d5fb --- /dev/null +++ b/Tank/Tank/TanksGenericCollection.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms.VisualStyles; +using Tank.DrawningObjects; +using Tank.MovementStrategy; + +namespace Tank.Generics +{ + internal class TanksGenericCollection + where T : DrawArmoVehicle + where U : IMoveableObject + { + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 180; + private readonly int _placeSizeHeight = 90; + private readonly SetGeneric _collection; + + public TanksGenericCollection(int pictureWidth, int pictureHeight) + { + int width = pictureWidth / _placeSizeWidth; + int height = pictureHeight / _placeSizeHeight; + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + _collection = new SetGeneric(width * height); + } + + public static int? operator +(TanksGenericCollection collect, T? obj) + { + if (obj == null) + { + return -1; + } + return collect?._collection.Insert(obj); + } + + public static bool operator -(TanksGenericCollection collect, int pos) + { + T? obj = collect._collection.Get(pos); + if (obj == null) + { + return false; + } + return collect._collection.Remove(pos); + } + + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + + public Bitmap ShowCars() + { + 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 width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + for (int i = 0; i < _collection.Count; i++) + { + DrawArmoVehicle? tank = _collection.Get(i); + if (tank == null) + continue; + tank.SetPosition(i % width * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); + tank.DrawTransport(g); + } + } + } +} +