예제 #1
0
        public static bool TryPlayClassicRound(
            GameState initialGameState,
            out GameState finalGameState)
        {
            finalGameState = initialGameState;
            var deckCopies = new List <Deck>();

            foreach (var deck in initialGameState.Decks)
            {
                var deckCopy = DeckHelper.GetDeckCopy(deck);
                deckCopies.Add(deckCopy);
            }

            var topCards            = new List <int>();
            var bestCardPlayerIndex = -1;
            var bestCard            = -1;

            for (int playerIndex = 0; playerIndex < deckCopies.Count; playerIndex++)
            {
                var deck = deckCopies[playerIndex];
                if (deck.SpaceCards.Count == 0)
                {
                    continue;
                }
                var topCard = deck.SpaceCards.Dequeue();
                topCards.Add(topCard);

                if (topCard > bestCard)
                {
                    bestCard            = topCard;
                    bestCardPlayerIndex = playerIndex;
                }
            }

            // If one or fewer players still have cards, then the game is over
            if (topCards.Count < 2)
            {
                return(false);
            }

            // Add the cards to the winner's deck
            if (bestCardPlayerIndex >= 0)
            {
                var deck       = deckCopies[bestCardPlayerIndex];
                var cardsToAdd = topCards
                                 .OrderByDescending(card => card)
                                 .Select(card => card)
                                 .ToList();
                foreach (var card in cardsToAdd)
                {
                    deck.SpaceCards.Enqueue(card);
                }
            }

            finalGameState = new GameState(deckCopies, false);
            return(true);
        }
예제 #2
0
        private static IList <Deck> GetDay22Input()
        {
            string filePath = Path.Combine(Directory.GetCurrentDirectory(), "InputData", FILE_NAME);

            if (!File.Exists(filePath))
            {
                throw new Exception($"Cannot locate file {filePath}");
            }

            var inputLines = File.ReadAllLines(filePath);
            var result     = DeckHelper.ParseInputLines(inputLines);

            return(result);
        }
예제 #3
0
        public GameState(IList <Deck> decks, bool isAwaitingSubgameWinner)
        {
            var deckCopies = new List <Deck>();

            foreach (var deck in decks)
            {
                var deckCopy = DeckHelper.GetDeckCopy(deck);
                deckCopies.Add(deckCopy);
            }
            Decks = deckCopies;
            IsAwaitingSubgameWinner = isAwaitingSubgameWinner;
            StateString             = $"{IsAwaitingSubgameWinner}->{string.Join(";", Decks.Select(deck => deck.ToString()))}";
            StateStringHashCode     = StateString.GetHashCode();
        }
예제 #4
0
        public static GameState GetNextGameStateGivenSubgameWinner(
            GameState initialGameState,
            Deck roundWinner)
        {
            //var result = new GameState(initialGameState.Decks, false);

            if (TryGetWinner(initialGameState, out Deck _))
            {
                return(initialGameState);
            }
            var  decks      = DeckHelper.GetDeckCopies(initialGameState.Decks);
            var  topCards   = new List <int>();
            Deck winnerDeck = null;

            foreach (var deck in decks)
            {
                if (deck.SpaceCards.Count > 0)
                {
                    var topCard = deck.SpaceCards.Dequeue();
                    if (roundWinner.PlayerName.Equals(deck.PlayerName))
                    {
                        topCards.Insert(0, topCard);
                        winnerDeck = deck;
                    }
                    else
                    {
                        topCards.Add(topCard);
                    }
                }
            }
            foreach (var card in topCards)
            {
                winnerDeck.SpaceCards.Enqueue(card);
            }
            var result = new GameState(decks, false);

            return(result);
        }
예제 #5
0
        public static bool TryStartSubgame(GameState gameState, out GameState subgameState)
        {
            // Need to start a subgame if all players with cards remaining
            // draw cards such that the players have at least as many cards
            // remaining in their decks as the values of the cards
            // If so, each deck should contain the number of cards equal to
            // the value of the card that the player drew
            subgameState = null;
            var subgameDecks = new List <Deck>();

            foreach (var deck in gameState.Decks)
            {
                var deckCopy = DeckHelper.GetDeckCopy(deck);
                if (deckCopy.SpaceCards.Count == 0)
                {
                    continue;
                }
                var topCard = deckCopy.SpaceCards.Dequeue();
                if (deckCopy.SpaceCards.Count < topCard)
                {
                    return(false);
                }
                var subgameDeckCards = new List <int>();
                for (int i = 0; i < topCard; i++)
                {
                    subgameDeckCards.Add(deckCopy.SpaceCards.Dequeue());
                }
                var subgameDeck = new Deck(deckCopy.PlayerName, subgameDeckCards);
                subgameDecks.Add(subgameDeck);
            }
            if (subgameDecks.Count < 2)
            {
                return(false);
            }
            subgameState = new GameState(subgameDecks, false);
            return(true);
        }