/// <summary>
        /// Performs an action as part of the Computer's turn.
        /// </summary>
        /// <returns></returns>
        public static ActionResult ComputerAction()
        {
            if (IsPlaying == false)
            {
                throw new Exception("Game not started");
            }
            if (IsUserTurn == true)
            {
                throw new Exception("Sorry! Not Computer's turn");
            }

            if (!IsHandPlayable(ComputerHand))
            {
                return(DrawCard(ComputerHand));
            }
            else
            {
                // Computer plays a card

                int cardToPlay = 0;

                // Eights
                for (int i = 0; i < ComputerHand.GetCount(); i++)
                {
                    if (ComputerHand.GetCard(i).GetFaceValue() == FaceValue.Eight)
                    {
                        cardToPlay = i;
                        break;
                    }
                }
                //Matching Suits
                for (int i = 0; i < ComputerHand.GetCount(); i++)
                {
                    if (ComputerHand.GetCard(i).GetSuit() == TopDiscard.GetSuit())
                    {
                        cardToPlay = i;
                    }
                }
                // Matching FaceValues
                for (int i = 0; i < ComputerHand.GetCount(); i++)
                {
                    if (ComputerHand.GetCard(i).GetFaceValue() == TopDiscard.GetFaceValue())
                    {
                        cardToPlay = i;
                    }
                }

                return(PlayCard(ComputerHand, cardToPlay, ComputerHand.GetCard(cardToPlay).GetSuit()));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Perform an action according to the computer’s rules.
        /// </summary>
        /// <returns>Return the action according to the rules</returns>
        public static ActionResult ComputerAction()
        {
            if (!IsPlaying || IsUserTurn)
            {
                throw new Exception();
            }

            if (IsHandPlayable(ComputerHand))
            {
                int idx = GetCardCanPlay(ComputerHand);
                return(PlayCard(ComputerHand, idx, ComputerHand.GetCard(idx).GetSuit(), UserHand));
            }
            else
            {
                return(DrawCard(ComputerHand, UserHand));
            }
        }
Exemplo n.º 3
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;
             */
        }