How to Create Android Games: Blackjack App

Last updated on Nov 13,2024 28.2K Views


We hope you had fun building the Drawing Brush Android application using Edureka’s tutorial last week! So, you want to create Android games now, don’t you? Very well, here’s a tutorial that would help you create Android games (Blackjack, to be specific). Since, the demand for Android Certifed Developers is on a meteoric rise, developing an Android game should be a nice and fun way to start with Android development.

To create Android games, you must understand how they run. This video would help you understand how my Blackjack application runs:

How to create Android games: Prerequisites

1. Skills

Here are the skills that you need to create Android games:

  • Basic Knowledge of Java
  • Good knowledge of Android Programming
  • The mind of a Programmer!

 2. Resources:

i) Eclipse IDE: If you don’t have Eclipse yet, download this free guide to help you set it up.

ii) Images of Cards

iii) Sound clips like – click sounds, card shuffling sounds, clips to play when you win and lose.

Here’s a screenshot of the sound clips I used. :)

Creating Android games: Blackjack app

Okay, now that you have everything that you’ll require for creating a basic Black Jack game, let’s begin!

Step 1: Familiarize yourself with the game

Before making any application a programmer needs to understand what he wants to build. So,

  • Play the game a couple of times
  • Make sure that you know the rules of the game

For those of you who are in a hurry to dive in and create Android games, here’s a quick overview:

1. The basic premise of the game:

Android games

  • It is a comparing card game between a player and the dealer
  • You want a hand value closer to 21 than that of the dealer.But, remember, you don’t want your hand value to exceed 21.

2. Values of the Cards:

Android games for Blackjack app

i) Value of an Ace:

  • The value of an Ace can either be ‘1’ or ‘11’.
  • Its default value is 11.
  • It is counted as 1 if :the total ‘hand value’ is going over 21 ‘or’ if you have two Aces in the hand (of course, the hand-value will exceed 21 in this case as well).

Example:

Case 1:  (A, 7) has the hand value of 18

Case 2: (A, 7, 8) has the hand value of 16.

In Case 1, the value of Ace is ’11’, whereas it is ‘1’ in Case 2.

ii) Cards ‘2’ through ‘9’: The cards 2 through 9 are marked at their face value.

iii) 10, Jack, Queen, and King are all valued at ‘10’.

3. Hand value/Value of a hand

The value of a hand is simply the sum of the point counts of each card in hand. For instance, a hand containing (5, 7, and 8) has the value of 20.

4. Player Moves (These would be the actions on Button Clicks)

  • Hit – Means you want to draw another card.
  • Stand – Means you stop at the current total.
  • Surrender – You will lose half money you bet in the hand.

Great, you already know the layman part of it pretty well. Not too shabby, eh!

Let’s get technical now, shall we!

Step 2: Algorithm of the game!

Now that you’ll (hopefully) create Android games all the time, you need to get the logic right. Therefore, the most important (though frustrating) step is writing the Algorithm. Believe me; I had a really hard time creating the algorithm initially. Don’t get frustrated if it seems a little tricky in the beginning. It gets better, when you get the hang of it.

I wrote some basic steps of the algorithm while coding. I hope this would make the task a little easier for you.

1. Build the User Interface

Here you would:

  • Show the cards to the user
  • Place buttons and text views in your layout
  • Name the IDs and text of these controls buttons (you will need them later)

Below is a screenshot of my application’s User Interface.

Android games: Blackjack app

2. This is how the game runs:

Blackjack app: Android games

3. How will the computer play his move?

All the game enthusiasts here would agree that this part typically defines the quality of any game.

The key to creating great Android games is to keep the logic simple: Computer will keep drawing cards until its score is less than 17 or less than ‘or’ equal to the user’s score.

Step 3: Start Coding

Tighten your seat belts because this will be a rough ride! :)

  • Resources:Keep all resources in your project handy:
    • Card Images
    • Sound Clips
  • User Interface: Create a basic layout similar to UI screenshot shown above.
  • Make small modules: While coding always make small reusable modules.
  • Implement a module: Run it – check it for errors.
  • Rectify your errors: The more errors you resolve, the closer you get to you target.

Java Classes Used:

Logic for Drawing Cards:

1) Random class of Java: Cards in the game are drawn at random. For that I used the random class of Java.

2) Binding cards with numbers: This random Class of Java works on numbers so we would need to bind the images of cards with numbers. For instance:

0 – Ace of Clubs

1 – King of Clubs

2 – Queen of Clubs

3 – Jack of Clubs

4 – 10 of Clubs, and so on.

You’ll end up with 52 card images bound to 52 numbers.

This is what I used. It is not necessary for you to bind the images in this exact order. You can always use your own logic.

Random _random = new Random();

_random.nextInt(52); will give you a random number between 0 and 51.

3) Running the logic: Show the user the image related to this number. For instance, (according to the sequence that I used) if the random number is 2, the user gets a Queen of Clubs.

4) Create a list of all the numbers: Create a list for all the numbers to make sure no card comes twice. (All your effort would be futile if the user gets the same card twice).

Logic for Scoring

1. Separate module for scoring: For scoring you need a separate module which will return you a number between 1 and 11 based on the card you get by the _random.nextInt(52);

Example: If the card was Queen – return a number 10

2. Create a separate list to store these scores.

I used two different arrays to store scores:

// Dealer and Player Score Count

int[] _dealerScoreCount = new int[] { 0, 0, 0, 0, 0 };

int[] _playerScoreCount = new int[] { 0, 0, 0, 0, 0 };

3. Empty the lists after one hand:

After one complete hand:

  • Empty all your lists
  • Reset your card Images to original
  • Reset the scores (both user’s and computer’s)

To keep it from getting complex:

  • Make functions like emptyLists(), resetImages(), resetScore() etc and call them after one hand completes.

Remember I had advised you guys earlier to make smaller modules. You’ll feel the move’s benefits now.

So, do you guys feel comfortable enough to create Android games on your own now? If you have the slightest doubt, sign up for our free class here!

If you want us to send you more Android programming tips in email, sign up here for Free. Stay tuned for more on How to create Android games! :)

Happy Learning!

 

edureka- logoYou may also like these related posts:

Comments
9 Comments

Join the discussion

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.