20. Are we there yet?

We’re close! let’s start by finishing the “dealersTurn” method:

dealersTurn()
This method is pretty simple, if you know about Blackjack then you know the dealer simply has to hit until their score is 17 or better, in other words:

while (dealer.handValue() < 17) {
   dealer.addCard(deck.deal());
}

You see why good class creation is useful!

showResults()
This one isn’t too tricky either, it’s basically a problem-solving exercise more than an Object-Oriented challenge:

int playerScore = 0;
int dealerScore = 0;
if (dealer.busted()) {
   System.out.println("DEALER: Busted!");
} else {
   dealerScore = dealer.handValue();
   System.out.println("DEALER: " + Integer.toString(dealerScore));
}
System.out.println(dealer.showHand(true));
if (player.busted()) {
   System.out.println("PLAYER: Busted!");
} else {
   playerScore = player.handValue();
   System.out.println("PLAYER: " + Integer.toString(playerScore));
}
System.out.println(player.showHand(true));
if (dealerScore > playerScore) {
   System.out.println("Dealer wins!");
} else if (playerScore > dealerScore) {
   System.out.println("Player wins!");
} else {
   System.out.println("It's a draw!");
}

We’re also dealing with all the writing to the console in this method so it looks a bit bulky but the beauty of it is that it’s such a simple call from the “playGame” method to work out who won.

We’re there!!!!!!
That’s it! We’re done! Go and press that play button at the top…

…and you should get output at the bottom much like the following:

If you don’t then feel free to ask questions and I’ll help you get it sorted.

Leave a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>