Ejemplo n.º 1
0
        private void DisplayTurn(PlayerTurn currentTurn)
        {
            if (currentTurn.Result == TurnResult.ForceDraw)
            {
                Console.WriteLine("Player " + Position.ToString() + " is forced to draw.");
            }
            if (currentTurn.Result == TurnResult.ForceDrawPlay)
            {
                Console.WriteLine("Player " + Position.ToString() + " is forced to draw AND can play the drawn card!");
            }

            if (currentTurn.Result == TurnResult.PlayedCard ||
                currentTurn.Result == TurnResult.Skip ||
                currentTurn.Result == TurnResult.DrawTwo ||
                currentTurn.Result == TurnResult.WildCard ||
                currentTurn.Result == TurnResult.WildDrawFour ||
                currentTurn.Result == TurnResult.Reversed ||
                currentTurn.Result == TurnResult.ForceDrawPlay)
            {
                Console.WriteLine("Player " + Position.ToString() + " plays a " + currentTurn.Card.DisplayValue + " card.");
                if (currentTurn.Card.Color == CardColor.Wild)
                {
                    Console.WriteLine("Player " + Position.ToString() + " declares " + currentTurn.DeclaredColor.ToString() + " as the new color.");
                }
                if (currentTurn.Result == TurnResult.Reversed)
                {
                    Console.WriteLine("Turn order reversed!");
                }
            }

            if (Hand.Count == 1)
            {
                Console.WriteLine("Player " + Position.ToString() + " shouts Uno!");
            }
        }
Ejemplo n.º 2
0
        public PlayerTurn PlayTurn(PlayerTurn previousTurn, CardDeck drawPile)
        {
            PlayerTurn turn = new PlayerTurn();

            if (previousTurn.Result == TurnResult.Skip ||
                previousTurn.Result == TurnResult.DrawTwo ||
                previousTurn.Result == TurnResult.WildDrawFour)
            {
                return(ProcessAttack(previousTurn.Card, drawPile));
            }
            else if ((previousTurn.Result == TurnResult.WildCard ||
                      previousTurn.Result == TurnResult.Attacked ||
                      previousTurn.Result == TurnResult.ForceDraw) &&
                     HasMatch(previousTurn.DeclaredColor))
            {
                turn = PlayMatchingCard(previousTurn.DeclaredColor);
            }
            else if (HasMatch(previousTurn.Card))
            {
                turn = PlayMatchingCard(previousTurn.Card);
            }
            else //Draw a card and see if it can play
            {
                turn = DrawCard(previousTurn, drawPile);
            }

            DisplayTurn(turn);
            return(turn);
        }
Ejemplo n.º 3
0
        private PlayerTurn ProcessAttack(Card currentDiscard, CardDeck drawPile)
        {
            PlayerTurn turn = new PlayerTurn();

            turn.Result        = TurnResult.Attacked;
            turn.Card          = currentDiscard;
            turn.DeclaredColor = currentDiscard.Color;
            if (currentDiscard.Value == CardValue.Skip)
            {
                Console.WriteLine("Player " + Position.ToString() + " was skipped!");
                return(turn);
            }
            else if (currentDiscard.Value == CardValue.DrawTwo)
            {
                Console.WriteLine("Player " + Position.ToString() + " must draw two cards!");
                Hand.AddRange(drawPile.Draw(2));
            }
            else if (currentDiscard.Value == CardValue.DrawFour)
            {
                Console.WriteLine("Player " + Position.ToString() + " must draw four cards!");
                Hand.AddRange(drawPile.Draw(4));
            }

            return(turn);
        }
Ejemplo n.º 4
0
 private void AddToDiscardPile(PlayerTurn currentTurn)
 {
     if (currentTurn.Result == TurnResult.PlayedCard ||
         currentTurn.Result == TurnResult.DrawTwo ||
         currentTurn.Result == TurnResult.Skip ||
         currentTurn.Result == TurnResult.WildCard ||
         currentTurn.Result == TurnResult.WildDrawFour ||
         currentTurn.Result == TurnResult.Reversed)
     {
         DiscardPile.Insert(0, currentTurn.Card);
     }
 }
