コード例 #1
0
        private String retrieveThePlayersNames(InputOutput IO)
        {
            String numberOfPlayers = IO.obtainInputFromTheUser("How many player do you have? Enter 1 or 2");

            if (numberOfPlayers == "1")
            {
                player1Name = IO.obtainInputFromTheUser("Enter the name for Player 1: ");
            }
            else
            {
                player1Name = IO.obtainInputFromTheUser("Enter the name for player 1: ");

                player2Name = IO.obtainInputFromTheUser("Enter the name for player 2: ");
            }

            return(numberOfPlayers);
        }
コード例 #2
0
        private void checkForTheWinnerTwoPlayers()
        {
            InputOutput IO = new InputOutput();

            if (dealersHand == player1Hand)
            {
                IO.dislpayOutputToTheUser(player1Name + " and the dealer have tied\n");
            }
            if (dealersHand == player2Hand)
            {
                IO.dislpayOutputToTheUser(player2Name + " and the dealer have tied\n");
            }
            if (dealersHand < player1Hand && dealersHand <= 21 && player1Hand <= 21)
            {
                IO.dislpayOutputToTheUser(player1Name + " has won\n");
            }
            if (dealersHand < player2Hand && dealersHand <= 21 && player2Hand <= 21)
            {
                IO.dislpayOutputToTheUser(player2Name + " has won\n");
            }
            if (dealersHand > 21 && player1Hand <= 21)
            {
                IO.dislpayOutputToTheUser("The dealer has busted, " + player1Name + " has won\n");
            }
            if (dealersHand > 21 && player2Hand <= 21)
            {
                IO.dislpayOutputToTheUser("The dealer has busted, " + player2Name + " has won\n");
            }
            if (dealersHand == 21 && player1Hand != 21 && player2Hand != 21)
            {
                IO.dislpayOutputToTheUser("The dealer has won beat and beat both players\n");
            }
            if (dealersHand > player1Hand && dealersHand <= 21)
            {
                IO.dislpayOutputToTheUser("The dealer has beat " + player1Name + "\n");
            }
            if (dealersHand > player2Hand && dealersHand <= 21)
            {
                IO.dislpayOutputToTheUser("The dealer has beat " + player2Name + "\n");
            }
            if (dealersHand <= 21 && player1Hand > 21 && player2Hand > 21)
            {
                IO.dislpayOutputToTheUser("Both players have busted. The dealer is the winner.\n");
            }
        }
コード例 #3
0
        public void playTheGame()
        {
            InputOutput IO = new InputOutput();

            IO.dislpayOutputToTheUser("Welcome to the Blackjack Table\n");

            Thread.Sleep(milliseconds);

            String numberOfPlayers = retrieveThePlayersNames(IO);

            Thread.Sleep(milliseconds);

            if (numberOfPlayers == "1")
            {
                player1Blackjack();

                dealerBlackjack();

                checkForTheWinnerOnePlayer();

                decisionToContinuePlayingBlackjackOnePlayer();
            }
            else if (numberOfPlayers == "2")
            {
                player1Blackjack();

                player2Blackjack();

                dealerBlackjack();

                checkForTheWinnerTwoPlayers();

                decisionToContinuePlayingBlackjackTwoPlayer();
            }
            else
            {
                IO.dislpayOutputToTheUser("Invalid entry, try again");

                System.Environment.Exit(1);
            }
        }
コード例 #4
0
        private void checkForTheWinnerOnePlayer()
        {
            InputOutput IO = new InputOutput();

            if (dealersHand == player1Hand)
            {
                IO.dislpayOutputToTheUser(player1Name + " and the dealer have tied\n");
            }
            if (dealersHand < player1Hand && dealersHand <= 21 && player1Hand <= 21)
            {
                IO.dislpayOutputToTheUser(player1Name + " has won\n");
            }
            if (dealersHand > 21 && player1Hand <= 21)
            {
                IO.dislpayOutputToTheUser("The dealer has busted, " + player1Name + " has won\n");
            }
            if (dealersHand > player1Hand && dealersHand <= 21)
            {
                IO.dislpayOutputToTheUser("The dealer has beat " + player1Name + "\n");
            }
        }
