### Tags: 5-deck, blackjack, cant, cardsmy, figure, gameso, matlab, program, programming, simulate, simulation, withzrandperm

# BLACKJACK SIMULATION

On Programmer » Matlab

6,514 words with 3 Comments; publish: Sat, 26 Apr 2008 21:36:00 GMT; (20093.75, « »)

i want to simulate a 5-deck blackjack game

so i've started with

Z=randperm(260); FOR 260 CARDS

MY PROBLEM IS I CANT FIGURE OUT A PROGRAM THAT WILL RANDOMLY SELECT A

NUMBER OF Z AS A CARD FOR EITHER THE DEALER OR PLAYER, AND THEN ERASE

THAT ENTRY FROM Z.

WHEN Z IS DOWN TO 40 NUMBERS LEFT, I WANT THE PROGRAM TO END, SIMILAR

TO "RE-SHUFFLING"

PLEASE HELP

*http://matlab.todaysummary.com/q_matlab_5242.html*

All Comments

Leave a comment...

- 3 Comments
- In article <ef5509f.-1.matlab.todaysummary.com.webcrossing.raydaftYaTP>,
Seth Thomas <jay_bird_thomas.matlab.todaysummary.com.hotmail.com> wrote:

>i want to simulate a 5-deck blackjack game

>so i've started with

>Z=randperm(260); FOR 260 CARDS

You probably don't need to keep track of which deck each card

came from, so I would suggest

Z = 1 + (randperm(260) - 1) % 52;

>MY PROBLEM IS I CANT FIGURE OUT A PROGRAM THAT WILL RANDOMLY SELECT A

>NUMBER OF Z AS A CARD FOR EITHER THE DEALER OR PLAYER, AND THEN ERASE

>THAT ENTRY FROM Z.

You've already shuffled the cards by doing the randperm, so you do

not want to randomly select from Z: you just want to take the next

entry of Z.

>WHEN Z IS DOWN TO 40 NUMBERS LEFT, I WANT THE PROGRAM TO END, SIMILAR

>TO "RE-SHUFFLING"

What if you are in the middle of a deal when length(Z) reaches 40?

--

If you lie to the compiler, it will get its revenge. -- Henry Spencer

#1; Sat, 26 Apr 2008 21:37:00 GMT

- In article <ef5509f.-1.matlab.todaysummary.com.webcrossing.raydaftYaTP>,
- THANK YOU FOR YOUR QUICK REPLY. HOWEVER IM STILL STUCK IN THE SAME
SPOT. I'VE ASSIGNED VALUES 1-20 TO ACE, 21-40 KK, ECT.

HOWEVER, I STILL DON'T GET HOW YOU SET UP A LOOP OR SOMETHING TO PICK

THE FIRST VALUE, THEN SECOND, THEN THIRD.

I NEED TO GET AWAY FROM THE 1x260 MATRIX AND PICK INDIVIDUALS VALUES,

AND I DO SEE THE PROBLEM WITH RE-SHUFFLING.

>

> You probably don't need to keep track of which deck each card

> came from, so I would suggest

> Z = 1 + (randperm(260) - 1) % 52;

>

> SELECT A

> ERASE

> You've already shuffled the cards by doing the randperm, so you do

> not want to randomly select from Z: you just want to take the next

> entry of Z.

>

> SIMILAR

> What if you are in the middle of a deal when length(Z) reaches 40?

> --

> If you lie to the compiler, it will get its revenge. -- Henry

> Spencer

>

#2; Sat, 26 Apr 2008 21:38:00 GMT

- THANK YOU FOR YOUR QUICK REPLY. HOWEVER IM STILL STUCK IN THE SAME
- In article <ef5509f.1.matlab.todaysummary.com.webcrossing.raydaftYaTP>,
Seth Thomas <jay_bird_thomas.matlab.todaysummary.com.hotmail.com> wrote:

