6 лаба готова

This commit is contained in:
Казначеева Елизавета 2023-11-21 23:25:22 +04:00
parent 091db401f9
commit 8b4ed85f88
4 changed files with 18 additions and 16 deletions

View File

@ -244,8 +244,8 @@
private MenuStrip menuStrip1; private MenuStrip menuStrip1;
private ToolStripMenuItem файлToolStripMenuItem; private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem сохранениеToolStripMenuItem; private ToolStripMenuItem сохранениеToolStripMenuItem;
private ToolStripMenuItem загрузкаToolStripMenuItem;
private OpenFileDialog openFileDialog; private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog; private SaveFileDialog saveFileDialog;
private ToolStripMenuItem загрузкаToolStripMenuItem;
} }
} }

View File

@ -59,17 +59,18 @@ namespace Battleship
{ {
if (_storage.LoadData(openFileDialog.FileName)) if (_storage.LoadData(openFileDialog.FileName))
{ {
MessageBox.Show("Сохранение прошло успешно", MessageBox.Show("Загрузка прошла успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
ReloadObjects();
} }
else else
{ {
MessageBox.Show("Не загрузилось", "Результат", MessageBox.Show("Не загрузилось", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
ReloadObjects();
} }
private void ReloadObjects() private void ReloadObjects()
{ {
int index = listBoxStorages.SelectedIndex; int index = listBoxStorages.SelectedIndex;

View File

@ -119,7 +119,7 @@
// //
// panelPurple // 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.Location = new System.Drawing.Point(187, 78);
this.panelPurple.Name = "panelPurple"; this.panelPurple.Name = "panelPurple";
this.panelPurple.Size = new System.Drawing.Size(50, 50); this.panelPurple.Size = new System.Drawing.Size(50, 50);
@ -164,7 +164,7 @@
// //
// panelBlue // 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.Location = new System.Drawing.Point(131, 22);
this.panelBlue.Name = "panelBlue"; this.panelBlue.Name = "panelBlue";
this.panelBlue.Size = new System.Drawing.Size(50, 50); this.panelBlue.Size = new System.Drawing.Size(50, 50);
@ -173,7 +173,7 @@
// //
// panelGreen // 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.Location = new System.Drawing.Point(75, 22);
this.panelGreen.Name = "panelGreen"; this.panelGreen.Name = "panelGreen";
this.panelGreen.Size = new System.Drawing.Size(50, 50); this.panelGreen.Size = new System.Drawing.Size(50, 50);
@ -182,7 +182,7 @@
// //
// panelRed // 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.Location = new System.Drawing.Point(19, 22);
this.panelRed.Name = "panelRed"; this.panelRed.Name = "panelRed";
this.panelRed.Size = new System.Drawing.Size(50, 50); this.panelRed.Size = new System.Drawing.Size(50, 50);

View File

@ -100,6 +100,7 @@ namespace Battleship.Generics
/// </summary> /// </summary>
/// <param name="filename">Путь и имя файла</param> /// <param name="filename">Путь и имя файла</param>
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns> /// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
public bool LoadData(string filename) public bool LoadData(string filename)
{ {
if (!File.Exists(filename)) if (!File.Exists(filename))
@ -116,12 +117,13 @@ namespace Battleship.Generics
bufferTextFromFile += temp.GetString(b); 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) if (strs == null || strs.Length == 0)
{ {
return false; return false;
} }
if (!strs[0].StartsWith("ShipsStorage")) if (!strs[0].StartsWith("ShipStorage"))
{ {
//если нет такой записи, то это не те данные //если нет такой записи, то это не те данные
return false; return false;
@ -129,19 +131,18 @@ namespace Battleship.Generics
_shipStorages.Clear(); _shipStorages.Clear();
foreach (string data in strs) foreach (string data in strs)
{ {
string[] record = data.Split(_separatorForKeyValue, string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2) if (record.Length != 2)
{ {
continue; continue;
} }
ShipGenericCollection<DrawningShip, DrawningObjectShip> collection = new(_pictureWidth, _pictureHeight); ShipGenericCollection<DrawningShip, DrawningObjectShip>
collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, string[] set = record[1].Split(_separatorRecords,
StringSplitOptions.RemoveEmptyEntries); StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set) foreach (string elem in set)
{ {
DrawningShip? ship = DrawningShip? ship = elem?.CreateDrawningShip(_separatorForObject, _pictureWidth, _pictureHeight);
elem?.CreateDrawningShip(_separatorForObject, _pictureWidth, _pictureHeight);
if (ship != null) if (ship != null)
{ {
if (!(collection + ship)) if (!(collection + ship))