Blackjack how to define cards as their value

Hey everybody, I am trying to write a very simple blackjack code where the player either hits 21 and wins, or busts and loses. So far i have this,
function card = dealcard;
%This program will allow someone to draw a card at random from a full deck
and see what that card would be. you can go through the whole deck
persistent dec; %persistent makes it so matlab stores this variable
if length(dec)<53
dec=[1:52]; %one deck of cards
dec=dec(randperm(length(dec))); %this shuffles the cards
end;
card=dec(end);
I do not know how to define the cards as being an ace, 2,3,...queen, and king. If anyone could help me with this that would be great. Thank you.

 Accepted Answer

You don't need to make it persistent. Just pass the dec in. And to keep track of both the card number, and the card's value, you'll need a 2 D array.
cardNumbers = [1:52];
cardValues = [1:13, 1:13, 1:13, 1:13];
dec = [cardNumbers', cardValues']
% Now shuffle
shufflingOrder = randperm(size(dec, 1));
dec = dec(shufflingOrder, :)
% Prepare your output
cardNumber = dec(end, 1);
cardValue = dec(end, 2);

9 Comments

So keep most of what I have except for the persistent? Then add your code after it? thanks
I would do this:
function [cardNumber , cardValue, dec] = dealcard(dec)
cardNumbers = [1:52];
cardValues = [1:13, 1:13, 1:13, 1:13];
dec = [cardNumbers', cardValues']
% Now shuffle
shufflingOrder = randperm(size(dec, 1));
dec = dec(shufflingOrder, :)
% Prepare your output
cardNumber = dec(end, 1);
cardValue = dec(end, 2);
% Remove that card from the dec so next time it's not there
dec = dec(end-1,:);
Of course I would make it better by using try catch, and things like that, but this is a good start. Be sure to make the first two lines be able to handle an incoming "dec" that has less than 52 cards in it (because some were dealt/removed). Actually I'd probably use an array of structures (to make it easier to remove cards from the dec and send it back in), but I was just trying to stick as close as possible to the simplistic way you were doing it.
Thank you very much for your help so far. I was wondering if you could help me out further by pointing me in the right direction of how to define the cards so there are 4 2's, 4 3's,.....4 9's, 16 10's (grouping the 10's, jacks, queens, and kings together). Or what the next step would be in making this code. Sorry im so bad at matlab. Thanks for your time
After you assign cardValues, just set everything greater than 10 equal to 10. So add this line:
cardValues(cardValues>10) = 10
Alternatively you could do
cardValues = min(cardValues, 10);
after putting that in it is still giving me 47 and other large numbers. this is where i put it...
function [cardNumber , cardValue, dec] = Dealcards(dec)
cardNumbers = [1:52];
cardValues = [1:13, 1:13, 1:13, 1:13];
cardValues(cardValues>10) = 10;
dec = [cardNumbers', cardValues'];
%now shuffle
shufflingOrder = randperm(size(dec, 1));
dec = dec(shufflingOrder, :);
%prepare output
cardNumber = dec(end,1);
cardValue = dec(end,2);
%now removing card from deck
dec = dec(end-1,:);
The cardValue can/will never be greater than 10. Obviously the cardNumber might be greater than 10 - it can go from 1-52 because there are 52 cards. What exactly is giving you 47 that you think should not?
Well i was trying to get it so it would come out with an answer of 2-11 simulating two through ace. I was hoping that i could get 4 of the cards to show an answer of 2, answer of 3 ect. instead of having 1 2, 1 3, 1 4... ect. I hope im making sense. Thanks
If you want, just manually set up card values giving all 52 values. Then you can assign any value to any card number.
cardValues = [...
11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10,...
11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10,...
11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10,...
11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10];
(Assumes Aces are card numbers 1, 14, 27, and 40. Or set up whatever values you want for each of the 52 elements
Oh wow. I cant believe i didnt think of this. Thank you for all your help!

Sign in to comment.

More Answers (0)

Categories

Find more on Card games in Help Center and File Exchange

Tags

Asked:

on 19 Apr 2015

Commented:

on 22 Apr 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!