diff --git a/Stormtrooper/Stormtrooper/AbstractMap.cs b/Stormtrooper/Stormtrooper/AbstractMap.cs index 25844ba..edb6258 100644 --- a/Stormtrooper/Stormtrooper/AbstractMap.cs +++ b/Stormtrooper/Stormtrooper/AbstractMap.cs @@ -55,7 +55,6 @@ namespace Stormtrooper } public Bitmap MoveObject(Direction direction) { - // TODO проверка, что объект может переместится в требуемом направлении bool place = false; switch (direction) { @@ -87,7 +86,6 @@ namespace Stormtrooper int x = _random.Next(0, 10); int y = _random.Next(0, 10); _drawningObject.SetObject(x, y, _width, _height); - // TODO првоерка, что объект не "накладывается" на закрытые участки while (!CheckEnoughPlace(0, 0)) { x += 10; diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.Designer.cs b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.Designer.cs index 6d7b8c3..5844921 100644 --- a/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.Designer.cs +++ b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.Designer.cs @@ -28,12 +28,124 @@ /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.buttonShowOnMap = new System.Windows.Forms.Button(); + this.buttonShowStorage = new System.Windows.Forms.Button(); + this.buttonRemoveStormtrooper = new System.Windows.Forms.Button(); + this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); + this.buttonAddStormtrooper = new System.Windows.Forms.Button(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.pictureBox = new System.Windows.Forms.PictureBox(); + this.groupBox1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.SuspendLayout(); + // + // groupBox1 + // + this.groupBox1.Controls.Add(this.buttonShowOnMap); + this.groupBox1.Controls.Add(this.buttonShowStorage); + this.groupBox1.Controls.Add(this.buttonRemoveStormtrooper); + this.groupBox1.Controls.Add(this.maskedTextBoxPosition); + this.groupBox1.Controls.Add(this.buttonAddStormtrooper); + this.groupBox1.Controls.Add(this.comboBoxSelectorMap); + this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; + this.groupBox1.Location = new System.Drawing.Point(550, 0); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(250, 450); + this.groupBox1.TabIndex = 0; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "groupBox1"; + // + // buttonShowOnMap + // + this.buttonShowOnMap.Location = new System.Drawing.Point(41, 344); + this.buttonShowOnMap.Name = "buttonShowOnMap"; + this.buttonShowOnMap.Size = new System.Drawing.Size(142, 29); + this.buttonShowOnMap.TabIndex = 5; + this.buttonShowOnMap.Text = "Посмотреть карту"; + this.buttonShowOnMap.UseVisualStyleBackColor = true; + this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); + // + // buttonShowStorage + // + this.buttonShowStorage.Location = new System.Drawing.Point(41, 250); + this.buttonShowStorage.Name = "buttonShowStorage"; + this.buttonShowStorage.Size = new System.Drawing.Size(185, 29); + this.buttonShowStorage.TabIndex = 4; + this.buttonShowStorage.Text = "Посмотреть хранилище"; + this.buttonShowStorage.UseVisualStyleBackColor = true; + this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); + // + // buttonRemoveStormtrooper + // + this.buttonRemoveStormtrooper.Location = new System.Drawing.Point(41, 194); + this.buttonRemoveStormtrooper.Name = "buttonRemoveStormtrooper"; + this.buttonRemoveStormtrooper.Size = new System.Drawing.Size(94, 29); + this.buttonRemoveStormtrooper.TabIndex = 3; + this.buttonRemoveStormtrooper.Text = "Удалить"; + this.buttonRemoveStormtrooper.UseVisualStyleBackColor = true; + this.buttonRemoveStormtrooper.Click += new System.EventHandler(this.ButtonRemoveStormtrooper_Click); + // + // maskedTextBoxPosition + // + this.maskedTextBoxPosition.Location = new System.Drawing.Point(41, 146); + this.maskedTextBoxPosition.Mask = "00"; + this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + this.maskedTextBoxPosition.Size = new System.Drawing.Size(125, 27); + this.maskedTextBoxPosition.TabIndex = 2; + // + // buttonAddStormtrooper + // + this.buttonAddStormtrooper.Location = new System.Drawing.Point(41, 91); + this.buttonAddStormtrooper.Name = "buttonAddStormtrooper"; + this.buttonAddStormtrooper.Size = new System.Drawing.Size(94, 29); + this.buttonAddStormtrooper.TabIndex = 1; + this.buttonAddStormtrooper.Text = "Добавить"; + this.buttonAddStormtrooper.UseVisualStyleBackColor = true; + this.buttonAddStormtrooper.Click += new System.EventHandler(this.ButtonAddStormtrooper_Click); + // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.FormattingEnabled = true; + this.comboBoxSelectorMap.Location = new System.Drawing.Point(41, 35); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(151, 28); + this.comboBoxSelectorMap.TabIndex = 0; + // + // 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(550, 450); + this.pictureBox.TabIndex = 1; + this.pictureBox.TabStop = false; + // + // FormMapWithSetStormtroopers + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.pictureBox); + this.Controls.Add(this.groupBox1); + this.Name = "FormMapWithSetStormtroopers"; this.Text = "FormMapWithSetStormtroopers"; + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.ResumeLayout(false); + } #endregion + + private GroupBox groupBox1; + private Button buttonShowOnMap; + private Button buttonShowStorage; + private Button buttonRemoveStormtrooper; + private MaskedTextBox maskedTextBoxPosition; + private Button buttonAddStormtrooper; + private ComboBox comboBoxSelectorMap; + private PictureBox pictureBox; } } \ No newline at end of file diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.cs b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.cs index 0f1d925..0c3c489 100644 --- a/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.cs +++ b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.cs @@ -7,14 +7,159 @@ 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 FormMapWithSetStormtroopers : Form { + /// + /// Объект от класса карты с набором объектов + /// + private MapWithSetStormtroopersGeneric _mapCarsCollectionGeneric; + /// + /// Конструктор + /// public FormMapWithSetStormtroopers() { InitializeComponent(); } + + /// + /// Выбор карты + /// + /// + /// + private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, + EventArgs e) + { + AbstractMap map = null; + switch (comboBoxSelectorMap.Text) + { + case "Простая карта": + map = new SimpleMap(); + break; + } + if (map != null) + { + _mapCarsCollectionGeneric = new + MapWithSetStormtroopersGeneric( + pictureBox.Width, pictureBox.Height, map); + } + else + { + _mapCarsCollectionGeneric = null; + } + } + /// + /// Добавление объекта + /// + /// + /// + private void ButtonAddStormtrooper_Click(object sender, EventArgs e) + { + if (_mapCarsCollectionGeneric == null) + { + return; + } + FormStormtrooper form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + DrawningObjectStormtrooper car = new(form.SelectedStormtrooper); + if (_mapCarsCollectionGeneric + car) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _mapCarsCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта + /// + /// + /// + private void ButtonRemoveStormtrooper_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 (_mapCarsCollectionGeneric - pos) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _mapCarsCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Вывод набора + /// + /// + /// + private void ButtonShowStorage_Click(object sender, EventArgs e) + { + if (_mapCarsCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapCarsCollectionGeneric.ShowSet(); + } + /// + /// Вывод карты + /// + /// + /// + private void ButtonShowOnMap_Click(object sender, EventArgs e) + { + if (_mapCarsCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapCarsCollectionGeneric.ShowOnMap(); + } + /// + /// Перемещение + /// + /// + /// + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_mapCarsCollectionGeneric == 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 = _mapCarsCollectionGeneric.MoveObject(dir); + } } } diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.resx b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.resx index 1af7de1..f298a7b 100644 --- a/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.resx +++ b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.resx @@ -1,64 +1,4 @@ - - - + diff --git a/Stormtrooper/Stormtrooper/MapWithSetStormtroopersGeneric.cs b/Stormtrooper/Stormtrooper/MapWithSetStormtroopersGeneric.cs index 4142127..1a720de 100644 --- a/Stormtrooper/Stormtrooper/MapWithSetStormtroopersGeneric.cs +++ b/Stormtrooper/Stormtrooper/MapWithSetStormtroopersGeneric.cs @@ -29,7 +29,7 @@ namespace Stormtrooper /// /// Набор объектов /// - private readonly SetStormtroopersGeneric _setCars; + private readonly SetStormtroopersGeneric _setStormtroopers; /// /// Карта /// @@ -44,7 +44,7 @@ namespace Stormtrooper { int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; - _setCars = new SetStormtroopersGeneric(width * height); + _setStormtroopers = new SetStormtroopersGeneric(width * height); _pictureWidth = picWidth; _pictureHeight = picHeight; _map = map; @@ -57,7 +57,7 @@ namespace Stormtrooper /// public static bool operator + (MapWithSetStormtroopersGeneric map, T car) { - return map._setCars.Insert(car); + return map._setStormtroopers.Insert(car); } /// /// Перегрузка оператора вычитания @@ -68,7 +68,7 @@ namespace Stormtrooper public static bool operator - (MapWithSetStormtroopersGeneric map, int position) { - return map._setCars.Remove(position); + return map._setStormtroopers.Remove(position); } /// /// Вывод всего набора объектов @@ -89,9 +89,9 @@ namespace Stormtrooper public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setCars.Count; i++) + for (int i = 0; i < _setStormtroopers.Count; i++) { - var car = _setCars.Get(i); + var car = _setStormtroopers.Get(i); if (car != null) { return _map.CreateMap(_pictureWidth, _pictureHeight, car); @@ -117,18 +117,18 @@ namespace Stormtrooper /// private void Shaking() { - int j = _setCars.Count - 1; - for (int i = 0; i < _setCars.Count; i++) + int j = _setStormtroopers.Count - 1; + for (int i = 0; i < _setStormtroopers.Count; i++) { - if (_setCars.Get(i) == null) + if (_setStormtroopers.Get(i) == null) { for (; j > i; j--) { - var car = _setCars.Get(j); + var car = _setStormtroopers.Get(j); if (car != null) { - _setCars.Insert(car, i); - _setCars.Remove(j); + _setStormtroopers.Insert(car, i); + _setStormtroopers.Remove(j); break; } } @@ -163,10 +163,18 @@ namespace Stormtrooper /// private void DrawCars(Graphics g) { - for (int i = 0; i < _setCars.Count; i++) + int x = 5; + int y = 5; + for (int i = 0; i < _setStormtroopers.Count; i++) { - // TODO установка позиции - _setCars.Get(i)?.DrawningObject(g); + _setStormtroopers.Get(i)?.SetObject(x, y, _pictureWidth, _pictureHeight); + _setStormtroopers.Get(i)?.DrawningObject(g); + x += _placeSizeWidth; + if (x + _placeSizeWidth > _pictureWidth) + { + y += _placeSizeHeight; + x = 5; + } } } diff --git a/Stormtrooper/Stormtrooper/SetStormtroopersGeneric.cs b/Stormtrooper/Stormtrooper/SetStormtroopersGeneric.cs index c75c56d..985a84e 100644 --- a/Stormtrooper/Stormtrooper/SetStormtroopersGeneric.cs +++ b/Stormtrooper/Stormtrooper/SetStormtroopersGeneric.cs @@ -21,7 +21,7 @@ namespace Stormtrooper /// Конструктор /// /// - public SetCarsGeneric(int count) + public SetStormtroopersGeneric(int count) { _places = new T[count]; } @@ -30,10 +30,17 @@ namespace Stormtrooper /// /// Добавляемый автомобиль /// - public bool Insert(T car) + public int Insert(T stormtrooper) { - // TODO вставка в начало набора - return true; + for (int i = 0; i < _places.Length; i++) + { + if (_places[i] == null) + { + _places[i] = stormtrooper; + return i; + } + } + return -1; } /// /// Добавление объекта в набор на конкретную позицию @@ -45,10 +52,13 @@ namespace Stormtrooper { // TODO проверка позиции // TODO проверка, что элемент массива по этой позиции пустой, если нет, то - // проверка, что после вставляемого элемента в массиве есть пустой элемент - // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - // TODO вставка по позиции - _places[position] = car; + // проверка, что после вставляемого элемента в массиве есть пустой элемент + // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + // TODO вставка по позиции + + // hz kak sdelat s bool + + _places[position] = car; return true; } /// @@ -60,6 +70,7 @@ namespace Stormtrooper { // TODO проверка позиции // TODO удаление объекта из массива, присовив элементу массива значение null + // hz kak sdelat s bool return true; } /// @@ -69,7 +80,10 @@ namespace Stormtrooper /// public T Get(int position) { - // TODO проверка позиции + if (position < 0 || position > _places.Length) + { + return null; + } return _places[position]; }