Ejemplo n.º 1
0
        public void Play(bool recursive)
        {
            var combinations = new HashSet <CardCombination>();

            while (playerOne.Any() && playerTwo.Any())
            {
                if (recursive)
                {
                    var combination = new CardCombination(playerOne, playerTwo);
                    if (combinations.Contains(combination))
                    {
                        WinBy = Player.One;
                        break;
                    }

                    combinations.Add(combination);
                }

                int playerOneCard = playerOne.Dequeue();
                int playerTwoCard = playerTwo.Dequeue();

                Player winBy;
                if (recursive && playerOne.Count >= playerOneCard && playerTwo.Count >= playerTwoCard)
                {
                    var newPlayerOne = playerOne.Take(playerOneCard);
                    var newPlayerTwo = playerTwo.Take(playerTwoCard);

                    var game = new Game(newPlayerOne, newPlayerTwo);
                    game.Play(true);
                    winBy = game.WinBy.Value;
                }
                else
                {
                    winBy = playerOneCard > playerTwoCard ? Player.One : Player.Two;
                }

                if (winBy == Player.One)
                {
                    playerOne.Enqueue(playerOneCard);
                    playerOne.Enqueue(playerTwoCard);
                }
                else
                {
                    playerTwo.Enqueue(playerTwoCard);
                    playerTwo.Enqueue(playerOneCard);
                }
            }

            WinBy ??= playerOne.Any() ? Player.One : Player.Two;
        }
Ejemplo n.º 2
0
 protected bool Equals(CardCombination other)
 {
     return(card0.SequenceEqual(other.card0) && card1.SequenceEqual(other.card1));
 }