/// <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())); } }
/// <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; */ }
/// <summary> /// a /// </summary> /// <param name="cardNum"></param> /// <param name="chosenSuit"></param> /// <returns></returns> public static ActionResult UserPlayCard(int cardNum, Suit?chosenSuit = null) { if (IsPlaying == false) { throw new System.ArgumentException("This game is not start!"); } if (IsUserTurn == false) { throw new System.ArgumentException("This is not your turn!"); } Card card = _userhand.GetCard(cardNum); if (_discardPile.GetCount() == 1 && TopDiscard.GetFaceValue() == FaceValue.Eight) { IsUserTurn = false; _userhand.RemoveCardAt(cardNum); _discardPile.AddCard(card); return(ActionResult.ValidPlay); } 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() || c.GetFaceValue() == FaceValue.Eight) { return(ActionResult.InvalidPlay); } } return(ActionResult.InvalidPlayAndMustDraw); }
/// <summary> /// a /// </summary> /// <returns></returns> public static ActionResult UserDrawCard() { if (IsPlaying == false) { throw new System.ArgumentException("This game is not start!"); } if (IsUserTurn == false) { throw new System.ArgumentException("This is not your turn!"); } if (TopDiscard.GetFaceValue() == FaceValue.Eight) { return(ActionResult.CannotDraw); } foreach (Card c in UserHand) { if (c.GetFaceValue() == TopDiscard.GetFaceValue() || c.GetSuit() == TopDiscard.GetSuit()) { return(ActionResult.CannotDraw); } } if (_drawPile.GetCount() == 0) { _discardPile.Reverse(); while (_discardPile.GetCount() > 1) { _drawPile.AddCard(_discardPile.DealOneCard()); } return(ActionResult.FlippedDeck); } Card drawCard = _drawPile.DealOneCard(); UserHand.AddCard(drawCard); if (drawCard.GetFaceValue() == TopDiscard.GetFaceValue() || drawCard.GetSuit() == TopDiscard.GetSuit()) { return(ActionResult.DrewPlayableCard); } else { if (UserHand.GetCount() == 13) { IsUserTurn = false; if (ComputerHand.GetCount() == 13) { return(ActionResult.DrewAndResetPiles); } else { return(ActionResult.DrewAndNoMovePossible); } } else { return(ActionResult.DrewUnplayableCard); } } }