Пример #1
0
        public int PartTwo()
        {
            var game = new CrabRecursiveCombat(_player1, _player2);

            var(player1, player2) = game.Play();
            return(Math.Max(player1.Score(), player2.Score()));
        }
Пример #2
0
        public (IEnumerable <int> Player1, IEnumerable <int> Player2) Play()
        {
            while (_player1.Count > 0 && _player2.Count > 0)
            {
                // Before either player deals a card, if there was a previous round in this game that had exactly
                // the same cards in the same order in the same players' decks,the game instantly ends in a win for
                // player 1
                if (!_rounds.Add(_player1.Concat(_player2)))
                {
                    return(_player1.ToArray(), Enumerable.Empty <int>());
                }

                // Otherwise, this round's cards must be in a new configuration; the players begin the round by each
                // drawing the top card of their deck as normal
                var card1       = _player1.Dequeue();
                var card2       = _player2.Dequeue();
                var player1Wins = card1 > card2;

                if (card1 <= _player1.Count && card2 <= _player2.Count)
                {
                    // To play a sub-game of Recursive Combat, each player creates a new deck by making a copy of the
                    // next cards in their deck (the quantity of cards copied is equal to the number on the card they
                    // drew to trigger the sub-game).
                    var subGame = new CrabRecursiveCombat(_player1.Take(card1), _player2.Take(card2));
                    var(p1, p2) = subGame.Play();
                    player1Wins = p1.Any();
                }

                if (player1Wins)
                {
                    _player1.Enqueue(card1);
                    _player1.Enqueue(card2);
                }
                else
                {
                    _player2.Enqueue(card2);
                    _player2.Enqueue(card1);
                }
            }

            return(_player1, _player2);
        }