How to Create Android Games: Blackjack App

Last updated on Dec 20,2023 28K Views

How to Create Android Games: Blackjack App

edureka.co

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:

 2. Resources:

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

[contact-form-7 id=”773″ title=”FREE Android Installation Guide Download”]

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. :)

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,

This Wikipedia link would help you familiarize yourself with 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:

2. Values of the Cards:

i) Value of an Ace:

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)

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:

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

2. This is how the game runs:

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! :)

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:

To keep it from getting complex:

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!

 Resources: Wikipedia, abccounter.com.

You may also like these related posts:

BROWSE COURSES