IGNORED

# Programming a Blackjack game is hard.

## Recommended Posts

I have just begun programming a Blackjack game for the Virtual Boy, and I am in awe of how Atari was able to fit a blackjack game on an Atari 2600 cartridge, not only that, they used just 2k! I don't know how they did that. My biggest problem so far is having to keep track of which cards have been used and which haven't. How on earth did they do that using only 2k? If anyone has any tips for me, please tell me them. I know "21 Blue" was a homebrew blackjack game as well. So how did they manage it using just 2k?!

##### Share on other sites

How many decks did they use? If one, that's only 52 bits of RAM. If 8, that's still only 52 bytes.

Probably there's more efficient ways, but pick a random number (up to 52 or 416 for example), and if card/bit is already used (0), then repeat until you "pick" one that's not used yet (1). Set it as used (0), and carry on.

##### Share on other sites

No idea, but I suppose if you can waste 7 bytes out of the 128 available, you can store 7*8 = 56 bits of information which is enough to hold an entire deck of cards. If you can waste another byte, you can assign two bytes per colour of the deck and still have three bits left for something else in each colour.

##### Share on other sites

I have no idea how Atari Blackjack does it, but for 21 Blue I managed to squeeze four decks worth of cards into 7 bytes. Multiple decks were important, because I wanted the game to be realistic for people who enjoy counting cards.

The trick mostly is that you don't really care about the suit of cards in blackjack, so you can store the count of each card type in a nibble.

22223333 byte 1
44445555 byte 2
66667777 byte 3
88889999 byte 4
0000JJJJ byte 5
QQQQKKKK byte 6
AAAA---- byte 7

If you wanted to display different card suits for realism, it wouldn't be that hard to fake it. 21 Blue doesn't display the card suit, since it's too hard to squeeze into player graphics, so it was a moot point for me.

The main drawback of this approach is you need to decode the values, so it's trade-off of ROM for RAM. It was well worth it, in my case.

##### Share on other sites

I have no idea how Atari Blackjack does it, but for 21 Blue I managed to squeeze four decks worth of cards into 7 bytes. Multiple decks were important, because I wanted the game to be realistic for people who enjoy counting cards.

The trick mostly is that you don't really care about the suit of cards in blackjack, so you can store the count of each card type in a nibble.

22223333 byte 1

44445555 byte 2

66667777 byte 3

88889999 byte 4

0000JJJJ byte 5

QQQQKKKK byte 6

AAAA---- byte 7

If you wanted to display different card suits for realism, it wouldn't be that hard to fake it. 21 Blue doesn't display the card suit, since it's too hard to squeeze into player graphics, so it was a moot point for me.

The main drawback of this approach is you need to decode the values, so it's trade-off of RAM for ROM. It was well worth it, in my case.

Still waiting for the 21 Blue release in the AA store..............

##### Share on other sites

You and me both It still needs a proper label and AA worthy manual. I think part of the problem is it can't be shown live PRGE and other live events, so it gets a bit less priority.

And to bring it back on topic, since the atari2600land mentioned he's working on the Virtual Boy, I think he can probably use bytes instead of nibbles or bits, which should simplify the code a fair bit.

##### Share on other sites

You say 2K - that was for the program code in ROM. Keeping track of all the cards, money etc was done in RAM - 128 bytes!

Edited by davyK
##### Share on other sites

I programmed a blackjack style games years ago and kept track of the cards by using an array to store the cards used. I then made sure to compare the cards used with ones stored in the array. Once the hand was over the cards would be copied into the array.

I am sure however there are lots of other great ways to do that are more efficient but what I love about programming is that there are many ways to skin a camel though a cats needle eye.

##### Share on other sites

Lots of clever ways of squeezing a deck into a small number of bytes using bit wise operators. With blackjack you don't need to worry about suits so that's information you don't need to store. You only need 52 bits (7 bytes) to track whether a card has been dealt or not - at least in theory.

However Atari's Casino has poker games too that require suits. So even with it being a 4K ROM there is still the challenge of managing a deck of cards complete with suits in 128 bytes.

Edited by davyK
##### Share on other sites

Lots of clever ways of squeezing a deck into a small number of bytes using bit wise operators. With blackjack you don't need to worry about suits so that's information you don't need to store. You only need 52 bits (7 bytes) to track whether a card has been dealt or not - at least in theory.

However Atari's Casino has poker games too that require suits. So even with it being a 4K ROM there is still the challenge of managing a deck of cards complete with suits in 128 bytes.

52 bits is enough to track a single deck with suits, since each bit position represents a unique card. 52 bits isn't particularly onerous. If you don't need suits you can actually represent a single full deck with 26 bits.

FWIW, you can also simulate an "infinite" number of decks by using no storage, and just pulling each card with a random value from 0 to 51. This would be the simplest approach, and most players wouldn't notice the difference. I believe even card counters wouldn't be able to detect the difference between this, and a casino using continuous shuffling machines.

##### Share on other sites

I have just begun programming a Blackjack game for the Virtual Boy, and I am in awe of how Atari was able to fit a blackjack game on an Atari 2600 cartridge, not only that, they used just 2k! I don't know how they did that. My biggest problem so far is having to keep track of which cards have been used and which haven't. How on earth did they do that using only 2k? If anyone has any tips for me, please tell me them. I know "21 Blue" was a homebrew blackjack game as well. So how did they manage it using just 2k?!

Just use a stack or queue data structure. 52 bytes for the deck. when you use a card, "pop" it off the stack. And if 52 bytes is too much, you can pack the data more

Edited by zzip

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.