Пример #1
0
        /// <summary>
        /// Function to toggle the mouse cursor when card is being hovered over other panel
        /// </summary>
        private void OnCardDragOver(object sender, DragEventArgs e)
        {
            CardPanel panel = (CardPanel)sender;

            if (panel.GetType() == typeof(FreeCellCardPanel))
            {
                bool canDrop = (GameRules.IsFreeCellCardPanel(panel));
                if (!canDrop)
                {
                    Cursor.Current = System.Windows.Forms.Cursors.No;
                }
            }
            else if (panel.GetType() == typeof(HomeCellCardPanel))
            {
                Cursor.Current = System.Windows.Forms.Cursors.Default;
            }
            else if (panel.GetLength() == 0)
            {
                Cursor.Current = System.Windows.Forms.Cursors.Default;
            }
            else
            {
                Card card    = (Card)e.Data.GetData(typeof(Card));
                bool canDrop = GameRules.IsMoveValid(card, panel.GetTopCard());

                if (!canDrop)
                {
                    Cursor.Current = System.Windows.Forms.Cursors.No;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Function to check if the top card in each stack can make a valid move
        /// </summary>
        private bool EvaluateCard(Card card)
        {
            bool canMoveFlag = false;

            for (int i = 0; i < cardPanels.Count; i++)
            {
                cardPanels[i].AllowDrop = false;
                cardPanels[i].DragOver += new DragEventHandler(OnCardDragOver);
                cardPanels[i].DragDrop += new DragEventHandler(OnCardDragDrop);

                CardPanel currentCardPanel = (CardPanel)card.Parent;

                if (cardPanels[i].GetLength() == 0)
                {
                    cardPanels[i].AllowDrop = true;
                    canMoveFlag             = true;
                }
                else
                {
                    // if the card panel is not the panel of the card that was clicked on
                    if (cardPanels[i] != currentCardPanel)
                    {
                        if (GameRules.IsMoveValid(card, cardPanels[i].GetTopCard()))
                        {
                            cardPanels[i].AllowDrop = true;
                            canMoveFlag             = true;
                        }
                    }
                }
            }

            if (canMoveFlag)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #3
0
        /// <summary>
        /// Function to enable the dragdrop event to each card
        /// </summary>
        private void onCardMouseDown(object sender, MouseEventArgs e)
        {
            Card      card  = (Card)sender;
            CardPanel panel = (CardPanel)card.Parent;

            bool canMove = EvaluateCard(card);

            if (canMove)
            {
                this.DoDragDrop(card, DragDropEffects.Move);
            }
            else if (panel.GetCardIndex(card) == panel.GetLength() - 1) //Check if the selected card is at the top of the stack
            {
                this.DoDragDrop(card, DragDropEffects.Move);
            }
            else if (GameRules.IsStackValid(panel.cardStack, panel.GetCardIndex(card))) //Check if the stack is valid
            {
                this.DoDragDrop(card, DragDropEffects.Move);
            }
            else
            {
                MessageBox.Show("This card cannot be moved.");
            }

            bool isGameCompleted = GameRules.isGameCompleted(cardPanels, freecellCardPanels, homecellCardPanels); //Check if the user has won the game

            if (isGameCompleted)
            {
                MessageBox.Show("Congratulations! You have won the game!");
                DialogResult res = MessageBox.Show("Do you want to review your gameplay?", "Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (res == DialogResult.OK)
                {
                    startCurrentGameAnimation();
                }
            }
        }
Пример #4
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();
            }
        }