示例#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 move made with the homecell panel is valid
 /// </summary>
 public static bool IsHomeCellCardValid(CardPanel cp, Card card1, Card card2)
 {
     if (IsSuitsMatched(card1, card2) && IsHomeCellCardFacesValid(card1, card2))
     {
         return(true);
     }
     return(false);
 }
示例#3
0
 /// <summary>
 /// Function to check if the freecell panel is not occupied
 /// </summary>
 public static bool IsFreeCellCardPanel(CardPanel fcPanel)
 {
     if (fcPanel.GetLength() == 0)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
示例#4
0
 /// <summary>
 /// Function to check if the homecell panel is occupied
 /// </summary>
 public static bool IsHomeCellCardPanel(CardPanel hcPanel)
 {
     if (hcPanel.GetLength() >= 0)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
示例#5
0
        /// <summary>
        /// Timer to animate the gameplay
        /// </summary>
        private void animation_Tick(object sender, EventArgs e)
        {
            if (counter < cardMoveMade.Count)
            {
                Card      tempCard     = cardMoveMade[counter];
                CardPanel tempNewPanel = newCardPanelMoveMade[counter];
                CardPanel tempOldPanel = oldCardPanelMoveMade[counter];
                addCardMoveToTextBox(tempCard.cardId, tempNewPanel.Name);

                tempNewPanel.AddCard(tempCard);
                tempOldPanel.RemoveCard(tempCard);
                tempCard.Parent = tempNewPanel;

                updateNumberOfMove();

                if (tempNewPanel.GetType() == typeof(HomeCellCardPanel) || tempNewPanel.GetType() == typeof(FreeCellCardPanel))
                {
                    tempOldPanel.OrganizePanel();
                    tempNewPanel.OrganizePanelWithoutOffset();
                }
                else
                {
                    tempOldPanel.OrganizePanel();
                    tempNewPanel.OrganizePanel();
                }
                counter++;
            }
            else
            {
                animation.Stop();
                timeRecorder.Stop();

                saveGameToolStripMenuItem.Enabled       = false;
                loadGameToolStripMenuItem.Enabled       = false;
                reviewGameplayToolStripMenuItem.Enabled = false;

                DialogResult res = MessageBox.Show("Gameplay animation is finished! Do you want to restart a new game?", "Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (res == DialogResult.OK)
                {
                    ResetGame();
                }
            }
        }
示例#6
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);
            }
        }
示例#7
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();
                }
            }
        }
示例#8
0
 /// <summary>
 /// Function to store data needed to produce the animation timer
 /// </summary>
 private void storeAnimationState(Card card, CardPanel oldCardPanel, CardPanel newCardPanel)
 {
     cardMoveMade.Add(card);
     oldCardPanelMoveMade.Add(oldCardPanel);
     newCardPanelMoveMade.Add(newCardPanel);
 }
示例#9
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();
            }
        }