private void btLoad_Click(object sender, EventArgs e) { Slots slotsForm = new Slots(); slotsForm.SetTitles("Load", SaveLoad.Load); if (slotsForm.ShowDialog() != DialogResult.OK) { return; } if (MessageBox.Show("All your current progress will be lost. Are you sure?", "Load", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK) { return; } this.slotNumber = slotsForm.SlotNumber; try { IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); Stream stream = new FileStream(System.Environment.CurrentDirectory + "\\Save\\Save" + this.slotNumber + ".bin", FileMode.Open, FileAccess.Read); object obj = formatter.Deserialize(stream); stream.Close(); StateGameData gameData = (StateGameData)obj; this.currentLevel = gameData.CurrentLevel; this.player.MovesCount = gameData.Moves; this.pushes = gameData.Pushes; this.UpdateStatus(); this.LoadLevel(gameData.CurrentLevelState); } catch (Exception ex) { MessageBox.Show("The game could not be loaded: " + ex.Message, "Fail", MessageBoxButtons.OK, MessageBoxIcon.Error) } }
private void btSave_Click(object sender, EventArgs e) { if (this.slotNumber == -1) { Slots slotsForm = new Slots(); slotsForm.SetTitles("Save", SaveLoad.Save); if (slotsForm.ShowDialog() != DialogResult.OK) { return; } this.slotNumber = slotsForm.SlotNumber; } try { string[,] currentLevelStateIndividual = new string[this.currentLevel_Rigid.GetUpperBound(0) + 1, this.currentLevel_Rigid.GetUpperBound(1) + 1]; for (int y = 0; y < this.currentLevel_Rigid.GetUpperBound(1) + 1; y++) { for (int x = 0; x < this.currentLevel_Rigid.GetUpperBound(0) + 1; x++) { if (this.currentLevel_Rigid[x, y].Picture != null) { if (this.currentLevel_Rigid[x, y].Picture.Tag.ToString() == "B" && this.slotPositionList.Contains(new Point(x * this.increment, y * this.increment))) { currentLevelStateIndividual[x, y] = "X"; } else { currentLevelStateIndividual[x, y] = this.currentLevel_Rigid[x, y].Picture.Tag.ToString(); } } else { currentLevelStateIndividual[x, y] = " "; } } } if (this.slotPositionList.Contains(this.player.Picture.Location)) { currentLevelStateIndividual[this.player.Position.X, this.player.Position.Y] = "Y"; } else { currentLevelStateIndividual[this.player.Position.X, this.player.Position.Y] = this.player.Picture.Tag.ToString(); } StringBuilder sb = new StringBuilder(); string[] currentLevelStateFinal = new string[currentLevelStateIndividual.GetUpperBound(1) + 1]; for (int y = 0; y < this.currentLevel_Rigid.GetUpperBound(1) + 1; y++) { for (int x = 0; x < this.currentLevel_Rigid.GetUpperBound(0) + 1; x++) { sb.Append(currentLevelStateIndividual[x, y]); } currentLevelStateFinal[y] = sb.ToString(); sb.Clear(); } //File.WriteAllText("levelTest.txt", sb.ToString()); sb = null; StateGameData gameData = new StateGameData(this.currentLevel, this.player.MovesCount, this.pushes); gameData.CurrentLevelState = currentLevelStateFinal; IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); Stream stream = new FileStream(System.Environment.CurrentDirectory + "\\Save\\Save" + this.slotNumber + ".bin", FileMode.Create, FileAccess.Write); formatter.Serialize(stream, gameData); stream.Close(); MessageBox.Show("Game successfully saved!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("Game could not be saved: " + ex.Message, "Fail", MessageBoxButtons.OK, MessageBoxIcon.Error); } }