From 5172ab0e40a13b92878cb5f8be4882145b1907bf Mon Sep 17 00:00:00 2001 From: ker73rus Date: Tue, 1 Nov 2022 18:05:19 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=9D=D0=B5=D0=BA=D0=BE=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D1=8B=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Stormtrooper/Stormtrooper/Direction.cs | 2 +- .../Stormtrooper/DrawningMilitaryAirplane.cs | 2 +- .../Stormtrooper/EntityMilitaryAirplane.cs | 2 +- .../FormMapWithSetAirplane.Designer.cs | 214 ++++++++++++++++++ .../Stormtrooper/FormMapWithSetAirplane.cs | 156 +++++++++++++ .../Stormtrooper/FormMapWithSetAirplane.resx | 60 +++++ .../Stormtrooper/MainForm.Designer.cs | 13 ++ Stormtrooper/Stormtrooper/MainForm.cs | 7 + .../Stormtrooper/MapWithSetAirplaneGeneric.cs | 172 ++++++++++++++ Stormtrooper/Stormtrooper/Program.cs | 2 +- .../Stormtrooper/SetAirplaneGeneric.cs | 77 +++++++ 11 files changed, 703 insertions(+), 4 deletions(-) create mode 100644 Stormtrooper/Stormtrooper/FormMapWithSetAirplane.Designer.cs create mode 100644 Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs create mode 100644 Stormtrooper/Stormtrooper/FormMapWithSetAirplane.resx create mode 100644 Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs create mode 100644 Stormtrooper/Stormtrooper/SetAirplaneGeneric.cs diff --git a/Stormtrooper/Stormtrooper/Direction.cs b/Stormtrooper/Stormtrooper/Direction.cs index f4826fe..f4b14f8 100644 --- a/Stormtrooper/Stormtrooper/Direction.cs +++ b/Stormtrooper/Stormtrooper/Direction.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Stormtrooper { - internal enum Direction + public enum Direction { None, diff --git a/Stormtrooper/Stormtrooper/DrawningMilitaryAirplane.cs b/Stormtrooper/Stormtrooper/DrawningMilitaryAirplane.cs index a59af2b..6e65760 100644 --- a/Stormtrooper/Stormtrooper/DrawningMilitaryAirplane.cs +++ b/Stormtrooper/Stormtrooper/DrawningMilitaryAirplane.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Stormtrooper { - internal class DrawningMilitaryAirplane + public class DrawningMilitaryAirplane { /// /// Класс-сущность diff --git a/Stormtrooper/Stormtrooper/EntityMilitaryAirplane.cs b/Stormtrooper/Stormtrooper/EntityMilitaryAirplane.cs index d7aa97e..6bb340d 100644 --- a/Stormtrooper/Stormtrooper/EntityMilitaryAirplane.cs +++ b/Stormtrooper/Stormtrooper/EntityMilitaryAirplane.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Stormtrooper { - internal class EntityMilitaryAirplane + public class EntityMilitaryAirplane { public int Speed { get; private set; } public int Weight { get; private set; } diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.Designer.cs b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.Designer.cs new file mode 100644 index 0000000..a7582cb --- /dev/null +++ b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.Designer.cs @@ -0,0 +1,214 @@ +namespace Stormtrooper +{ + partial class FormMapWithSetAirplane + { + /// + /// 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() + { + this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.buttonRight = new System.Windows.Forms.Button(); + this.buttonLeft = new System.Windows.Forms.Button(); + this.buttonDown = new System.Windows.Forms.Button(); + this.buttonUp = new System.Windows.Forms.Button(); + this.buttonShowOnMap = new System.Windows.Forms.Button(); + this.buttonShowStorage = new System.Windows.Forms.Button(); + this.buttonRemove = new System.Windows.Forms.Button(); + this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); + this.buttonCreate = new System.Windows.Forms.Button(); + this.comboBoxMapSelector = new System.Windows.Forms.ComboBox(); + this.pictureBox = new System.Windows.Forms.PictureBox(); + this.groupBoxTools.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.SuspendLayout(); + // + // groupBoxTools + // + this.groupBoxTools.Controls.Add(this.buttonRight); + this.groupBoxTools.Controls.Add(this.buttonLeft); + this.groupBoxTools.Controls.Add(this.buttonDown); + this.groupBoxTools.Controls.Add(this.buttonUp); + this.groupBoxTools.Controls.Add(this.buttonShowOnMap); + this.groupBoxTools.Controls.Add(this.buttonShowStorage); + this.groupBoxTools.Controls.Add(this.buttonRemove); + this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); + this.groupBoxTools.Controls.Add(this.buttonCreate); + this.groupBoxTools.Controls.Add(this.comboBoxMapSelector); + this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; + this.groupBoxTools.Location = new System.Drawing.Point(600, 0); + this.groupBoxTools.Name = "groupBoxTools"; + this.groupBoxTools.Size = new System.Drawing.Size(200, 450); + this.groupBoxTools.TabIndex = 0; + this.groupBoxTools.TabStop = false; + this.groupBoxTools.Text = "Инструменты"; + // + // buttonRight + // + this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonRight.Location = new System.Drawing.Point(119, 402); + this.buttonRight.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + this.buttonRight.Name = "buttonRight"; + this.buttonRight.Size = new System.Drawing.Size(35, 35); + this.buttonRight.TabIndex = 18; + this.buttonRight.UseVisualStyleBackColor = true; + this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonLeft + // + this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonLeft.Location = new System.Drawing.Point(35, 402); + this.buttonLeft.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(35, 35); + this.buttonLeft.TabIndex = 17; + this.buttonLeft.UseVisualStyleBackColor = true; + this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonDown + // + this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonDown.Location = new System.Drawing.Point(77, 403); + this.buttonDown.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(35, 35); + this.buttonDown.TabIndex = 16; + this.buttonDown.UseVisualStyleBackColor = true; + this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonUp + // + this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonUp.Location = new System.Drawing.Point(77, 361); + this.buttonUp.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(35, 35); + this.buttonUp.TabIndex = 15; + this.buttonUp.UseVisualStyleBackColor = true; + this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonShowOnMap + // + this.buttonShowOnMap.Location = new System.Drawing.Point(8, 323); + this.buttonShowOnMap.Name = "buttonShowOnMap"; + this.buttonShowOnMap.Size = new System.Drawing.Size(174, 23); + this.buttonShowOnMap.TabIndex = 14; + this.buttonShowOnMap.Text = "Посмотреть карту"; + this.buttonShowOnMap.UseVisualStyleBackColor = true; + this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); + // + // buttonShowStorage + // + this.buttonShowStorage.Location = new System.Drawing.Point(8, 275); + this.buttonShowStorage.Name = "buttonShowStorage"; + this.buttonShowStorage.Size = new System.Drawing.Size(174, 23); + this.buttonShowStorage.TabIndex = 13; + this.buttonShowStorage.Text = "Посмотреть хранилище"; + this.buttonShowStorage.UseVisualStyleBackColor = true; + this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); + // + // buttonRemove + // + this.buttonRemove.Location = new System.Drawing.Point(7, 172); + this.buttonRemove.Name = "buttonRemove"; + this.buttonRemove.Size = new System.Drawing.Size(174, 23); + this.buttonRemove.TabIndex = 12; + this.buttonRemove.Text = "Удалить"; + this.buttonRemove.UseVisualStyleBackColor = true; + this.buttonRemove.Click += new System.EventHandler(this.ButtonRemove_Click); + // + // maskedTextBoxPosition + // + this.maskedTextBoxPosition.Location = new System.Drawing.Point(7, 122); + this.maskedTextBoxPosition.Mask = "00"; + this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23); + this.maskedTextBoxPosition.TabIndex = 11; + this.maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonCreate + // + this.buttonCreate.Location = new System.Drawing.Point(6, 74); + this.buttonCreate.Name = "buttonCreate"; + this.buttonCreate.Size = new System.Drawing.Size(175, 23); + this.buttonCreate.TabIndex = 10; + this.buttonCreate.Text = "Добавить"; + this.buttonCreate.UseVisualStyleBackColor = true; + this.buttonCreate.Click += new System.EventHandler(this.ButtonAdd_Click); + // + // comboBoxMapSelector + // + this.comboBoxMapSelector.BackColor = System.Drawing.SystemColors.Window; + this.comboBoxMapSelector.FormattingEnabled = true; + this.comboBoxMapSelector.Items.AddRange(new object[] { + "Простая карта", + "Опасная карта", + "Облачная карта"}); + this.comboBoxMapSelector.Location = new System.Drawing.Point(6, 22); + this.comboBoxMapSelector.Name = "comboBoxMapSelector"; + this.comboBoxMapSelector.Size = new System.Drawing.Size(175, 23); + this.comboBoxMapSelector.TabIndex = 9; + this.comboBoxMapSelector.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + // + // pictureBox + // + this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBox.Location = new System.Drawing.Point(0, 0); + this.pictureBox.Name = "pictureBox"; + this.pictureBox.Size = new System.Drawing.Size(600, 450); + this.pictureBox.TabIndex = 1; + this.pictureBox.TabStop = false; + // + // FormMapWithSetAirplane + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.pictureBox); + this.Controls.Add(this.groupBoxTools); + this.Name = "FormMapWithSetAirplane"; + this.Text = "FormMapWithSetAirplane"; + this.groupBoxTools.ResumeLayout(false); + this.groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private GroupBox groupBoxTools; + private PictureBox pictureBox; + private Button buttonShowOnMap; + private Button buttonShowStorage; + private Button buttonRemove; + private MaskedTextBox maskedTextBoxPosition; + private Button buttonCreate; + private ComboBox comboBoxMapSelector; + private Button buttonRight; + private Button buttonLeft; + private Button buttonDown; + private Button buttonUp; + } +} \ No newline at end of file diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs new file mode 100644 index 0000000..2af4f62 --- /dev/null +++ b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs @@ -0,0 +1,156 @@ +using Cars; +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 static System.Windows.Forms.DataFormats; + +namespace Stormtrooper +{ + public partial class FormMapWithSetAirplane : Form + { + private MapWithSetAirplaneGeneric _mapAirsCollectionGeneric; + public FormMapWithSetAirplane() + { + InitializeComponent(); + } + /// + /// Выбор карты + /// + /// + /// + private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + { + AbstractMap map = null; + switch (comboBoxMapSelector.Text) + { + case "Простая карта": + map = new SimpleMap(); + break; + } + if (map != null) + { + _mapAirsCollectionGeneric = new MapWithSetAirplaneGeneric( + pictureBox.Width, pictureBox.Height, map); + } + else + { + _mapAirsCollectionGeneric = null; + } + } + /// + /// Добавление объекта + /// + /// + /// + private void ButtonAdd_Click(object sender, EventArgs e) + { + if (_mapAirsCollectionGeneric == null) + { + return; + } + MainForm form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + DrawningObject airplane = new(form.SelectedAirplane); + if (_mapAirsCollectionGeneric + airplane) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _mapAirsCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта + /// + /// + /// + private void ButtonRemove_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) + { + return; + } + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxPosition.Text); + if (_mapAirsCollectionGeneric - pos) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _mapAirsCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Вывод набора + /// + /// + /// + private void ButtonShowStorage_Click(object sender, EventArgs e) + { + if (_mapAirsCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapAirsCollectionGeneric.ShowSet(); + } + /// + /// Вывод карты + /// + /// + /// + private void ButtonShowOnMap_Click(object sender, EventArgs e) + { + if (_mapAirsCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapAirsCollectionGeneric.ShowOnMap(); + } + /// + /// Перемещение + /// + /// + /// + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_mapAirsCollectionGeneric == null) + { + return; + } + //получаем имя кнопки + string name = ((Button)sender)?.Name ?? string.Empty; + Direction dir = Direction.None; + switch (name) + { + case "buttonUp": + dir = Direction.Up; + break; + case "buttonDown": + dir = Direction.Down; + break; + case "buttonLeft": + dir = Direction.Left; + break; + case "buttonRight": + dir = Direction.Right; + break; + } + pictureBox.Image = _mapAirsCollectionGeneric.MoveObject(dir); + } + } +} diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.resx b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.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/Stormtrooper/Stormtrooper/MainForm.Designer.cs b/Stormtrooper/Stormtrooper/MainForm.Designer.cs index e8d1930..c3d39fc 100644 --- a/Stormtrooper/Stormtrooper/MainForm.Designer.cs +++ b/Stormtrooper/Stormtrooper/MainForm.Designer.cs @@ -40,6 +40,7 @@ namespace Stormtrooper this.buttonLeft = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonCreateMod = new System.Windows.Forms.Button(); + this.button_Add = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxAirplane)).BeginInit(); this.toolStripStatus.SuspendLayout(); this.SuspendLayout(); @@ -157,11 +158,22 @@ namespace Stormtrooper this.buttonCreateMod.UseVisualStyleBackColor = true; this.buttonCreateMod.Click += new System.EventHandler(this.buttonCreateMod_Click); // + // button_Add + // + this.button_Add.Location = new System.Drawing.Point(573, 600); + this.button_Add.Name = "button_Add"; + this.button_Add.Size = new System.Drawing.Size(75, 23); + this.button_Add.TabIndex = 8; + this.button_Add.Text = "Добавить"; + this.button_Add.UseVisualStyleBackColor = true; + this.button_Add.Click += new System.EventHandler(this.button_Add_Click); + // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1343, 703); + this.Controls.Add(this.button_Add); this.Controls.Add(this.buttonCreateMod); this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonLeft); @@ -194,6 +206,7 @@ namespace Stormtrooper private System.Windows.Forms.Button buttonLeft; private System.Windows.Forms.Button buttonRight; private Button buttonCreateMod; + private Button button_Add; } } diff --git a/Stormtrooper/Stormtrooper/MainForm.cs b/Stormtrooper/Stormtrooper/MainForm.cs index ee5ad95..9b1ad61 100644 --- a/Stormtrooper/Stormtrooper/MainForm.cs +++ b/Stormtrooper/Stormtrooper/MainForm.cs @@ -13,6 +13,7 @@ namespace Stormtrooper public partial class MainForm : Form { DrawningMilitaryAirplane _airplane; + public DrawningMilitaryAirplane SelectedAirplane { get; private set; } public MainForm() { InitializeComponent(); @@ -80,5 +81,11 @@ namespace Stormtrooper SetData(); Draw(); } + + private void button_Add_Click(object sender, EventArgs e) + { + SelectedAirplane = _airplane; + DialogResult = DialogResult.OK; + } } } diff --git a/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs b/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs new file mode 100644 index 0000000..18d580b --- /dev/null +++ b/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs @@ -0,0 +1,172 @@ +using Cars; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Stormtrooper +{ + internal class MapWithSetAirplaneGeneric + where T : class, IDrawningObject + where U : AbstractMap + { + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 210; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 90; + /// + /// Набор объектов + /// + private readonly SetAirplaneGeneric _setAirs; + /// + /// Карта + /// + private readonly U _map; + /// + /// Конструктор + /// + /// + /// + /// + public MapWithSetAirplaneGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setAirs = new SetAirplaneGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static bool operator +(MapWithSetAirplaneGeneric map, T car) + { + return map._setAirs.Insert(car); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(MapWithSetAirplaneGeneric map, int position) + { + return map._setAirs.Remove(position); + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawAirs(gr); + return bmp; + } + /// + /// Просмотр объекта на карте + /// + /// + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setAirs.Count; i++) + { + var air = _setAirs.Get(i); + if (air != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, air); + } + } + return new(_pictureWidth, _pictureHeight); + } + /// + /// Перемещение объекта по крате + /// + /// + /// + public Bitmap MoveObject(Direction direction) + { + + if (_map != null) + { + return _map.MoveObject(direction); + } + return new(_pictureWidth, _pictureHeight); + } + /// + /// "Взбалтываем" набор, чтобы все элементы оказались в начале + /// + private void Shaking() + { + int j = _setAirs.Count - 1; + for (int i = 0; i < _setAirs.Count; i++) + { + if (_setAirs.Get(i) == null) + { + for (; j > i; j--) + { + var air = _setAirs.Get(j); + if (air != null) + { + _setAirs.Insert(air, i); + _setAirs.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + /// + /// Метод отрисовки фона + /// + /// + 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 DrawAirs(Graphics g) + { + for (int i = 0; i < _setAirs.Count; i++) + { + // TODO установка позиции + _setAirs.Get(i)?.DrawningObject(g); + } + } + } +} diff --git a/Stormtrooper/Stormtrooper/Program.cs b/Stormtrooper/Stormtrooper/Program.cs index 63f467d..65b5334 100644 --- a/Stormtrooper/Stormtrooper/Program.cs +++ b/Stormtrooper/Stormtrooper/Program.cs @@ -16,7 +16,7 @@ namespace Stormtrooper { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new FormMap()); + Application.Run(new FormMapWithSetAirplane()); } } } diff --git a/Stormtrooper/Stormtrooper/SetAirplaneGeneric.cs b/Stormtrooper/Stormtrooper/SetAirplaneGeneric.cs new file mode 100644 index 0000000..dedf2a0 --- /dev/null +++ b/Stormtrooper/Stormtrooper/SetAirplaneGeneric.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Stormtrooper +{ + internal class SetAirplaneGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetAirplaneGeneric(int count) + { + _places = new T[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый самолёт + /// + public bool Insert(T airplane) + { + _places[0] = airplane; + // TODO вставка в начало набора + return true; + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый самолёт + /// Позиция + /// + public bool Insert(T airplane, int position) + { + // TODO проверка позиции + // TODO проверка, что элемент массива по этой позиции пустой, если нет, то + // проверка, что после вставляемого элемента в массиве есть пустой элемент + // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + // TODO вставка по позиции + _places[position] = airplane; + return true; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + // TODO проверка позиции + // TODO удаление объекта из массива, присовив элементу массива значение null + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T Get(int position) + { + // TODO проверка позиции + return _places[position]; + } + } +} -- 2.25.1 From e28f87485373f7d38c8c11b8795d6e5a331882f3 Mon Sep 17 00:00:00 2001 From: foxkerik6 Date: Tue, 15 Nov 2022 02:15:02 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=92=D1=81=D1=91=20=D0=B3=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Stormtrooper/Stormtrooper/AbstractMap.cs | 4 +- .../Stormtrooper/FormMapWithSetAirplane.cs | 26 +++++-- Stormtrooper/Stormtrooper/MainForm.cs | 13 +++- .../Stormtrooper/MapWithSetAirplaneGeneric.cs | 36 ++++++++-- .../Stormtrooper/SetAirplaneGeneric.cs | 70 ++++++++++++++----- 5 files changed, 114 insertions(+), 35 deletions(-) diff --git a/Stormtrooper/Stormtrooper/AbstractMap.cs b/Stormtrooper/Stormtrooper/AbstractMap.cs index 28d28ad..6ecfaa1 100644 --- a/Stormtrooper/Stormtrooper/AbstractMap.cs +++ b/Stormtrooper/Stormtrooper/AbstractMap.cs @@ -103,9 +103,9 @@ namespace Cars break; } - for (int i = left; i < right; i++) + for (int i = left; i < right && i < _map.GetLength(0); i++) { - for (int j = top; j < bottom; j++) + for (int j = top; j < bottom && j < _map.GetLength(1); j++) { if (_map[i, j] == _barrier) { diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs index 2af4f62..fc10273 100644 --- a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs +++ b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs @@ -32,6 +32,12 @@ namespace Stormtrooper case "Простая карта": map = new SimpleMap(); break; + case "Опасная карта": + map = new DangerMap(); + break; + case "Облачная карта": + map = new CloudMap(); + break; } if (map != null) { @@ -57,16 +63,24 @@ namespace Stormtrooper MainForm form = new(); if (form.ShowDialog() == DialogResult.OK) { - DrawningObject airplane = new(form.SelectedAirplane); - if (_mapAirsCollectionGeneric + airplane) + if(form.SelectedAirplane != null) { - MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapAirsCollectionGeneric.ShowSet(); + DrawningObject airplane = new (form.SelectedAirplane); + if (_mapAirsCollectionGeneric + airplane != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _mapAirsCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } } else { - MessageBox.Show("Не удалось добавить объект"); + MessageBox.Show("Объект не создан"); } + } } /// @@ -85,7 +99,7 @@ namespace Stormtrooper return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapAirsCollectionGeneric - pos) + if (_mapAirsCollectionGeneric - pos != null) { MessageBox.Show("Объект удален"); pictureBox.Image = _mapAirsCollectionGeneric.ShowSet(); diff --git a/Stormtrooper/Stormtrooper/MainForm.cs b/Stormtrooper/Stormtrooper/MainForm.cs index 9b1ad61..76af6d6 100644 --- a/Stormtrooper/Stormtrooper/MainForm.cs +++ b/Stormtrooper/Stormtrooper/MainForm.cs @@ -29,9 +29,10 @@ namespace Stormtrooper } private void buttonCreate_Click(object sender, EventArgs e) { - Random random = new Random(); + Random rnd = new Random(); + _airplane = new DrawningMilitaryAirplane(10, 50); - _airplane.SetPosition(random.Next(100,150), random.Next(100,150), pictureBoxAirplane.Width, pictureBoxAirplane.Height); + _airplane.SetPosition(rnd.Next(100,150), rnd.Next(100,150), pictureBoxAirplane.Width, pictureBoxAirplane.Height); SetData(); Draw(); } @@ -74,8 +75,14 @@ namespace Stormtrooper { Random random = new Random(); + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } _airplane = new DrawningStormtrooper(random.Next(10,100),random.Next(50,250),random.Next(1,100), - Color.FromArgb(random.Next(0,256), random.Next(0, 256), random.Next(0, 256)), + color, Convert.ToBoolean(random.Next(0,2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); _airplane.SetPosition(random.Next(100, 150), random.Next(100, 150), pictureBoxAirplane.Width, pictureBoxAirplane.Height); SetData(); diff --git a/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs b/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs index 18d580b..732696b 100644 --- a/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs +++ b/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs @@ -26,7 +26,7 @@ namespace Stormtrooper /// /// Размер занимаемого объектом места (высота) /// - private readonly int _placeSizeHeight = 90; + private readonly int _placeSizeHeight = 110; /// /// Набор объектов /// @@ -56,9 +56,9 @@ namespace Stormtrooper /// /// /// - public static bool operator +(MapWithSetAirplaneGeneric map, T car) + public static int operator +(MapWithSetAirplaneGeneric map, T air) { - return map._setAirs.Insert(car); + return map._setAirs.Insert(air); } /// /// Перегрузка оператора вычитания @@ -66,7 +66,7 @@ namespace Stormtrooper /// /// /// - public static bool operator -(MapWithSetAirplaneGeneric map, int position) + public static T operator -(MapWithSetAirplaneGeneric map, int position) { return map._setAirs.Remove(position); } @@ -147,10 +147,14 @@ namespace Stormtrooper private void DrawBackground(Graphics g) { Pen pen = new(Color.Black, 3); + g.FillRectangle(new SolidBrush(Color.DarkGray),0,0,_pictureWidth, _pictureHeight); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { - for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) - {//линия рамзетки места + g.FillRectangle(new SolidBrush(Color.Gray), i * _placeSizeWidth, 0, _placeSizeWidth / 2, _placeSizeHeight * (_pictureHeight / _placeSizeHeight)); + 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); @@ -162,10 +166,28 @@ namespace Stormtrooper /// private void DrawAirs(Graphics g) { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + int currentWidth = width - 1; + int currentHeight = 0; + for (int i = 0; i < _setAirs.Count; i++) { - // TODO установка позиции + + _setAirs.Get(i)?.SetObject(currentWidth * _placeSizeWidth, + currentHeight * _placeSizeHeight, + _pictureWidth, _pictureHeight); _setAirs.Get(i)?.DrawningObject(g); + + if (currentWidth > 0) + currentWidth--; + else + { + currentWidth = width - 1; + currentHeight++; + } + if (currentHeight > height) return; } } } diff --git a/Stormtrooper/Stormtrooper/SetAirplaneGeneric.cs b/Stormtrooper/Stormtrooper/SetAirplaneGeneric.cs index dedf2a0..719eccc 100644 --- a/Stormtrooper/Stormtrooper/SetAirplaneGeneric.cs +++ b/Stormtrooper/Stormtrooper/SetAirplaneGeneric.cs @@ -30,11 +30,10 @@ namespace Stormtrooper /// /// Добавляемый самолёт /// - public bool Insert(T airplane) + public int Insert(T airplane) { - _places[0] = airplane; - // TODO вставка в начало набора - return true; + Insert(airplane, 0); + return 0; } /// /// Добавление объекта в набор на конкретную позицию @@ -42,26 +41,60 @@ namespace Stormtrooper /// Добавляемый самолёт /// Позиция /// - public bool Insert(T airplane, int position) + public int Insert(T airplane, int position) { - // TODO проверка позиции - // TODO проверка, что элемент массива по этой позиции пустой, если нет, то - // проверка, что после вставляемого элемента в массиве есть пустой элемент - // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - // TODO вставка по позиции - _places[position] = airplane; - return true; + if (position < 0 || position >= Count) + { + return -1; + } + if (_places[position] == null || _places[position] == default(T)) + { + _places[position] = airplane; + return position; + } + else + { + bool hasEmptySpace = false; + int indexOfEmptyPlace = 0; + for (int i = position + 1; i < Count; ++i) + { + if (_places[i] == null || _places[i] == default(T)) + { + hasEmptySpace = true; + indexOfEmptyPlace = i; + break; + } + } + if (hasEmptySpace) + { + for (int i = indexOfEmptyPlace; i > position; --i) + { + _places[i] = _places[i - 1]; + } + _places[position] = airplane; + return position; + } + else + { + return -1; + } + } + } /// /// Удаление объекта из набора с конкретной позиции /// /// /// - public bool Remove(int position) + public T Remove(int position) { - // TODO проверка позиции - // TODO удаление объекта из массива, присовив элементу массива значение null - return true; + if (position < 0 || position >= Count) + { + return null; + } + T memoryObj = _places[position]; + _places[position] = null; + return memoryObj; } /// /// Получение объекта из набора по позиции @@ -70,7 +103,10 @@ namespace Stormtrooper /// public T Get(int position) { - // TODO проверка позиции + if (position < 0 || position >= Count) + { + return null; + } return _places[position]; } } -- 2.25.1 From 468f859fe0af6af86ef9f77198f28c4c9d45a3b6 Mon Sep 17 00:00:00 2001 From: foxkerik6 Date: Sat, 19 Nov 2022 13:23:13 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=A7=D0=B8=D1=81=D1=82=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Stormtrooper/Stormtrooper/AbstractMap.cs | 2 +- Stormtrooper/Stormtrooper/CloudMap.cs | 3 +-- Stormtrooper/Stormtrooper/DangerMap.cs | 3 +-- Stormtrooper/Stormtrooper/FormMap.cs | 3 +-- Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs | 5 ++--- Stormtrooper/Stormtrooper/SimpleMap.cs | 2 +- 6 files changed, 7 insertions(+), 11 deletions(-) diff --git a/Stormtrooper/Stormtrooper/AbstractMap.cs b/Stormtrooper/Stormtrooper/AbstractMap.cs index 6ecfaa1..e998f59 100644 --- a/Stormtrooper/Stormtrooper/AbstractMap.cs +++ b/Stormtrooper/Stormtrooper/AbstractMap.cs @@ -5,7 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Cars +namespace Stormtrooper { internal abstract class AbstractMap { diff --git a/Stormtrooper/Stormtrooper/CloudMap.cs b/Stormtrooper/Stormtrooper/CloudMap.cs index c4c8170..291072f 100644 --- a/Stormtrooper/Stormtrooper/CloudMap.cs +++ b/Stormtrooper/Stormtrooper/CloudMap.cs @@ -1,5 +1,4 @@ -using Cars; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/Stormtrooper/Stormtrooper/DangerMap.cs b/Stormtrooper/Stormtrooper/DangerMap.cs index 31dc977..86e38f9 100644 --- a/Stormtrooper/Stormtrooper/DangerMap.cs +++ b/Stormtrooper/Stormtrooper/DangerMap.cs @@ -1,5 +1,4 @@ -using Cars; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/Stormtrooper/Stormtrooper/FormMap.cs b/Stormtrooper/Stormtrooper/FormMap.cs index ce5dca7..520f620 100644 --- a/Stormtrooper/Stormtrooper/FormMap.cs +++ b/Stormtrooper/Stormtrooper/FormMap.cs @@ -1,5 +1,4 @@ -using Cars; -using System; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; diff --git a/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs b/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs index 732696b..a1c1d29 100644 --- a/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs +++ b/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs @@ -1,5 +1,4 @@ -using Cars; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -54,7 +53,7 @@ namespace Stormtrooper /// Перегрузка оператора сложения /// /// - /// + /// /// public static int operator +(MapWithSetAirplaneGeneric map, T air) { diff --git a/Stormtrooper/Stormtrooper/SimpleMap.cs b/Stormtrooper/Stormtrooper/SimpleMap.cs index 82d90fb..84845c4 100644 --- a/Stormtrooper/Stormtrooper/SimpleMap.cs +++ b/Stormtrooper/Stormtrooper/SimpleMap.cs @@ -1,4 +1,4 @@ -namespace Cars +namespace Stormtrooper { /// /// Простая реализация абсрактного класса AbstractMap -- 2.25.1