//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; } }
//Initialize the playfield with lewels with random color public void InitPlayField(char symbol) { Random randColor = new Random(); for (int i = 0; i < boxes.GetLength(0); i++) { for (int j = 0; j < boxes.GetLength(1); j++) { Box box = new Box(i * 4 + 1, j * 4 + 1, symbol, colors[randColor.Next(0, colors.Length)]); box.InitBox(symbol); boxes[i, j] = box; } } }