//Save current game, player and score in a file public static void SaveGame(string filePath, PlayField playField, Player player) { try { StreamWriter writer = new StreamWriter(filePath); using (writer) { writer.WriteLine(player.Name); writer.WriteLine(player.Score); for (int i = 0; i < playField.GetLength(0); i++) { for (int j = 0; j < playField.GetLength(1); j++) { writer.Write(playField[i, j].X + " "); writer.Write(playField[i, j].Y + " "); writer.Write(playField[i, j].Symbol + " "); writer.Write(playField[i, j].Color + " "); } writer.WriteLine(); } } } catch (Exception) { Console.Clear(); throw; } }
//Load the last saved game, player and score from a file public static void LoadGame(string filePath, PlayField playField, Player player) { try { StreamReader reader = new StreamReader(filePath); using (reader) { player.Name = reader.ReadLine(); player.Score = int.Parse(reader.ReadLine()); for (int i = 0; i < playField.GetLength(0); i++) { string line = reader.ReadLine(); string[] currentLine = line.Trim().Split(' '); int counter = 0; for (int j = 0; j < currentLine.Length; j += 4) { int x = int.Parse(currentLine[j]); int y = int.Parse(currentLine[j + 1]); char symbol = char.Parse(currentLine[j + 2]); ConsoleColor color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), currentLine[j + 3]); Box box = new Box(x, y, symbol, color); box.InitBox(symbol); playField[i, counter] = box; counter++; } } } } catch (Exception) { Console.Clear(); throw; } }