コード例 #1
0
        private int GetWinnerOfSubGame(int playerOneCard, int playerTwoCard)
        {
            var subGame = new Game(_RecursiveLevel + 1)
            {
                PlayerOneDeck = new Queue <int>(PlayerOneDeck.Take(playerOneCard)),
                PlayerTwoDeck = new Queue <int>(PlayerTwoDeck.Take(playerTwoCard)),
            };

            while (subGame.WinningPlayer == None)
            {
                subGame.PlayRecursiveRound();
            }
            return(subGame.WinningPlayer);
        }
コード例 #2
0
        public void PlayRecursiveRound()
        {
            var deckStatus = string.Join(",", PlayerOneDeck) + "|" + string.Join(",", PlayerTwoDeck);

            _HadCardsPreviouslyPlayed = _DeckArrangements.Contains(deckStatus);
            _DeckArrangements.Add(deckStatus);

            if (_HadCardsPreviouslyPlayed)
            {
                return;
            }

            var card1 = PlayerOneDeck.Dequeue();
            var card2 = PlayerTwoDeck.Dequeue();

            int winner;

            if (card1 <= PlayerOneDeck.Count && card2 <= PlayerTwoDeck.Count)
            {
                winner = GetWinnerOfSubGame(card1, card2);
            }
            else if (card1 > card2)
            {
                winner = PlayerOne;
            }
            else if (card1 < card2)
            {
                winner = PlayerTwo;
            }
            else
            {
                throw new NotSupportedException($"Game does not support ties: {card1}");
            }

            if (winner == PlayerOne)
            {
                PlayerOneDeck.Enqueue(card1);
                PlayerOneDeck.Enqueue(card2);
            }
            else if (winner == PlayerTwo)
            {
                PlayerTwoDeck.Enqueue(card2);
                PlayerTwoDeck.Enqueue(card1);
            }
        }
コード例 #3
0
        public void PlayRound()
        {
            var card1 = PlayerOneDeck.Dequeue();
            var card2 = PlayerTwoDeck.Dequeue();

            if (card1 > card2)
            {
                PlayerOneDeck.Enqueue(card1);
                PlayerOneDeck.Enqueue(card2);
            }
            else if (card1 < card2)
            {
                PlayerTwoDeck.Enqueue(card2);
                PlayerTwoDeck.Enqueue(card1);
            }
            else
            {
                throw new NotSupportedException($"Game does not support ties: {card1}");
            }
        }
コード例 #4
0
ファイル: Game.cs プロジェクト: HeilOliver/AdventOfCode2020
        public int CalculateGameScore()
        {
            if (WinBy == null)
            {
                return(-1);
            }

            int[] score = new int[0];
            if (WinBy == Player.One)
            {
                score = PlayerOneDeck.Reverse().ToArray();
            }
            if (WinBy == Player.Two)
            {
                score = PlayerTwoDeck.Reverse().ToArray();
            }

            for (int i = 0; i < score.Length; i++)
            {
                score[i] *= i + 1;
            }

            return(score.Sum());
        }