示例#1
0
 public void Undo()
 {
     if (Stack.Count > 0)
     {
         UndoAction action = Stack[Stack.Count - 1];
         if (action.RevealedCard)
         {
             Card lastCard = action.SourceStack.GetLastCard();
             lastCard.Visible = false;
         }
         int pos = action.DestStack.Count - action.CardCount;
         Board.MoveCardsWithoutChecking(action.DestStack, pos, action.SourceStack);
         Stack.Remove(action);
     }
 }
示例#2
0
        public void Touch(Point pt)
        {
            if (_completed)
            {
                StartNewGame();
                Deal();
            }
            else if (_currentAction == CardAction.Selecting)
            {
                _currentAction = CardAction.None;

                for (int i = 0; i < _cardsSelectionAnimation.Count; i++)
                {
                    if (_cardsSelectionAnimation[i].CurrentRect.Contains(pt))
                    {
                        _currentAction = CardAction.Moving;
                        _cardsInAction = new List <Card>();
                        for (; i < _cardsSelectionAnimation.Count; i++)
                        {
                            _cardsInAction.Add(_cardsSelectionAnimation[i].Card);
                        }
                    }
                }
                CloseStack(_board.GetStack(_currentStack), _cardsSelectionAnimation);
                _cardsSelectionAnimation = null;
            }
            else if (_currentAction == CardAction.Dragging)
            {
                _currentAction = CardAction.None;

                CardStack destStack;
                Rectangle card0Rect  = _cardsInAction[0].View.Rect;
                CardStack destStack1 = GetStackAtPoint(card0Rect.Location);
                CardStack destStack2 = GetStackAtPoint(new Point(card0Rect.Right, card0Rect.Top));
                CardStack destStack3 = GetStackAtPoint(pt);
                if (destStack1 != null && destStack2 != null)                 // Check based on card position first
                {
                    Rectangle destRect1 = GetAreaOfStack(destStack1);
                    Rectangle destRect2 = GetAreaOfStack(destStack2);

                    if (destRect1.Width - (card0Rect.Left - destRect1.Left) > card0Rect.Right - destRect2.Left)
                    {
                        destStack = destStack1;
                    }
                    else
                    {
                        destStack = destStack2;
                    }
                }
                else if (destStack1 != null)                 // Prefer card location instead of top right
                {
                    destStack = destStack1;
                }
                else if (destStack2 != null)                 // Prefer top right instead of touch pos
                {
                    destStack = destStack2;
                }
                else                 // Accept touch pos if we don't have a better option
                {
                    destStack = destStack3;
                }

                CardStack srcStack = _board.GetStack(_currentStack);
                bool      movedRun = false;
                for (int t = 0; t < 2 && !movedRun; t++)
                {
                    if (destStack != null && destStack.Index != _currentStack)
                    {
                        for (int i = 0; i < _cardsInAction.Count; i++)
                        {
                            if (_board.CanMoveCardToStack(_cardsInAction[i], destStack))
                            {
                                MoveCards(srcStack, srcStack.Count - _cardsInAction.Count + i, destStack);
                                if (destStack.ContainsCompleteRun())
                                {
                                    ClearRun(destStack);
                                }
                                movedRun = true;
                                break;
                            }
                        }
                    }
                    destStack = (destStack == destStack1 ? destStack2 : destStack1);
                }
                if (!movedRun)
                {
                    FixupStackCardViewRects(srcStack);
                }
                _cardsInAction = null;
            }
            else if (_currentAction == CardAction.Moving)
            {
                _currentAction = CardAction.None;

                CardStack stack = GetStackAtPoint(pt);
                if (stack != null)
                {
                    if (stack.Index == _currentStack)
                    {
                        PickFromStack(stack, pt);
                    }
                    else
                    {
                        for (int i = 0; i < _cardsInAction.Count; i++)
                        {
                            if (_board.CanMoveCardToStack(_cardsInAction[i], stack))
                            {
                                MoveCards(_board.GetStack(_currentStack), _board.GetStack(_currentStack).Count - _cardsInAction.Count + i, stack);
                                if (stack.ContainsCompleteRun())
                                {
                                    ClearRun(stack);
                                }
                                _cardsInAction = null;
                                break;
                            }
                        }

                        if (_cardsInAction != null)
                        {
                            _cardsInAction = null;
                            if (stack.Count > 0)
                            {
                                SelectStack(stack);
                            }
                        }
                    }
                }
            }
            else if (GetExtraStackArea().Contains(pt))
            {
                if (_board.CountOfExtraDealingsLeft() > 0)
                {
                    Deal();
                }
            }
            else if (GetCardArea().Contains(pt))
            {
                CardStack stack = GetStackAtPoint(pt);
                if (stack != null)
                {
                    if (stack.Count > 0)
                    {
                        SelectStack(stack);
                    }
                }
            }
            else if (_undoButtonRect.Contains(pt) && _board.CanUndo())
            {
                UndoAction nextAction = _board.NextUndoAction();
                _board.Undo();
                FixupStackCardViewRects(nextAction.SourceStack);
                FixupStackCardViewRects(nextAction.DestStack);
            }

            _dragInfo = null;
            FixupCardHighlights();
        }
示例#3
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);
        }