I was bored at university one night so coded blackjack in Java. Here are some of the more interesting aspects of coding up the game.

#### The Cards

With no cards, we can’t play blackjack. So the question is how to manage and keep track of them. There are only two things we need to know about a card:

- Suit
- Value

So I made a “Card” class with just these attributes. The easiest way to store these are just as integers with 0, 1, 2, 3 meaning spades, clubs, hearts, diamonds and using numbers 2 – 14 for values 2 through to 11 for jack and 14 for the aces.

public class Card { private int value = 0; private int suit = 0; public Card(int v, int s){ value = v; suit = s; }

The card class also has a “getScore” method that returns the face value of a card, returning 10 for picture cards and 11 for aces, which helps later on when we evaluate who wins a hand or who busts.

#### The Deck

We want a way of putting all valid cards together and doing stuff like shuffling them and drawing cards from the top, so it makes sense to make a Deck class that will handle these events.

public Deck(){ random = new Random(); deck = new ArrayList(); emptyDeck = new ArrayList(); addCards(); }

The deck is an array list of cards (array lists are like lists but it is a bit more easy to dynamically add, remove and reference things). The empty deck is used for shuffling, you will see that in a minute.

We want a way of actually populating the deck so I make a function called addCards.

private void addCards() { deck.clear(); for(int s = 0; s < 4;s++){ for(int v = 2; v < 15; v++){ deck.add(new Card(v, s)); } } }

We iterate through all suits, 0 – 3 and make all of our new cards in the nested loop where we iterate through values (remember, 11 – 14 are the face cards).

The other function we need is to shuffle these cards. I can think of a better solution now but here is what I went with at the time:

public void shuffle(){ addCards(); random = new Random(); int randomNumber = 0; for(int i = 0; i < 52; i++){ randomNumber = random.nextInt(deck.size()); emptyDeck.add(deck.get(randomNumber)); deck.remove(randomNumber); } for(int i = 0; i < 52; i++) { deck.add(emptyDeck.get(i)); } }

We re-add in order all the cards into the deck. Then, 52 times we pick a random card we haven’t picked before from the deck and temporarily store it in a different deck – “emptyDeck”. We then make our main deck to be the emptyDeck.

This looks a bit clunky but any method would be fairly similar to this. It’s actually kind of hard to “shuffle” something because you have to keep track of what you’ve already pulled out and what you have left to pull out – hence why here we use a dummy deck. I think the above is O(N^2) so I might make another blog post discussing the cheapest way to shuffle N objects (although it doesn’t matter much here as there is always only 52).

To pick cards from this deck we can just skim the Card in last position and then remove it from the deck. In our main class we can introduce more than one deck or shuffle after X% of the cards are dealt etc.

#### Evaluating a Hand

There’s a slight difficulty in evaluating a players/dealers hand in the form of aces. Aces can be 1 or 11 so you have to take this into account:

private int calculateScore(ArrayList cards){ int aces = 0; int sum = 0; for(int i = 0; i < cards.size(); i++){ if(cards.get(i).getValue() == 14){ aces++; } sum += cards.get(i).getScore(); } if(aces != 0){ for(int i = 1; i 21){ sum -= 10; } } } return sum; }

For each card in the list of cards a player/ dealer has, we get the score associated with each card and sum them up. Whilst we do this we note if any of these cards were aces and keep a tally of how many. After running through all cards and adding up the final sum of scores, if the sum is over 21, the algorithm runs through all the aces. It sets each in turn to 1 instead of 11 (by taking off 10 from the sum) until either the sum goes below 22, or there are no more aces. Return the sum.

Those are really only the main bits required for Blackjack. The rest is just frilly housekeeping, making a GUI and keeping track of the flow of the game and keeping track of chips the player has. Hopefully this gives you some idea of how to get it up and running.