コード例 #1
0
 internal State EvaluateHands()
 {
     //BlackJack
     if (HandLogic.IsBlackJack(_defaultPlayer, _dealer))
     {
         return(State.BlackJack);
     }
     //Win
     else if (HandLogic.IsWin(_defaultPlayer, _dealer))
     {
         return(State.Win);
     }
     //Push
     else if (HandLogic.IsPush(_defaultPlayer, _dealer))
     {
         return(State.Push);
     }
     //Bust
     else if (HandLogic.IsBust(_defaultPlayer))
     {
         return(State.Bust);
     }
     //Loss
     else
     {
         return(State.Loss);
     }
 }
コード例 #2
0
        public GameState Deal(double bet)
        {
            //Clear last game. Make new game with old balance.
            NewGame(_defaultPlayer.Balance);

            //Try to set new bet balance
            TryPlaceBet(bet);

            //Check if insufficient funds
            if (_gameState.CurrentState == State.LowBalance)
            {
                return(null);
            }

            //Shuffle a new deck
            _deckService.NewDeck();

            //At the beginning of each hand the dealer deals two cards,
            //first to the player and then to himself.
            DealFirstHands();

            //Check for BlackJack
            if (HandLogic.HasBlackJack(_defaultPlayer))
            {
                EndGame();
            }

            //Setup a new GameState
            _gameState = new GameState(_defaultPlayer, _dealer, State.Open);

            return(_gameState);
        }
コード例 #3
0
        public GameState Hit()
        {
            //throw Exception if Hit() is called when game state is not open.
            if (_gameState.CurrentState != State.Open)
            {
                throw new GameStateException($"Can not call Hit() if game State is not Open. " +
                                             $"Current state is {_gameState.CurrentState.ToString()}");
            }

            //If the game has no winner, give a card to the player
            AddCardToHand(_defaultPlayer);

            //Check for player blackjack or bust
            if (HandLogic.HasBlackJack(_defaultPlayer) || HandLogic.IsBust(_defaultPlayer))
            {
                //Init the end game
                EndGame();
            }

            return(_gameState);
        }
コード例 #4
0
        internal void EndGame()
        {
            //Reveal dealer second card
            _dealer.Hand[1] = _hiddenCard;

            //Calculate initial dealer score
            CalculateScore(_dealer);

            //If the dealer has a total of less than 17 points, he must hit.
            while (!HandLogic.IsSoft17(_dealer))
            {
                AddCardToHand(_dealer);
            }
            ;

            //Evaluate player hands
            if (_gameState.CurrentState == State.Open)
            {
                _gameState.CurrentState = EvaluateHands();
            }

            //Pay if won
            DoPayout();
        }