15. BlackJackGame

In our previous tutorial you may recall we added BlackJackGame as an inner class. Well this is once again required…this time though it is being placed inside our “StartActivity.java” file. You can copy and paste the inner class from our previous project but it will require a few changes:

Member Variables
In our old project we had five member variables, two of those were specific to our console-based solution and this project no longer requires that so you can delete the “keyboard” and “input” member variable declarations.

Because we’ve deleted two of our member variables, they’re instantiation is obviously no longer necessary in our constructor. In our case we had only created the “keyboard” object and so you can delete that line now.

The “playGame()” Method
This method used to control the entire structure of a game. Our new game no longer needs a single method to have that much control…all we need is to start the game off so let’s delete that method and replace it with “startGame()“:

public void startGame() {
    // ... code goes here ...

The “showResults()” Method
The last change to make is to remove any reference to the console from showResults. Instead of printing information out we need to create a String that gets placed in the “txt_instruct” object, i.e:

public void showResults() {
    int playerScore = 0;
    int dealerScore = 0;
    String message = "";
    if (dealer.busted()) {
        message = "Dealer busted. ";
    } else {
        dealerScore = dealer.handValue();
    if (player.busted()) {
        message = message + "Player busted. ";
    } else {
        playerScore = player.handValue();
    if (dealerScore > playerScore) {
        message = message + "Dealer wins!";
    } else if (playerScore > dealerScore) {
        message = message + "Player wins!";
    } else {
        message = message + "It's a draw!";

And that should do for now. We’re going to add more to this class but at least we have a structure to build on.

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>