public static Protocol RoundInformation(Card lastCard, Player playerWhoIsOnTurn, List <Player> playerList) { string value = string.Empty; if (lastCard is ActionCard) { ActionCard actionCard = (ActionCard)lastCard; value = ((char)actionCard.Type).ToString(); } else if (lastCard is NumericCard) { NumericCard numericCard = (NumericCard)lastCard; value = numericCard.Number.ToString(); } string amountOfPlayerCards = string.Empty; foreach (Player player in playerList) { amountOfPlayerCards += player.Deck.Cards.Count; if (player != playerList.Last()) { amountOfPlayerCards += "-"; } } Protocol protocol = new Protocol(ProtocolTypes.RoundInformation, Encoding.ASCII.GetBytes((char)lastCard.Color + "-" + value + "-" + playerWhoIsOnTurn.PlayerID + "-" + amountOfPlayerCards)); return(protocol); }
public static Protocol PlayerCards(Player player) { string playerCards = string.Empty; foreach (Card card in player.Deck.Cards) { string value = string.Empty; if (card is ActionCard) { ActionCard actionCard = (ActionCard)card; value = ((char)actionCard.Type).ToString(); } else if (card is NumericCard) { NumericCard numericCard = (NumericCard)card; value = numericCard.Number.ToString(); } playerCards += (char)card.Color + "-" + value; if (card != player.Deck.Cards.Last()) { playerCards += "-"; } } Protocol protocol = new Protocol(ProtocolTypes.PlayerCards, Encoding.ASCII.GetBytes(playerCards)); return(protocol); ; }
public int GetPositionOfCard(Card cardToGetPositionOf) { int position = 0; foreach (Card card in this.Cards) { if (card.Color == cardToGetPositionOf.Color) { if (cardToGetPositionOf is ActionCard && card is ActionCard) { ActionCard ctc = (ActionCard)cardToGetPositionOf; ActionCard c = (ActionCard)card; if (ctc.Type == c.Type) { break; } } else if (cardToGetPositionOf is NumericCard && card is NumericCard) { NumericCard ctc = (NumericCard)cardToGetPositionOf; NumericCard c = (NumericCard)card; if (ctc.Number == c.Number) { break; } } } position++; } return(position); }
public bool CheckIfValidCard(Card cardToCheck, Player player) { bool playerOwnsCard = false; bool cardCanBePlayed = false; foreach (Card card in player.Deck.Cards) { if (card.Color == cardToCheck.Color) { if (cardToCheck is ActionCard && card is ActionCard) { ActionCard ctc = (ActionCard)cardToCheck; ActionCard c = (ActionCard)card; if (ctc.Type == c.Type) { playerOwnsCard = true; break; } } else if (cardToCheck is NumericCard && card is NumericCard) { NumericCard ctc = (NumericCard)cardToCheck; NumericCard c = (NumericCard)card; if (ctc.Number == c.Number) { playerOwnsCard = true; break; } } } else if (cardToCheck is ActionCard && card is ActionCard) { ActionCard ctc = (ActionCard)cardToCheck; ActionCard c = (ActionCard)card; if ((ctc.Type == ActionCardType.Wild && c.Type == ActionCardType.Wild) || (ctc.Type == ActionCardType.WildDrawFour && c.Type == ActionCardType.WildDrawFour)) { playerOwnsCard = true; } } } if (playerOwnsCard == false) { cardCanBePlayed = false; } else { // Checks if the last card and the card to be played have the same color. if (cardToCheck.Color == this.discardPile.Cards[0].Color) { cardCanBePlayed = true; } else if (cardToCheck is ActionCard) { ActionCard ac = (ActionCard)cardToCheck; // Checks if the card is a wild card. Then it can be always played. if (ac.Type == ActionCardType.Wild || ac.Type == ActionCardType.WildDrawFour) { cardCanBePlayed = true; } // Checks if the last card is an action card and if it has the same type as the card to be played. else if (this.discardPile.Cards[0] is ActionCard) { ActionCard lc = (ActionCard)this.discardPile.Cards[0]; if (ac.Type == lc.Type) { cardCanBePlayed = true; } } } // Checks if the last card is a numeric card and if it has the same number as the card to be played. else if (cardToCheck is NumericCard) { NumericCard nc = (NumericCard)cardToCheck; if (this.discardPile.Cards[0] is NumericCard) { NumericCard lc = (NumericCard)this.discardPile.Cards[0]; if (nc.Number == lc.Number) { cardCanBePlayed = true; } } } } return(cardCanBePlayed); }
public void NewCardSet(string[] setCardArray) { if (setCardArray.Length == 5) { foreach (Player player in this.Players) { if (player.PlayerID.ToString() == setCardArray[1]) { Card card = null; Color color; if (Enum.IsDefined(typeof(Color), (int)(setCardArray[2].ToCharArray()[0])) == true) { color = (Color)(setCardArray[2].ToCharArray()[0]); if (int.TryParse(setCardArray[3], out int number) == false) { if (Enum.IsDefined(typeof(ActionCardType), (int)(setCardArray[3].ToCharArray()[0])) == true) { ActionCardType type = (ActionCardType)(setCardArray[3].ToCharArray()[0]); card = new ActionCard(color, type); } } else { card = new NumericCard(color, number); } if (int.TryParse(setCardArray[4], out int uno) == true) { if (uno == 0 || uno == 1) { if (this.CheckIfValidCard(card, player) == true) { this.RemoveCardAfterSet(card, player); player.NetworkManager.Send(ProtocolManager.OK()); if (this.CheckIfGameOver() == true) { this.GameOver(); break; } else { this.CheckIfUno(setCardArray[4]); this.ExecuteCardEffect(card, this.GetNextPlayer(), false); ChangePlayerTurn(); } } else { player.NetworkManager.Send(ProtocolManager.Invalid()); } } } } } } } }