16. startGame()

The “startGame” method needs a little bit of thought and will be slightly different to our old “playGame” method. Some aspects will remain but because we now need to show pictures representing the cards, some new skills are required. Place the following code inside our “startGame” method:

// ... show dealer card 1 ...
// ... show player card 1 ...
// ... show player card 2 ...
txt_instruct.setText("So what would you like to do...Hit or Stay?");

The first two lines are pretty obvious, we did those in our previous project and they set up the start of the game. What differs then is that we need to show a picture representing the cards in both the dealer and player Hand objects.

If you want to change the image that is shown in an ImageView object then the setImageResource method is used. If you know the name of the file in your drawable folder you want to use then you simply put it as the parameter with “R.drawable.” at the front minus the “.png” file extension. (Note: Be aware that you can only use “png” images in an Android application and you can’t have subfolders in your drawable folders!)

The problem with the three commented lines is that we don’t actually have the exact resource name we require. We can find out what cards have been dealt to the dealer and the player but how do we show them? This requires some new methods.

The first thing we need is a method that can tell us what card is in a Hand object at a particular position, so we’re going to add a new method to our Hand class called “cardAt()“, i.e:

public Card cardAt(int cardToReturn) {
    return cardsInHand.get(cardToReturn);

Notice that this method simply takes an Integer as a parameter and then returns the Card at that position in the “cardsInHand” ArrayList. You may have noticed that I’ve not included any kind of error-checking, that’s something you can add if you like.

Now that we can determine the Card in a particular position we need to be able to find the drawable resource that represents that card so we can change the picture. So in our BlackJackGame class we’re going to add another method called “getCard()“:

private int getCard(Card cardToFind) {
    return getResources().getIdentifier(
        getPackageName() +
        ":drawable/" +
        null, null);

This method takes a Card as a parameter and then works out what the name of that card is (this will match the name in our drawable folder) and then returns an Integer that can be used in the setImageResource method in order to show the correct picture for the passed Card.

There are many ways to solve this problem we faced but rather than have you research and stress over it this method will do the job fine. I suggest when you’ve finished this tutorial though it may be worth your while coming back to this lesson to make sure you have an understanding of how this method works.

Showing a particular card
Now that we’ve created those helper methods we can show a Card from a Hand object fairly easily. So where we had comment place holders in our “startGame” method like this:

// ... show dealer card 1 ...

We can now put a line like this:


It’s pretty important you understand how that line works so I’ll leave it to you to work out how you would show the players cards using the knowledge in this lesson! Once you do that,¬†our “startGame()” method is complete! So let’s get those buttons to start doing something useful.

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>