Пример #1
0
        /// <summary>
        /// Defend override method for processing a computer ai's defence to an attack
        /// </summary>
        /// <param name="trumpSuit"></param>
        /// <param name="bout"></param>
        /// <returns>CardBox representing the card played</returns>
        public override CardBox Defend(Suit trumpSuit, List <CardBox> bout)
        {
            CardBox playedCard = null;                                                                                                           //create a new cardbox for storing the card to be played, default set to null

            Thread.Sleep(3000);                                                                                                                  //simulate thinking of computer with 5 second delay

            foreach (CardBox handCard in myCardBoxes)                                                                                            //loop through hand of cards
            {
                if (handCard.MyCard.MySuit == bout[bout.Count - 1].MyCard.MySuit && handCard.MyCard.MyRank > bout[bout.Count - 1].MyCard.MyRank) //check if the current card in hand is of same suit as the last played card in the bout and of higher rank
                {
                    playedCard = handCard;                                                                                                       //set the playedCard to the current hand card
                    Remove(handCard.MyCard);                                                                                                     //remove the hand card from cardboxes
                    return(playedCard);                                                                                                          //return the bout with the new card added
                }
                else if (handCard.MyCard.MySuit == trumpSuit && bout[bout.Count - 1].MyCard.MySuit != trumpSuit)                                 //check if current card in hand is of same suit as trump suit and the last played card in the bout is not of trump suit
                {
                    playedCard = handCard;                                                                                                       //set the playedCard to the current hand card
                    Remove(handCard.MyCard);                                                                                                     //remove the hand card from cardboxes
                    return(playedCard);                                                                                                          //return the bout with the new card added
                }
            }

            //no cards were playable
            return(playedCard); //return null playedCard
        }
