//adds the list of cards to the computer's hand
        public void AddCardsToComputerHand(List <Card> cards)
        {
            //sort the cards
            cardsInComputerHand = cards;
            cardsInComputerHand.Sort();

            int x = 20;

            computerHand.ClearCards();
            foreach (Card card in cardsInComputerHand)
            {
                computerHand.AddCard(card, x);
                x = x + 80;
                card.CardState = face;
            }
        }
Exemple #2
0
        /// <summary>
        /// a
        /// </summary>
        /// <returns></returns>
        public static ActionResult ComputerAction()
        {
            if (IsPlaying == false)
            {
                throw new System.ArgumentException("This game is not start!");
            }
            if (IsUserTurn == true)
            {
                throw new System.ArgumentException("This is not your turn!");
            }


            if (TopDiscard.GetFaceValue() == FaceValue.Eight)
            {
                IsUserTurn = true;
                _discardPile.AddCard(ComputerHand.GetCard(0));
                ComputerHand.RemoveCardAt(0);
                return(ActionResult.ValidPlay);
            }

            for (int j = 0; j < 3; ++j) // j = 0, match face, j = 1, match suit, j = 2, match eight
            {
                for (int i = ComputerHand.GetCount() - 1; i >= 0; --i)
                {
                    Card c = ComputerHand.GetCard(i);
                    if ((c.GetFaceValue() == TopDiscard.GetFaceValue() && j == 0) || (c.GetSuit() == TopDiscard.GetSuit() && j == 1) || (c.GetFaceValue() == FaceValue.Eight && j == 2))
                    {
                        IsUserTurn = false;
                        ComputerHand.RemoveCardAt(i);
                        _discardPile.AddCard(c);

                        if (ComputerHand.GetCount() == 0)
                        {
                            IsPlaying = false;
                            return(ActionResult.WinningPlay);
                        }

                        if (UserHand.GetCount() == 13)
                        {
                            foreach (Card c1 in UserHand)
                            {
                                if (c1.GetFaceValue() == TopDiscard.GetFaceValue() ||
                                    c1.GetSuit() == TopDiscard.GetSuit())
                                {
                                    IsUserTurn = true;
                                    return(ActionResult.ValidPlay);
                                }
                            }

                            IsUserTurn = false;
                            return(ActionResult.ValidPlayAndExtraTurn);
                        }

                        IsUserTurn = true;
                        return(ActionResult.ValidPlay);
                    }
                }
            }

            if (_drawPile.GetCount() == 0)
            {
                _discardPile.Reverse();
                while (_discardPile.GetCount() > 1)
                {
                    _drawPile.AddCard(_discardPile.DealOneCard());
                }
                return(ActionResult.FlippedDeck);
            }

            Card drawCard = _drawPile.DealOneCard();

            ComputerHand.AddCard(drawCard);

            if (drawCard.GetFaceValue() == TopDiscard.GetFaceValue() || drawCard.GetSuit() == TopDiscard.GetSuit())
            {
                return(ActionResult.DrewPlayableCard);
            }
            else
            {
                if (ComputerHand.GetCount() == 13)
                {
                    IsUserTurn = true;
                    if (UserHand.GetCount() == 13)
                    {
                        return(ActionResult.DrewAndResetPiles);
                    }
                    else
                    {
                        return(ActionResult.DrewAndNoMovePossible);
                    }
                }
                else
                {
                    return(ActionResult.DrewUnplayableCard);
                }
            }

            /*
             * if (card.GetFaceValue() == FaceValue.Eight)
             * {
             *  if (!chosenSuit.HasValue)
             *  {
             *      return ActionResult.SuitRequired;
             *  }
             *
             *  IsUserTurn = false;
             *  _userhand.RemoveCardAt(cardNum);
             *  _discardPile.AddCard(new Card(chosenSuit, FaceValue.Eight));
             *  return ActionResult.ValidPlay;
             * }
             *
             * if (card.GetFaceValue() == TopDiscard.GetFaceValue() || card.GetSuit() == TopDiscard.GetSuit())
             * {
             *  _userhand.RemoveCardAt(cardNum);
             *  _discardPile.AddCard(card);
             *
             *  if (UserHand.GetCount() == 0)
             *  {
             *      IsPlaying = false;
             *      return ActionResult.WinningPlay;
             *  }
             *
             *  if (ComputerHand.GetCount() == 13)
             *  {
             *      foreach (Card c in ComputerHand)
             *      {
             *          if (c.GetFaceValue() == TopDiscard.GetFaceValue() || c.GetSuit() == TopDiscard.GetSuit())
             *          {
             *              IsUserTurn = false;
             *              return ActionResult.ValidPlay;
             *          }
             *      }
             *      return ActionResult.ValidPlayAndExtraTurn;
             *  }
             *
             *  IsUserTurn = false;
             *  return ActionResult.ValidPlay;
             * }
             *
             * // Invalid Play
             *
             * foreach (Card c in UserHand)
             * {
             *  if (c.GetFaceValue() == TopDiscard.GetFaceValue() || c.GetSuit() == TopDiscard.GetSuit())
             *  {
             *      return ActionResult.InvalidPlay;
             *  }
             * }
             *
             * return ActionResult.InvalidPlayAndMustDraw;
             */
        }