Example #1
0
        /// <summary>
        /// Deserialize the game state from the hard drive
        /// </summary>
        public void LoadGame()
        {
            if (!File.Exists(SavePath))
            {
                throw new FileNotFoundException("Error: saveGameState.xml not found!");
            }

            SaveLoadAPI gameState;

            using (StreamReader reader = new StreamReader(SavePath))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(SaveLoadAPI));
                gameState = xmlSerializer.Deserialize(reader) as SaveLoadAPI;
            }

            if (gameState == null)
            {
                throw new ArgumentNullException("Saved state cannot be null");
            }

            //Mementofield can only be initialized if the size of the current playfield equals the size of the saved one.
            //if (gameState.MementoField.FieldDimension != Playfield.Instance.PlayfieldSize)
            //{
            //    throw new InvalidOperationException("Current PlayField size is different than the size of the saved object and it cannot be initialized.");

            //}

            this.MementoField = gameState.MementoField;
            this.MementoPlayer = gameState.MementoPlayer;

            Console.SetCursorPosition(0, 0);
            Console.WriteLine("Game successfully loaded!");
            Thread.Sleep(500);
        }
Example #2
0
        /// <summary>
        /// Deserialize the game state from the hard drive
        /// </summary>
        public void LoadGame()
        {
            if (!File.Exists(SavePath))
            {
                throw new FileNotFoundException("Error: saveGameState.xml not found!");
            }

            SaveLoadAPI gameState;

            using (StreamReader reader = new StreamReader(SavePath))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(SaveLoadAPI));
                gameState = xmlSerializer.Deserialize(reader) as SaveLoadAPI;
            }

            if (gameState == null)
            {
                throw new ArgumentNullException("Saved state cannot be null");
            }

            //Mementofield can only be initialized if the size of the current playfield equals the size of the saved one.
            //if (gameState.MementoField.FieldDimension != Playfield.Instance.PlayfieldSize)
            //{
            //    throw new InvalidOperationException("Current PlayField size is different than the size of the saved object and it cannot be initialized.");

            //}

            this.MementoField  = gameState.MementoField;
            this.MementoPlayer = gameState.MementoPlayer;


            Console.SetCursorPosition(0, 0);
            Console.WriteLine("Game successfully loaded!");
            Thread.Sleep(500);
        }
Example #3
0
        public void PlayerRestoreMemento()
        {
            Player player = new Player("Ivan");
            player.DetonatedMines = 12;
            player.MovesCount = 11;
            MementoPlayer mementoPlayer = new MementoPlayer();

            player.LoadMemento(mementoPlayer);
        }