Пример #1
0
        public IAmTheGameOver Play()
        {
            Players = _distributor.Distribute();

            Player winner     = null;
            int    iterations = 0;
            int    iterationsAfterReShuffle = 0;
            bool   reShuffled = false;

            while (!GameOver(ref winner))
            {
                if (ShouldContinue(ref iterations, ref iterationsAfterReShuffle, ref reShuffled, out IAmTheGameOver draw))
                {
                    continue;
                }
                if (draw is Draw)
                {
                    return(draw);
                }

                var takes = Players.TakeOneCardEach(Visibility.FaceUp);

                TableViewsHistory.Add(takes.BuildView());

                var playerOfStrongestTake = takes.UniqueStrongestPlayerIfExit();
                if (playerOfStrongestTake != null)
                {
                    playerOfStrongestTake.Gather(takes);
                    continue;
                }

                RunBattleIfNecessary(takes);

                BuildDroppedCards(takes);
            }

            if (winner == null)
            {
                return(Draw.Instance);
            }

            System.Diagnostics.Debug.Assert(winner.CardStack.Size + DroppedCards.Count == _distributor.DistributedCardsSize, "We have leaking cards here");

            return(new HasWinner(winner));
        }
Пример #2
0
        /// <summary>
        /// Build a battle's initial state.
        /// </summary>
        /// <param name="distribution">Represent a stubbed distribution of cards.
        ///     By Player, we have a list of CardStack(a list of cards list).
        /// </param>
        /// <param name="source"></param>
        /// <returns></returns>
        public static Game BuildGame(IList <List <Card> > distribution, IEnumerable <Player> source)
        {
            var numberOfJoueurs = distribution.Count;

            var playersCollection = source.ToArray();

            for (int i = 0; i < numberOfJoueurs; i++)
            {
                playersCollection[i].CardStack = new CardStack(distribution[i]);
            }

            IDistributeCards cardsDistributor = Substitute.For <IDistributeCards>();

            cardsDistributor.DistributedCardsSize.Returns(distribution.SelectMany(x => x).Count());
            cardsDistributor.Distribute().Returns(playersCollection.ToList());

            var game = new Game(cardsDistributor);

            return(game);
        }