From 7bb13c445e86effbf868918faffcfc824cfcbb37 Mon Sep 17 00:00:00 2001 From: Sem730 Date: Sun, 23 Oct 2022 22:09:08 +0300 Subject: [PATCH] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=201.=20=D0=A1=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=D0=B0?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D1=81=D0=BF=D0=B8=D1=81=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectLocomotive/FormLocomotive.cs | 16 +--- .../MapWithSetLocomotivesGeneric.cs | 38 ++------- .../SetLocomotivesGeneric.cs | 77 +++++++++---------- 3 files changed, 46 insertions(+), 85 deletions(-) diff --git a/ProjectLocomotive/ProjectLocomotive/FormLocomotive.cs b/ProjectLocomotive/ProjectLocomotive/FormLocomotive.cs index 8fbcbc4..75d6c35 100644 --- a/ProjectLocomotive/ProjectLocomotive/FormLocomotive.cs +++ b/ProjectLocomotive/ProjectLocomotive/FormLocomotive.cs @@ -7,12 +7,10 @@ namespace ProjectLocomotive /// Βϋαπΰννϋι ξαϊεκς /// public DrawningLocomotive SelectedLocomotive { get; private set; } - public FormLocomotive() { InitializeComponent(); } - /// /// Μεςξδ οπξπθρξβκθ ύλεκςπξλξκξμξςθβΰ /// @@ -39,11 +37,9 @@ namespace ProjectLocomotive /// /// /// - private void ButtonCreate_Click(object sender, EventArgs e) { Random rnd = new(); - //_elloc = new DrawningLocomotive(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); ColorDialog dialog = new(); if (dialog.ShowDialog() == DialogResult.OK) @@ -113,19 +109,11 @@ namespace ProjectLocomotive } _elloc = new DrawningElectroLocomotive(rnd.Next(100, 300), rnd.Next(1000, 2000), color, - dopColor, - //Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), - //Color.FromArgb(rnd.Next(1, 100), rnd.Next(1, 100), rnd.Next(1, 100)), + dopColor, Convert.ToBoolean(rnd.Next(0, 1)), Convert.ToBoolean(rnd.Next(0, 1))); SetData(); Draw(); - } - - private void pictureBoxLocomotive_Click(object sender, EventArgs e) - { - - } - + } private void buttonSelectLocomotive_Click(object sender, EventArgs e) { SelectedLocomotive = _elloc; diff --git a/ProjectLocomotive/ProjectLocomotive/MapWithSetLocomotivesGeneric.cs b/ProjectLocomotive/ProjectLocomotive/MapWithSetLocomotivesGeneric.cs index 09e40b0..dc30b2c 100644 --- a/ProjectLocomotive/ProjectLocomotive/MapWithSetLocomotivesGeneric.cs +++ b/ProjectLocomotive/ProjectLocomotive/MapWithSetLocomotivesGeneric.cs @@ -55,13 +55,9 @@ namespace ProjectLocomotive public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setLocomotives.Count; i++) + foreach (var locomotive in _setLocomotives.GetLocomotives()) { - var locomotive = _setLocomotives.Get(i); - if (locomotive != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive); } return new(_pictureWidth, _pictureHeight); } @@ -80,11 +76,11 @@ namespace ProjectLocomotive int j = _setLocomotives.Count - 1; for (int i = 0; i < _setLocomotives.Count; i++) { - if (_setLocomotives.Get(i) == null) + if (_setLocomotives[i] == null) { for (; j > i; j--) { - var locomotive = _setLocomotives.Get(j); + var locomotive = _setLocomotives[j]; if (locomotive != null) { _setLocomotives.Insert(locomotive, i); @@ -101,43 +97,27 @@ namespace ProjectLocomotive } /// ΠœΠ΅Ρ‚ΠΎΠ΄ отрисовки Ρ„ΠΎΠ½Π° 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); - //} + { Pen pen; for (int j = _placeSizeHeight; j < _pictureHeight; j += _placeSizeHeight) { //ниТняя линия Ρ€Π΅Π»ΡŒΡ pen = new(Color.Black, 5); - g.DrawLine(pen, 0, j, _pictureWidth, j); for (int i = 0; i < _pictureWidth; i += 20) { g.DrawLine(pen, i, j, i, j + 10); } g.DrawLine(pen, 0, j + 10, _pictureWidth, j + 10); - //вСрхняя линия Ρ€Π΅Π»ΡŒΡ - pen = new(Color.DarkGray, 4); - g.DrawLine(pen, 0, j - 20, _pictureWidth, j - 20); for (int i = 0; i < _pictureWidth; i += 20) { g.DrawLine(pen, i, j - 20, i, j - 10); } g.DrawLine(pen, 0, j - 10, _pictureWidth, j - 10); - //Ρ„ΠΎΠ½Π°Ρ€ΠΈ for (int i = _placeSizeWidth; i < _pictureWidth; i += _placeSizeWidth) { @@ -157,12 +137,10 @@ namespace ProjectLocomotive int curWidth = 0; int curHeight = 0; - for (int i = 0; i < _setLocomotives.Count; i++) + foreach (var locomotive in _setLocomotives.GetLocomotives()) { - // установка ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ - //_setLocomotives.Get(i)?.SetObject(curWidth * _placeSizeWidth, curHeight * _placeSizeHeight, _pictureWidth, _pictureHeight); - _setLocomotives.Get(i)?.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 470, _pictureWidth, _pictureHeight); - _setLocomotives.Get(i)?.DrawningObject(g); + locomotive?.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 15, _pictureWidth, _pictureHeight); + locomotive?.DrawningObject(g); if (curWidth < width) curWidth++; else { diff --git a/ProjectLocomotive/ProjectLocomotive/SetLocomotivesGeneric.cs b/ProjectLocomotive/ProjectLocomotive/SetLocomotivesGeneric.cs index f15e904..dda3033 100644 --- a/ProjectLocomotive/ProjectLocomotive/SetLocomotivesGeneric.cs +++ b/ProjectLocomotive/ProjectLocomotive/SetLocomotivesGeneric.cs @@ -13,14 +13,17 @@ namespace ProjectLocomotive internal class SetLocomotivesGeneric where T : class { - private readonly T[] _places; + /// Бписок Ρ…Ρ€Π°Π½ΠΈΠΌΡ‹Ρ… ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ² + private readonly List _places; - /// ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ² Π² массивС - public int Count => _places.Length; + /// ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ² Π² спискС + public int Count => _places.Count; + private readonly int _maxCount; /// ΠšΠΎΠ½ΡΡ‚Ρ€ΡƒΠΊΡ‚ΠΎΡ€ public SetLocomotivesGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } /// Π”ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° Π² Π½Π°Π±ΠΎΡ€ public int Insert(T locomotive) @@ -30,54 +33,46 @@ namespace ProjectLocomotive /// Π”ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° Π² Π½Π°Π±ΠΎΡ€ Π½Π° ΠΊΠΎΠ½ΠΊΡ€Π΅Ρ‚Π½ΡƒΡŽ ΠΏΠΎΠ·ΠΈΡ†ΠΈΡŽ public int Insert(T locomotive, int position) { - if (position >= _places.Length || position < 0) return -1; - - if (_places[position] == null) - { - _places[position] = locomotive; - return position; - } - - int emptyEl = -1; - - for (int i = position + 1; i < Count; i++) - { - if (_places[i] == null) - { - emptyEl = i; - break; - } - - } - - if (emptyEl == -1) - { - return -1; - } - - for (int i = emptyEl; i > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = locomotive; + if (position >= _maxCount || position < 0) return -1; + _places.Insert(position, locomotive); return position; } /// Π£Π΄Π°Π»Π΅Π½ΠΈΠ΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° ΠΈΠ· Π½Π°Π±ΠΎΡ€Π° с ΠΊΠΎΠ½ΠΊΡ€Π΅Ρ‚Π½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ public T Remove(int position) { - if (position >= _places.Length || position < 0) return null; - _places[position] = null; + if (position >= _maxCount || position < 0) return null; T result = _places[position]; + _places.RemoveAt(position); return result; } - /// ΠŸΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° ΠΈΠ· Π½Π°Π±ΠΎΡ€Π° ΠΏΠΎ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ - public T Get(int position) + // Π˜Π½Π΄Π΅ΠΊΡΠ°Ρ‚ΠΎΡ€ + public T this[int position] { - if (position >= _places.Length || position < 0) + get { - return null; + if (position >= _maxCount || position < 0) return null; + return _places[position]; + } + set + { + if (position >= _maxCount || position < 0) return; + Insert(value, position); + } + } + /// ΠŸΡ€ΠΎΡ…ΠΎΠ΄ ΠΏΠΎ Π½Π°Π±ΠΎΡ€Ρƒ Π΄ΠΎ ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ пустого + public IEnumerable GetLocomotives() + { + foreach (var locomotive in _places) + { + if (locomotive != null) + { + yield return locomotive; + } + else + { + yield break; + } } - return _places[position]; } } }