public void SaveData(SaveFile saveFile, string pPath) { Stream fileStreamer; BinaryFormatter bf; fileStreamer = File.Open(pPath, FileMode.Create); bf = new BinaryFormatter(); bf.Serialize(fileStreamer, saveFile); fileStreamer.Close(); }
public SaveFile loadData(SaveFile pSaveFile, string pPath) { try { Stream fileStreamer; BinaryFormatter bf; fileStreamer = File.Open(pPath, FileMode.Open); bf = new BinaryFormatter(); var tmpGame = bf.Deserialize(fileStreamer); pSaveFile = (SaveFile)tmpGame; fileStreamer.Close(); return pSaveFile; } catch { return pSaveFile; } }
/// <summary> /// Save the current state of the game /// </summary> private void saveGame(string pPath) { earlierLetters = new String[5, 5]; earlierResult = new Colors[5, 5]; ///For each row for (int i = 0; i <= lingoGame.AmountOfGuesses; i++) ///For each letter for (int j = 0; j < 5; j++) { ///Save the letter earlierLetters[i, j] = playBoard[i, j].Text; ///Save the color of the textbox on the playboard if (playBoard[i, j].BackColor == Color.Green) { earlierResult[i, j] = Colors.Green; } else if (playBoard[i, j].BackColor == Color.Orange) { earlierResult[i, j] = Colors.Orange; } else if (playBoard[i, j].BackColor == Color.Red) { earlierResult[i, j] = Colors.Red; } } ///Create savefile object with the games objects saveFile = new SaveFile(lingoGame, wordManager, resultColors, foundLetters, earlierResult, earlierLetters); ///Save to file loadSaveManager = new LoadSave(); loadSaveManager.SaveData(saveFile, pPath); }
/// <summary> /// Load a previous game-session into the current session of the game /// </summary> /// <returns></returns> private bool loadGame() { LoadForm loadForm = new LoadForm(); loadForm.ShowDialog(); string loadPath = loadForm.saveFilePath; loadSaveManager = new LoadSave(); saveFile = loadSaveManager.loadData(saveFile, loadPath); if (saveFile != null) { resetBoard(); lingoGame = saveFile.lingoGame; wordManager = saveFile.wordManager; resultColors = saveFile.result; foundLetters = saveFile.foundLetters; earlierLetters = saveFile.earlierLetters; earlierResult = saveFile.earlierResult; ///If a game is loaded after the previous is finnished if (!btnCheck.Enabled) btnCheck.Enabled = true; return true; } else { return false; } }