Exemplo n.º 1
0
        public PlayerTurn PlayTurn(PlayerTurn previousTurn, CardDeck drawPile)
        {
            PlayerTurn turn = new PlayerTurn();

            if (previousTurn.Result == TurnResult.Skip ||
                previousTurn.Result == TurnResult.DrawTwo ||
                previousTurn.Result == TurnResult.WildCardDrawFour)
            {
                return(ProcessAttack(previousTurn.Card, drawPile));
            }
            else if ((previousTurn.Result == TurnResult.WildCard ||
                      previousTurn.Result == TurnResult.DrawTwoByOpponent ||
                      previousTurn.Result == TurnResult.ForceDrawNoMatch) &&
                     HasMatch(previousTurn.DeclaredColor))
            {
                turn = PlayMatchingCard(previousTurn.DeclaredColor);
            }
            else if (HasMatch(previousTurn.Card))
            {
                turn = PlayMatchingCard(previousTurn.Card);
            }
            else //See if drawn card is playable
            {
                turn = DrawCard(previousTurn, drawPile);
            }

            DisplayTurn(turn);
            return(turn);
        }
Exemplo n.º 2
0
        private void DisplayTurn(PlayerTurn currentTurn)
        {
            if (currentTurn.Result == TurnResult.ForceDrawNoMatch)
            {
                Console.WriteLine("Player " + Position.ToString() + " is forced to draw.");
            }
            if (currentTurn.Result == TurnResult.ForceDrawNoMatch)
            {
                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.WildCardDrawFour ||
                currentTurn.Result == TurnResult.Reversed ||
                currentTurn.Result == TurnResult.ForceDrawNoMatch)
            {
                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!");
            }
        }
Exemplo n.º 3
0
        private PlayerTurn ProcessAttack(Card currentDiscard, CardDeck drawPile)
        {
            PlayerTurn turn = new PlayerTurn();

            turn.Result        = TurnResult.DrawTwoByOpponent;
            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);
        }
Exemplo n.º 4
0
        public PlayerTurn PlayerTurn(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);
        }
Exemplo n.º 5
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.WildCardDrawFour ||
         currentTurn.Result == TurnResult.Reversed)
     {
         DiscardPile.Insert(0, currentTurn.Card);
     }
 }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
0
        public void PlayGame()
        {
            int  i           = 0;
            bool isAscending = true;

            //displaying player's beginner cards
            foreach (var player in Players)
            {
                player.ShowHand();
            }

            Console.ReadLine();

            PlayerTurn currentTurn = new PlayerTurn()
            {
                Result        = TurnResult.StartGame,
                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
                    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)
                    {
                        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.");
            }
        }
Exemplo n.º 8
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();

            //unless there are no other matches, we cannot play wild card
            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);
            }


            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 == 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 //Matches depend on the value of the card
                {
                    turn.Card          = matchOnValue.First();
                    turn.DeclaredColor = turn.Card.Color;
                    Hand.Remove(matchOnValue.First());

                    return(turn);
                }
            }
            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);
            }

            //If a wild becomes our last card, player wins
            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);
            }


            turn.Result = TurnResult.ForceDrawNoMatch;
            return(turn);
        }
Exemplo n.º 9
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 the wild draw four card 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);
            }


            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);
            }


            turn.Result = TurnResult.ForceDrawNoMatch;
            return(turn);
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
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);
        }