From 091db401f93b3600ef6e11c392f966c1f5f0efa0 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Tue, 21 Nov 2023 16:56:40 +0400 Subject: [PATCH 1/7] =?UTF-8?q?=D0=BF=D0=BE=D1=87=D1=82=D0=B8=20=D0=B3?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Battleship/ExtentionDrawningShip.cs | 65 ++++++++++ .../Battleship/FormShipCollection.Designer.cs | 86 +++++++++++-- Battleship/Battleship/FormShipCollection.cs | 43 +++++++ Battleship/Battleship/FormShipCollection.resx | 9 ++ .../Battleship/ShipGenericCollection.cs | 4 + Battleship/Battleship/ShipsGenericStorage.cs | 121 ++++++++++++++++-- 6 files changed, 305 insertions(+), 23 deletions(-) create mode 100644 Battleship/Battleship/ExtentionDrawningShip.cs diff --git a/Battleship/Battleship/ExtentionDrawningShip.cs b/Battleship/Battleship/ExtentionDrawningShip.cs new file mode 100644 index 0000000..cb9f40d --- /dev/null +++ b/Battleship/Battleship/ExtentionDrawningShip.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Battleship.Entities; + +namespace Battleship.DrawningObjects +{ + public static class ExtentionDrawningShip + { + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Разделитель даннных + /// Ширина + /// Высота + /// Объект + public static DrawningShip? CreateDrawningShip(this string info, char + separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawningShip(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height); + } + if (strs.Length == 6) + { + return new DrawningBattleship(Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), + Color.FromName(strs[2]), + Color.FromName(strs[3]), + Convert.ToBoolean(strs[4]), + Convert.ToBoolean(strs[5]), width, height); + } + return null; + } + /// + /// Получение данных для сохранения в файл + /// + /// Сохраняемый объект + /// Разделитель даннных + /// Строка с данными по объекту + public static string GetDataForSave(this DrawningShip drawningShip, + char separatorForObject) + { + var ship = drawningShip.EntityShip; + if (ship == null) + { + return string.Empty; + } + var str = $"{ship.Speed}{separatorForObject}{ship.Weight}{separatorForObject}{ship.BodyColor.Name}"; + if (ship is not EntityBattleship battleship) + { + return str; + } + return $"{str}{separatorForObject}{battleship.AdditionalColor.Name}" + + $"{separatorForObject}{battleship.Tower}{separatorForObject}{battleship.Section}"; + } + } +} + + diff --git a/Battleship/Battleship/FormShipCollection.Designer.cs b/Battleship/Battleship/FormShipCollection.Designer.cs index a597aed..8de68bf 100644 --- a/Battleship/Battleship/FormShipCollection.Designer.cs +++ b/Battleship/Battleship/FormShipCollection.Designer.cs @@ -38,9 +38,16 @@ this.buttonRefreshCollection = new System.Windows.Forms.Button(); this.buttonRemoveShip = new System.Windows.Forms.Button(); this.buttonAddShip = new System.Windows.Forms.Button(); + this.menuStrip1 = new System.Windows.Forms.MenuStrip(); + this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.сохранениеToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.загрузкаToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.groupBoxBattleShip.SuspendLayout(); this.groupBox1.SuspendLayout(); + this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.SuspendLayout(); // @@ -53,6 +60,7 @@ this.groupBoxBattleShip.Controls.Add(this.buttonRefreshCollection); this.groupBoxBattleShip.Controls.Add(this.buttonRemoveShip); this.groupBoxBattleShip.Controls.Add(this.buttonAddShip); + this.groupBoxBattleShip.Controls.Add(this.menuStrip1); this.groupBoxBattleShip.Location = new System.Drawing.Point(618, 1); this.groupBoxBattleShip.Name = "groupBoxBattleShip"; this.groupBoxBattleShip.Size = new System.Drawing.Size(183, 450); @@ -66,16 +74,16 @@ this.groupBox1.Controls.Add(this.listBoxStorages); this.groupBox1.Controls.Add(this.buttonAddObject); this.groupBox1.Controls.Add(this.buttonDelObject); - this.groupBox1.Location = new System.Drawing.Point(6, 22); + this.groupBox1.Location = new System.Drawing.Point(6, 61); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(171, 253); + this.groupBox1.Size = new System.Drawing.Size(174, 223); this.groupBox1.TabIndex = 1; this.groupBox1.TabStop = false; this.groupBox1.Text = "Набор"; // // textBoxStorageName // - this.textBoxStorageName.Location = new System.Drawing.Point(17, 40); + this.textBoxStorageName.Location = new System.Drawing.Point(17, 22); this.textBoxStorageName.Name = "textBoxStorageName"; this.textBoxStorageName.Size = new System.Drawing.Size(147, 23); this.textBoxStorageName.TabIndex = 4; @@ -84,15 +92,15 @@ // this.listBoxStorages.FormattingEnabled = true; this.listBoxStorages.ItemHeight = 15; - this.listBoxStorages.Location = new System.Drawing.Point(17, 124); + this.listBoxStorages.Location = new System.Drawing.Point(18, 95); this.listBoxStorages.Name = "listBoxStorages"; - this.listBoxStorages.Size = new System.Drawing.Size(147, 94); + this.listBoxStorages.Size = new System.Drawing.Size(147, 79); this.listBoxStorages.TabIndex = 3; this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged); // // buttonAddObject // - this.buttonAddObject.Location = new System.Drawing.Point(18, 81); + this.buttonAddObject.Location = new System.Drawing.Point(17, 51); this.buttonAddObject.Name = "buttonAddObject"; this.buttonAddObject.Size = new System.Drawing.Size(147, 23); this.buttonAddObject.TabIndex = 2; @@ -102,7 +110,7 @@ // // buttonDelObject // - this.buttonDelObject.Location = new System.Drawing.Point(17, 224); + this.buttonDelObject.Location = new System.Drawing.Point(18, 181); this.buttonDelObject.Name = "buttonDelObject"; this.buttonDelObject.Size = new System.Drawing.Size(147, 23); this.buttonDelObject.TabIndex = 1; @@ -112,16 +120,16 @@ // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(23, 330); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(24, 335); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(147, 23); this.maskedTextBoxNumber.TabIndex = 3; // // buttonRefreshCollection // - this.buttonRefreshCollection.Location = new System.Drawing.Point(23, 411); + this.buttonRefreshCollection.Location = new System.Drawing.Point(23, 406); this.buttonRefreshCollection.Name = "buttonRefreshCollection"; - this.buttonRefreshCollection.Size = new System.Drawing.Size(147, 33); + this.buttonRefreshCollection.Size = new System.Drawing.Size(147, 31); this.buttonRefreshCollection.TabIndex = 2; this.buttonRefreshCollection.Text = "Обновить коллекцию"; this.buttonRefreshCollection.UseVisualStyleBackColor = true; @@ -129,9 +137,9 @@ // // buttonRemoveShip // - this.buttonRemoveShip.Location = new System.Drawing.Point(23, 359); + this.buttonRemoveShip.Location = new System.Drawing.Point(23, 364); this.buttonRemoveShip.Name = "buttonRemoveShip"; - this.buttonRemoveShip.Size = new System.Drawing.Size(147, 33); + this.buttonRemoveShip.Size = new System.Drawing.Size(147, 36); this.buttonRemoveShip.TabIndex = 1; this.buttonRemoveShip.Text = "Удалить корабль"; this.buttonRemoveShip.UseVisualStyleBackColor = true; @@ -139,14 +147,47 @@ // // buttonAddShip // - this.buttonAddShip.Location = new System.Drawing.Point(23, 281); + this.buttonAddShip.Location = new System.Drawing.Point(23, 290); this.buttonAddShip.Name = "buttonAddShip"; - this.buttonAddShip.Size = new System.Drawing.Size(147, 33); + this.buttonAddShip.Size = new System.Drawing.Size(147, 30); this.buttonAddShip.TabIndex = 0; this.buttonAddShip.Text = "Добавить корабль"; this.buttonAddShip.UseVisualStyleBackColor = true; this.buttonAddShip.Click += new System.EventHandler(this.buttonAddShip_Click); // + // menuStrip1 + // + this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.файлToolStripMenuItem}); + this.menuStrip1.Location = new System.Drawing.Point(3, 19); + this.menuStrip1.Name = "menuStrip1"; + this.menuStrip1.Size = new System.Drawing.Size(177, 24); + this.menuStrip1.TabIndex = 4; + this.menuStrip1.Text = "menuStrip1"; + // + // файлToolStripMenuItem + // + this.файлToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.сохранениеToolStripMenuItem, + this.загрузкаToolStripMenuItem}); + this.файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + this.файлToolStripMenuItem.Size = new System.Drawing.Size(48, 20); + this.файлToolStripMenuItem.Text = "Файл"; + // + // сохранениеToolStripMenuItem + // + this.сохранениеToolStripMenuItem.Name = "сохранениеToolStripMenuItem"; + this.сохранениеToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.сохранениеToolStripMenuItem.Text = "Сохранение"; + this.сохранениеToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); + // + // загрузкаToolStripMenuItem + // + this.загрузкаToolStripMenuItem.Name = "загрузкаToolStripMenuItem"; + this.загрузкаToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.загрузкаToolStripMenuItem.Text = "Загрузка"; + this.загрузкаToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); + // // pictureBoxCollection // this.pictureBoxCollection.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) @@ -158,6 +199,14 @@ this.pictureBoxCollection.TabIndex = 0; this.pictureBoxCollection.TabStop = false; // + // openFileDialog + // + this.openFileDialog.Filter = "txt file | *.txt"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // // FormShipCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -165,12 +214,15 @@ this.ClientSize = new System.Drawing.Size(800, 450); this.Controls.Add(this.pictureBoxCollection); this.Controls.Add(this.groupBoxBattleShip); + this.MainMenuStrip = this.menuStrip1; this.Name = "FormShipCollection"; this.Text = "FormShipCollection"; this.groupBoxBattleShip.ResumeLayout(false); this.groupBoxBattleShip.PerformLayout(); this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); this.ResumeLayout(false); @@ -189,5 +241,11 @@ private ListBox listBoxStorages; private Button buttonAddObject; private Button buttonDelObject; + private MenuStrip menuStrip1; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem сохранениеToolStripMenuItem; + private ToolStripMenuItem загрузкаToolStripMenuItem; + private OpenFileDialog openFileDialog; + private SaveFileDialog saveFileDialog; } } \ No newline at end of file diff --git a/Battleship/Battleship/FormShipCollection.cs b/Battleship/Battleship/FormShipCollection.cs index a047311..a73971a 100644 --- a/Battleship/Battleship/FormShipCollection.cs +++ b/Battleship/Battleship/FormShipCollection.cs @@ -27,6 +27,49 @@ namespace Battleship InitializeComponent(); _storage = new ShipsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); } + /// + /// Обработка нажатия "Сохранение" + /// + /// + /// + private void SaveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storage.SaveData(saveFileDialog.FileName)) + { + MessageBox.Show("Сохранение прошло успешно", + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не сохранилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + /// + /// Обработка нажатия "Загрузка" + /// + /// + /// + private void LoadToolStripMenuItem_Click(object sender, EventArgs e) + { + if(openFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storage.LoadData(openFileDialog.FileName)) + { + MessageBox.Show("Сохранение прошло успешно", + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + ReloadObjects(); + } + else + { + MessageBox.Show("Не загрузилось", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } private void ReloadObjects() { int index = listBoxStorages.SelectedIndex; diff --git a/Battleship/Battleship/FormShipCollection.resx b/Battleship/Battleship/FormShipCollection.resx index f298a7b..ddcfdb1 100644 --- a/Battleship/Battleship/FormShipCollection.resx +++ b/Battleship/Battleship/FormShipCollection.resx @@ -57,4 +57,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 132, 17 + + + 272, 17 + \ No newline at end of file diff --git a/Battleship/Battleship/ShipGenericCollection.cs b/Battleship/Battleship/ShipGenericCollection.cs index 86d4fb8..a95b907 100644 --- a/Battleship/Battleship/ShipGenericCollection.cs +++ b/Battleship/Battleship/ShipGenericCollection.cs @@ -17,6 +17,10 @@ namespace Battleship.Generics private readonly int _placeSizeWidth = 185; private readonly int _placeSizeHeight = 90; private readonly SetGeneric _collection; + /// + /// Получение объектов коллекции + /// + public IEnumerable GetShips => _collection.GetShips(); public ShipGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; diff --git a/Battleship/Battleship/ShipsGenericStorage.cs b/Battleship/Battleship/ShipsGenericStorage.cs index 6260ff3..06d5cbf 100644 --- a/Battleship/Battleship/ShipsGenericStorage.cs +++ b/Battleship/Battleship/ShipsGenericStorage.cs @@ -12,13 +12,25 @@ namespace Battleship.Generics { readonly Dictionary> _shipStorages; - + public List Keys => _shipStorages.Keys.ToList(); - + private readonly int _pictureWidth; - + private readonly int _pictureHeight; - + /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private static readonly char _separatorForKeyValue = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char _separatorRecords = ';'; + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + public ShipsGenericStorage(int pictureWidth, int pictureHeight) { _shipStorages = new Dictionary(_pictureWidth,_pictureHeight); + _shipStorages[name] = new ShipGenericCollection(_pictureWidth, _pictureHeight); } - + public void DelSet(string name) { if (!_shipStorages.ContainsKey(name)) return; _shipStorages.Remove(name); } - + public ShipGenericCollection? this[string ind] { get { - if(_shipStorages.ContainsKey(ind)) + if (_shipStorages.ContainsKey(ind)) return _shipStorages[ind]; return null; } } + /// + /// Сохранение информации по автомобилям в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _shipStorages) + { + StringBuilder records = new(); + foreach (DrawningShip? elem in record.Value.GetShips) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + using FileStream fs = new(filename, FileMode.Create); + byte[] info = new + UTF8Encoding(true).GetBytes($"ShipStorage{Environment.NewLine}{data}"); + fs.Write(info, 0, info.Length); + return true; + } + /// + /// Загрузка информации по кораблям в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных + 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 == null || strs.Length == 0) + { + return false; + } + if (!strs[0].StartsWith("ShipsStorage")) + { + //если нет такой записи, то это не те данные + return false; + } + _shipStorages.Clear(); + foreach (string data in strs) + { + string[] record = data.Split(_separatorForKeyValue, + StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + ShipGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, + StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawningShip? ship = + elem?.CreateDrawningShip(_separatorForObject, _pictureWidth, _pictureHeight); + if (ship != null) + { + if (!(collection + ship)) + { + return false; + } + } + } + _shipStorages.Add(record[0], collection); + } + return true; + } } } -- 2.25.1 From 8b4ed85f882d555c5a23a0288b201a1dde256bc3 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Tue, 21 Nov 2023 23:25:22 +0400 Subject: [PATCH 2/7] =?UTF-8?q?6=20=D0=BB=D0=B0=D0=B1=D0=B0=20=D0=B3=D0=BE?= =?UTF-8?q?=D1=82=D0=BE=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Battleship/FormShipCollection.Designer.cs | 2 +- Battleship/Battleship/FormShipCollection.cs | 9 +++++---- Battleship/Battleship/FormShipConfig.Designer.cs | 8 ++++---- Battleship/Battleship/ShipsGenericStorage.cs | 15 ++++++++------- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Battleship/Battleship/FormShipCollection.Designer.cs b/Battleship/Battleship/FormShipCollection.Designer.cs index 8de68bf..cae2aff 100644 --- a/Battleship/Battleship/FormShipCollection.Designer.cs +++ b/Battleship/Battleship/FormShipCollection.Designer.cs @@ -244,8 +244,8 @@ private MenuStrip menuStrip1; private ToolStripMenuItem файлToolStripMenuItem; private ToolStripMenuItem сохранениеToolStripMenuItem; - private ToolStripMenuItem загрузкаToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private ToolStripMenuItem загрузкаToolStripMenuItem; } } \ No newline at end of file diff --git a/Battleship/Battleship/FormShipCollection.cs b/Battleship/Battleship/FormShipCollection.cs index a73971a..d7a3d54 100644 --- a/Battleship/Battleship/FormShipCollection.cs +++ b/Battleship/Battleship/FormShipCollection.cs @@ -59,17 +59,18 @@ namespace Battleship { if (_storage.LoadData(openFileDialog.FileName)) { - MessageBox.Show("Сохранение прошло успешно", - "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); - ReloadObjects(); + MessageBox.Show("Загрузка прошла успешно", + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Не загрузилось", "Результат", - MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBoxButtons.OK, MessageBoxIcon.Error); } } + ReloadObjects(); } + private void ReloadObjects() { int index = listBoxStorages.SelectedIndex; diff --git a/Battleship/Battleship/FormShipConfig.Designer.cs b/Battleship/Battleship/FormShipConfig.Designer.cs index 3c5dee8..6cd4303 100644 --- a/Battleship/Battleship/FormShipConfig.Designer.cs +++ b/Battleship/Battleship/FormShipConfig.Designer.cs @@ -119,7 +119,7 @@ // // panelPurple // - this.panelPurple.BackColor = System.Drawing.Color.Purple; + this.panelPurple.BackColor = System.Drawing.Color.Plum; this.panelPurple.Location = new System.Drawing.Point(187, 78); this.panelPurple.Name = "panelPurple"; this.panelPurple.Size = new System.Drawing.Size(50, 50); @@ -164,7 +164,7 @@ // // panelBlue // - this.panelBlue.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192))))); + this.panelBlue.BackColor = System.Drawing.Color.SlateBlue; this.panelBlue.Location = new System.Drawing.Point(131, 22); this.panelBlue.Name = "panelBlue"; this.panelBlue.Size = new System.Drawing.Size(50, 50); @@ -173,7 +173,7 @@ // // panelGreen // - this.panelGreen.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(128))))); + this.panelGreen.BackColor = System.Drawing.Color.OliveDrab; this.panelGreen.Location = new System.Drawing.Point(75, 22); this.panelGreen.Name = "panelGreen"; this.panelGreen.Size = new System.Drawing.Size(50, 50); @@ -182,7 +182,7 @@ // // panelRed // - this.panelRed.BackColor = System.Drawing.Color.Red; + this.panelRed.BackColor = System.Drawing.Color.Maroon; this.panelRed.Location = new System.Drawing.Point(19, 22); this.panelRed.Name = "panelRed"; this.panelRed.Size = new System.Drawing.Size(50, 50); diff --git a/Battleship/Battleship/ShipsGenericStorage.cs b/Battleship/Battleship/ShipsGenericStorage.cs index 06d5cbf..34b004b 100644 --- a/Battleship/Battleship/ShipsGenericStorage.cs +++ b/Battleship/Battleship/ShipsGenericStorage.cs @@ -100,6 +100,7 @@ namespace Battleship.Generics /// /// Путь и имя файла /// true - загрузка прошла успешно, false - ошибка при загрузке данных + public bool LoadData(string filename) { if (!File.Exists(filename)) @@ -116,12 +117,13 @@ namespace Battleship.Generics bufferTextFromFile += temp.GetString(b); } } - var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); + var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, + StringSplitOptions.RemoveEmptyEntries); if (strs == null || strs.Length == 0) { return false; } - if (!strs[0].StartsWith("ShipsStorage")) + if (!strs[0].StartsWith("ShipStorage")) { //если нет такой записи, то это не те данные return false; @@ -129,19 +131,18 @@ namespace Battleship.Generics _shipStorages.Clear(); foreach (string data in strs) { - string[] record = data.Split(_separatorForKeyValue, - StringSplitOptions.RemoveEmptyEntries); + string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); if (record.Length != 2) { continue; } - ShipGenericCollection collection = new(_pictureWidth, _pictureHeight); + ShipGenericCollection + collection = new(_pictureWidth, _pictureHeight); string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { - DrawningShip? ship = - elem?.CreateDrawningShip(_separatorForObject, _pictureWidth, _pictureHeight); + DrawningShip? ship = elem?.CreateDrawningShip(_separatorForObject, _pictureWidth, _pictureHeight); if (ship != null) { if (!(collection + ship)) -- 2.25.1 From f9fa6794b5ca0285b1a566af5771a3c6dbb1e371 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Wed, 22 Nov 2023 11:11:29 +0400 Subject: [PATCH 3/7] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F?= =?UTF-8?q?=20=D1=82=D0=BE=D1=87=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Battleship/Battleship/ShipsGenericStorage.cs | 79 ++++++++++---------- 1 file changed, 38 insertions(+), 41 deletions(-) diff --git a/Battleship/Battleship/ShipsGenericStorage.cs b/Battleship/Battleship/ShipsGenericStorage.cs index 34b004b..63b6d35 100644 --- a/Battleship/Battleship/ShipsGenericStorage.cs +++ b/Battleship/Battleship/ShipsGenericStorage.cs @@ -75,8 +75,7 @@ namespace Battleship.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _shipStorages) + foreach (KeyValuePair> record in _shipStorages) { StringBuilder records = new(); foreach (DrawningShip? elem in record.Value.GetShips) @@ -89,10 +88,10 @@ namespace Battleship.Generics { return false; } - using FileStream fs = new(filename, FileMode.Create); - byte[] info = new - UTF8Encoding(true).GetBytes($"ShipStorage{Environment.NewLine}{data}"); - fs.Write(info, 0, info.Length); + using (StreamWriter writer = new StreamWriter(filename)) + { + writer.Write($"ShipStorage{Environment.NewLine}{data}"); + } return true; } /// @@ -107,53 +106,51 @@ namespace Battleship.Generics { return false; } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + + using (StreamReader fs = File.OpenText(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) + string str = fs.ReadLine(); + if (str == null || str.Length == 0) { - bufferTextFromFile += temp.GetString(b); + return false; } - } - var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, - StringSplitOptions.RemoveEmptyEntries); - if (strs == null || strs.Length == 0) - { - return false; - } - if (!strs[0].StartsWith("ShipStorage")) - { - //если нет такой записи, то это не те данные - return false; - } - _shipStorages.Clear(); - foreach (string data in strs) - { - string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 2) + if (!str.StartsWith("ShipStorage")) { - continue; + return false; } - ShipGenericCollection - collection = new(_pictureWidth, _pictureHeight); - string[] set = record[1].Split(_separatorRecords, - StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set) + + _shipStorages.Clear(); + string strs = ""; + + while ((strs = fs.ReadLine()) != null) { - DrawningShip? ship = elem?.CreateDrawningShip(_separatorForObject, _pictureWidth, _pictureHeight); - if (ship != null) + if (strs == null) { - if (!(collection + ship)) + return false; + } + + string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + ShipGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawningShip? plane = elem?.CreateDrawningShip(_separatorForObject, _pictureWidth, _pictureHeight); + if (plane != null) { - return false; + if (!(collection + plane)) + { + return false; + } } } + _shipStorages.Add(record[0], collection); } - _shipStorages.Add(record[0], collection); + return true; } - return true; } } } -- 2.25.1 From 1c81aec3af02b85a1dac9ed5abd2603e123c1e64 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Tue, 5 Dec 2023 21:52:05 +0400 Subject: [PATCH 4/7] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Battleship/Battleship/DrawningBattleship.cs | 4 ++++ Battleship/Battleship/DrawningShip.cs | 8 ++++++++ Battleship/Battleship/EntityBattleship.cs | 6 +++--- Battleship/Battleship/EntityShip.cs | 2 +- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Battleship/Battleship/DrawningBattleship.cs b/Battleship/Battleship/DrawningBattleship.cs index c0c8d7a..524c6ba 100644 --- a/Battleship/Battleship/DrawningBattleship.cs +++ b/Battleship/Battleship/DrawningBattleship.cs @@ -48,5 +48,9 @@ namespace Battleship.DrawningObjects g.DrawRectangle(pen, _startPosX + 75, _startPosY + 70, 40, 10); } } + public void SetAddColor(Color color) + { + ((EntityBattleship)EntityShip).AdditionalColor = color; + } } } diff --git a/Battleship/Battleship/DrawningShip.cs b/Battleship/Battleship/DrawningShip.cs index 6a56b76..a2153b7 100644 --- a/Battleship/Battleship/DrawningShip.cs +++ b/Battleship/Battleship/DrawningShip.cs @@ -224,6 +224,14 @@ namespace Battleship.DrawningObjects break; } } + public void SetColor(Color color) + { + if (EntityShip == null) + { + return; + } + EntityShip.BodyColor = color; + } } } diff --git a/Battleship/Battleship/EntityBattleship.cs b/Battleship/Battleship/EntityBattleship.cs index f3cd02c..449953c 100644 --- a/Battleship/Battleship/EntityBattleship.cs +++ b/Battleship/Battleship/EntityBattleship.cs @@ -10,7 +10,7 @@ namespace Battleship.Entities internal class EntityBattleship: EntityShip { - public Color AdditionalColor { get; private set; } + public Color AdditionalColor { get; set; } /// /// Признак (опция) наличия башни /// @@ -39,10 +39,10 @@ namespace Battleship.Entities Tower = tower; Section = section; } - - internal void setAdditionalColor(Color color) + public void setAdditionalColor(Color color) { AdditionalColor = color; } + } } diff --git a/Battleship/Battleship/EntityShip.cs b/Battleship/Battleship/EntityShip.cs index db19207..e46dcc4 100644 --- a/Battleship/Battleship/EntityShip.cs +++ b/Battleship/Battleship/EntityShip.cs @@ -19,7 +19,7 @@ namespace Battleship.Entities /// /// Основной цвет /// - public Color BodyColor { get; private set; } + public Color BodyColor { get; set; } /// /// Дополнительный цвет (для опциональных элементов) /// -- 2.25.1 From 3afc6e4ab68ed9dea9e4b625df241d2427260cd2 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Wed, 6 Dec 2023 10:07:54 +0400 Subject: [PATCH 5/7] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Battleship/Battleship/DrawningBattleship.cs | 4 ---- Battleship/Battleship/DrawningShip.cs | 8 -------- Battleship/Battleship/EntityShip.cs | 2 +- 3 files changed, 1 insertion(+), 13 deletions(-) diff --git a/Battleship/Battleship/DrawningBattleship.cs b/Battleship/Battleship/DrawningBattleship.cs index 524c6ba..c0c8d7a 100644 --- a/Battleship/Battleship/DrawningBattleship.cs +++ b/Battleship/Battleship/DrawningBattleship.cs @@ -48,9 +48,5 @@ namespace Battleship.DrawningObjects g.DrawRectangle(pen, _startPosX + 75, _startPosY + 70, 40, 10); } } - public void SetAddColor(Color color) - { - ((EntityBattleship)EntityShip).AdditionalColor = color; - } } } diff --git a/Battleship/Battleship/DrawningShip.cs b/Battleship/Battleship/DrawningShip.cs index a2153b7..6a56b76 100644 --- a/Battleship/Battleship/DrawningShip.cs +++ b/Battleship/Battleship/DrawningShip.cs @@ -224,14 +224,6 @@ namespace Battleship.DrawningObjects break; } } - public void SetColor(Color color) - { - if (EntityShip == null) - { - return; - } - EntityShip.BodyColor = color; - } } } diff --git a/Battleship/Battleship/EntityShip.cs b/Battleship/Battleship/EntityShip.cs index e46dcc4..ea7eef1 100644 --- a/Battleship/Battleship/EntityShip.cs +++ b/Battleship/Battleship/EntityShip.cs @@ -38,7 +38,7 @@ namespace Battleship.Entities BodyColor = bodyColor; } - internal void setBodyColor(Color color) + public void setBodyColor(Color color) { BodyColor = color; } -- 2.25.1 From a55aef24c00f8a156b6f70aa038509535d3e4ed9 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Wed, 6 Dec 2023 10:32:27 +0400 Subject: [PATCH 6/7] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Battleship/Battleship/EntityBattleship.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Battleship/Battleship/EntityBattleship.cs b/Battleship/Battleship/EntityBattleship.cs index 449953c..6175764 100644 --- a/Battleship/Battleship/EntityBattleship.cs +++ b/Battleship/Battleship/EntityBattleship.cs @@ -39,10 +39,10 @@ namespace Battleship.Entities Tower = tower; Section = section; } + public void setAdditionalColor(Color color) { AdditionalColor = color; } - } } -- 2.25.1 From 54610933191886a0d37bcfb469ca1f2cfc699692 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Wed, 6 Dec 2023 10:41:12 +0400 Subject: [PATCH 7/7] =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=B8=D1=82=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D0=B0=206?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Battleship/Battleship/EntityBattleship.cs | 2 +- Battleship/Battleship/EntityShip.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Battleship/Battleship/EntityBattleship.cs b/Battleship/Battleship/EntityBattleship.cs index 6175764..1d144ac 100644 --- a/Battleship/Battleship/EntityBattleship.cs +++ b/Battleship/Battleship/EntityBattleship.cs @@ -10,7 +10,7 @@ namespace Battleship.Entities internal class EntityBattleship: EntityShip { - public Color AdditionalColor { get; set; } + public Color AdditionalColor { get; private set; } /// /// Признак (опция) наличия башни /// diff --git a/Battleship/Battleship/EntityShip.cs b/Battleship/Battleship/EntityShip.cs index ea7eef1..889491c 100644 --- a/Battleship/Battleship/EntityShip.cs +++ b/Battleship/Battleship/EntityShip.cs @@ -19,7 +19,7 @@ namespace Battleship.Entities /// /// Основной цвет /// - public Color BodyColor { get; set; } + public Color BodyColor { get; private set; } /// /// Дополнительный цвет (для опциональных элементов) /// -- 2.25.1