Blackjack how to define cards as their value

1 view (last 30 days)
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

Image Analyst
Image Analyst on 19 Apr 2015
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
Image Analyst
Image Analyst on 22 Apr 2015
Edited: Image Analyst on 22 Apr 2015
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
Ryan B
Ryan B on 22 Apr 2015
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 Signal Integrity Kits for Industry Standards in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!