Reputation: 25
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?
Upvotes: 0
Views: 1491
Reputation: 2712
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;
Upvotes: 1