public int PlayRound()
        {
            Console.WriteLine($"Playing round {RoundId} of game {GameId}");
            RoundId++;
            int roundWinner = 0;

            if (Finished)
            {
                return(roundWinner);
            }
            if (RecursiveCheck())
            {
                //the game instantly ends in a win for player 1
                Finished = true;
                return(1);
            }
            var player1Card = Player1Deck.Draw();
            var player2Card = Player2Deck.Draw();

            //If both players have at least as many cards remaining in their deck as the value of the card they just drew,
            //the winner of the round is determined by playing a new game of Recursive Combat (see below).
            if (player1Card <= Player1Deck.Count && player2Card <= Player2Deck.Count)
            {
                var innerGame   = new RecursiveCombatGame(Player1Deck.CopyOfDeck(player1Card), Player2Deck.CopyOfDeck(player2Card), GameId + 1);
                var winnerInner = innerGame.PlayGame();
                if (winnerInner == 1)
                {
                    Player1Deck.Return(player1Card, player2Card);
                    roundWinner = 1;
                }
                else
                {
                    Player2Deck.Return(player2Card, player1Card);
                    roundWinner = 2;
                }
            }
            else
            {
                //at least one player must not have enough cards left in their deck to recurse; the winner of the round is the player with the higher-value card
                if (player1Card > player2Card)
                {
                    Player1Deck.Return(player1Card, player2Card);
                    roundWinner = 1;
                }
                else
                {
                    Player2Deck.Return(player2Card, player1Card);
                    roundWinner = 2;
                }
            }
            if (Player1Deck.Count == 0 || Player2Deck.Count == 0)
            {
                Finished = true;
            }
            return(roundWinner);
        }
Example #2
0
        static long Second(string inputFile)
        {
            var parser = new IntDeckParser();
            var decks  = parser.ReadDecks(inputFile);
            var game   = new RecursiveCombatGame(decks[0], decks[1], 1);
            var winner = game.PlayGame();
            var result = game.CalculateScore(winner);

            return(result);
        }