готовая точно

This commit is contained in:
Казначеева Елизавета 2023-11-22 11:11:29 +04:00
parent 8b4ed85f88
commit f9fa6794b5

View File

@ -75,8 +75,7 @@ namespace Battleship.Generics
File.Delete(filename); File.Delete(filename);
} }
StringBuilder data = new(); StringBuilder data = new();
foreach (KeyValuePair<string, foreach (KeyValuePair<string, ShipGenericCollection<DrawningShip, DrawningObjectShip>> record in _shipStorages)
ShipGenericCollection<DrawningShip, DrawningObjectShip>> record in _shipStorages)
{ {
StringBuilder records = new(); StringBuilder records = new();
foreach (DrawningShip? elem in record.Value.GetShips) foreach (DrawningShip? elem in record.Value.GetShips)
@ -89,10 +88,10 @@ namespace Battleship.Generics
{ {
return false; return false;
} }
using FileStream fs = new(filename, FileMode.Create); using (StreamWriter writer = new StreamWriter(filename))
byte[] info = new {
UTF8Encoding(true).GetBytes($"ShipStorage{Environment.NewLine}{data}"); writer.Write($"ShipStorage{Environment.NewLine}{data}");
fs.Write(info, 0, info.Length); }
return true; return true;
} }
/// <summary> /// <summary>
@ -107,45 +106,42 @@ namespace Battleship.Generics
{ {
return false; return false;
} }
string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open)) using (StreamReader fs = File.OpenText(filename))
{ {
byte[] b = new byte[fs.Length]; string str = fs.ReadLine();
UTF8Encoding temp = new(true); if (str == null || str.Length == 0)
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; return false;
} }
if (!strs[0].StartsWith("ShipStorage")) if (!str.StartsWith("ShipStorage"))
{ {
//если нет такой записи, то это не те данные
return false; return false;
} }
_shipStorages.Clear(); _shipStorages.Clear();
foreach (string data in strs) string strs = "";
while ((strs = fs.ReadLine()) != null)
{ {
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); if (strs == null)
{
return false;
}
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2) if (record.Length != 2)
{ {
continue; continue;
} }
ShipGenericCollection<DrawningShip, DrawningObjectShip> ShipGenericCollection<DrawningShip, DrawningObjectShip> collection = new(_pictureWidth, _pictureHeight);
collection = new(_pictureWidth, _pictureHeight); string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
string[] set = record[1].Split(_separatorRecords,
StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set) foreach (string elem in set)
{ {
DrawningShip? ship = elem?.CreateDrawningShip(_separatorForObject, _pictureWidth, _pictureHeight); DrawningShip? plane = elem?.CreateDrawningShip(_separatorForObject, _pictureWidth, _pictureHeight);
if (ship != null) if (plane != null)
{ {
if (!(collection + ship)) if (!(collection + plane))
{ {
return false; return false;
} }
@ -156,5 +152,6 @@ namespace Battleship.Generics
return true; return true;
} }
} }
}
} }