Beispiel #1
0
        /// <summary>
        /// Function to add, remove, organize the card stack when valid card is being dropped
        /// </summary>
        private void OnCardDragDrop(object sender, DragEventArgs e)
        {
            CardPanel panel       = (CardPanel)sender;
            Card      droppedCard = (Card)e.Data.GetData(typeof(Card));
            CardPanel oldParent   = (CardPanel)droppedCard.Parent;

            if (panel.GetType() == typeof(CardPanel) && panel.GetLength() == 0) //Check if the Tableau Card Panel is emtpy
            {
                addCardMoveToTextBox(droppedCard.cardId, panel.Name);
                storeAnimationState(droppedCard, oldParent, panel);

                CardPanel oldPanel = (CardPanel)droppedCard.Parent;
                oldPanel.RemoveCard(droppedCard);
                ((Card)e.Data.GetData(typeof(Card))).Parent = (Panel)sender;
                panel.AddCard(droppedCard);

                updateNumberOfMove();
            }
            else if (panel.GetType() == typeof(FreeCellCardPanel) && oldParent.GetCardIndex(droppedCard) == oldParent.GetLength() - 1) //Check if CardPanel is FreeCell and selected card is at the top of the stack
            {
                if (GameRules.IsFreeCellCardPanel(panel))                                                                              //Check if the freecell cardstack is not occupied
                {
                    addCardMoveToTextBox(droppedCard.cardId, panel.Name);
                    storeAnimationState(droppedCard, oldParent, panel);

                    CardPanel oldPanel = (CardPanel)droppedCard.Parent;
                    oldPanel.RemoveCard(droppedCard);
                    ((Card)e.Data.GetData(typeof(Card))).Parent = (Panel)sender;
                    panel.AddCard(droppedCard);

                    updateNumberOfMove();
                }
            }
            else if (panel.GetType() == typeof(HomeCellCardPanel)) //Check if CardPanel is HomeCell
            {
                //Check if homecell cardstack is not occupied, the destination has no card, the card being dropped is Ace and the selected card is at the top of the stack
                if (GameRules.IsHomeCellCardPanel(panel) && panel.GetLength() == 0 && droppedCard.face == Face.Ace && oldParent.GetCardIndex(droppedCard) == oldParent.GetLength() - 1)
                {
                    addCardMoveToTextBox(droppedCard.cardId, panel.Name);
                    storeAnimationState(droppedCard, oldParent, panel);

                    CardPanel oldPanel = (CardPanel)droppedCard.Parent;
                    oldPanel.RemoveCard(droppedCard);
                    ((Card)e.Data.GetData(typeof(Card))).Parent = (Panel)sender;
                    panel.AddCard(droppedCard);

                    panel.OrganizePanelWithoutOffset();
                    updateNumberOfMove();
                }
                //Check if homecell cardstack is not occupied, the destination has cards, the card being dropped is Ace and the selected card is at the top of the stack
                else if (GameRules.IsHomeCellCardPanel(panel) && panel.GetLength() > 0 && droppedCard.face != Face.Ace && oldParent.GetCardIndex(droppedCard) == oldParent.GetLength() - 1)
                {
                    //Check if Suits are matched and Faces are valid
                    if ((GameRules.IsHomeCellCardValid(panel, droppedCard, panel.GetTopCard())))
                    {
                        addCardMoveToTextBox(droppedCard.cardId, panel.GetTopCard().cardId, panel.Name);
                        storeAnimationState(droppedCard, oldParent, panel);

                        CardPanel oldPanel = (CardPanel)droppedCard.Parent;
                        oldPanel.RemoveCard(droppedCard);
                        ((Card)e.Data.GetData(typeof(Card))).Parent = (Panel)sender;
                        panel.AddCard(droppedCard);

                        panel.OrganizePanelWithoutOffset();
                        updateNumberOfMove();
                    }
                }
            }
            //Check if CardPanel is Tableau and if the stack is valid
            else if (panel.GetType() == typeof(CardPanel) && GameRules.IsStackValid(oldParent.cardStack, oldParent.GetCardIndex(droppedCard), panel.GetTopCard()))
            {
                // loop through all cards that are being moved
                int droppedCardIndex = oldParent.GetCardIndex(droppedCard);

                while (oldParent.cardStack.Count >= droppedCardIndex + 1)
                {
                    Card temp = oldParent.cardStack[droppedCardIndex];
                    addCardMoveToTextBox(temp.cardId, panel.GetTopCard().cardId, panel.Name);
                    storeAnimationState(temp, oldParent, panel);

                    panel.AddCard(temp);
                    temp.Parent = panel;

                    if (!oldParent.RemoveCard(temp))
                    {
                        MessageBox.Show("Invalid stack");
                    }
                }
                updateNumberOfMove();

                oldParent.OrganizePanel();
                panel.OrganizePanel();
            }
            //Check if CardPanel is Tableau and if single card move is valid
            else if (droppedCard == (droppedCard.Parent as CardPanel).GetTopCard() && GameRules.IsMoveValid(droppedCard, panel.GetTopCard()))
            {
                addCardMoveToTextBox(droppedCard.cardId, panel.GetTopCard().cardId, panel.Name);
                storeAnimationState(droppedCard, oldParent, panel);

                CardPanel oldPanel = (CardPanel)droppedCard.Parent;
                oldPanel.RemoveCard(droppedCard);
                ((Card)e.Data.GetData(typeof(Card))).Parent = (Panel)sender;
                panel.AddCard(droppedCard);
                updateNumberOfMove();
            }
        }