Exemple #1
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();
                }
            }
        }
Exemple #2
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();
            }
        }