>THANK YOU FOR YOUR QUICK REPLY. HOWEVER IM STILL STUCK IN THE SAME

>SPOT. I'VE ASSIGNED VALUES 1-20 TO ACE, 21-40 KK, ECT.

>HOWEVER, I STILL DON'T GET HOW YOU SET UP A LOOP OR SOMETHING TO PICK

>THE FIRST VALUE, THEN SECOND, THEN THIRD.

>I NEED TO GET AWAY FROM THE 1x260 MATRIX AND PICK INDIVIDUALS VALUES,

>AND I DO SEE THE PROBLEM WITH RE-SHUFFLING.

As indicated earlier, your randperm has already shuffled the deck.

You don't need to extract individual values from the permutation:

just keep track of how far you got.

You will need to adjust the following to take into account the

dual behaviour of aces. You can detect an ace in the below by

checking that cardscores(cardnumber) = 11

numdecks = 5;

cardfaces = repmat({'A', '2', '3', '4', '5', '6', '7', '8', '9', ...

'10', 'J', 'Q', 'K'}, 1, numdecks);

cardscores = repmat([11, 2:10, 10, 10, 10], 1, 4*numdecks);

cardsuits = repmat([repmat('H', 1, 13), repmat('D', 1, 13), ...

repmat('C', 1, 13), repmat('S', 1, 13)], 1, numdecks);

cardnames = [char(cardfaces),cardsuits'];

%adjust the player strategy according to your simulation needs

pdealsagain = [repmat(true,1,17),false(1,3)];

%blackjack rules: dealer has fixed strategy, must keep going at 16 or less

ddealsagain = [repmat(true,1,16),false(1,4)];

shuffleddeck = randperm(52 * numdecks);

usedcards = 0;

while usedcards < 52 * numdecks - 40

%player dealt, then dealer, then player, then dealer

pcards = suffleddeck(usedcards+[1,3]);

dcards = suffleddeck(usedcards+[2,4]);

usedcards = usedcards + 4;

pscore = sum(cardscores(pcards)); % fix this for dual ace handling

dscore = sum(cardscores(dcards)); % fix this for dual ace handling

%player then plays continuously until holding or busting

while pscore < 21 && length(pcards) < 5 && pdealsagain(pscore)

usedcards = usedcards + 1;

pcards = [pcards, suffleddeck(usedcards)];

pscore = sum(cardscores(pcards)); % fix this for dual ace handling

end

%if the player did not bust, the dealer plays continuously until

%holding or busting

while pscore <= 21 && dscore < 21 && ddealsagain(dscore)

usedcards = usedcards + 1;

dcards = [dcards, suffleddeck(usedcards)];

dscore = sum(cardscores(dcards)); % fix this for dual ace handling

end

%there is a little bit of trickery to wrap the card names into

%single strings. This is only the first half of the trick.

pcn = cardnames(pcards)';

dcn = cardnames(dcards)';

%now decide who won

if pscore <= 21 && pscore > dscore

%player wins

disp(['player wins. player: ', pcn(:)', ' dealer: ', dcn(:)']);

elseif pscore <= 21 && pscore == dscore

%tie

disp(['tie. player: ', pcn(:)', ' dealer: ', dcn(:)']);

else

%player loses

disp(['dealer wins. player: ', pcn(:)', ' dealer: ', dcn(:)']);

end

end

Modifying this to take into account blackjack is left as an exercise.

If the player has blackjack, then the dealer does not receive

any more cards -- but if the dealer has blackjack too then it is

still a tie.

Modifying the code to reflect bets and payouts (blackjack pays higher

than simply reaching 21) is left as an exercise.

--

Is there any thing whereof it may be said, See, this is new? It hath

been already of old time, which was before us. -- Ecclesiastes

#3; Sat, 26 Apr 2008 21:39:00 GMT

- In article <ef5509f.1.matlab.todaysummary.com.webcrossing.raydaftYaTP>,