Ejemplo n.º 5
0
        private PlayerTurn DrawCard(PlayerTurn previousTurn, CardDeck drawPile)
        {
            PlayerTurn turn      = new PlayerTurn();
            var        drawnCard = drawPile.Draw(1);

            Hand.AddRange(drawnCard);

            if (HasMatch(previousTurn.Card))
            {
                turn        = PlayMatchingCard(previousTurn.Card);
                turn.Result = TurnResult.ForceDrawPlay;
            }
            else
            {
                turn.Result = TurnResult.ForceDraw;
                turn.Card   = previousTurn.Card;
            }

            return(turn);
        }
Ejemplo n.º 6
0
        public void PlayGame()
        {
            int  i           = 0;
            bool isAscending = true;

            //First, let's show what each player starts with
            foreach (var player in Players)
            {
                player.ShowHand();
            }

            Console.ReadLine();

            PlayerTurn currentTurn = new PlayerTurn()
            {
                Result        = TurnResult.GameStart,
                Card          = DiscardPile.First(),
                DeclaredColor = DiscardPile.First().Color
            };

            Console.WriteLine("First card is a " + currentTurn.Card.DisplayValue + ".");

            while (!Players.Any(x => !x.Hand.Any()))
            {
                if (DrawPile.Cards.Count < 4) //Cheating a bit here
                {
                    var currentCard = DiscardPile.First();

                    //Take the discarded cards, shuffle them, and make them the new draw pile.
                    DrawPile.Cards = DiscardPile.Skip(1).ToList();
                    DrawPile.Shuffle();

                    //Reset the discard pile to only have the current card.
                    DiscardPile = new List <Card>();
                    DiscardPile.Add(currentCard);

                    Console.WriteLine("Shuffling cards!");
                }

                var currentPlayer = Players[i];

                currentTurn = Players[i].PlayTurn(currentTurn, DrawPile);

                AddToDiscardPile(currentTurn);

                if (currentTurn.Result == TurnResult.Reversed)
                {
                    isAscending = !isAscending;
                }

                if (isAscending)
                {
                    i++;
                    if (i >= Players.Count) //Reset player counter
                    {
                        i = 0;
                    }
                }
                else
                {
                    i--;
                    if (i < 0)
                    {
                        i = Players.Count - 1;
                    }
                }
            }

            var winningPlayer = Players.Where(x => !x.Hand.Any()).First();

            Console.WriteLine("Player " + winningPlayer.Position.ToString() + " wins!!");

            foreach (var player in Players)
            {
                Console.WriteLine("Player " + player.Position.ToString() + " has " + player.Hand.Sum(x => x.Score).ToString() + " points in his hand.");
            }
        }
