From ee3326095d9030d8f67295ef6accc895007a2aaf Mon Sep 17 00:00:00 2001 From: prodigygirl Date: Sat, 5 Nov 2022 19:24:32 +0400 Subject: [PATCH 01/10] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0,=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=B4=D0=B3=D0=BE=D1=82=D0=B0=D0=B2=D0=BB=D0=B8=D0=B2?= =?UTF-8?q?=D0=B0=D1=8E=D1=89=D0=B5=D0=B3=D0=BE=20=D0=B4=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D0=B5=20=D0=BE=D0=B1=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA?= =?UTF-8?q?=D1=82=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ArmoredCar/ArmoredCar/ExtensionArmoredCar.cs | 57 ++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 ArmoredCar/ArmoredCar/ExtensionArmoredCar.cs diff --git a/ArmoredCar/ArmoredCar/ExtensionArmoredCar.cs b/ArmoredCar/ArmoredCar/ExtensionArmoredCar.cs new file mode 100644 index 0000000..ea0969a --- /dev/null +++ b/ArmoredCar/ArmoredCar/ExtensionArmoredCar.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ArmoredCar +{ + internal static class ExtensionArmoredCar + { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + /// + /// Создание объекта из строки + /// + /// + /// + public static DrawningArmoredCar CreateDrawningArmoredCar(this string info) + { + string[] strs = info.Split(_separatorForObject); + if (strs.Length == 3) + { + return new DrawningArmoredCar(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2])); + } + if (strs.Length == 6) + { + return new DrawningTank(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), + Color.FromName(strs[3]), Convert.ToBoolean(strs[4]), + Convert.ToBoolean(strs[5])); + } + return null; + } + /// + /// Получение данных для сохранения в файл + /// + /// + /// + public static string GetDataForSave(this DrawningArmoredCar drawningArmoredCar) + { + var armoredCar = drawningArmoredCar.ArmoredCar; + var str = + $"{armoredCar.Speed}{_separatorForObject}{armoredCar.Weight}{_separatorForObject}{armoredCar.BodyColor.Name}"; + if (armoredCar is not EntityTank tank) + { + return str; + } + return + $"{str}{_separatorForObject}{tank.DopColor.Name}{_separatorForObject}{tank.TowerWeapon}{_separatorForObject}{tank.AMachineGun}"; + } + } +} + -- 2.25.1 From e778b6b0607227b0a04bf5d5bd86c9904e93686e Mon Sep 17 00:00:00 2001 From: prodigygirl Date: Sat, 5 Nov 2022 19:29:12 +0400 Subject: [PATCH 02/10] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=B2=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5?= =?UTF-8?q?=D0=B9=D1=81=20=D0=B8=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs | 3 +++ ArmoredCar/ArmoredCar/IDrawningObject.cs | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs b/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs index 09aebde..22effca 100644 --- a/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs +++ b/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs @@ -33,5 +33,8 @@ namespace ArmoredCar if (_armCar != null) _armCar.DrawTransport(g); } + public string GetInfo() => _armCar?.GetDataForSave(); + public static IDrawningObject Create(string data) => new + DrawningObjectArmCar(data.CreateDrawningArmoredCar()); } } diff --git a/ArmoredCar/ArmoredCar/IDrawningObject.cs b/ArmoredCar/ArmoredCar/IDrawningObject.cs index 64307a1..4e97bfa 100644 --- a/ArmoredCar/ArmoredCar/IDrawningObject.cs +++ b/ArmoredCar/ArmoredCar/IDrawningObject.cs @@ -36,5 +36,10 @@ namespace ArmoredCar /// /// (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + /// + /// Получение информации по объекту + /// + /// + string GetInfo(); } } -- 2.25.1 From d566a5ad4d97569f9a2e82db8f51dbc0e860ad15 Mon Sep 17 00:00:00 2001 From: prodigygirl Date: Sat, 5 Nov 2022 19:40:33 +0400 Subject: [PATCH 03/10] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=B8=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20=D0=BA=D0=B0=D1=80=D1=82?= =?UTF-8?q?=D1=8B=20=D0=B8=20=D0=BA=D0=BE=D0=BB=D0=BB=D0=B5=D0=BA=D1=86?= =?UTF-8?q?=D0=B8=D0=B8=20=D0=BA=D0=B0=D1=80=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MapWithSetArmoredCarsGeneric.cs | 25 +++++ ArmoredCar/ArmoredCar/MapsCollection.cs | 96 ++++++++++++++++++- 2 files changed, 117 insertions(+), 4 deletions(-) diff --git a/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs b/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs index ce1d67a..e4a7e64 100644 --- a/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs +++ b/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs @@ -181,5 +181,30 @@ namespace ArmoredCar k++; } } + /// + /// Получение данных в виде строки + /// + /// + /// + public string GetData(char separatorType, char separatorData) + { + string data = $"{_map.GetType().Name}{separatorType}"; + foreach (var armoredCar in _setCars.GetCars()) + { + data += $"{armoredCar.GetInfo()}{separatorData}"; + } + return data; + } + /// + /// Загрузка списка из массива строк + /// + /// + public void LoadData(string[] records) + { + foreach (var rec in records) + { + _setCars.Insert(DrawningObjectArmCar.Create(rec) as T); + } + } } } diff --git a/ArmoredCar/ArmoredCar/MapsCollection.cs b/ArmoredCar/ArmoredCar/MapsCollection.cs index 6d7db7a..32ab235 100644 --- a/ArmoredCar/ArmoredCar/MapsCollection.cs +++ b/ArmoredCar/ArmoredCar/MapsCollection.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -10,7 +11,7 @@ namespace ArmoredCar { /// Словарь (хранилище) с картами /// - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; /// /// Возвращение списка названий карт /// @@ -24,13 +25,21 @@ namespace ArmoredCar /// private readonly int _pictureHeight; /// + /// Разделитель для записи информации по элементу словаря в файл + /// + private readonly char separatorDict = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char separatorData = ';'; + /// /// Конструктор /// /// /// public MapsCollection(int pictureWidth, int pictureHeight) { - _mapStorages = new Dictionary>(); + _mapStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -42,7 +51,7 @@ namespace ArmoredCar public void AddMap(string name, AbstractMap map) { if (!Keys.Contains(name)) - _mapStorages.Add(name, new MapWithSetArmoredCarsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.Add(name, new MapWithSetArmoredCarsGeneric(_pictureWidth, _pictureHeight, map)); } /// /// Удаление карты @@ -58,7 +67,7 @@ namespace ArmoredCar /// /// /// - public MapWithSetArmoredCarsGeneric this[string + public MapWithSetArmoredCarsGeneric this[string ind] { get @@ -68,5 +77,84 @@ namespace ArmoredCar return null; } } + + /// + /// Метод записи информации в файл + /// + /// Строка, которую следует записать + /// Поток для записи + private static void WriteToFile(string text, FileStream stream) + { + byte[] info = new UTF8Encoding(true).GetBytes(text); + stream.Write(info, 0, info.Length); + } + /// + /// Сохранение информации по автомобилям в хранилище в файл + /// + /// Путь и имя файла + /// + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + using (FileStream fs = new(filename, FileMode.Create)) + { + WriteToFile($"MapsCollection{Environment.NewLine}", fs); + foreach (var storage in _mapStorages) + { + + WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", fs); + } + } + return true; + } + /// + /// Загрузка нформации по автомобилям на парковках из файла + /// + /// + /// + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + string bufferTextFromFile = ""; + using (FileStream fs = new(filename, FileMode.Open)) + { + byte[] b = new byte[fs.Length]; + UTF8Encoding temp = new(true); + while (fs.Read(b, 0, b.Length) > 0) + { + bufferTextFromFile += temp.GetString(b); + } + } + var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, + StringSplitOptions.RemoveEmptyEntries); + if (!strs[0].Contains("MapsCollection")) + { + //если нет такой записи, то это не те данные + return false; + } + //очищаем записи + _mapStorages.Clear(); + for (int i = 1; i < strs.Length; ++i) + { + var elem = strs[i].Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetArmoredCarsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + } + return true; + } + } } -- 2.25.1 From 112adcda3a5ca4675650287dc92d02459747fd36 Mon Sep 17 00:00:00 2001 From: prodigygirl Date: Sat, 5 Nov 2022 19:41:17 +0400 Subject: [PATCH 04/10] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=B2=20=D1=84=D0=BE=D1=80=D0=BC=D0=B5:?= =?UTF-8?q?=20=D0=B4=D0=B8=D0=B0=D0=BB=D0=BE=D0=B3=D0=BE=D0=B2=D1=8B=D0=B5?= =?UTF-8?q?=20=D0=BE=D0=BA=D0=BD=D0=B0=20=D0=B8=20=D0=BC=D0=B5=D0=BD=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetArmoredCars.Designer.cs | 75 +++++++++++++++++-- .../ArmoredCar/FormMapWithSetArmoredCars.cs | 45 +++++++++++ .../ArmoredCar/FormMapWithSetArmoredCars.resx | 3 + 3 files changed, 115 insertions(+), 8 deletions(-) diff --git a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.Designer.cs b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.Designer.cs index ba49d7a..2a51009 100644 --- a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.Designer.cs +++ b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.Designer.cs @@ -47,9 +47,16 @@ namespace ArmoredCar this.buttonShowOnMap = new System.Windows.Forms.Button(); this.buttonAddCar = new System.Windows.Forms.Button(); this.pictureBox = new System.Windows.Forms.PictureBox(); + this.menuStrip1 = new System.Windows.Forms.MenuStrip(); + this.FileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.groupBoxTools.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // groupBoxTools @@ -65,9 +72,9 @@ namespace ArmoredCar this.groupBoxTools.Controls.Add(this.buttonShowOnMap); this.groupBoxTools.Controls.Add(this.buttonAddCar); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(811, 0); + this.groupBoxTools.Location = new System.Drawing.Point(811, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(204, 632); + this.groupBoxTools.Size = new System.Drawing.Size(204, 608); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; @@ -168,7 +175,7 @@ namespace ArmoredCar this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::ArmoredCar.Properties.Resources.Down; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(91, 582); + this.buttonDown.Location = new System.Drawing.Point(91, 558); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 10; @@ -180,7 +187,7 @@ namespace ArmoredCar this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::ArmoredCar.Properties.Resources.Right; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(127, 582); + this.buttonRight.Location = new System.Drawing.Point(127, 558); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 9; @@ -192,7 +199,7 @@ namespace ArmoredCar this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::ArmoredCar.Properties.Resources.Left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(55, 582); + this.buttonLeft.Location = new System.Drawing.Point(55, 558); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; @@ -204,7 +211,7 @@ namespace ArmoredCar this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::ArmoredCar.Properties.Resources.Up; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(91, 546); + this.buttonUp.Location = new System.Drawing.Point(91, 522); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 7; @@ -234,12 +241,53 @@ namespace ArmoredCar // pictureBox // this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBox.Location = new System.Drawing.Point(0, 0); + this.pictureBox.Location = new System.Drawing.Point(0, 24); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(811, 632); + this.pictureBox.Size = new System.Drawing.Size(811, 608); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // + // menuStrip1 + // + this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.FileToolStripMenuItem}); + this.menuStrip1.Location = new System.Drawing.Point(0, 0); + this.menuStrip1.Name = "menuStrip1"; + this.menuStrip1.Size = new System.Drawing.Size(1015, 24); + this.menuStrip1.TabIndex = 2; + this.menuStrip1.Text = "menuStrip1"; + // + // FileToolStripMenuItem + // + this.FileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.SaveToolStripMenuItem, + this.LoadToolStripMenuItem}); + this.FileToolStripMenuItem.Name = "FileToolStripMenuItem"; + this.FileToolStripMenuItem.Size = new System.Drawing.Size(48, 20); + this.FileToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.SaveToolStripMenuItem.Text = "Сохранение"; + this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); + // + // LoadToolStripMenuItem + // + this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.LoadToolStripMenuItem.Text = "Загрузка"; + this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); + // + // openFileDialog + // + this.openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormMapWithSetArmoredCars // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -247,6 +295,8 @@ namespace ArmoredCar this.ClientSize = new System.Drawing.Size(1015, 632); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); + this.Controls.Add(this.menuStrip1); + this.MainMenuStrip = this.menuStrip1; this.Name = "FormMapWithSetArmoredCars"; this.Text = "Карта с набором объектов"; this.groupBoxTools.ResumeLayout(false); @@ -254,7 +304,10 @@ namespace ArmoredCar this.groupBoxMaps.ResumeLayout(false); this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -277,5 +330,11 @@ namespace ArmoredCar private ListBox listBoxMaps; private TextBox textBoxNewMapName; private Button buttonAddMap; + private MenuStrip menuStrip1; + private ToolStripMenuItem FileToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs index 89f0d00..3b9fd7e 100644 --- a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs +++ b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs @@ -220,5 +220,50 @@ namespace ArmoredCar } pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); } + /// + /// Обработка нажатия "Сохранение" + /// + /// + /// + private void SaveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_mapsCollection.SaveData(saveFileDialog.FileName)) + { + MessageBox.Show("Сохранение прошло успешно", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не сохранилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + /// + /// Обработка нажатия "Загрузка" + /// + /// + /// + private void LoadToolStripMenuItem_Click(object sender, EventArgs e) + { + // TODO продумать логику + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + if (_mapsCollection.LoadData(openFileDialog.FileName)) + { + MessageBox.Show("Загрузка прошла успешно", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Information); + ReloadMaps(); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } } \ No newline at end of file diff --git a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.resx b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.resx index f298a7b..938108a 100644 --- a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.resx +++ b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.resx @@ -57,4 +57,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + \ No newline at end of file -- 2.25.1 From d17fb9ac57558eb6e614c8fb31dc73e43e30c0d7 Mon Sep 17 00:00:00 2001 From: prodigygirl Date: Sat, 5 Nov 2022 20:22:02 +0400 Subject: [PATCH 05/10] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D1=85=D0=BE?= =?UTF-8?q?=D0=B4=20=D0=BD=D0=B0=20StreamWriter/StreamReader?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ArmoredCar/ArmoredCar/MapsCollection.cs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/ArmoredCar/ArmoredCar/MapsCollection.cs b/ArmoredCar/ArmoredCar/MapsCollection.cs index 32ab235..70f5d27 100644 --- a/ArmoredCar/ArmoredCar/MapsCollection.cs +++ b/ArmoredCar/ArmoredCar/MapsCollection.cs @@ -83,10 +83,9 @@ namespace ArmoredCar /// /// Строка, которую следует записать /// Поток для записи - private static void WriteToFile(string text, FileStream stream) + private static void WriteToFile(string text, StreamWriter stream) { - byte[] info = new UTF8Encoding(true).GetBytes(text); - stream.Write(info, 0, info.Length); + stream.Write(text, 0, text.Length); } /// /// Сохранение информации по автомобилям в хранилище в файл @@ -99,7 +98,7 @@ namespace ArmoredCar { File.Delete(filename); } - using (FileStream fs = new(filename, FileMode.Create)) + using (StreamWriter fs = new(filename)) { WriteToFile($"MapsCollection{Environment.NewLine}", fs); foreach (var storage in _mapStorages) @@ -122,13 +121,12 @@ namespace ArmoredCar return false; } string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader fs = new(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) + string? line; + while ((line = fs.ReadLine()) != null) { - bufferTextFromFile += temp.GetString(b); + bufferTextFromFile += line + "\r\n"; } } var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, @@ -149,6 +147,12 @@ namespace ArmoredCar case "SimpleMap": map = new SimpleMap(); break; + case "MyMapLabirinth": + map = new MyMapLabirinth(); + break; + case "MyMapWooden": + map = new MyMapWooden(); + break; } _mapStorages.Add(elem[0], new MapWithSetArmoredCarsGeneric(_pictureWidth, _pictureHeight, map)); _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); -- 2.25.1 From 8768836c298a20aa45304916daa66ce2de594477 Mon Sep 17 00:00:00 2001 From: prodigygirl Date: Tue, 8 Nov 2022 00:27:30 +0400 Subject: [PATCH 06/10] =?UTF-8?q?=D0=A4=D0=BE=D1=80=D0=BC=D0=B0=20=D1=81?= =?UTF-8?q?=D1=82=D0=B0=D0=BB=D0=B0=20=D1=87=D1=83=D1=82=D1=8C=20=D0=B1?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D1=88=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetArmoredCars.Designer.cs | 22 +++++++++---------- .../ArmoredCar/FormMapWithSetArmoredCars.resx | 9 ++++++++ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.Designer.cs b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.Designer.cs index 2a51009..0cd41b5 100644 --- a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.Designer.cs +++ b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.Designer.cs @@ -72,9 +72,9 @@ namespace ArmoredCar this.groupBoxTools.Controls.Add(this.buttonShowOnMap); this.groupBoxTools.Controls.Add(this.buttonAddCar); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(811, 24); + this.groupBoxTools.Location = new System.Drawing.Point(802, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(204, 608); + this.groupBoxTools.Size = new System.Drawing.Size(204, 637); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; @@ -175,7 +175,7 @@ namespace ArmoredCar this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::ArmoredCar.Properties.Resources.Down; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(91, 558); + this.buttonDown.Location = new System.Drawing.Point(91, 587); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 10; @@ -187,7 +187,7 @@ namespace ArmoredCar this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::ArmoredCar.Properties.Resources.Right; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(127, 558); + this.buttonRight.Location = new System.Drawing.Point(127, 587); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 9; @@ -199,7 +199,7 @@ namespace ArmoredCar this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::ArmoredCar.Properties.Resources.Left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(55, 558); + this.buttonLeft.Location = new System.Drawing.Point(55, 587); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; @@ -211,7 +211,7 @@ namespace ArmoredCar this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::ArmoredCar.Properties.Resources.Up; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(91, 522); + this.buttonUp.Location = new System.Drawing.Point(91, 551); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 7; @@ -243,7 +243,7 @@ namespace ArmoredCar this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 24); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(811, 608); + this.pictureBox.Size = new System.Drawing.Size(802, 637); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -253,7 +253,7 @@ namespace ArmoredCar this.FileToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; - this.menuStrip1.Size = new System.Drawing.Size(1015, 24); + this.menuStrip1.Size = new System.Drawing.Size(1006, 24); this.menuStrip1.TabIndex = 2; this.menuStrip1.Text = "menuStrip1"; // @@ -269,14 +269,14 @@ namespace ArmoredCar // SaveToolStripMenuItem // this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(141, 22); this.SaveToolStripMenuItem.Text = "Сохранение"; this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // // LoadToolStripMenuItem // this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(141, 22); this.LoadToolStripMenuItem.Text = "Загрузка"; this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // @@ -292,7 +292,7 @@ namespace ArmoredCar // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1015, 632); + this.ClientSize = new System.Drawing.Size(1006, 661); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.menuStrip1); diff --git a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.resx b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.resx index 938108a..a990664 100644 --- a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.resx +++ b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.resx @@ -58,6 +58,15 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + 279, 17 + + 17, 17 + + 150, 17 + + + 25 + \ No newline at end of file -- 2.25.1 From 99886df933b2c329a262e2b4eb6588a18827c92d Mon Sep 17 00:00:00 2001 From: prodigygirl Date: Tue, 8 Nov 2022 08:28:03 +0400 Subject: [PATCH 07/10] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83?= =?UTF-8?q?=D0=B7=D0=BA=D0=B8=20=D0=BF=D0=BE=D1=81=D1=82=D1=80=D0=BE=D1=87?= =?UTF-8?q?=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ArmoredCar/ArmoredCar/MapsCollection.cs | 57 +++++++++++-------------- 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/ArmoredCar/ArmoredCar/MapsCollection.cs b/ArmoredCar/ArmoredCar/MapsCollection.cs index 70f5d27..368a85f 100644 --- a/ArmoredCar/ArmoredCar/MapsCollection.cs +++ b/ArmoredCar/ArmoredCar/MapsCollection.cs @@ -118,47 +118,40 @@ namespace ArmoredCar { if (!File.Exists(filename)) { - return false; + throw new Exception("Файл не найден"); } string bufferTextFromFile = ""; using (StreamReader fs = new(filename)) { + + if (!fs.ReadLine().Contains("MapsCollection")) + //если нет такой записи, то это не те данные + return false; string? line; + //очищаем записи + _mapStorages.Clear(); while ((line = fs.ReadLine()) != null) { - bufferTextFromFile += line + "\r\n"; + bufferTextFromFile += line; + var elem = line.Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "MyMapLabirinth": + map = new MyMapLabirinth(); + break; + case "MyMapWooden": + map = new MyMapWooden(); + break; + } + _mapStorages.Add(elem[0], new MapWithSetArmoredCarsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); } + return true; } - var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, - StringSplitOptions.RemoveEmptyEntries); - if (!strs[0].Contains("MapsCollection")) - { - //если нет такой записи, то это не те данные - return false; - } - //очищаем записи - _mapStorages.Clear(); - for (int i = 1; i < strs.Length; ++i) - { - var elem = strs[i].Split(separatorDict); - AbstractMap map = null; - switch (elem[1]) - { - case "SimpleMap": - map = new SimpleMap(); - break; - case "MyMapLabirinth": - map = new MyMapLabirinth(); - break; - case "MyMapWooden": - map = new MyMapWooden(); - break; - } - _mapStorages.Add(elem[0], new MapWithSetArmoredCarsGeneric(_pictureWidth, _pictureHeight, map)); - _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); - } - return true; } - } } -- 2.25.1 From 47faa0de7edb032065f36fb654d9f52940fc7a1c Mon Sep 17 00:00:00 2001 From: prodigygirl Date: Tue, 8 Nov 2022 08:30:46 +0400 Subject: [PATCH 08/10] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ArmoredCar/ArmoredCar/MapsCollection.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/ArmoredCar/ArmoredCar/MapsCollection.cs b/ArmoredCar/ArmoredCar/MapsCollection.cs index 368a85f..a16d389 100644 --- a/ArmoredCar/ArmoredCar/MapsCollection.cs +++ b/ArmoredCar/ArmoredCar/MapsCollection.cs @@ -118,9 +118,8 @@ namespace ArmoredCar { if (!File.Exists(filename)) { - throw new Exception("Файл не найден"); - } - string bufferTextFromFile = ""; + return false; + } using (StreamReader fs = new(filename)) { @@ -131,8 +130,7 @@ namespace ArmoredCar //очищаем записи _mapStorages.Clear(); while ((line = fs.ReadLine()) != null) - { - bufferTextFromFile += line; + { var elem = line.Split(separatorDict); AbstractMap map = null; switch (elem[1]) -- 2.25.1 From 3b328133e67023e1dc5515327680678cf2c473df Mon Sep 17 00:00:00 2001 From: prodigygirl Date: Wed, 16 Nov 2022 18:26:52 +0400 Subject: [PATCH 09/10] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ArmoredCar/FormMapWithSetArmoredCars.cs | 1 - ArmoredCar/ArmoredCar/MapsCollection.cs | 17 +++-------------- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs index 3b9fd7e..c99cb31 100644 --- a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs +++ b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs @@ -248,7 +248,6 @@ namespace ArmoredCar /// private void LoadToolStripMenuItem_Click(object sender, EventArgs e) { - // TODO продумать логику if (openFileDialog.ShowDialog() == DialogResult.OK) { if (_mapsCollection.LoadData(openFileDialog.FileName)) diff --git a/ArmoredCar/ArmoredCar/MapsCollection.cs b/ArmoredCar/ArmoredCar/MapsCollection.cs index a16d389..0bd5205 100644 --- a/ArmoredCar/ArmoredCar/MapsCollection.cs +++ b/ArmoredCar/ArmoredCar/MapsCollection.cs @@ -78,15 +78,6 @@ namespace ArmoredCar } } - /// - /// Метод записи информации в файл - /// - /// Строка, которую следует записать - /// Поток для записи - private static void WriteToFile(string text, StreamWriter stream) - { - stream.Write(text, 0, text.Length); - } /// /// Сохранение информации по автомобилям в хранилище в файл /// @@ -100,11 +91,11 @@ namespace ArmoredCar } using (StreamWriter fs = new(filename)) { - WriteToFile($"MapsCollection{Environment.NewLine}", fs); + fs.WriteLine("MapsCollection"); foreach (var storage in _mapStorages) { - WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", fs); + fs.WriteLine($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}"); } } return true; @@ -123,11 +114,9 @@ namespace ArmoredCar using (StreamReader fs = new(filename)) { - if (!fs.ReadLine().Contains("MapsCollection")) - //если нет такой записи, то это не те данные + if (!fs.ReadLine().Contains("MapsCollection")) return false; string? line; - //очищаем записи _mapStorages.Clear(); while ((line = fs.ReadLine()) != null) { -- 2.25.1 From 7636d8c3784b871ecbed576348e3bec9e904c524 Mon Sep 17 00:00:00 2001 From: prodigygirl Date: Wed, 16 Nov 2022 18:37:19 +0400 Subject: [PATCH 10/10] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ArmoredCar/ArmoredCar/MapsCollection.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/ArmoredCar/ArmoredCar/MapsCollection.cs b/ArmoredCar/ArmoredCar/MapsCollection.cs index 0bd5205..19195c8 100644 --- a/ArmoredCar/ArmoredCar/MapsCollection.cs +++ b/ArmoredCar/ArmoredCar/MapsCollection.cs @@ -89,13 +89,14 @@ namespace ArmoredCar { File.Delete(filename); } - using (StreamWriter fs = new(filename)) + using (FileStream f = new FileStream(filename, FileMode.Create)) + using (StreamWriter sw = new StreamWriter(f, Encoding.UTF8)) { - fs.WriteLine("MapsCollection"); + sw.WriteLine("MapsCollection"); foreach (var storage in _mapStorages) { - fs.WriteLine($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}"); + sw.WriteLine($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}"); } } return true; @@ -110,15 +111,16 @@ namespace ArmoredCar if (!File.Exists(filename)) { return false; - } - using (StreamReader fs = new(filename)) + } + using (FileStream f = new(filename, FileMode.Open)) + using (StreamReader sw = new(f, Encoding.UTF8)) { - if (!fs.ReadLine().Contains("MapsCollection")) + if (!sw.ReadLine().Contains("MapsCollection")) return false; string? line; _mapStorages.Clear(); - while ((line = fs.ReadLine()) != null) + while ((line = sw.ReadLine()) != null) { var elem = line.Split(separatorDict); AbstractMap map = null; -- 2.25.1