Пример #2
0
        /// <summary>
        /// DisplayAllCards Method for testing the display of all cardBoxes
        /// </summary>
        /// <param name="form">Default = null</param>
        public void DisplayAllCards(Form form = null)
        {
            if (form != null)                                             //check if form is not null
            {
                int       positionX  = 0;                                 //integer for storing x position of cardbox control
                CardBox[] cardBoxes  = new CardBox[(int)MyDeck.DeckSize]; //create array for holding all cards as card boxes
                ComboBox  cmbTestBox = new ComboBox();                    //combobox for storing all cards in text form
                cmbTestBox.Location = new Point(300, 300);                //set combobox location

                for (int x = 0; x < MyDeck.DeckSize; x++)                 //loop through deck
                {
                    form.Invoke((MethodInvoker) delegate
                    {
                        cardBoxes[x] = new CardBox(MyDeck.Cards[x]);     //create a new cardbox of the current card

                        cardBoxes[x].FaceUp = true;                      //set faceup

                        cardBoxes[x].Location = new Point(positionX, 0); //set cardbox position

                        form.Controls.Add(cardBoxes[x]);                 //add cardbox to form

                        cmbTestBox.Items.Add(cardBoxes[x]);              //add cardbox to combobox
                    });

                    positionX += 30; //increase position x
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Attack override method for processing a computer ai attack
        /// </summary>
        /// <param name="trumpSuit"></param>
        /// <param name="bout">Default = null</param>
        /// <returns>CardBox representing the card played</returns>
        public override CardBox Attack(Suit trumpSuit, List <CardBox> bout = null)
        {
            CardBox playedCard = null;           //create a new cardbox for storing the card to be played, default set to null

            Thread.Sleep(3000);                  //simulate thinking of computer with 5 second delay

            if (bout == null || bout.Count == 0) //check if no cards have been played in the bout yet
            {
                playedCard = myCardBoxes[0];     //set the playedCard to the first card in computers hand
                Remove(myCardBoxes[0].MyCard);   //remove the first card in hand
                return(playedCard);              //return the bout with the new card added
            }
            else //cards have already been played in the bout
            {
                foreach (CardBox boutCard in bout)                            //loop through bout of cards
                {
                    foreach (CardBox handCard in myCardBoxes)                 //loop through hand of cards
                    {
                        if (handCard.MyCard.MyRank == boutCard.MyCard.MyRank) //check if the current card in hand is of same rank as current card in bout
                        {
                            playedCard = handCard;                            //set the card of the same rank from hand
                            Remove(handCard.MyCard);                          //remove the card of the same rank from hand
                            return(playedCard);                               //return the bout with the new card added
                        }
                    }
                }
            }

            //no cards were playable
            return(playedCard); //return null playedCard
        }
Пример #4
0
        /// <summary>
        /// Defend virtual method for representing a defense from a player
        /// </summary>
        /// <param name="trumpSuit"></param>
        /// <param name="bout"></param>
        /// <returns></returns>
        public virtual CardBox Defend(Suit trumpSuit, List <CardBox> bout = null)
        {
            CardBox firstCard = myCardBoxes[0]; //create a new cardbox for storing the first card in players hand

            myCardBoxes.Remove(myCardBoxes[0]); //remove the first card in hand

            return(firstCard);                  //return the first card in players hand
        }
Пример #5
0
        /// <summary>
        /// CardBox click event handler.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Card_Click(object sender, EventArgs e)
        {
            // Send CardBox that was clicked to a method.
            CardBox cbBox = (CardBox)sender;

            AddCardToBout(cbBox);
            myPlayers[0].Remove(cbBox.MyCard);
            SoundPlayer audio = new SoundPlayer(CardGameLibrary.Properties.Resources.flip_sound); //create new soundplayer for the card flip sound

            audio.PlaySync();                                                                     //play the card flip sound on same thread haulting program
            phaseOver = true;
        }
Пример #6
0
        /// <summary>
        /// Processes an attack turn for the player passed to it.
        /// </summary>
        /// <param name="player"></param>
        public void ProcessTurn(int playerIndex)
        {
            turnOver = false;     //boolean for storing wheter a turn is over or not

            if (playerIndex == 1) //check if it's the computer players turn
            {
                while (turnOver == false)
                {
                    #region computer player attacks

                    myForm.Invoke((MethodInvoker) delegate                           //use the Invoke method to safely alter a control of the ui thread
                    {
                        promptLabel.Text = myPlayers[1].MyName + "'s attack phase."; //alter the prompt label's text
                    });

                    CardBox tempCard = myPlayers[1].Attack(trump, firstBout);                                 //create a temp card and set it to the result of the computers attack

                    if (tempCard != null)                                                                     //check if the computer had a card to play
                    {
                        AddCardToBout(tempCard);                                                              //add computer played card to bout
                        SoundPlayer audio = new SoundPlayer(CardGameLibrary.Properties.Resources.flip_sound); //create new soundplayer for the card flip sound
                        audio.PlaySync();                                                                     //play the card flip sound on same thread haulting program
                    }
                    else //the computer didn't have a card to play
                    {
                        myDiscardPile.AddRange(firstBout); //add the bout to the discard pile
                        firstBout.Clear();                 //clear the cards from the bout
                        DisplayBout(myForm);               //display the bout
                        DisplayDiscardPile(myForm);        //display the discard pile
                        turnOver = true;                   //set turn over to true
                        nextTurn = 0;                      //set the next turn to 0 representing the human
                        break;                             //break from loop
                    }

                    #endregion

                    #region human player defends

                    myForm.Invoke((MethodInvoker) delegate                            //use the Invoke method to safely alter a control of the ui thread
                    {
                        promptLabel.Text = myPlayers[0].MyName + "'s defense phase."; //alter the prompt label's text
                    });

                    foreach (CardBox cardBox in myPlayers[0].myCardBoxes)                                                                                                  //loop through human players hand of cards
                    {
                        if (cardBox.MyCard.MySuit == firstBout[firstBout.Count - 1].MyCard.MySuit && cardBox.MyCard.MyRank > firstBout[firstBout.Count - 1].MyCard.MyRank) //check if the current card in hand is of same suit as the last played card in the bout and of higher rank
                        {
                            myForm.Invoke((MethodInvoker) delegate                                                                                                         //use the Invoke method to safely alter a control of the ui thread
                            {
                                cardBox.Enabled = true;                                                                                                                    //enable the current human player card
                            });
                        }
                        else if (cardBox.MyCard.MySuit == trump && firstBout[firstBout.Count - 1].MyCard.MySuit != trump) //check if current card in hand is of same suit as trump suit and the last played card in the bout is not of trump suit
                        {
                            myForm.Invoke((MethodInvoker) delegate                                                        //use the Invoke method to safely alter a control of the ui thread
                            {
                                cardBox.Enabled = true;                                                                   //enable the current human player card
                            });
                        }
                    }

                    myForm.Invoke((MethodInvoker) delegate //use the Invoke method to safely alter a control of the ui thread
                    {
                        passButton.Enabled = true;         //enable the pass button
                    });

                    while (phaseOver == false) //loop until the human player phase is over
                    {
                        //waiting for pass button to be clicked or a cardbox to be clicked
                    }

                    foreach (CardBox cardBox in myPlayers[0].myCardBoxes.ToList()) //loop through human players hand of cards
                    {
                        myForm.Invoke((MethodInvoker) delegate                     //use the Invoke method to safely alter a control of the ui thread
                        {
                            cardBox.Enabled = false;                               //disable the current human player card
                        });
                    }

                    myForm.Invoke((MethodInvoker) delegate //use the Invoke method to safely alter a control of the ui thread
                    {
                        passButton.Enabled = false;        //disable the pass button
                    });

                    phaseOver = false; //set phaseOver to false for next phase

                    #endregion
                }
            }
            else if (playerIndex == 0) //check if it's the human players turn
            {
                while (turnOver == false)
                {
                    #region human player attacks

                    myForm.Invoke((MethodInvoker) delegate                           //use the Invoke method to safely alter a control of the ui thread
                    {
                        promptLabel.Text = myPlayers[0].MyName + "'s attack phase."; //alter the prompt label's text
                    });

                    if (firstBout == null || firstBout.Count == 0) //check if no cards have been played in the bout yet
                    {
                        //enable all cards in human players hand
                        foreach (CardBox cardBox in myPlayers[0].myCardBoxes) //loop through human players hand of cards
                        {
                            myForm.Invoke((MethodInvoker) delegate            //use the Invoke method to safely alter a control of the ui thread
                            {
                                cardBox.Enabled = true;                       //enable the current human player card
                            });
                        }
                    }
                    else //cards have already been played in the bout
                    {
                        foreach (CardBox boutCard in firstBout) //loop through bout of cards
                        {
                            foreach (CardBox handCard in myPlayers[0].myCardBoxes)    //loop through hand of cards
                            {
                                if (handCard.MyCard.MyRank == boutCard.MyCard.MyRank) //check if the current card in hand is of same rank as current card in bout
                                {
                                    myForm.Invoke((MethodInvoker) delegate            //use the Invoke method to safely alter a control of the ui thread
                                    {
                                        handCard.Enabled = true;                      //enable the current human player card
                                    });
                                }
                            }
                        }
                    }

                    myForm.Invoke((MethodInvoker) delegate //use the Invoke method to safely alter a control of the ui thread
                    {
                        passButton.Enabled = true;         //enable the pass button
                    });

                    while (phaseOver == false) //loop until the human player phase is over
                    {
                        //waiting for pass button to be clicked or a cardbox to be clicked
                    }

                    foreach (CardBox cardBox in myPlayers[0].myCardBoxes.ToList()) //loop through human players hand of cards
                    {
                        myForm.Invoke((MethodInvoker) delegate                     //use the Invoke method to safely alter a control of the ui thread
                        {
                            cardBox.Enabled = false;                               //disable the current human player card
                        });
                    }

                    myForm.Invoke((MethodInvoker) delegate //use the Invoke method to safely alter a control of the ui thread
                    {
                        passButton.Enabled = false;        //disable the pass button
                    });

                    phaseOver = false;    //set phaseOver to false for next phase

                    if (turnOver == true) //check if turnOver is true
                    {
                        break;            //break from loop
                    }

                    #endregion

                    #region computer player defends

                    myForm.Invoke((MethodInvoker) delegate                            //use the Invoke method to safely alter a control of the ui thread
                    {
                        promptLabel.Text = myPlayers[1].MyName + "'s defense phase."; //alter the prompt label's text
                    });

                    CardBox tempCard = myPlayers[1].Defend(trump, firstBout);                                 //create a temp card and set it to the result of the computers attack

                    if (tempCard != null)                                                                     //check if the computer had a card to play
                    {
                        AddCardToBout(tempCard);                                                              //add computer played card to bout
                        SoundPlayer audio = new SoundPlayer(CardGameLibrary.Properties.Resources.flip_sound); //create new soundplayer for the card flip sound
                        audio.PlaySync();                                                                     //play the card flip sound on same thread haulting program
                    }
                    else //the computer didn't have a card to play
                    {
                        foreach (CardBox cardBox in firstBout)        //loop through cards in bout
                        {
                            cardBox.FaceUp = false;                   //filp card face down
                        }
                        myPlayers[1].myCardBoxes.AddRange(firstBout); //add the bout cards to the computer players hand
                        myPlayers[1].DisplayHand(myForm);             //display computer players hand
                        firstBout.Clear();                            //clear the bout
                        DisplayBout(myForm);                          //display the bout
                        turnOver = true;                              //set turn over to true
                        break;                                        //break from loop
                    }

                    #endregion
                }
            }
        }
Пример #7
0
 /// <summary>
 /// Adds a card to a bout.
 /// </summary>
 /// <param name="cbInput"></param>
 public void AddCardToBout(CardBox cbInput)
 {
     firstBout.Add(cbInput); //add cardbox to bout
     DisplayBout(myForm);    //display the new bout
 }
Пример #8
0
        /// <summary>
        /// Game constructor that can take a deck size and a windows form
        /// </summary>
        /// <param name="deckSize">Default = 36</param>
        /// <param name="form">Default = null</param>
        /// <param name="playerName">Default = "Player 1"</param>
        public Game(int deckSize = 36, Form form = null, string playerName = "Player 1")
        {
            myForm = form;                                                //set myForm to entered form

            myDeck = new StandardDeck(deckSize);                          // Create a deck based on the deck size arugment and assign to myDeck.

            myDealer = new CardDealer(myDeck, form);                      // Create a new dealer with myDeck and assign to myDealer.

            myPlayers.Add(new Human(newName: playerName, newForm: form)); //add new real player to players list

            myPlayers.Add(new Computer("Computer 1", newForm: form));     //add new computer player to players list

            foreach (Player player in myPlayers)                          //deal out 6 cards for each player
            {
                if (form != null)                                         //check if form is not null
                {
                    myDealer.DealHand(player, 6, form);                   //deal out 6 card for player passing along entered form
                }
                else //form is null
                {
                    myDealer.DealHand(player, 6); //deal out 6 card for player passing along entered form
                }
            }

            trumpCard             = new CardBox(myDealer.SelectTrumpCard()); //select trump card
            trump                 = trumpCard.Suit;                          //set trump
            PlayingCard.UseTrumps = true;
            PlayingCard.IsAceHigh = true;
            PlayingCard.Trump     = trumpCard.Suit;

            if (form != null)                                                                                                             //check if form is null
            {
                trumpCard.MyOrientation = Orientation.Horizontal;                                                                         //chang trumpCard orientation to horizontal
                trumpCard.Location      = new Point((form.Width - (trumpCard.Width + 40)), ((form.Height / 2) - (trumpCard.Height / 2))); //set trumpCard location

                form.Invoke((MethodInvoker) delegate                                                                                      //use the Invoke method to safely alter a control of the ui thread
                {
                    form.Controls.Add(trumpCard);                                                                                         //add trumpCard control to form
                });

                Label lblPlayerName = new Label();                          //create a label for showing player name
                lblPlayerName.Location = new Point(70, (form.Height - 80)); //set label location
                lblPlayerName.Text     = myPlayers[0].MyName;               //set text of label to players name

                form.Invoke((MethodInvoker) delegate                        //use the Invoke method to safely alter a control of the ui thread
                {
                    form.Controls.Add(lblPlayerName);                       //add label to form
                });

                Label lblComputerName = new Label();            //create a label for showing player name
                lblComputerName.Location = new Point(70, 20);   //set label location
                lblComputerName.Text     = myPlayers[1].MyName; //set text of label to players name

                form.Invoke((MethodInvoker) delegate            //use the Invoke method to safely alter a control of the ui thread
                {
                    form.Controls.Add(lblComputerName);         //add label to form
                });

                Button btnPass = new Button();
                btnPass.Size     = new Size(100, 40);
                btnPass.Location = new Point(900, 500);
                btnPass.Enabled  = false;
                btnPass.Text     = "Pass";
                passButton       = btnPass;
                form.Invoke((MethodInvoker) delegate //use the Invoke method to safely alter a control of the ui thread
                {
                    form.Controls.Add(passButton);
                });
                passButton.Click += new EventHandler(Pass_Click);

                Label lblGamePrompt = new Label();
                lblGamePrompt.Width    = 200;
                lblGamePrompt.Text     = "Game starting...";
                lblGamePrompt.Location = new Point(50, 300);
                promptLabel            = lblGamePrompt;
                form.Invoke((MethodInvoker) delegate //use the Invoke method to safely alter a control of the ui thread
                {
                    form.Controls.Add(promptLabel);
                });
            }

            // Disable all cards.
            // Add click event handler for each card.
            foreach (Player player in myPlayers)
            {
                foreach (CardBox cardBox in player.myCardBoxes)
                {
                    cardBox.Click += new EventHandler(Card_Click);
                    form.Invoke((MethodInvoker) delegate //use the Invoke method to safely alter a control of the ui thread
                    {
                        cardBox.Enabled = false;
                    });
                }
            }
        }