Skip to main content
GameDev.net gamedev.net
🔒 Locked

AI in Blackjack

Started by MetroidHunter Dec 18, 2002 at 11:08 AM 8 replies 1.7k views
Original Post
MetroidHunter
MetroidHunter
I need help programming good AI in a blackjack game. In the program, it''s the dealer vs. the player. here''s what I have so far: // dealreal is the dealer''s total, score is player''s total if ( ( dealreal < 12 ) || ( dealreal < score ) ) { dcard = deal(deck); // dealer takes a card dealer[dealcards]= dcard; // gives card to dealer deck[dcard] = 0; // removes card from deck dealcards++; // increments dealer''s card count } any ideas?
The Heretic
The Heretic
As for AI, you can use the simple don't hit over 13 (?) rule used in Las Vegas casinos, or you can have cheap AI that loks at the deck, or you can read one of the books on it and have tables on when to split and what not.

I would personlaly prefer the third, just because of realism.

[edited by - The Heretic on December 18, 2002 12:16:48 PM]
antistuff
antistuff
There are set rules the dealer in blackjack has to follow, they dont have a choice of what to do (they have to go until they beat the player, some other things i cant remember). A search on google would probobly help you find them.



Your code looks okay, cept for one thing. You should use either structs or classes for the dealer and the player (and possibly the deck and one for a card too). The reason is to make it easier for you to code. Lets take the dealer as an example.

In your snipplet you have three variables that have to do with the dealer. Unless these are global, each time you call a function that has soemthing to do with the dealer you have to pass all three . Now if you go like this

typedef struct dealer_typ
{
int score;

card hand[7];

int num_in_hand;
}dealer;

you just have to pass dealer.

Just a good general progaming practice.
Hyatus
Hyatus
''I need help programming good AI in a blackjack game. In the program, it''s the dealer vs. the player. here''s what I have so far:

// dealreal is the dealer''s total, score is player''s total
if ( ( dealreal < 12 ) || ( dealreal < score ) )''

That''s not good AI. That''s cheating.
The ''dealer'' should not know the player''s score.

-Hyatus
"da da da"
krez
krez
quote:
Original post by Hyatus
That's not good AI. That's cheating.
The 'dealer' should not know the player's score.

nope, in blackjack the player's cards are all face up, and the dealer goes last. so, they would know the player's score. the first of the dealer's cards is face-down (unknown to the players, and even to the dealer until his turn).

either way, the dealer (house) must hit until his total is 17 or greater, or he goes over 21. there is no decision for him to make, these are the rules.

for an actual player-like A.I. (where you want to simulate a player, not the dealer with his set rules), you could find the probability of getting a good card based on the cards already dealt. that is, don't look ahead in the deck, but you still can "remember" which cards have been dealt. some people with photographic memories do this, and they have a better chance of winning towards the end of the deck. the size of the bet, and if it is worth "doubling down", are also a factor.

[edited by - krez on December 18, 2002 3:46:37 PM]
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
BeerNutts
BeerNutts
Blackjack is not a game abot thinking at all, but about probabilities of getting the best wining hand (unless your into counting cards, which is very tough except for the "pros").

The dealer always has the same rules (though the rules vary by casino''s). Dealer must hit on any thing under 17, and, at some casino''s, dealer must hit on "soft 17''s" (an Ace and a 6, for example).

The player, on the other hand, is given some leeway, and thats where most epople lose there money. Depending on the number of decks used per shuffle, there is a set way to play your hand depending on what card the dealer has showing. A quick search on the internet will provide you with all the rules.

Actually no casino game should require AI, it''s all based on "the sacred geometry of chance"
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
Stilton
Stilton
That''s a bit of a sweeping statement. How would you write a decent artificial poker player without using any AI?
IADaveMark
IADaveMark
Since I make money playing Blackjack, I figured I could help out here.

What was (finally) said about the dealer is true with one exception. Some houses have the "soft 17" rule. That is, rather than the dealer having to stand on 17, he can hit on 17 whenever it is "soft" one... that is if it has an ace that is currently being used as an 11. Standard BJ, however states that the dealer must stand on 17.

As for the player, there are much publicized "cards" that dictate basic strategy from the most mathematcially beneficial approach. Considering some of the morons that I have sat with, this is apparently not as well known as it needs to be. That surprises me since many casinos actually give away credit card sized versions of them for free.

