private static ActionResult DrawCard(Hand hand) { if (IsUserTurn == true) { hand = _userhand; } else { hand = _computerhand; } if (_canplay && TopDiscard.GetFaceValue() == FaceValue.Eight) { return(ActionResult.CannotDraw); } for (int i = 0; i < hand.GetCount(); i++) { if (hand.GetCard(i).GetSuit() == TopDiscard.GetSuit()) { return(ActionResult.CannotDraw); } else if (hand.GetCard(i).GetFaceValue() == TopDiscard.GetFaceValue()) { return(ActionResult.CannotDraw); } else if (hand.GetCard(i).GetFaceValue() == FaceValue.Eight) { return(ActionResult.CannotDraw); } } _canplay = false; return(ActionResult.DrewPlayableCard); }
/// <summary> /// Determines if the Hand contains (holds) a playable card. /// </summary> /// <param name="hand">a player's hand of cards</param> /// <returns>True: if hand contains playable card; /// False: if not!</returns> private static bool IsHandPlayable(Hand hand) { if ((_discardPile.GetCount() == 1) && (TopDiscard.GetFaceValue() == FaceValue.Eight)) { return(true); } foreach (Card card in hand) { if (TopDiscard.GetFaceValue() == card.GetFaceValue()) { return(true); } else if (TopDiscard.GetSuit() == card.GetSuit()) { return(true); } else if (card.GetFaceValue() == FaceValue.Eight) { return(true); } } return(false); }
/// <summary> /// Get a card that is playable from a hand /// </summary> /// <param name="hand">Give hand</param> /// <returns>Playable card</returns> private static int GetCardCanPlay(Hand hand) { int idx = 0; for (int i = 0; i < hand.GetCount(); i++) { if (hand.GetCard(i).GetFaceValue() == FaceValue.Eight) { idx = i; } } for (int i = 0; i < hand.GetCount(); i++) { if (hand.GetCard(i).GetSuit() == TopDiscard.GetSuit() && hand.GetCard(i).GetFaceValue() != FaceValue.Eight) { idx = i; } } for (int i = 0; i < hand.GetCount(); i++) { if (hand.GetCard(i).GetFaceValue() == TopDiscard.GetFaceValue() && hand.GetCard(i).GetFaceValue() != FaceValue.Eight) { idx = i; } } return(idx); }
/// <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> /// <param name="card"></param> /// <returns></returns> private static bool IsCardPlayable(Card card) { if (card.GetFaceValue() == TopDiscard.GetFaceValue()) { return(true); } else if (card.GetSuit() == TopDiscard.GetSuit()) { return(true); } else if (_canplay && card.GetFaceValue() == FaceValue.Eight) { return(true); } return(false); }
/// <summary> /// Check if the card is playable /// </summary> /// <param name="card">Card</param> /// <returns>Returns true if the given Card is currently playable.</returns> private static bool IsCardPlayable(Card card) { if (TopDiscard.GetFaceValue() == FaceValue.Eight) { return(true); } if (card.GetSuit() == TopDiscard.GetSuit() || card.GetFaceValue() == TopDiscard.GetFaceValue() || card.GetFaceValue() == FaceValue.Eight) { return(true); } else { return(false); } }
/// <summary> /// Check if the hand is playable /// </summary> /// <param name="hand">Hand</param> /// <returns>Returns true if the given Hand contains a playable Card.</returns> private static bool IsHandPlayable(Hand hand) { if (TopDiscard.GetFaceValue() == FaceValue.Eight) { return(true); } foreach (Card card in hand) { if (card.GetSuit() == TopDiscard.GetSuit() || card.GetFaceValue() == TopDiscard.GetFaceValue() || card.GetFaceValue() == FaceValue.Eight) { return(true); } } return(false); }
/// <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); } } }