コード例 #5
0
        private void player1Blackjack()
        {
            InputOutput IO = new InputOutput();

            while (true)
            {
                int cardOne = retrieveOneCard();

                player1UnicodeSuit = (char)decodeTheSuitOfTheCard(cardOne);

                player1FaceValue = Convert.ToString(decodeTheFaceOfTheCard(cardOne));

                player1Card1Value = decodeTheValueOfTheCard(cardOne);

                IO.dislpayOutputToTheUser("First card for " + player1Name + " is a: " + player1FaceValue + " of " + player1UnicodeSuit + "'s\n");

                Thread.Sleep(milliseconds);

                int cardTwo = retrieveOneCard();

                player1UnicodeSuit = (char)decodeTheSuitOfTheCard(cardTwo);

                player1FaceValue = decodeTheFaceOfTheCard(cardTwo);

                player1Card2Value = decodeTheValueOfTheCard(cardTwo);

                IO.dislpayOutputToTheUser("Second card for " + player1Name + " is a: " + player1FaceValue + " of " + player1UnicodeSuit + "'s\n");

                Thread.Sleep(milliseconds);

                player1Hand = player1Card1Value + player1Card2Value; // add the cards to create the hand

                IO.dislpayOutputToTheUser(player1Name + "'s hand value is: " + player1Hand + "\n");

                Thread.Sleep(milliseconds);

                if (player1Hand > 21) // checks for an Ace on first two cards, if the hand it greater than 21 it subtracts 10
                {
                    player1Hand -= 10;
                }

                if (player1Hand == 21) // check for blackjack, if so player wins and breaks
                {
                    IO.dislpayOutputToTheUser("BlackJack you win!!!\n");

                    Thread.Sleep(milliseconds);

                    break;
                }

                while (player1Hand < 21) // if hand is less than 21 ask player to hit or stay
                {
                    String player1Answer = IO.obtainInputFromTheUser(player1Name + ", Do you want to hit? Y or N\n");

                    if (player1Answer == "Y" || player1Answer == "y") // if player hits
                    {
                        getAnotherCard = retrieveOneCard();

                        player1UnicodeSuit = (char)decodeTheSuitOfTheCard(getAnotherCard);

                        player1FaceValue = decodeTheFaceOfTheCard(getAnotherCard);

                        getAnotherCard = decodeTheValueOfTheCard(getAnotherCard);

                        checkForAcePlayer1(getAnotherCard);

                        Thread.Sleep(milliseconds);

                        IO.dislpayOutputToTheUser(player1Name + " you were dealt " + player1FaceValue + " of " + player1UnicodeSuit + "'s\n");

                        Thread.Sleep(milliseconds);

                        IO.dislpayOutputToTheUser(player1Name + "'s hand value is " + player1Hand + "\n");

                        Thread.Sleep(milliseconds);

                        if (player1Hand > 21) // over 21 is a bust and game is over for the player
                        {
                            IO.dislpayOutputToTheUser(player1Name + " you busted\n");

                            Thread.Sleep(milliseconds);
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else if (player1Answer == "N" || player1Answer == "n") // player does not want another card
                    {
                        IO.dislpayOutputToTheUser("You decided to stay. " + player1Name + "'s hand value is: " + player1Hand + "\n");

                        Thread.Sleep(milliseconds);

                        break;
                    }
                    else
                    {
                        IO.dislpayOutputToTheUser("Incorrect input, try again\n"); // invalid entry try again

                        continue;
                    }
                }

                break;
            }
        }
コード例 #6
0
        private void dealerBlackjack()
        {
            InputOutput IO = new InputOutput();

            while (true)
            {
                int cardOne = retrieveOneCard();

                dealerUnicodeSuit = (char)decodeTheSuitOfTheCard(cardOne);

                dealerFaceValue = Convert.ToString(decodeTheFaceOfTheCard(cardOne));

                dealerCard1Value = decodeTheValueOfTheCard(cardOne);

                Thread.Sleep(milliseconds);

                IO.dislpayOutputToTheUser("Dealer's first card is a " + dealerFaceValue + " of " + dealerUnicodeSuit + "'s\n");

                int cardTwo = retrieveOneCard();

                dealerUnicodeSuit = (char)decodeTheSuitOfTheCard(cardTwo);

                dealerFaceValue = Convert.ToString(decodeTheFaceOfTheCard(cardTwo));

                dealerCard2Value = decodeTheValueOfTheCard(cardTwo);

                Thread.Sleep(milliseconds);

                IO.dislpayOutputToTheUser("Dealer's second card is a " + dealerFaceValue + " of " + dealerUnicodeSuit + "'s\n");

                Thread.Sleep(milliseconds);

                dealersHand = dealerCard1Value + dealerCard2Value;

                if (dealersHand > 21) // checks for an ace
                {
                    dealersHand -= 10;
                }

                IO.dislpayOutputToTheUser("Dealer's hand value is: " + dealersHand + "\n");

                Thread.Sleep(milliseconds);

                if (dealersHand <= 21 && player1Hand > 21)
                {
                    break;
                }

                if (dealersHand <= 21 && player1Hand > 21 && player2Hand > 21)
                {
                    break;
                }

                if (player1Hand <= 21 || player2Hand <= 21 && dealersHand < 21 || dealersHand < 17) // dealer plays if player1 or 2 did not bust
                {
                    while (dealersHand < 21 && dealersHand <= 16)                                   // dealer hits on these conditions in logical expression
                    {
                        getAnotherCard = retrieveOneCard();

                        dealerUnicodeSuit = (char)decodeTheSuitOfTheCard(getAnotherCard);

                        dealerFaceValue = decodeTheFaceOfTheCard(getAnotherCard);

                        getAnotherCard = decodeTheValueOfTheCard(getAnotherCard);

                        Thread.Sleep(milliseconds);

                        IO.dislpayOutputToTheUser("Dealer must hit\n");

                        Thread.Sleep(milliseconds);

                        IO.dislpayOutputToTheUser("Dealer was dealt a " + dealerFaceValue + " of " + dealerUnicodeSuit + "'s\n");

                        dealersHand = dealersHand + getAnotherCard;

                        Thread.Sleep(milliseconds);

                        IO.dislpayOutputToTheUser("Dealers hand value is: " + dealersHand + "\n");

                        if (dealersHand < 17)
                        {
                            continue;
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                break;
            }
        }