Exemplo n.º 1
0
        private (int player, IEnumerable <int> cards) SubGame(IEnumerable <int> player1, IEnumerable <int> player2, bool subGames)
        {
            var decks = new Decks();

            var cards1 = new Queue <int>(player1);
            var cards2 = new Queue <int>(player2);

            while (cards1.Count != 0 && cards2.Count != 0)
            {
                if (decks.HasPlayed(cards1, cards2))
                {
                    return(1, cards1);
                }

                var c1 = cards1.Dequeue();
                var c2 = cards2.Dequeue();

                int winner;
                if (subGames && cards1.Count >= c1 && cards2.Count >= c2)
                {
                    winner = SubGame(cards1.Take(c1), cards2.Take(c2), subGames).player;
                }
                else
                {
                    winner = c1 > c2 ? 1 : 2;
                }

                if (winner == 1)
                {
                    cards1.Enqueue(c1);
                    cards1.Enqueue(c2);
                }
                else
                {
                    cards2.Enqueue(c2);
                    cards2.Enqueue(c1);
                }
            }

            return(cards1.Count > 0 ? (1, cards1) : (2, cards2));
        }