Question

I have a problem with my dealerhand method in my blackjack game.

I have a method to produce a random card from the class deck.

The cards have assigned values to them and so forth. however the problem lies in the code where i want the dealer to draw a new card, and add the value to the existing total hand value. the code is a following.

//Basics for the values of dealers cards

    int dealerHandValue = 0;
    int tempDealerHandValue = 0;
    int totalDealerHandValue= 0;

//Dealers first card

    randomGenNum = (int)((range * Math.random()) + 1)*2;
    dealerHandValue = arrayCardRank[randomGenNum];
    CardSuit = arrayCardSuit[randomGenNum];


    System.out.println("Dealer First Card Shows : " + (CardSuit));

    tempDealerHandValue = dealerHandValue;

//Code executed when player stops drawing and stands.

           while (totalDealerHandValue < 18 && totalDealerHandValue <21)
           {

           randomGenNum = (int)((range * Math.random()) + 1)*2;
           dealerHandValue = arrayCardRank[randomGenNum];
           CardSuit = arrayCardSuit[randomGenNum];


           System.out.println("Dealer next Card Shows : " + (CardSuit));

           tempDealerHandValue = dealerHandValue;

           totalDealerHandValue = (tempDealerHandValue) + (dealerHandValue);

           System.out.println("Dealer total hand value is " + (totalDealerHandValue));

       }



       {
           System.out.println("Dealer stopped drawing");

           if (totalDealerHandValue >= totalUserHandValue)
           {

               System.out.println("Dealer wins");
               return;

           }
           else

               System.out.println("Congratulations! You Win!");
           return;


       }

This method will just add the new cards value to itself, on and on until the while statement ends.

i have gone blind on the problem, and i know it is easily fixed. can anyone help me towards what i am missing?

Was it helpful?

Solution

you're never incrementing totalDealerHandValue, just overwriting the value over and over again.

Replace these two lines:

tempDealerHandValue = dealerHandValue;

totalDealerHandValue = (tempDealerHandValue) + (dealerHandValue);

with

totalDealerHandValue += dealerHandValue;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top