Esempio n. 1
0
        /// <summary>
        /// Stops a current blackjack game and
        /// evaluates all of the hands against
        /// the dealer.
        /// </summary>
        public override void Stop()
        {
            base.Stop();
            _playerPointer = -1;

            // Finish off the dealers hand.
            Dealer.CompleteHand(this);

            // Loop through each player and assign them
            // a win or lose status based on the results
            // of comparing against the dealers hand.
            for (int i = 0; i < Players.Count; i++)
            {
                BlackjackPlayer player = (BlackjackPlayer)Players[i];

                // If someone busted the automatically
                // lose.
                if (true == player.Hand.IsBusted)
                {
                    player.State = PlayerStateEnum.Lost;
                    AddHistory(player + " busted. They lose.");
                }
                // If the dealer busted, everyong wins.
                else if (true == Dealer.Hand.IsBusted)
                {
                    player.State = PlayerStateEnum.Won;
                    AddHistory("Dealer busted. " + player + " wins.");
                }
                // Otherwise, we need to compare against
                // the dealer's hand.
                else
                {
                    if (player.Hand.Value > Dealer.Hand.Value)
                    {
                        player.State = PlayerStateEnum.Won;
                        AddHistory(player + " wins. " + player.Hand.Value + " vs " + Dealer.Hand.Value);
                    }
                    else if (player.Hand.Value == Dealer.Hand.Value)
                    {
                        player.State = PlayerStateEnum.Push;
                        AddHistory(player + " pushes with " + Dealer.Hand.Value);
                    }
                    else
                    {
                        player.State = PlayerStateEnum.Lost;
                        AddHistory(player + " loses. " + player.Hand.Value + " vs " + Dealer.Hand.Value);
                    }
                }
            }

            AddHistory("Waiting for " + Owner + " to start the next game.");
        }
Esempio n. 2
0
        /// <summary>
        /// Processes a stay request.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private BlackjackResponse processStay(BlackjackRequest request)
        {
            BlackjackTable table = (BlackjackTable)_tables.Get(request.Table);

            if (null == table)
            {
                throw new Exception(
                          "Cannot perform action. Table does not exist.");
            }

            BlackjackPlayer player = (BlackjackPlayer)table.Get(request.Player);

            if (null == player)
            {
                throw new Exception(
                          "Cannot perform action. Player does not exist at table.");
            }

            if (table.State != TableState.Started)
            {
                throw new Exception(
                          "Cannot perform action. Table has not started.");
            }

            if (player.State != PlayerStateEnum.Turn)
            {
                throw new Exception("It is not " + player.Name + " turn.");
            }

            table.AddHistory(
                "Player " + player.Name + " stays.");

            BlackjackResponse response = null;

            if (true == table.IsLastPlayer)
            {
                response = processEvaluate(request);
            }
            else
            {
                table.NextPlayer();

                response = new BlackjackResponse(
                    table,
                    BlackjackResult.Success);
            }

            return(response);
        }
Esempio n. 3
0
        /// <summary>
        /// Starts or restarts the blackjack
        /// game.
        /// </summary>
        public override void Start()
        {
            base.Start();

            _playerPointer = Players.Count - 1;

            Dealer.Hand.Clear();

            for (int i = 0; i < Players.Count; i++)
            {
                BlackjackPlayer player = (BlackjackPlayer)Players[i];
                player.State = PlayerStateEnum.Playing;
                player.Hand.Clear();
            }
            Players[_playerPointer].State = PlayerStateEnum.Turn;
            CurrentDeck.Shuffle();
            dealToEveryone();
        }
Esempio n. 4
0
        /// <summary>
        /// Returns all the current table information.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private BlackjackResponse processGet(BlackjackRequest request)
        {
            BlackjackTable table = (BlackjackTable)_tables.Get(request.Table.ID);

            if (null == table)
            {
                throw new Exception(
                          "Cannot perform action. Table does not exist.");
            }

            BlackjackPlayer player = (BlackjackPlayer)table.Get(request.Player);

            if (null == player)
            {
                throw new Exception(
                          "Cannot perform action. Player does not exist at table.");
            }

            return(new BlackjackResponse(
                       table,
                       BlackjackResult.Success));
        }
Esempio n. 5
0
        private BlackjackResponse processLeaveTable(BlackjackRequest request)
        {
            BlackjackTable table = (BlackjackTable)_tables.Get(request.Table);

            if (null == table)
            {
                throw new Exception(
                          "Cannot perform action. Table does not exist.");
            }

            BlackjackPlayer player = (BlackjackPlayer)table.Get(request.Player);

            if (null == player)
            {
                throw new Exception(
                          "Cannot perform action. Player does not exist at table.");
            }
            if (table.State == TableState.Started)
            {
                throw new Exception(
                          "Cannot perform action. Game has started. Finish the game first.");
            }

            table.Remove(player);
            table.AddHistory(player + " has left the table.");
            if (null != table.Owner)
            {
                table.AddHistory(table.Owner + " is the new owner of the table.");
            }
            else
            {
                _tables.Remove(table);
            }

            return(new BlackjackResponse(
                       table,
                       BlackjackResult.Success));
        }
Esempio n. 6
0
        /// <summary>
        /// Processes a hit request.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private BlackjackResponse processHit(BlackjackRequest request)
        {
            BlackjackTable table = (BlackjackTable)_tables.Get(request.Table);

            if (null == table)
            {
                throw new Exception(
                          "Cannot perform action. Table does not exist.");
            }

            BlackjackPlayer player = (BlackjackPlayer)table.Get(request.Player);

            if (null == player)
            {
                throw new Exception(
                          "Cannot perform action. Player does not exist at table.");
            }

            if (table.State != TableState.Started)
            {
                throw new Exception(
                          "Cannot perform action. Table has not started.");
            }

            if (player.State != PlayerStateEnum.Turn)
            {
                throw new Exception("It is not " + player.Name + " turn.");
            }

            Card card = table.CurrentDeck.Deal();

            player.Hand.AddCard(card);
            table.AddHistory(
                "Player " + player.Name + " drew a " + card);

            BlackjackResponse response = null;

            if (true == player.Hand.IsBusted)
            {
                table.AddHistory(
                    "Player " + player.Name + " busted.");

                if (true == table.IsLastPlayer)
                {
                    response = processEvaluate(request);
                }
                else
                {
                    table.NextPlayer();

                    response = new BlackjackResponse(
                        table,
                        BlackjackResult.Busted);
                }
            }
            else
            {
                response = new BlackjackResponse(
                    table,
                    BlackjackResult.Success);
            }

            return(response);
        }