public void SaveGameData(List<DataCluePin> currentClue, List<DataPin> currentGuess) { // Save Data using LINQ and a new instantiation of GameData List<DataPin> savedGameBoardData = (from pin in currentGuess select pin).ToList(); List<DataCluePin> savedGameBoardClueData = (from clue in currentClue select clue).ToList(); List<DataPin> savedCreatedCode = (from codepin in createdCode select codepin).ToList(); gameBoard.Add(savedGameBoardData); gameBoardClues.Add(savedGameBoardClueData); GameData gameData = new GameData { gameBoard = gameBoard, gameBoardClues = gameBoardClues, CurrentLevel = CurrentLevel, GuessesLeft = GuessesLeft, createdCode = savedCreatedCode, IsGameOver = IsGameOver, WhosCodeBreaker = whosCodeBreaker, WhosCodeMaker = WhosCodeMaker }; using ( StreamWriter file = File.CreateText(filesavelocation) ) { JsonSerializer seralizer = new JsonSerializer(); seralizer.Serialize(file, gameData); } }
void LoadGameAtStartup(GameData gameData) { ///<summary> /// This function sets up the GUI with the saved state information ///</summary> var boardpins = GetGameBoardPins(); var cluepins = GetGameBoardClues(); // Select the GUI Codepins with LINQ var codepins = (from codepin in CreateCodeGrid.Children.OfType<Pin>() select codepin).ToList(); // Query the game data with LINQ var queryGameBoardPins = from pinRow in gameData.gameBoard select pinRow; var queryGameBoardClues = from clueRow in gameData.gameBoardClues select clueRow; var querySavedCode = from codePin in gameData.createdCode select codePin; foreach (var gameDataPinList in queryGameBoardPins) { foreach (var gameDataPin in gameDataPinList) { boardpins[gameDataPin.YPosition][gameDataPin.XPosition].PinColor = gameDataPin.PinColor; } } foreach (var gameDataCluePinList in queryGameBoardClues) { foreach (var gameDataCluePin in gameDataCluePinList) { Console.WriteLine("internal x: {0}", gameDataCluePin.InternalXPosition); cluepins[gameDataCluePin.YPosition][gameDataCluePin.InternalXPosition].PinColor = gameDataCluePin.PinColor; } } foreach (var savedDataPin in querySavedCode) { codepins[savedDataPin.XPosition].PinColor = savedDataPin.PinColor; } }