Esempio n. 1
0
        public static bool TryStartSubgame(GameState gameState, out GameState subgameState)
        {
            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);
        }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }