Example #1
0
        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);
        }
Example #2
0
        private void RemoveCardAfterSet(Card card, Player player)
        {
            if (card is ActionCard)
            {
                ActionCard ac = (ActionCard)card;

                // If the card to be played is a wild card a card with the same action card type gets removed from the player deck.
                if (ac.Type == ActionCardType.Wild || ac.Type == ActionCardType.WildDrawFour)
                {
                    foreach (Card playerCard in player.Deck.Cards)
                    {
                        if (playerCard is ActionCard)
                        {
                            ActionCard pac = (ActionCard)playerCard;

                            if (ac.Type == pac.Type)
                            {
                                player.Deck.RemoveCard(playerCard);
                                break;
                            }
                        }
                    }
                }
                else
                {
                    player.Deck.RemoveCard(card);
                }
            }
            else if (card is NumericCard)
            {
                player.Deck.RemoveCard(card);
            }
        }
Example #3
0
        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);

            ;
        }
Example #4
0
        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);
        }
Example #5
0
        private void DrawPileIsEmpty(object sender, EventArgs args)
        {
            Deck newDrawPile = new Deck();

            newDrawPile.Cards = new List <Card>();

            for (int i = 1; i < this.discardPile.Cards.Count; i++)
            {
                newDrawPile.AddCard(this.discardPile.Cards[i]);
            }

            for (int i = 1, end = discardPile.Cards.Count; i < end; i++)
            {
                this.discardPile.Cards.RemoveAt(1);
            }

            foreach (Card card in newDrawPile.Cards)
            {
                if (card is ActionCard)
                {
                    ActionCard actionCard = (ActionCard)card;

                    if (actionCard.Type == ActionCardType.Wild || actionCard.Type == ActionCardType.WildDrawFour)
                    {
                        card.Color = Color.White;
                    }
                }
            }

            this.MixUntilValidFirstCard(newDrawPile);


            foreach (Card card in newDrawPile.Cards)
            {
                this.drawPile.Cards.Add(card);
            }
        }
Example #6
0
        public void ExecuteCardEffect(Card card, Player playerToBeAffected, bool firstRound)
        {
            this.discardPile.AddCard(card);

            if (card is ActionCard)
            {
                ActionCard ac = (ActionCard)card;

                if (ac.Type == ActionCardType.WildDrawFour)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        playerToBeAffected.Deck.AddCard(this.drawPile.DrawCard());
                    }

                    if (firstRound == true)
                    {
                        this.playerWhoIsOnTurn = this.GetNextPlayer();
                    }
                    else
                    {
                        this.playerWhoIsOnTurn = playerToBeAffected;
                    }
                }
                else if (ac.Type == ActionCardType.Reverse)
                {
                    if (this.Players.Count() == 2)
                    {
                        this.playerWhoIsOnTurn = playerToBeAffected;
                    }

                    if (this.direction == Direction.Left)
                    {
                        this.direction = Direction.Right;
                    }
                    else if (this.direction == Direction.Right)
                    {
                        this.direction = Direction.Left;
                    }
                }
                else if (ac.Type == ActionCardType.DrawTwo)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        playerToBeAffected.Deck.AddCard(this.drawPile.DrawCard());
                    }

                    if (firstRound == true)
                    {
                        this.playerWhoIsOnTurn = this.GetNextPlayer();
                    }
                    else
                    {
                        this.playerWhoIsOnTurn = playerToBeAffected;
                    }
                }
                else if (ac.Type == ActionCardType.Skip)
                {
                    if (firstRound == true)
                    {
                        this.playerWhoIsOnTurn = this.GetNextPlayer();
                    }
                    else
                    {
                        this.playerWhoIsOnTurn = playerToBeAffected;
                    }
                }
            }
        }
Example #7
0
        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);
        }
Example #8
0
        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());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }