Example #1
0
        public void Start()
        {
            IPlayer      firstToPlay;
            IPlayer      secondToPlay;
            IList <Card> firstToPlayCards;
            IList <Card> secondToPlayCards;

            if (this.whoWillPlayFirst == PlayerPosition.FirstPlayer)
            {
                firstToPlay       = this.firstPlayer;
                firstToPlayCards  = this.firstPlayerCards;
                secondToPlay      = this.secondPlayer;
                secondToPlayCards = this.secondPlayerCards;
            }
            else
            {
                firstToPlay       = this.secondPlayer;
                firstToPlayCards  = this.secondPlayerCards;
                secondToPlay      = this.firstPlayer;
                secondToPlayCards = this.firstPlayerCards;
            }

            var context = new PlayerTurnContext(this.state, deck.GetTrumpCard, deck.CardsLeft);

            PlayerAction firstPlayerAction = null;

            do
            {
                firstPlayerAction =
                    this.FirstPlayerTurn(firstToPlay, context);

                if (!this.actionValidator.IsValid(firstPlayerAction, context, firstToPlayCards))
                {
                    // TODO: Do something more graceful?
                    throw new InternalGameException("Invalid turn!");
                }
            }while (firstPlayerAction.Type !=
                    PlayerActionType.PlayCard);

            context.FirstPlayedCard = firstPlayerAction.Card;

            PlayerAction secondPlayerAction = secondToPlay.GetTurn(
                context,
                this.actionValidator);

            if (!this.actionValidator.IsValid(secondPlayerAction, context, secondToPlayCards))
            {
                // TODO: Do something more graceful?
                throw new InternalGameException("Invalid turn!");
            }

            context.SecondPlayedCard = secondPlayerAction.Card;

            if (firstToPlay == this.firstPlayer)
            {
                this.firstPlayerCard      = firstPlayerAction.Card;
                this.firstPlayerAnnounce  = firstPlayerAction.Announce;
                this.secondPlayerCard     = secondPlayerAction.Card;
                this.secondPlayerAnnounce = secondPlayerAction.Announce;
            }
            else
            {
                this.firstPlayerCard      = secondPlayerAction.Card;
                this.firstPlayerAnnounce  = secondPlayerAction.Announce;
                this.secondPlayerCard     = firstPlayerAction.Card;
                this.secondPlayerAnnounce = firstPlayerAction.Announce;
            }

            firstToPlay.EndTurn(context);
            secondToPlay.EndTurn(context);

            ICardWinner cardWinner = new CardWinner();

            if (firstToPlay == this.firstPlayer)
            {
                this.winner = cardWinner.Winner(
                    firstPlayerAction.Card,
                    secondPlayerAction.Card,
                    this.deck.GetTrumpCard.Suit);
            }
            else
            {
                this.winner = cardWinner.Winner(
                    secondPlayerAction.Card,
                    firstPlayerAction.Card,
                    this.deck.GetTrumpCard.Suit);
            }
        }
        public void Start()
        {
            IPlayer firstToPlay;
            IPlayer secondToPlay;
            IList<Card> firstToPlayCards;
            IList<Card> secondToPlayCards;
            if (this.whoWillPlayFirst == PlayerPosition.FirstPlayer)
            {
                firstToPlay = this.firstPlayer;
                firstToPlayCards = this.firstPlayerCards;
                secondToPlay = this.secondPlayer;
                secondToPlayCards = this.secondPlayerCards;
            }
            else
            {
                firstToPlay = this.secondPlayer;
                firstToPlayCards = this.secondPlayerCards;
                secondToPlay = this.firstPlayer;
                secondToPlayCards = this.firstPlayerCards;
            }

            var context = new PlayerTurnContext(this.deck.GetTrumpCard, this.state, deck.CardsLeft);

            PlayerAction firstPlayerAction = null;
            do
            {
                firstPlayerAction = this.FirstPlayerTurn(firstToPlay, context);

                if (!this.actionValidater.isValid(firstPlayerAction, context, firstToPlayCards))
                {
                    //TODO: something more graceful?
                    throw new InternalGameExceptions("Invalid turn");
                }

            } while (firstPlayerAction.Type == PlayerActionType.PlayCard);

            context.FirstPlayedCard = firstPlayerAction.Card;

            PlayerAction secondPlayerAction = secondToPlay.GetTurn(
                context,
                this.actionValidater);
            if (!this.actionValidater.isValid(secondPlayerAction, context, secondToPlayCards))
            {
                //TODO: something more graceful?
                throw new InternalGameExceptions("Invalid turn");
            }

            context.SecondPlayedCard = secondPlayerAction.Card;

            if (firstToPlay == this.firstPlayer)
            {
                this.firstPlayerCard = secondPlayerAction.Card;
                this.firstPlayerAnnounce = secondPlayerAction.Announce;
                this.secondPlayerCard = firstPlayerAction.Card;
                this.secondPlayerAnnounce = firstPlayerAction.Announce;
            }
            else
            {

            }

            firstToPlay.EndTurn(context);
            secondToPlay.EndTurn(context);

            ICardWinner cardWinner = new CardWinner();
            if (firstToPlay == this.firstPlayer)
            {
                this.winner = cardWinner.Winner(firstPlayerAction.Card, secondPlayerAction.Card, this.deck.GetTrumpCard.Suit);
            }
            else
            {
                this.winner = cardWinner.Winner(secondPlayerAction.Card, firstPlayerAction.Card, this.deck.GetTrumpCard.Suit);
            }
        }