Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        private void ComputerAttacks()
        {
            Random rand = new Random();                                              //initialize a random object
            int    computerChoiceIndex = rand.Next(pnlComputerCards.Controls.Count); //generates a random number between 0 and the number of cards the computer has

            ReenableAllCards();

            if (computerChoiceIndex >= 0)
            {
                CardBox.CardBox computerCard = pnlComputerCards.Controls[computerChoiceIndex] as CardBox.CardBox; //create a copy of the card object
                computerCard.FaceUp = true;


                logs.WriteLine("Computer Plays " + computerCard.ToString() + " as an attack");
                pnlComputerCards.Controls.Remove(computerCard); //remove the card from the computers hand
                pnlActiveCards.Controls.Add(computerCard);      //place the card into the active play panel

                if (playerAttacking == false)
                {
                    ReenableAllCards();
                    DisableInvalidPlayerDefenseChoices(computerCard);
                }
            }

            RealignAllCards();
            UpdateDefendedAndDiscardPanelControls();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Update the Cards in a hand
        /// </summary>
        private void UpdateHand(Cards hand)
        {
            //  Determin which hand to clear
            if (hand == player.PlayHand)
            {
                pnlPlayerHand.Controls.Clear();
            }
            else if (hand == game.Table.InPlay)
            {
                pnlTable.Controls.Clear();
            }

            CardBox.CardBox aCardBox;

            //  Add each card to play area.
            foreach (Card card in hand)
            {
                aCardBox        = new CardBox.CardBox(card);
                aCardBox.Click += CardBoxClick;

                if (hand == player.PlayHand)
                {
                    System.Diagnostics.Debug.WriteLine("Adding " + card.ToString() + " to player's panel");
                    pnlPlayerHand.Controls.Add(aCardBox);
                }
                else if (hand == game.Table.InPlay)
                {
                    System.Diagnostics.Debug.WriteLine("Adding " + card.ToString() + " to table panel");
                    pnlTable.Controls.Add(aCardBox);
                }
            }
        }
Exemplo n.º 3
0
        }//end of COMPARECARDS

        private void DisableInvalidPlayerDefenseChoices(CardBox.CardBox attack)
        {
            ReenableAllCards();
            int counter             = 0;
            int numberOfPlayerCards = pnlPlayerCards.Controls.Count;

            foreach (CardBox.CardBox playerCard in pnlPlayerCards.Controls)
            {
                //MessageBox.Show(playerCard.Card.ToString());
                if (playerCard.Card < attack.Card)
                {
                    //MessageBox.Show((playerCard.Card < attack.Card).ToString());
                    playerCard.Enabled = false;
                    counter++;
                }
            }

            if (counter == numberOfPlayerCards) //player cannot defend against the attacking card
            {
                //MessageBox.Show("Player cannot defend. Picking up cards");
                if (pnlDefended.Controls.Count > 0)
                {
                    MoveCards(pnlDefended, pnlPlayerCards);
                }

                MoveCards(pnlActiveCards, pnlPlayerCards);

                txtComputerAttacker.Visible = false;
                playerAttacking             = true;

                RoundDeal();
                ReenableAllCards();
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Displays the playing field updating playing field panel
        /// </summary>
        private void DisplayDiscardPile()
        {
            //initialize counters
            int counter        = 0;
            int topOffset      = 10;
            int displayCounter = 10;
            //populate the discardpile
            ArrayList discardedCards = field.getDiscard();

            //populate the display discard pile list
            for (int i = 0; i < discardedCards.Count; i++)
            {
                CardBox.CardBox newCardBox = new CardBox.CardBox((Card)discardedCards[i]);
                discardPile.Add(newCardBox);
            }
            //offset the cards for display purposes (rows of ten cards)
            for (int i = discardedCards.Count - 1; i >= 0; i--)
            {
                discardPile[i].Left = (displayCounter * 20) + 100;
                discardPile[i].Top  = topOffset;
                counter++;
                displayCounter--;
                //add the card with the current offset to the output
                this.pnDiscard.Controls.Add(discardPile[i]);
                //checks if the line is at 10 cards and change the offsets if it is
                if (counter > 9)
                {
                    topOffset      = topOffset + 120;
                    counter        = 0;
                    displayCounter = 10;
                }
            }
        }
Exemplo n.º 5
0
        public void PlayerHandHandler(object sender, EventArgs e)
        {
            pnlPlayerHand.Controls.Clear();
            game.Players[0].UpdatePlayableCards(game);
            game.Players[1].UpdatePlayableCards(game);
            int offset = 0;

            for (int j = 0; j < game.Players[0].Hand.Count; j++)
            {
                CardBox.CardBox cbx = new CardBox.CardBox
                {
                    Visible = true,
                    Card    = game.Players[0].Hand[j],
                    Width   = cbxHand.Width,
                    Height  = cbxHand.Height,
                    Left    = cbxHand.Left + ((j * (cbxHand.Width / 3)) + offset)
                };
                if (game.Players[0].Hand.Count > 13)
                {
                    cbx.Width  = cbx.Width - pnlPlayerHand.Width / game.Players[0].Hand.Count;
                    cbx.Height = cbx.Height - (pnlPlayerHand.Width / game.Players[0].Hand.Count);
                    offset    -= 5;
                    cbx.Left   = cbxHand.Left + ((j * (cbxHand.Width / 10)) + offset);
                }
                offset    += 10;
                cbx.Click += HandClickHandler;
                this.pnlPlayerHand.Controls.Add(cbx);

                if (game.Players[0].PlayableCards.Contains(game.Players[0].Hand[j]))
                {
                    cbx.Top = cbxHand.Top - 20;
                }
                else
                {
                    cbx.Top = cbxHand.Top;
                }
            }
            if (!game.Players[0].firstTurn)
            {
                if (game.Players[0].Status == Statuses.Attacking)
                {
                    btnOkay.Enabled = true;
                }
                else
                {
                    btnOkay.Enabled = false;
                }
            }
            if (game.Players[0].Status == Statuses.Defending)
            {
                btnTake.Enabled = true;
            }
            else
            {
                btnTake.Enabled = false;
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Start a card move on drag
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void CardBox_MouseDown(object sender, MouseEventArgs e)
 {
     // Set dragCard
     dragCard = sender as CardBox.CardBox;
     if (dragCard != null)
     {
         // Set the data to be dragged and the allowed effect dragging will have.
         DoDragDrop(dragCard, DragDropEffects.Move);
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// Sets up the CardBox object inside the form with the right
        /// values
        /// </summary>
        /// <param name="givenCardBox">The Object itself</param>
        /// <param name="givenCard">The chosen card that will be used for the card itself</param>
        /// <param name="isFaceUp">Makes sure if the card is faced up or not</param>
        /// <param name="isVert">Checks if the card is vertical or horizontal</param>
        /// <param name="givenPosX">Takes in the x position of the CardBox </param>
        /// <param name="givenPosY">Takes in the y position of the CardBox  </param>
        /// <returns></returns>
        private CardBox.CardBox setCardBoxValues(CardBox.CardBox givenCardBox, Card givenCard, bool isFaceUp, bool isVert, int givenPosX, int givenPosY)
        {
            givenCardBox.Location        = new Point(givenPosX, givenPosY);
            givenCardBox.Name            = "cardboxPlayer" + cardNumber;
            givenCardBox.FaceUp          = isFaceUp;
            givenCardBox.CardOrientation = isVert? Orientation.Vertical : Orientation.Horizontal;
            givenCardBox.Rank            = givenCard.rank;
            givenCardBox.Suit            = givenCard.suit;

            return(givenCardBox);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Adds mouse and drag-and-drop events to each cardbox instance
        /// </summary>
        /// <param name="aCardBox">The cardbox that drag-and-drop, as well as click events are to be wired to</param>
        /// TODO: If there are any other events to be wired, add them here (GOOD FOR NOW)
        private void WireCardBoxEventHandlers(CardBox.CardBox aCardBox)
        {
            //wire cardbox mouse enter
            aCardBox.MouseEnter += CardBox_MouseEnter;
            //wire cardbox mouse leave
            aCardBox.MouseLeave += CardBox_MouseLeave;

            aCardBox.MouseDown += CardBox_MouseDown;
            aCardBox.DragEnter += CardBox_DragEnter;
            aCardBox.DragDrop  += CardBox_DragDrop;
        }
Exemplo n.º 9
0
        /// <summary>
        /// When a drag is dropped on a card, drop on the parent panel instead.
        /// </summary>
        private void CardBox_DragDrop(object sender, DragEventArgs e)
        {
            // Convert sender to a CardBox
            CardBox.CardBox aCardBox = sender as CardBox.CardBox;

            // If the conversion worked
            if (aCardBox != null)
            {
                // Do the operation on the parent panel instead
                Panel_DragDrop(aCardBox.Parent, e);
            }
        }
Exemplo n.º 10
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 void CardBox_MouseLeave(object sender, EventArgs e)
 {
     //Convert sender to a cardbox
     CardBox.CardBox aCardBox = sender as CardBox.CardBox;
     //if the conversion worked
     if (aCardBox != null)
     {
         //Return to normal
         aCardBox.Size = normalCardSize;
         //Move the card to the top edge of the panel
         aCardBox.Top = ENLARGE;
     }
 }
Exemplo n.º 11
0
        /// <summary>
        /// Make a card bigger when entering its box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CardBox_MouseEnter(object sender, EventArgs e)
        {
            //Convert sender to a cardbox
            CardBox.CardBox aCardBox = sender as CardBox.CardBox;

            //if the conversion worked
            if (aCardBox != null)
            {
                //Enlarge
                aCardBox.Size = new Size(normalCardSize.Width + ENLARGE, normalCardSize.Height + ENLARGE);
                //Move the card to the top edge of the panel
                aCardBox.Top = 0;
            }
        }
Exemplo n.º 12
0
        public void HandClickHandler(object sender, EventArgs e)
        {
            CardBox.CardBox cardBoxClicked = (CardBox.CardBox)sender;
            PlayingCard     cardClicked    = cardBoxClicked.Card;

            if (game.Players[0].PlayableCards.Contains(cardClicked))
            {
                if (game.Players[0].Status == Statuses.Attacking)
                {
                    game.Players[0].Play(cardClicked, game.River);
                    game.Players[0].firstTurn = false;
                }
                else if ((game.Players[0].Status == Statuses.Defending) && game.Players[0].firstTurn)
                {
                    if (cardClicked.Rank == game.River[0].Rank)
                    {
                        game.Transfer();
                        MessageBox.Show("You transferred to your opponent, you are now attacking!", "Transfer Warning");
                    }
                    game.Players[0].Play(cardClicked, game.River);
                    game.Players[0].firstTurn = false;
                }
                else
                {
                    game.Players[0].Play(cardClicked, game.River);
                }



                if (game.Players[1].Status == Statuses.Attacking)
                {
                    game.Players[1].Attack(game);
                    game.Players[1].firstTurn = false;
                }
                else if (game.Players[1].Status == Statuses.Defending)
                {
                    game.Players[1].Defend(game);
                }
            }

            if (game.Players[0].Hand.Count == 0 && game.River.Count == 0 && game.Deck.Cards.Count == 0)
            {
                WinMessage();
            }

            if (game.Players[1].Hand.Count == 0 && game.River.Count == 0 && game.Deck.Cards.Count == 0)
            {
                LoseMessage();
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// This method is used to set the CardBox objects for
        /// both players and playing field
        /// </summary>
        private void setNewCardField()
        {
            //Clears the cards from the form
            removeAllCardBox();
            defaultValues();

            //The player's hand is shown up
            foreach (Card card in players[0].PlayHand)
            {
                cardBoxObject        = new CardBox.CardBox();
                cardBoxObject        = setCardBoxValues(cardBoxObject, card, true, true, PlayPosX, PlayPosY);
                cardBoxObject.Click += new EventHandler(CardboxTest_Click);

                this.Controls.Add(cardBoxObject);
                cardNumber++;
                PlayPosX += 40;
            }

            //The AI Hand is shown up
            foreach (Card card in players[1].PlayHand)
            {
                cardBoxObject = new CardBox.CardBox();
                cardBoxObject = setCardBoxValues(cardBoxObject, card, true, true, PlayAiPosX, PlayAiPosY);
                this.Controls.Add(cardBoxObject);

                cardNumber++;
                PlayAiPosX += 60;
            }

            //Sets the middle playing cards
            foreach (Card card in cardsDealt)
            {
                cardBoxObject = new CardBox.CardBox();
                cardBoxObject = setCardBoxValues(cardBoxObject, card, true, true, dealtX, dealtY);

                this.Controls.Add(cardBoxObject);

                cardNumber++;
                dealtX += 60;
            }

            //Checks if there is more cards up or not
            if (cardsDealt.Count >= MAX_NUMBER_OF_PLAYING_CARDS)
            {
                lblWarning.Text = "Cannot have anymore cards";
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Computers successive attack movements after initial attack is successfully defended
        /// </summary>
        /// <param name="card1"></param>
        /// <param name="card2"></param>
        private void ComputerSuccessiveAttacks(CardBox.CardBox card1, CardBox.CardBox card2)
        {
            Dictionary <int, CardBox.CardBox> validCards = new Dictionary <int, CardBox.CardBox>();
            int cardIndex = 0;

            for (int i = 1; i < pnlComputerCards.Controls.Count; i++)
            {
                CardBox.CardBox tempCard = (CardBox.CardBox)pnlComputerCards.Controls[i];

                if (tempCard.Rank == card1.Rank || tempCard.Rank == card2.Rank)
                {
                    validCards.Add(i, tempCard);
                    cardIndex = i;
                }
            }

            if (validCards.Count == 0)
            {
                logs.WriteLine("Computer can no longer attacks. Switching turns.");
                MoveCards(pnlDefended, pnlDiscard);
                RoundDeal();

                txtComputerAttacker.Visible = false;

                playerAttacking = true;
                ReenableAllCards();
            }
            //generates a random number between 0 and the number of cards the computer has
            if (playerAttacking == false)
            {
                CardBox.CardBox computerCard = validCards[cardIndex];
                logs.WriteLine("Computer attacks with " + computerCard.ToString());
                pnlComputerCards.Controls.Remove(computerCard); //remove the card from the computers hand
                pnlActiveCards.Controls.Add(computerCard);      //place the card into the active play panel

                Wait(1500);

                DisableInvalidPlayerDefenseChoices(computerCard);
            }
            UpdateDefendedAndDiscardPanelControls();
            DisableInvalidCardsInHands();
        }
Exemplo n.º 15
0
        /// <summary>
        /// Move a card/control when it is dropped from one Panel to another.
        /// </summary>
        private void Panel_DragDrop(object sender, DragEventArgs e)
        {
            if (dragCard != null)
            {
                Panel           thisPanel = sender as Panel;
                Panel           fromPanel = dragCard.Parent as Panel;
                CardBox.CardBox aCardBox  = new CardBox.CardBox();

                if (thisPanel != null && fromPanel != null)
                {
                    if (thisPanel != fromPanel)
                    {
                        fromPanel.Controls.Remove(dragCard);
                        thisPanel.Controls.Add(dragCard);

                        logs.WriteLine("Players plays " + dragCard.ToString());

                        RealignCards(thisPanel);
                        RealignCards(fromPanel);
                        /************************ATTACKING PLAYER LOGIC**************************************/
                        // Check if the player is attacking or defending, then trigger the appropriate events
                        if (playerAttacking)
                        {
                            ComputerDefends();
                        }
                        /************************DEFENDING PLAYER LOGIC**************************************/
                        else if (playerAttacking == false)
                        {
                            MoveCards(pnlActiveCards, pnlDefended);

                            CardBox.CardBox attackCard  = (CardBox.CardBox)pnlDefended.Controls[0];
                            CardBox.CardBox defenseCard = (CardBox.CardBox)pnlDefended.Controls[1];

                            ComputerSuccessiveAttacks(attackCard, defenseCard);
                        }
                    }
                }
                RealignAllCards();
                UpdateDefendedAndDiscardPanelControls();
            }
        }
Exemplo n.º 16
0
        public void UpdateRiver(object sender, EventArgs e)
        {
            pnlRiver.Controls.Clear();

            int offset = 0;

            for (int i = 0; i < game.River.Count; i++)
            {
                CardBox.CardBox cbx = new CardBox.CardBox
                {
                    Visible = true,
                    Card    = game.River[i],
                    Width   = cbxRiver.Width,
                    Height  = cbxRiver.Height,
                    Left    = cbxRiver.Left + ((i * (cbxRiver.Width / 3)) + offset),
                    Top     = cbxRiver.Top
                };
                offset += 10;
                this.pnlRiver.Controls.Add(cbx);
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// This helper function will move all the cards from one panel to another. Used for moving active, or successfully defended cards to the discard pile. Or using it to
        /// move the active cards to a failed defense players hand.
        /// </summary>
        /// <param name="panelWithCards">The panel where the cards currently reside</param>
        /// <param name="panelCardsGoTo">The panel we want the cards to move to</param>
        private void MoveCards(Panel panelWithCards, Panel panelCardsGoTo)
        {
            foreach (CardBox.CardBox card in panelWithCards.Controls)
            {
                //Make it a cardbox for the player
                CardBox.CardBox movedCard = new CardBox.CardBox(card.Card);
                movedCard.Size = normalCardSize; //resize the card, in case it was already the appropriate size

                //Add cardbox to panel
                panelCardsGoTo.Controls.Add(movedCard);

                //RealignDefendedCards();
                panelCardsGoTo.Controls[0].Top  = 38;
                panelCardsGoTo.Controls[0].Left = 5;
            }
            //remove the cards from the active panel
            while (panelWithCards.Controls.Count > 0)
            {
                panelWithCards.Controls.RemoveAt(0);
            }
            ReenableAllCards();
        }
Exemplo n.º 18
0
        //  private void CanPlayerContinueAttack(Panel playerPanel, CardBox.CardBox)

        /// <summary>
        /// Compares the attacking and defending cards and establishes which cards should have their functionality disabled if they are not possible options for moving
        /// the game forward. Modelled on SuperDurak tutorial referenced in final project outline document.
        /// </summary>
        /// <param name="attackingCard">The CardBox object presented by the attacking player</param>
        /// <param name="defendingCard">The CardBox object presented by the defending player</param>
        /// <param name="initialAttackDefended">A boolean representing the status of the initial attack having been successful defended - significant for disabling of
        /// invalid card selections within player hand</param>
        private void CompareCards(CardBox.CardBox attackingCard, CardBox.CardBox defendingCard, bool initialAttackDefended)
        {
            if (playerAttacking)
            {
                this.initialAttackDefended = true;
                MoveCards(pnlActiveCards, pnlDefended);     //Move cards from active panel to successfully defended panel
            }
            else
            {
                if (playerAttacking == false)
                {
                    DisableInvalidPlayerDefenseChoices(attackingCard);
                }
                bool playerHasChoices = false;

                foreach (Control playerCard in pnlPlayerCards.Controls)
                {
                    if (playerCard.Enabled == true)
                    {
                        playerHasChoices = true;
                    }
                }

                // If all the players cards are disabled as invalid choices, inform them of their loss
                if (playerHasChoices == false)
                {
                    MoveCards(pnlDefended, pnlPlayerCards);
                    MoveCards(pnlActiveCards, pnlPlayerCards);
                    RoundDeal();     //deal back to 6 cards
                    txtComputerAttacker.Visible = false;
                    playerAttacking             = true;

                    Wait(1500);
                }
            }
            disableInvalidChoices(attackingCard.Card.Rank, defendingCard.Card.Rank);
            rankOfLastDefense = defendingCard.Rank;
        }//end of COMPARECARDS
Exemplo n.º 19
0
        private void ComputerDefends()
        {
            //AI DEFENSE Logic
            // AI Function to determine best card
            int computerChoiceIndex = determineBestPlay(pnlComputerCards);

            if (computerChoiceIndex >= 0) //Computer Can Defend
            {
                CardBox.CardBox computerCardBox = pnlComputerCards.Controls[computerChoiceIndex] as CardBox.CardBox;
                pnlComputerCards.Controls.Remove(computerCardBox); //remove from computers hand
                pnlActiveCards.Controls.Add(computerCardBox);      //add to the active play panel

                PlayingCard card = computerCardBox.Card;
                card.FaceUp = true;

                logs.WriteLine("Computer responds with " + card.ToString());

                //Compares cards in players hands, determines if they can attack again by comparing their hand to pair in active panel
                CardBox.CardBox tempCard = (CardBox.CardBox)pnlActiveCards.Controls[0];

                CompareCards(tempCard, computerCardBox, this.initialAttackDefended); //deciding which cards can be played on a successive attack
                ReenableAllCards();
                disableInvalidChoices(tempCard.Rank, computerCardBox.Card.Rank);
            }
            else //Computer Cannot Defend
            {
                MoveCards(pnlDefended, pnlComputerCards);
                MoveCards(pnlActiveCards, pnlComputerCards);

                ReenableAllCards();
                RealignAllCards();
                txtComputerAttacker.Visible = true;
                playerAttacking             = false;

                ComputerAttacks();
            }
            //DisableInvalidCardsInHands();
        }
Exemplo n.º 20
0
        /// <summary>
        /// Event fired when a CardBox is clicked.
        /// </summary>
        /// <param name="sender">Sending CardBox</param>
        /// <param name="e"></param>
        void CardBoxClick(object sender, EventArgs e)
        {
            //  Convert sender to a CardBox
            CardBox.CardBox aCardBox = sender as CardBox.CardBox;

            if (aCardBox != null)
            {
                if (aCardBox.Parent == pnlPlayerHand)
                {
                    //System.Diagnostics.Debug.WriteLine("clicked on a player's card: " + aCardBox.Card.ToString());

                    Player player = Players.GetHumanPlayer();

                    //System.Diagnostics.Debug.WriteLine("current player is: " + player.Name);

                    //  Check to ensure it is the player's turn to attack or defend
                    if (player == attacker || player == defender)
                    {
                        System.Diagnostics.Debug.WriteLine(player.Name + " is playing: " + aCardBox.Card.ToString());
                        PlayerTurn(aCardBox.Card);
                    }
                }
            }
        }
Exemplo n.º 21
0
        /// <summary>
        /// initialDeal - deals players 6 cards to start
        /// </summary>
        private void InitialDeal()
        {
            cbxDeck.FaceUp = false;

            PlayingCard playersCard;
            PlayingCard AIsCard;
            PlayingCard lowestCard = new PlayingCard(PlayingCard.trumpSuit, CardRank.Ace); // start as the highest possible card

            // setting the first card
            cbxDeck.Card = mainDeck.GetCard(0);
            mainDeck.DrawCard();
            logs.WriteLine("Initial Deal Starting: \n");
            for (int i = 0; i < 6; i++)
            {
                PlayingCard card = cbxDeck.Card;

                if (card != null) //if card isn't null
                {
                    card.FaceUp = true;

                    //Make it a cardbox for the player
                    CardBox.CardBox playerCardBox = new CardBox.CardBox(card);
                    playersCard = card;

                    playerCardBox.Size = normalCardSize;

                    //Wire events
                    WireCardBoxEventHandlers(playerCardBox);
                    //playerCardBox.Click += CardBox_Click; //When the player clicks a card in their hand

                    //Add cardbox to panel
                    pnlPlayerCards.Controls.Add(playerCardBox);
                    logs.WriteLine("Player draws " + playerCardBox.ToString());
                    cbxDeck.Card = mainDeck.DrawCard();

                    card = cbxDeck.Card;
                    CardBox.CardBox computerCardBox = new CardBox.CardBox(card);
                    AIsCard = card;

                    computerCardBox.Size = normalCardSize;
                    //Make a cardbox for the computer
                    pnlComputerCards.Controls.Add(computerCardBox);

                    PlayingCard cardToLog = computerCardBox.Card;
                    cardToLog.FaceUp = true;

                    logs.WriteLine("Computer draws " + cardToLog.ToString());
                    cbxDeck.Card = mainDeck.DrawCard();

                    // determine who has the lowest trump card
                    // If no entity has a trump card then the player goes first
                    if (AIsCard < lowestCard && AIsCard.Suit == PlayingCard.trumpSuit)
                    {
                        lowestCard = AIsCard;
                        firstTurn  = "the AI";
                    }
                    if (playersCard < lowestCard && playersCard.Suit == PlayingCard.trumpSuit)
                    {
                        lowestCard = playersCard;
                        firstTurn  = "the player";
                    }
                }
            }

            ReenableAllCards();
            RealignAllCards();
        }
Exemplo n.º 22
0
        /// <summary>
        /// This Click method happens for the player's CardBox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CardboxTest_Click(object sender, EventArgs e)
        {
            int cardClickedValue   = 0;
            int lastCardDealtValue = 0;

            lblMessage.Text = "";

            //Sets the player to become the defender and the AI being the Attacker
            if (cardsDealt.Count == 0)
            {
                players[0].isAttack = false; players[1].isAttack = true; btnClearCards.Text = TAKE_BUTTON;
            }

            //Clicked card is set up to check values
            CardBox.CardBox item = (CardBox.CardBox)sender;

            DurakLog("\tPlayer Clicked on " + item.ToString());

            //Checks if playing cards has less than 6 cards
            if (cardsDealt.Count <= MAX_NUMBER_OF_PLAYING_CARDS)
            {
                //Checks if the there is any cards on the playing field
                if (cardsDealt.Count > 0)
                {
                    //Sets the cards values
                    cardClickedValue   = (int)item.Rank;
                    lastCardDealtValue = (int)cardsDealt.Last <Card>().rank;

                    //Checks if the clicked object is a trump card
                    if (item.Suit == trumpCard.Suit)
                    {
                        cardClickedValue += 10;
                    }
                    //Checks if the last card on the field, being a trump card
                    if (cardsDealt.Last <Card>().suit == trumpCard.Suit)
                    {
                        lastCardDealtValue += 10;
                    }

                    //Checks if the clicked card value is equal or greater than the last card dealt
                    if (cardClickedValue >= lastCardDealtValue)
                    {
                        DurakLog("\t\t(" + item.ToString() + ") beats last playing card (" + cardsDealt.Last <Card>().ToString() + ")");
                        Card clickedCard = new Card(item.Suit, item.Rank);
                        cardsDealt.Add(clickedCard);

                        players[0].PlayHand.Remove(clickedCard);

                        //Checks if there is enough space for AI to play
                        if (cardsDealt.Count < MAX_NUMBER_OF_PLAYING_CARDS)
                        {
                            //Lets the AI play next
                            if (players[1].PlayHand.Count > 0)
                            {
                                playAiPlayer();
                            }

                            //If AI makes the playing cards full, then it discards the cards.
                            if (cardsDealt.Count == MAX_NUMBER_OF_PLAYING_CARDS)
                            {
                                DurakLog("\tAI Player made the cards full, discarded the cards in playing field");

                                cardsDealt = new Cards();
                                addMoreCardsToPlayerHands();

                                //Makes the player defend and AI attacks
                                if (cardsDealt.Count == 0)
                                {
                                    players[0].isAttack = false; players[1].isAttack = true; btnClearCards.Text = TAKE_BUTTON;
                                }

                                showWhoseAttackDefend();
                            }
                        }
                        else //AI has to discard, since the player played the last card to reach the maximum cards in play
                        {
                            cardsDealt = new Cards();

                            AiStartsNewPlayingCards();
                        }


                        setNewCardField();
                    }
                    else//Shows a message that it is not able to be played
                    {
                        lblMessage.Text = item.ToString() + " cannot beat " + cardsDealt.Last <Card>().ToString();
                        DurakLog("\t\t(" + item.ToString() + ") cannot beat last playing card (" + cardsDealt.Last <Card>().ToString() + ")");
                    }
                }
                else //Sets the first card for the playing field from the Player themself
                {
                    Card setCard = new Card(item.Suit, item.Rank);
                    cardsDealt.Add(setCard);

                    players[0].PlayHand.Remove(setCard);
                    DurakLog("\t\t(" + item.ToString() + ") starts the playing cards");

                    //checks if AI player has enough cards
                    if (players[1].PlayHand.Count > 0)
                    {
                        playAiPlayer();
                    }

                    setNewCardField();
                }

                //If AI Player wins or Player Wins
                if (players[1].PlayHand.Count == 0 || players[0].PlayHand.Count == 0)
                {
                    setWinner();
                }
            }
            else
            {
                DurakLog("\t\t\tPlaying field is full");
                lblWarning.Text = "Cannot have anymore cards";
            }
        }
Exemplo n.º 23
0
        /// <summary>
        /// Basic algorithm to determine best choice, based on lowest value card that can be opposing hand. If no options exist, defaults to index 9.
        /// </summary>
        /// <param name="computerHand">The computer's current hand that the best possible choice is being determines from</param>
        /// <returns>bestChoiceIndex - the best possible choice the computer could make by considering entire hand.</returns>
        protected int determineBestPlay(Panel computerHand)
        {
            bool noGoodChoice = true;
            // Keep track of best player choice based on nearest winnable rank to attacking card
            int idealChoiceIndex = 0;

            CardBox.CardBox cardToBeat = pnlActiveCards.Controls[pnlActiveCards.Controls.Count - 1] as CardBox.CardBox;

            // See what is being retrieved from the computer's hand of cards
            for (int i = 0; i < computerHand.Controls.Count; i++)
            {
                CardBox.CardBox idealChoice = computerHand.Controls[idealChoiceIndex] as CardBox.CardBox;

                // Ensure only CardBox instances are being compared to the player's selected card
                if (computerHand.Controls[i].GetType().ToString().Contains("CardBox"))
                {
                    CardBox.CardBox currentCard = computerHand.Controls[i] as CardBox.CardBox;

                    if (currentCard.Card > cardToBeat.Card)
                    { //current card wins
                        noGoodChoice = false;

                        // Check to see if option to beat player card is a more efficient (AKA lower value card that current selection) way to beat the opponent and reserve high ranking cards for later
                        if (currentCard.Card < idealChoice.Card)
                        {
                            idealChoiceIndex = i;
                        }


                        idealChoiceIndex = i;
                    }
                    //else if (cardToBeat.Card.Suit == cbxTrumpCard.Card.Suit && (currentCard.Card.Suit != cbxTrumpCard.Card.Suit))
                    //{ //cardto beat wins

                    //    txtPlayHistory.Text += Environment.NewLine + currentCard.Rank + " of " + currentCard.Suit + " CANNOT win against the opponent's " + cardToBeat.Rank + " of " + cardToBeat.Suit;
                    //    //DELETE FOR SUBMISSION
                    //}
                    else //if neither cards are trump, or both cards are
                    {
                        if (currentCard.Card > cardToBeat.Card)  //win
                        {
                            noGoodChoice = false;
                            // txtPlayHistory.Text += Environment.NewLine + currentCard.Rank + " of " + currentCard.Suit + " could win against the opponent's " + cardToBeat.Rank + " of " + cardToBeat.Suit
                            //                    + "   " + i + "  " + pnlComputerCards.Controls.IndexOf(currentCard).ToString(); //DELETE FOR SUBMISSION
                            idealChoiceIndex = i;
                            //end players turn
                        }
                    }
                }
            }//end of loop

            // If the computer has no cards prospective to attack or defend, admit defeat and pass the attack or return value that will cause computer to
            // take the discarded cards
            if (noGoodChoice) //computer wins
            {
                logs.WriteLine("Computer cannot defend, player wins the attack");

                txtComputerAttacker.Visible = true;
                playerAttacking             = false;
                RoundDeal();
                ReenableAllCards();
                return(-1);
            }

            return(idealChoiceIndex);
        }