Esempio n. 1
0
        public bool Load()
        {
            if (ResumeGameExists())
            {
                Reset();

                GameSerialization gameSerialization;
                try
                {
                    var storage = IsolatedStorageFile.GetUserStoreForApplication();
                    var stream  = storage.OpenFile(ResumeGameFilename, FileMode.Open, FileAccess.Read);

                    TextReader reader     = new StreamReader(stream);
                    var        serializer = new XmlSerializer(typeof(GameSerialization));
                    gameSerialization = serializer.Deserialize(reader) as GameSerialization;
                    reader.Close();
                }
                catch (FileNotFoundException)
                {
                    // We thought we had a file...?
                    return(false);
                }
                catch (Exception exc)
                {
                    // Something else bad happened.  Crap.
                    Debug.WriteLine(exc);
                    return(false);
                }

                if (gameSerialization == null)
                {
                    return(false);
                }

                SuitCount = gameSerialization.Suits;
                MoveCount = gameSerialization.MoveCount;
                Score     = gameSerialization.Score;

                for (int i = 0; i < StackCount; i++)
                {
                    CardStackSerialization stackSerialization = gameSerialization.Board.Stacks[i];
                    CardStack stack = GetStack(i);
                    foreach (CardSerialization cardSerialization in stackSerialization.Cards)
                    {
                        var card = new Card((Suit)cardSerialization.Suit, (Value)cardSerialization.Value, new Random())
                        {
                            Visible = cardSerialization.Visible
                        };
                        card.View = new CardView(card);
                        stack.Add(card);
                    }
                }

                for (int i = 0; i < gameSerialization.Board.Extras.Count; i++)
                {
                    CardStackSerialization extraSerialization = gameSerialization.Board.Extras[i];
                    _extras[i] = new List <Card>();
                    foreach (CardSerialization cardSerialization in extraSerialization.Cards)
                    {
                        var card = new Card((Suit)cardSerialization.Suit, (Value)cardSerialization.Value, new Random())
                        {
                            Visible = cardSerialization.Visible
                        };
                        card.View = new CardView(card);
                        _extras[i].Add(card);
                    }
                }

                for (int i = 0; i < gameSerialization.Board.Completed.Count; i++)
                {
                    CardStackSerialization completedSerialization = gameSerialization.Board.Completed[i];
                    CardSerialization      cardSerialization      = completedSerialization.Cards[0];
                    var card = new Card((Suit)cardSerialization.Suit, (Value)cardSerialization.Value, new Random())
                    {
                        Visible = cardSerialization.Visible
                    };
                    card.View = new CardView(card);
                    _completed.Add(card);
                }

                for (int i = 0; i < gameSerialization.UndoStack.Actions.Count; i++)
                {
                    UndoActionSerialization actionSerialization = gameSerialization.UndoStack.Actions[i];
                    var action = new UndoAction(actionSerialization.CardCount, GetStack(actionSerialization.SourceStack),
                                                GetStack(actionSerialization.DestStack), actionSerialization.RevealedCard);
                    _undoStack.Stack.Add(action);
                }

                for (int i = 0; i < StackCount; i++)
                {
                    View.FixupStackCardViewRects(GetStack(i));
                }

                // Stats were reset with a suspended game - bump the count since we have a game in progress
                if (Statistics.TotalGames == 0)
                {
                    Statistics.TotalGames++;
                    switch (SuitCount)
                    {
                    case 1:
                        Statistics.EasyGames++;
                        break;

                    case 2:
                        Statistics.MediumGames++;
                        break;

                    case 4:
                        Statistics.HardGames++;
                        break;
                    }
                }
                return(true);
            }
            return(false);
        }
Esempio n. 2
0
        public void Save()
        {
            // TODO: Ensure animation finished

            if (IsBoardClear())
            {
                RemoveResumeGame();
                return;
            }

            // Build serialized model of the game
            var gameSerialization = new GameSerialization {
                Suits = SuitCount, MoveCount = MoveCount, Score = Score
            };

            foreach (CardStack cardStack in _stacks)
            {
                var stackSerialization = new CardStackSerialization();
                foreach (Card card in cardStack.GetCards())
                {
                    var cardSerialization = new CardSerialization
                    {
                        Suit    = (int)card.Suit,
                        Value   = (int)card.Value,
                        Visible = card.Visible
                    };
                    stackSerialization.Cards.Add(cardSerialization);
                }
                gameSerialization.Board.Stacks.Add(stackSerialization);
            }
            foreach (var extraList in _extras)
            {
                if (extraList != null)
                {
                    var stackSerialization = new CardStackSerialization();
                    foreach (Card card in extraList)
                    {
                        var cardSerialization = new CardSerialization
                        {
                            Suit    = (int)card.Suit,
                            Value   = (int)card.Value,
                            Visible = card.Visible
                        };
                        stackSerialization.Cards.Add(cardSerialization);
                    }
                    gameSerialization.Board.Extras.Add(stackSerialization);
                }
            }
            foreach (Card card in _completed)
            {
                var stackSerialization = new CardStackSerialization();
                var cardSerialization  = new CardSerialization
                {
                    Suit    = (int)card.Suit,
                    Value   = (int)card.Value,
                    Visible = card.Visible
                };
                stackSerialization.Cards.Add(cardSerialization);
                gameSerialization.Board.Completed.Add(stackSerialization);
            }

            gameSerialization.UndoStack.Actions = new List <UndoActionSerialization>();
            foreach (UndoAction action in _undoStack.Stack)
            {
                var actionSerialization = new UndoActionSerialization
                {
                    CardCount    = action.CardCount,
                    SourceStack  = action.SourceStack.Index,
                    DestStack    = action.DestStack.Index,
                    RevealedCard = action.RevealedCard
                };
                gameSerialization.UndoStack.Actions.Add(actionSerialization);
            }

            try
            {
                var storage = IsolatedStorageFile.GetUserStoreForApplication();
                var stream  = storage.CreateFile(ResumeGameFilename);

                var writer     = new StreamWriter(stream);
                var serializer = new XmlSerializer(typeof(GameSerialization));
                serializer.Serialize(writer, gameSerialization);
                writer.Close();
            }
            catch
            {
                // Couldn't save stats file - crap
            }
        }