The card is broken up into 3 sections. The first one is simply a matrix of "what do I have?" vs. "what is the dealer''s up card?". e.g. I have 12, the dealer is showing a 7.

The second one (which I call the ace combos) is a variation of this where your hand includes an ace. e.g. A-6 vs. the dealer''s 3.

The third one is for determining whether or not you should split like cards into 2 hands. For example 8-8 vs. the dealer''s 5.

Rather than memorizing every single box on each of the 3 matricies, you can narrow it down to some quick mental rules. First off, the rule that you use most often involves the concept of a "breaker" or a hand that has the potential to go over 21. These obviously include hands from 12-16 (you would rarely hit on >=17). The difference between you, the player, having a breaker and the dealer having one is that YOU have a choice as to what to do with it - the dealer does not. Since ~30% of the cards are 10s, that means with a breaker hand, you have at least a 30% chance of breaking on the first card. If it is small, say adding a 3 to a 12, the dealer has to take ANOTHER one that could put him over. Of course, the 30% rule also applies to the dealer''s "down" card.

For purposes of, you can usually assume that he has a 10 down. Therefore, if you see a 2-6 showing for the dealer, and you assume that he has a 10 (or 9-6 would be just as bad depending on the up card). That means the dealer has a "breaker" hand... he is likely between 12-16 and could break quite easily. When that is the case, you don''t want to take risks. The result is that, in the situation, if you have a "breaker" vs. a "breaker", you stand and let the dealer break. This confuses many people who can''t understand how they can possibly win by standing on a 13.

Of course, if the dealer is showing a 7 or higher, we call it a "winner". Again, we are assuming he has a 10 down and therefore has a 17 or more. He very well could end up standing on a winning hand there and that means you need to try to beat it. When the dealer has a "winner" showing, very often you are going to play by the dealer''s rule and hit until you have 17 or more yourself.

Those 2 rules - breaker vs. breaker and winner vs. breaker form the majority of the hands you would ever play.

Other rules that you should memorize:

Never mess with your 19s or 20s.
Double on 10s and 11s unless the dealer is showing an A
Double on 9 when dealer has a breaker
Also Double the following:
A-2 vs a 6
A-3 & A-4 vs a 5,6
A-5 vs. 4,5,6
A-6 vs. 3,4,5,6
A-7 vs. 2,3,4,5,6

Always split 8s and As
Never split fours, fives and faces (10s)
The other split rules are confusing so I would recommend you to one of those charts.

If you want to fuzzy the AI up a bit, you can put some randomness on the borderline plays as to whether the player would do it or not. For a good touch of realism, you can also create profiles of play based on a lot of the moronic players that are out there.

Anyway, if you play by the chart, you usually only lose about 1-2%. That, of course, is different from slots where you also lose 2% if you play long enough. The constant losses, however, are offset by the rare, large payouts. This is not the case in blackjack. You can sit at a $5 BJ table with $100 and play for hours and walk away with $95 at the end of it (or more). Now, if you add card counting and bet variation on a 6 deck shoe with a 1 1/2 deck cut, you can MAKE money rather than lose it. (like me!) Also, much to the contrary of what is believed, card counting is NOT illegal. However, casinos, like other businesses, can refuse service to you... so it''s not a good idea to count with your fingers or out loud!


Dave Mark - President and Lead Designer
Intrinsic Algorithm -
"Reducing the world to mathematical equations!"
RIPPL Sports - NFL Statistical Analysis and Prediction System
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit<
krez
krez
quote:
Original post by InnocuousFox
Also, much to the contrary of what is believed, card counting is NOT illegal. However, casinos, like other businesses, can refuse service to you... so it''s not a good idea to count with your fingers or out loud!

i saw this documentary once about casino cheaters, and this guy built a device that he could enter cards into by tapping his foot (in code i guess). then, it''d tap his toe to tell him whether he should hit or stand. the casino guard guys noticed his was tapping his foot in certain patterns based on the cards showing, and caught him, and took his money. so it must be illegal to count cards using anything other than your head
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
IADaveMark
IADaveMark
You are not allowed to use any device, that''s true.

Dave Mark - President and Lead Designer
Intrinsic Algorithm -
"Reducing the world to mathematical equations!"
RIPPL Sports - NFL Statistical Analysis and Prediction System
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit<

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.