From 7dda1f708a1ae71e325166cfeecd15f90d2d91e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=88=D0=B8=D0=BD=20=D0=9C=D0=B0=D0=BA=D1=81?= =?UTF-8?q?=D0=B8=D0=BC?= Date: Tue, 4 Oct 2022 15:55:12 +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=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetGasolineTanker.Designer.cs | 4 ++ .../FormMapWithSetGasolineTanker.cs | 12 ++-- .../MapWithSetGasolienTankerGeneric.cs | 21 ++++-- .../SetGasolineTankerGeneric.cs | 67 ++++++++++++++----- 4 files changed, 75 insertions(+), 29 deletions(-) diff --git a/GasolineTanker/GasolineTanker/FormMapWithSetGasolineTanker.Designer.cs b/GasolineTanker/GasolineTanker/FormMapWithSetGasolineTanker.Designer.cs index 8e862fd..311cc56 100644 --- a/GasolineTanker/GasolineTanker/FormMapWithSetGasolineTanker.Designer.cs +++ b/GasolineTanker/GasolineTanker/FormMapWithSetGasolineTanker.Designer.cs @@ -82,6 +82,7 @@ this.keyRight.Size = new System.Drawing.Size(30, 30); this.keyRight.TabIndex = 10; this.keyRight.UseVisualStyleBackColor = true; + this.keyRight.Click += new System.EventHandler(this.ButtonMove_Click); // // keyLeft // @@ -93,6 +94,7 @@ this.keyLeft.Size = new System.Drawing.Size(30, 30); this.keyLeft.TabIndex = 9; this.keyLeft.UseVisualStyleBackColor = true; + this.keyLeft.Click += new System.EventHandler(this.ButtonMove_Click); // // keyUp // @@ -104,6 +106,7 @@ this.keyUp.Size = new System.Drawing.Size(30, 30); this.keyUp.TabIndex = 8; this.keyUp.UseVisualStyleBackColor = true; + this.keyUp.Click += new System.EventHandler(this.ButtonMove_Click); // // keyDown // @@ -115,6 +118,7 @@ this.keyDown.Size = new System.Drawing.Size(30, 30); this.keyDown.TabIndex = 7; this.keyDown.UseVisualStyleBackColor = true; + this.keyDown.Click += new System.EventHandler(this.ButtonMove_Click); // // buttonShowOnMap // diff --git a/GasolineTanker/GasolineTanker/FormMapWithSetGasolineTanker.cs b/GasolineTanker/GasolineTanker/FormMapWithSetGasolineTanker.cs index 9039fb5..66b1d1c 100644 --- a/GasolineTanker/GasolineTanker/FormMapWithSetGasolineTanker.cs +++ b/GasolineTanker/GasolineTanker/FormMapWithSetGasolineTanker.cs @@ -29,7 +29,7 @@ namespace GasolineTanker if (form.ShowDialog() == DialogResult.OK) { DrawingObjectGasolineTanker GasolineTanker = new(form.SelectedGasolineTanker); - if (_mapGasolineTankerCollectionGeneric + GasolineTanker) + if (_mapGasolineTankerCollectionGeneric + GasolineTanker >= 0) { MessageBox.Show("Object added"); pictureBox.Image = _mapGasolineTankerCollectionGeneric.ShowSet(); @@ -52,7 +52,7 @@ namespace GasolineTanker return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapGasolineTankerCollectionGeneric - pos) + if (_mapGasolineTankerCollectionGeneric - pos != null) { MessageBox.Show("Object deleted"); pictureBox.Image = _mapGasolineTankerCollectionGeneric.ShowSet(); @@ -91,16 +91,16 @@ namespace GasolineTanker Direction dir = Direction.None; switch (name) { - case "buttonUp": + case "keyUp": dir = Direction.Up; break; - case "buttonDown": + case "keyDown": dir = Direction.Down; break; - case "buttonLeft": + case "keyLeft": dir = Direction.Left; break; - case "buttonRight": + case "keyRight": dir = Direction.Right; break; } diff --git a/GasolineTanker/GasolineTanker/MapWithSetGasolienTankerGeneric.cs b/GasolineTanker/GasolineTanker/MapWithSetGasolienTankerGeneric.cs index d8fe38f..272210a 100644 --- a/GasolineTanker/GasolineTanker/MapWithSetGasolienTankerGeneric.cs +++ b/GasolineTanker/GasolineTanker/MapWithSetGasolienTankerGeneric.cs @@ -26,12 +26,12 @@ namespace GasolineTanker _map = map; } - public static bool operator +(MapWithSetGasolienTankerGeneric map, T GasolineTanker) + public static int operator +(MapWithSetGasolienTankerGeneric map, T GasolineTanker) { return map._setGasolineTanker.Insert(GasolineTanker); } - public static bool operator -(MapWithSetGasolienTankerGeneric map, int position) + public static T operator -(MapWithSetGasolienTankerGeneric map, int position) { return map._setGasolineTanker.Remove(position); } @@ -95,23 +95,30 @@ namespace GasolineTanker private void DrawBackground(Graphics g) { - Pen pen = new(Color.Black, 3); + Pen pen = new(Color.Black, 5); 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 + 10, j * _placeSizeHeight + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2); + g.DrawLine(pen, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20); + g.DrawLine(pen, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20); } - g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); } } private void DrawGasolineTanker(Graphics g) { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + for (int i = 0; i < _setGasolineTanker.Count; i++) { - // TODO установка позиции - _setGasolineTanker.Get(i)?.DrawningObject(g); + if (_setGasolineTanker.Get(i) != null) + { + _setGasolineTanker.Get(i).SetObject(i % width * _placeSizeWidth + 10, (height - 1 - i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); + _setGasolineTanker.Get(i)?.DrawningObject(g); + } } } } diff --git a/GasolineTanker/GasolineTanker/SetGasolineTankerGeneric.cs b/GasolineTanker/GasolineTanker/SetGasolineTankerGeneric.cs index 5e68e46..69ae434 100644 --- a/GasolineTanker/GasolineTanker/SetGasolineTankerGeneric.cs +++ b/GasolineTanker/GasolineTanker/SetGasolineTankerGeneric.cs @@ -15,30 +15,65 @@ namespace GasolineTanker { _places = new T[count]; } - public bool Insert(T GasolineTanker) + public int Insert(T GasolineTanker) { - // TODO вставка в начало набора - return true; + if (_places[Count - 1] == null) + { + for (int i = Count - 1; i > 0; i--) + { + _places[i] = _places[i - 1]; + } + _places[0] = GasolineTanker; + return 0; + } + return -1; } - public bool Insert(T GasolineTanker, int position) + public int Insert(T GasolineTanker, int position) { - // TODO проверка позиции - // TODO проверка, что элемент массива по этой позиции пустой, если нет, то - // проверка, что после вставляемого элемента в массиве есть пустой элемент - // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - // TODO вставка по позиции - _places[position] = GasolineTanker; - return true; + if (position < 0 || position >= Count) return -1; + if (_places[position] == null) + { + _places[position] = GasolineTanker; + return position; + } + else + { + if (_places[Count - 1] == null) + { + for (int i = Count - 1; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = GasolineTanker; + return position; + } + return -1; + } } - public bool Remove(int position) + public T Remove(int position) { - // TODO проверка позиции - // TODO удаление объекта из массива, присовив элементу массива значение null - return true; + if (position < 0 || position >= Count) + return null; + if (_places[position] != null) + { + T removed = _places[position]; + _places[position] = null; + if (position < Count - 1) + { + for (int k = position; k < Count - 1; k++) + { + _places[k] = _places[k + 1]; + } + } + return removed; + } + return null; } public T Get(int position) { - // TODO проверка позиции + if (position >= Count || position < 0) + return null; + return _places[position]; } }