Ejemplo n.º 7
0
        private PlayerTurn PlayMatchingCard(Card currentDiscard)
        {
            var turn = new PlayerTurn();

            turn.Result = TurnResult.PlayedCard;
            var matching = Hand.Where(x => x.Color == currentDiscard.Color || x.Value == currentDiscard.Value || x.Color == CardColor.Wild).ToList();

            //We cannot play wild draw four unless there are no other matches.
            if (matching.All(x => x.Value == CardValue.DrawFour))
            {
                turn.Card          = matching.First();
                turn.DeclaredColor = SelectDominantColor();
                turn.Result        = TurnResult.WildCard;
                Hand.Remove(matching.First());

                return(turn);
            }

            //Otherwise, we play the card that would cause the most damage to the next player.
            if (matching.Any(x => x.Value == CardValue.DrawTwo))
            {
                turn.Card          = matching.First(x => x.Value == CardValue.DrawTwo);
                turn.Result        = TurnResult.DrawTwo;
                turn.DeclaredColor = turn.Card.Color;
                Hand.Remove(turn.Card);

                return(turn);
            }

            if (matching.Any(x => x.Value == CardValue.Skip))
            {
                turn.Card          = matching.First(x => x.Value == CardValue.Skip);
                turn.Result        = TurnResult.Skip;
                turn.DeclaredColor = turn.Card.Color;
                Hand.Remove(turn.Card);

                return(turn);
            }

            if (matching.Any(x => x.Value == CardValue.Reverse))
            {
                turn.Card          = matching.First(x => x.Value == CardValue.Reverse);
                turn.Result        = TurnResult.Reversed;
                turn.DeclaredColor = turn.Card.Color;
                Hand.Remove(turn.Card);

                return(turn);
            }

            //At this point the player has a choice of sorts
            //Assuming he has a match on color AND a match on value, he can choose which to play
            //For this demo, we'll assume that playing the match with MORE possible plays from his hand is the better option.

            var matchOnColor = matching.Where(x => x.Color == currentDiscard.Color);
            var matchOnValue = matching.Where(x => x.Value == currentDiscard.Value);

            if (matchOnColor.Any() && matchOnValue.Any())
            {
                var correspondingColor = Hand.Where(x => x.Color == matchOnColor.First().Color);
                var correspondingValue = Hand.Where(x => x.Value == matchOnValue.First().Value);
                if (correspondingColor.Count() >= correspondingValue.Count())
                {
                    turn.Card          = matchOnColor.First();
                    turn.DeclaredColor = turn.Card.Color;
                    Hand.Remove(matchOnColor.First());

                    return(turn);
                }
                else //Match on value
                {
                    turn.Card          = matchOnValue.First();
                    turn.DeclaredColor = turn.Card.Color;
                    Hand.Remove(matchOnValue.First());

                    return(turn);
                }
                //Figure out which of these is better
            }
            else if (matchOnColor.Any())
            {
                turn.Card          = matchOnColor.First();
                turn.DeclaredColor = turn.Card.Color;
                Hand.Remove(matchOnColor.First());

                return(turn);
            }
            else if (matchOnValue.Any())
            {
                turn.Card          = matchOnValue.First();
                turn.DeclaredColor = turn.Card.Color;
                Hand.Remove(matchOnValue.First());

                return(turn);
            }

            //Play regular wilds last.  If a wild becomes our last card, we win on the next turn!
            if (matching.Any(x => x.Value == CardValue.Wild))
            {
                turn.Card          = matching.First(x => x.Value == CardValue.Wild);
                turn.DeclaredColor = SelectDominantColor();
                turn.Result        = TurnResult.WildCard;
                Hand.Remove(turn.Card);

                return(turn);
            }

            //This should never happen
            turn.Result = TurnResult.ForceDraw;
            return(turn);
        }
Ejemplo n.º 8
0
        private PlayerTurn PlayMatchingCard(CardColor color)
        {
            var turn = new PlayerTurn();

            turn.Result = TurnResult.PlayedCard;
            var matching = Hand.Where(x => x.Color == color || x.Color == CardColor.Wild).ToList();

            //We cannot play wild draw four unless there are no other matches.
            if (matching.All(x => x.Value == CardValue.DrawFour))
            {
                turn.Card          = matching.First();
                turn.DeclaredColor = SelectDominantColor();
                turn.Result        = TurnResult.WildCard;
                Hand.Remove(matching.First());

                return(turn);
            }

            //Otherwise, we play the card that would cause the most damage to the next player.
            if (matching.Any(x => x.Value == CardValue.DrawTwo))
            {
                turn.Card          = matching.First(x => x.Value == CardValue.DrawTwo);
                turn.Result        = TurnResult.DrawTwo;
                turn.DeclaredColor = turn.Card.Color;
                Hand.Remove(turn.Card);

                return(turn);
            }

            if (matching.Any(x => x.Value == CardValue.Skip))
            {
                turn.Card          = matching.First(x => x.Value == CardValue.Skip);
                turn.Result        = TurnResult.Skip;
                turn.DeclaredColor = turn.Card.Color;
                Hand.Remove(turn.Card);

                return(turn);
            }

            if (matching.Any(x => x.Value == CardValue.Reverse))
            {
                turn.Card          = matching.First(x => x.Value == CardValue.Reverse);
                turn.Result        = TurnResult.Reversed;
                turn.DeclaredColor = turn.Card.Color;
                Hand.Remove(turn.Card);

                return(turn);
            }

            var matchOnColor = matching.Where(x => x.Color == color);

            if (matchOnColor.Any())
            {
                turn.Card          = matchOnColor.First();
                turn.DeclaredColor = turn.Card.Color;
                Hand.Remove(matchOnColor.First());

                return(turn);
            }

            if (matching.Any(x => x.Value == CardValue.Wild))
            {
                turn.Card          = matching.First(x => x.Value == CardValue.Wild);
                turn.DeclaredColor = SelectDominantColor();
                turn.Result        = TurnResult.WildCard;
                Hand.Remove(turn.Card);

                return(turn);
            }

            //This should never happen
            turn.Result = TurnResult.ForceDraw;
            return(turn);
        }