/// <summary>
        /// Loads the content from the save file and applies it to the current game.
        /// </summary>
        private bool ApplyStoredGameStateToCurrentGame()
        {
            string json;

            try
            {
                json = Files.GetFileContent(this.SaveGamePath);
            }
            catch (Exception)
            {
                Files.ClearFile(this.SaveGamePath);
                return(false);
            }
            //Deserialize the json
            GameState gameState = JsonConvert.DeserializeObject <GameState>(json);

            //For the next part we need to cast/convert all properties that are stored in our dyanmic list to the appropriate type
            this.IsPlayerOnesTurn = gameState.IsPlayerOnesTurn;
            this.SelectedTheme    = gameState.SelectedTheme;
            this.Rows             = gameState.Rows;
            this.Collumns         = gameState.Collumns;
            this.Players          = gameState.Players;
            this.AudioIsEnabled   = gameState.AudioEnabled;

            //It is important that we style our deck after we loaded in all the settings, otherwise we get a default 4*4 playing field
            //even though the settings may state otherwise
            List <CardPictureBoxJson> deck = new List <CardPictureBoxJson>();

            deck = gameState.Deck;

            foreach (CardPictureBoxJson jsonCard in deck)
            {
                CardNameAndImage pairNameAndImage = this.ThemeImages[this.SelectedTheme].Find(item => item.Name == jsonCard.PairName);
                Card             card             = new Card(pairNameAndImage.Name, pairNameAndImage.Resource)
                {
                    Name           = jsonCard.Name,
                    IsSolved       = jsonCard.IsSolved,
                    HasBeenVisible = jsonCard.HasBeenVisible
                };
                //Check if the card is currently selected, if so add it to the selectedCards list.
                if (jsonCard.IsSelected)
                {
                    this.SelectedCards.Add(card);
                }
                this.Deck.Add(card);
            }
            return(true);
        }
示例#2
0
        /// <summary>
        /// Resumes paused game. If it needs to resume game from savefile load file contents back into the memory class
        /// and rebuild the deck.
        /// </summary>
        /// <param name="loadFromSaveFile"></param>
        public void ResumeGame(bool loadFromSaveFile = false)
        {
            if (loadFromSaveFile)
            {
                //Since we are bypassing this.Populatedeck() we need to do some things again
                //Create new empty lists for deck and selectedcards
                this.Deck          = new List <CardPictureBox>();
                this.SelectedCards = new List <CardPictureBox>();
                //Use a dynamic object for the next part, could also create a custom class for this
                dynamic gameState = new System.Dynamic.ExpandoObject();
                //Load the data from the savegamefile
                string json = Files.GetFileContent(Path.Combine(Directory.GetCurrentDirectory(), "savegame.txt"));
                //Deserialize the json
                gameState = JsonConvert.DeserializeObject(json);
                //For the next part we need to cast/convert all properties that are stored in our dyanmic list to the appropriate type
                this.IsPlayerOnesTurn = (bool)gameState.IsPlayerOnesTurn;
                this.SelectedTheme    = (int)gameState.SelectedTheme;
                this.Rows             = (int)gameState.Rows;
                this.Collumns         = (int)gameState.Collumns;
                this.Players          = gameState.Players.ToObject <List <Player> >().ToArray(); //Seems dirty
                //It is important that we style our deck after we loaded in all the settings, otherwise we get a default 4*4 playing field
                //even though the settings may state otherwise
                this.ConfigurateDeckStyling();
                List <CardPictureBoxJson> deck = new List <CardPictureBoxJson>();
                deck = gameState.Deck.ToObject <List <CardPictureBoxJson> >();

                foreach (CardPictureBoxJson jsonCard in deck)
                {
                    CardNameAndImage pairNameAndImage = this.ThemeImages[this.SelectedTheme].Find(item => item.Name == jsonCard.PairName);
                    CardPictureBox   card             = new CardPictureBox()
                    {
                        Dock           = DockStyle.Fill,
                        SizeMode       = PictureBoxSizeMode.StretchImage,
                        Name           = jsonCard.Name,
                        IsSolved       = jsonCard.IsSolved,
                        HasBeenVisible = jsonCard.HasBeenVisible,
                        PairName       = pairNameAndImage.Name,
                        CardImage      = pairNameAndImage.Resource,
                        Image          = (jsonCard.IsSolved) ? pairNameAndImage.Resource : null //Show image based on if it was solved
                    };
                    card.Click += this.CardClicked;
                    //Check if the card is currently selected, if so add it to the selectedCards list.
                    if (jsonCard.IsSelected)
                    {
                        this.SelectedCards.Add(card);
                        card.Image = pairNameAndImage.Resource;
                    }
                    this.Deck.Add(card);
                }
                foreach (CardPictureBox card in this.Deck)
                {
                    this.Panel.Controls.Add(card);
                }
            }
            //Remove the savegame from the savefile to prevent abuse
            Files.WriteToFile(Path.Combine(Directory.GetCurrentDirectory(), "savegame.txt"), "", overwrite: true);
            this.HasUnfinishedGame = false;
            this.Form1.ShowLoadGameCheckbox();
            //Pass back control to the player
            this.GameIsFrozen = false;
        }