예제 #1
0
        public Player PlayRecursiveBattle()
        {
            Init();
            while (PlayerOneCards.Any() && PlayerTwoCards.Any())
            {
                if (IsRoundRepeated())
                {
                    return(Player.PlayerOne);
                }
                UpdateGameStates();

                Player roundWinner;
                var    playerOneCard = PlayerOneCards.Dequeue();
                var    playerTwoCard = PlayerTwoCards.Dequeue();

                if (playerOneCard <= PlayerOneCards.Count && playerTwoCard <= PlayerTwoCards.Count)
                {
                    var nextSubGamePlayerOneCards = PlayerOneCards.ToList().GetRange(0, playerOneCard);
                    var nextSubGamePlayerTwoCards = PlayerTwoCards.ToList().GetRange(0, playerTwoCard);

                    var subGame = new Combat(nextSubGamePlayerOneCards, nextSubGamePlayerTwoCards);
                    roundWinner = subGame.PlayRecursiveBattle();
                }
                else
                {
                    roundWinner = playerOneCard >= playerTwoCard ? Player.PlayerOne : Player.PlayerTwo;
                }

                switch (roundWinner)
                {
                case Player.PlayerOne:
                    PlayerOneCards.Enqueue(playerOneCard);
                    PlayerOneCards.Enqueue(playerTwoCard);
                    break;

                case Player.PlayerTwo:
                    PlayerTwoCards.Enqueue(playerTwoCard);
                    PlayerTwoCards.Enqueue(playerOneCard);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            var gameWinner = PlayerOneCards.Any() ? Player.PlayerOne : Player.PlayerTwo;

            return(gameWinner);
        }
예제 #2
0
        public int Play()
        {
            Init();
            while (PlayerOneCards.Any() && PlayerTwoCards.Any())
            {
                var playerOneCard = PlayerOneCards.Dequeue();
                var playerTwoCard = PlayerTwoCards.Dequeue();

                if (playerOneCard >= playerTwoCard)
                {
                    PlayerOneCards.Enqueue(playerOneCard);
                    PlayerOneCards.Enqueue(playerTwoCard);
                }
                else
                {
                    PlayerTwoCards.Enqueue(playerTwoCard);
                    PlayerTwoCards.Enqueue(playerOneCard);
                }
            }

            var winnerScore = CalculateGameWinnerScore();

            return(winnerScore);
        }