Example #1
0
        public SimRoundResult PlaySimulation(
            int firstPlayerTotalPoints,
            int secondPlayerTotalPoints,
            SimPlayerAction firstAction,
            SimPlayerAction secondAction,
            ICollection <Card> firstPlayerCards,
            ICollection <Card> secondPlayerCards)
        {
            this.CallStartRoundAndDealCardsSimulation(this.firstPlayer, firstPlayerTotalPoints, secondPlayerTotalPoints, firstPlayerCards);
            this.CallStartRoundAndDealCardsSimulation(this.secondPlayer, secondPlayerTotalPoints, firstPlayerTotalPoints, secondPlayerCards);

            SimRoundPlayerInfo lastTrickWinner;

            lastTrickWinner = this.PlayFirstTrickSimulation(firstAction, secondAction);

            while (!this.IsFinished())
            {
                if (this.deck.CardsLeft == 1)
                {
                    break;
                }

                lastTrickWinner = this.PlayTrickSimulation();
            }

            this.firstPlayer.Player.EndRound();
            this.secondPlayer.Player.EndRound();

            return(new SimRoundResult(this.firstPlayer, this.secondPlayer, lastTrickWinner));
        }
Example #2
0
        public SimRoundPlayerInfo PlayFirstTrickSimulation(SimPlayerAction firstAction, SimPlayerAction secondAction)
        {
            var trick = this.firstToPlay == PlayerPosition.FirstPlayer
                ? new SimTrick(this.firstPlayer, this.secondPlayer, this.stateManager, this.deck, this.gameRules)
                : new SimTrick(this.secondPlayer, this.firstPlayer, this.stateManager, this.deck, this.gameRules);

            var trickWinner = trick.PlayFirstSimulation(firstAction, secondAction);

            // The one who wins the trick should play first
            this.firstToPlay = trickWinner == this.firstPlayer
                                   ? PlayerPosition.FirstPlayer
                                   : PlayerPosition.SecondPlayer;

            if (this.stateManager.State.ShouldDrawCard)
            {
                // The player who wins last trick takes card first
                if (this.deck.CardsLeft > 0)
                {
                    if (this.firstToPlay == PlayerPosition.FirstPlayer)
                    {
                        this.GiveCardToPlayer(this.firstPlayer);
                        this.GiveCardToPlayer(this.secondPlayer);
                    }
                    else
                    {
                        this.GiveCardToPlayer(this.secondPlayer);
                        this.GiveCardToPlayer(this.firstPlayer);
                    }
                }
            }

            this.stateManager.State.PlayHand(this.deck.CardsLeft);

            return(trickWinner);
        }
Example #3
0
        public SimRoundPlayerInfo PlayFirstSimulation(SimPlayerAction firstAction, SimPlayerAction secondAction)
        {
            var context = new SimPlayerTurnContext(
                this.stateManager.State,
                this.deck.TrumpCard,
                this.deck.CardsLeft,
                this.firstToPlay.RoundPoints,
                this.secondToPlay.RoundPoints);

            // First player
            var firstPlayerAction = firstAction != null ? firstAction : this.GetFirstPlayerAction(this.firstToPlay, context);

            context.FirstPlayedCard        = firstPlayerAction.Card;
            context.FirstPlayerAnnounce    = firstPlayerAction.Announce;
            context.FirstPlayerRoundPoints = this.firstToPlay.RoundPoints;

            this.firstToPlay.Cards.Remove(firstPlayerAction.Card);

            // When player announces something he may immediately become round winner
            if (this.firstToPlay.RoundPoints >= this.gameRules.RoundPointsForGoingOut)
            {
                // Inform players for end turn
                this.firstToPlay.Player.EndTurn(context);
                this.secondToPlay.Player.EndTurn(context);
                return(this.firstToPlay);
            }

            // Second player
            var secondPlayerAction = secondAction; // GetPlayerAction(this.secondToPlay, context);

            context.SecondPlayedCard = secondPlayerAction.Card;
            this.secondToPlay.Cards.Remove(secondPlayerAction.Card);

            // Determine winner
            ICardWinnerLogic cardWinnerLogic = new CardWinnerLogic();
            var winnerPosition = cardWinnerLogic.Winner(
                firstPlayerAction.Card,
                secondPlayerAction.Card,
                this.deck.TrumpCard.Suit);

            var winner = winnerPosition == PlayerPosition.FirstPlayer ? this.firstToPlay : this.secondToPlay;

            winner.TrickCards.Add(firstPlayerAction.Card);
            winner.TrickCards.Add(secondPlayerAction.Card);

            // Inform players for end turn
            context.FirstPlayerRoundPoints  = this.firstToPlay.RoundPoints;
            context.SecondPlayerRoundPoints = this.secondToPlay.RoundPoints;
            this.firstToPlay.Player.EndTurn(context);
            this.secondToPlay.Player.EndTurn(context);

            return(winner);
        }
Example #4
0
        public bool IsValid(SimPlayerAction action, SimPlayerTurnContext context, ICollection <Card> playerCards)
        {
            if (context.State.CanAnnounce20Or40)
            {
                action.Announce = this.announceValidator.GetPossibleAnnounce(
                    playerCards,
                    action.Card,
                    context.TrumpCard,
                    context.IsFirstPlayerTurn);
            }

            if (action == null)
            {
                return(false);
            }

            if (action.Type == PlayerActionType.PlayCard)
            {
                var canPlayCard = SimPlayCardActionValidator.CanPlayCard(
                    context.IsFirstPlayerTurn,
                    action.Card,
                    context.FirstPlayedCard,
                    context.TrumpCard,
                    playerCards,
                    context.State.ShouldObserveRules);
                return(canPlayCard);
            }

            if (action.Type == PlayerActionType.ChangeTrump)
            {
                var canChangeTrump = SimChangeTrumpActionValidator.CanChangeTrump(
                    context.IsFirstPlayerTurn,
                    context.State,
                    context.TrumpCard,
                    playerCards);
                return(canChangeTrump);
            }

            // action.Type == PlayerActionType.CloseGame
            var canCloseGame = SimCloseGameActionValidator.CanCloseGame(context.IsFirstPlayerTurn, context.State);

            return(canCloseGame);
        }
        public bool IsValid(SimPlayerAction action, SimPlayerTurnContext context, ICollection<Card> playerCards)
        {
            if (context.State.CanAnnounce20Or40)
            {
                action.Announce = this.announceValidator.GetPossibleAnnounce(
                    playerCards,
                    action.Card,
                    context.TrumpCard,
                    context.IsFirstPlayerTurn);
            }

            if (action == null)
            {
                return false;
            }

            if (action.Type == PlayerActionType.PlayCard)
            {
                var canPlayCard = SimPlayCardActionValidator.CanPlayCard(
                    context.IsFirstPlayerTurn,
                    action.Card,
                    context.FirstPlayedCard,
                    context.TrumpCard,
                    playerCards,
                    context.State.ShouldObserveRules);
                return canPlayCard;
            }

            if (action.Type == PlayerActionType.ChangeTrump)
            {
                var canChangeTrump = SimChangeTrumpActionValidator.CanChangeTrump(
                    context.IsFirstPlayerTurn,
                    context.State,
                    context.TrumpCard,
                    playerCards);
                return canChangeTrump;
            }

            // action.Type == PlayerActionType.CloseGame
            var canCloseGame = SimCloseGameActionValidator.CanCloseGame(context.IsFirstPlayerTurn, context.State);
            return canCloseGame;
        }
Example #6
0
 protected SimPlayerAction CloseGame()
 {
     return(SimPlayerAction.CloseGame());
 }
Example #7
0
 protected SimPlayerAction PlayCard(Card card)
 {
     this.Cards.Remove(card);
     return(SimPlayerAction.PlayCard(card));
 }
Example #8
0
 protected SimPlayerAction ChangeTrump(Card trumpCard)
 {
     this.Cards.Remove(new Card(trumpCard.Suit, CardType.Nine));
     this.Cards.Add(trumpCard);
     return(SimPlayerAction.ChangeTrump());
 }
Example #9
0
        public SimRoundResult PlaySimulation(
            int firstPlayerTotalPoints,
            int secondPlayerTotalPoints,
            SimPlayerAction firstAction,
            SimPlayerAction secondAction,
            ICollection<Card> firstPlayerCards,
            ICollection<Card> secondPlayerCards)
        {
            this.CallStartRoundAndDealCardsSimulation(this.firstPlayer, firstPlayerTotalPoints, secondPlayerTotalPoints, firstPlayerCards);
            this.CallStartRoundAndDealCardsSimulation(this.secondPlayer, secondPlayerTotalPoints, firstPlayerTotalPoints, secondPlayerCards);

            SimRoundPlayerInfo lastTrickWinner;

            lastTrickWinner = this.PlayFirstTrickSimulation(firstAction, secondAction);

            while (!this.IsFinished())
            {
                if (this.deck.CardsLeft == 1)
                {
                    break;
                }

                lastTrickWinner = this.PlayTrickSimulation();
            }

            this.firstPlayer.Player.EndRound();
            this.secondPlayer.Player.EndRound();

            return new SimRoundResult(this.firstPlayer, this.secondPlayer, lastTrickWinner);
        }
Example #10
0
        public SimRoundPlayerInfo PlayFirstTrickSimulation(SimPlayerAction firstAction, SimPlayerAction secondAction)
        {
            var trick = this.firstToPlay == PlayerPosition.FirstPlayer
                ? new SimTrick(this.firstPlayer, this.secondPlayer, this.stateManager, this.deck, this.gameRules)
                : new SimTrick(this.secondPlayer, this.firstPlayer, this.stateManager, this.deck, this.gameRules);

            var trickWinner = trick.PlayFirstSimulation(firstAction, secondAction);

            // The one who wins the trick should play first
            this.firstToPlay = trickWinner == this.firstPlayer
                                   ? PlayerPosition.FirstPlayer
                                   : PlayerPosition.SecondPlayer;

            if (this.stateManager.State.ShouldDrawCard)
            {
                // The player who wins last trick takes card first
                if (this.deck.CardsLeft > 0)
                {
                    if (this.firstToPlay == PlayerPosition.FirstPlayer)
                    {
                        this.GiveCardToPlayer(this.firstPlayer);
                        this.GiveCardToPlayer(this.secondPlayer);
                    }
                    else
                    {
                        this.GiveCardToPlayer(this.secondPlayer);
                        this.GiveCardToPlayer(this.firstPlayer);
                    }
                }
            }

            this.stateManager.State.PlayHand(this.deck.CardsLeft);

            return trickWinner;
        }
Example #11
0
        public SimRoundPlayerInfo PlayFirstSimulation(SimPlayerAction firstAction, SimPlayerAction secondAction)
        {
            var context = new SimPlayerTurnContext(
                this.stateManager.State,
                this.deck.TrumpCard,
                this.deck.CardsLeft,
                this.firstToPlay.RoundPoints,
                this.secondToPlay.RoundPoints);

            // First player
            var firstPlayerAction = firstAction != null ? firstAction : this.GetFirstPlayerAction(this.firstToPlay, context);
            context.FirstPlayedCard = firstPlayerAction.Card;
            context.FirstPlayerAnnounce = firstPlayerAction.Announce;
            context.FirstPlayerRoundPoints = this.firstToPlay.RoundPoints;

            this.firstToPlay.Cards.Remove(firstPlayerAction.Card);

            // When player announces something he may immediately become round winner
            if (this.firstToPlay.RoundPoints >= this.gameRules.RoundPointsForGoingOut)
            {
                // Inform players for end turn
                this.firstToPlay.Player.EndTurn(context);
                this.secondToPlay.Player.EndTurn(context);
                return this.firstToPlay;
            }

            // Second player
            var secondPlayerAction = secondAction; // GetPlayerAction(this.secondToPlay, context);
            context.SecondPlayedCard = secondPlayerAction.Card;
            this.secondToPlay.Cards.Remove(secondPlayerAction.Card);

            // Determine winner
            ICardWinnerLogic cardWinnerLogic = new CardWinnerLogic();
            var winnerPosition = cardWinnerLogic.Winner(
                firstPlayerAction.Card,
                secondPlayerAction.Card,
                this.deck.TrumpCard.Suit);

            var winner = winnerPosition == PlayerPosition.FirstPlayer ? this.firstToPlay : this.secondToPlay;
            winner.TrickCards.Add(firstPlayerAction.Card);
            winner.TrickCards.Add(secondPlayerAction.Card);

            // Inform players for end turn
            context.FirstPlayerRoundPoints = this.firstToPlay.RoundPoints;
            context.SecondPlayerRoundPoints = this.secondToPlay.RoundPoints;
            this.firstToPlay.Player.EndTurn(context);
            this.secondToPlay.Player.EndTurn(context);

            return winner;
        }