%%
%Black Jack the card game in Matlab code
clc,clear,format compact%clear the command window for a fresh game
h = figure;
hp = uipanel(‘Title’, ‘BLACKJACK’, ‘BackgroundColor’, ‘blue’,’Position’, [0 0 1 1]);
StartGameButton = uicontrol(‘style’, ‘togglebutton’);
set(StartGameButton, ‘position’, [50 50, 120, 20]);
set(StartGameButton, ‘string’, ‘Start Game’);
set(StartGameButton, ‘callback’, {@Game});
function Game(~,~)
%global y
load(‘CardDeck.mat’)%load the CardDeck.mat origional program for all cards
ShuffledDeck = randperm(52);%Shuffle the deck
hand = ShuffledDeck(1:2);
CpuD = ShuffledDeck(30:31);%deal out two cards to the dealer
Sum = 0;
y = ‘Y’;
x = 1;
figure(3)
imshow([BlueDeck{ShuffledDeck(30)},BlueDeck{55}])
fprintf(‘To buy into the game costs $50, and this will be used in your bets’)
m = 50;
b = input(‘\nPlace your bet ‘,’s’);
if hand(1) <=4
v = 11;
elseif hand(1) <=8
v = 2;
elseif hand(1)<=12
v = 3;
elseif hand(1)<=16
v = 4;
elseif hand(1)<=20
v = 5;
elseif hand(1)<=24
v = 6;
elseif hand(1)<=28
v = 7;
elseif hand(1)<=32
v = 8;
elseif hand(1)<=36
v = 9;
else
v = 10;
end
Sum = Sum + v;
while y == ‘Y’ && Sum <= 10
x = x+1;
hand = ShuffledDeck(1:x);
figure(2)
imshow([BlueDeck{hand}])
if hand(x) <=4
p = 11;
elseif hand(x) <=8
p = 2;
elseif hand(x)<=12
p = 3;
elseif hand(x)<=16
p = 4;
elseif hand(x)<=20
p = 5;
elseif hand(x)<=24
p = 6;
elseif hand(x)<=28
p = 7;
elseif hand(x)<=32
p = 8;
elseif hand(x)<=36
p = 9;
else
p = 10;
end
Sum = Sum + p
y = input(‘Would you like to Hit? Y/N ‘,’s’)
end
while (Sum <= 21) && (y ==’Y’)
x = x+1;
hand = ShuffledDeck(1:x);
figure(2)
imshow([BlueDeck{hand}])
if hand(x) <=4
p = 1;
elseif hand(x) <=8
p = 2;
elseif hand(x)<=12
p = 3;
elseif hand(x)<=16
p = 4;
elseif hand(x)<=20
p = 5;
elseif hand(x)<=24
p = 6;
elseif hand(x)<=28
p = 7;
elseif hand(x)<=32
p = 8;
elseif hand(x)<=36
p = 9;
else
p = 10;
end
Sum = Sum + p
if Sum <= 21
y = input(‘Would you like to Hit? Y/N ‘,’s’)
else
fprintf(‘\nYou have Busted’)
m = m – b;
fprintf(‘\nYou now have %d in your acount’,m)
end
end
SumD = 0;
d = 30;
z = 1;
if CpuD(1) <=4
k = 11;
elseif CpuD(1) <=8
k = 2;
elseif CpuD(1)<=12
k = 3;
elseif CpuD(1)<=16
k = 4;
elseif CpuD(1)<=20
k = 5;
elseif CpuD(1)<=24
k = 6;
elseif CpuD(1)<=28
k = 7;
elseif CpuD(1)<=32
k = 8;
elseif CpuD(1)<=36
k = 9;
else
k = 10;
end
SumD = SumD + k;
if Sum <= 21 && y == ‘N’
fprintf(‘\nNow its the Dealers Turn’)
fprintf(‘\n’)
pause(2)
while (SumD <=10) && (SumD <16)
d = d+1;
z = z+1;
CpuD = ShuffledDeck(30:d);
figure(3)
imshow([BlueDeck{CpuD}])
pause(.5)
if CpuD(z) <=4
c = 11;
elseif CpuD(z) <=8
c = 2;
elseif CpuD(z)<=12
c = 3;
elseif CpuD(z)<=16
c = 4;
elseif CpuD(z)<=20
c = 5;
elseif CpuD(z)<=24
c = 6;
elseif CpuD(z)<=28
c = 7;
elseif CpuD(z)<=32
c = 8;
elseif CpuD(z)<=36
c = 9;
else
c = 10;
end
SumD = SumD + c
pause(.5)
while (SumD <= 21) && (SumD < 16)
d = d+1;
z = z+1;
CpuD = ShuffledDeck(30:d);
figure(3)
imshow([BlueDeck{CpuD}])
pause(1)
if CpuD(z) <=4
c = 1;
elseif CpuD(z) <=8
c = 2;
elseif CpuD(z)<=12
c = 3;
elseif CpuD(z)<=16
c = 4;
elseif CpuD(z)<=20
c = 5;
elseif CpuD(z)<=24
c = 6;
elseif CpuD(z)<=28
c = 7;
elseif CpuD(z)<=32
c = 8;
elseif CpuD(z)<=36
c = 9;
else
c = 10;
end
SumD = SumD + c
end
pause(1)
if (SumD <= 21) && (SumD > Sum)
fprintf(‘\nThe Dealer has won, You Lose’)
m = m – b;
fprintf(‘\nYou now have %d left if your account’,m)
fprintf(‘\n’)
elseif SumD == Sum
fprintf(‘\nYou and the dealer have tied’)
fprintf(‘\nThe amount in your account is %d’,m)
fprintf(‘\n’)
else
fprintf(‘\nYou Have Won!’)
m = m + b;
fprintf(‘\nYou now have %d in your account’,m)
fprintf(‘\n’)
end
end
pause(1)
else
fprintf(‘\n’)
fprintf(‘\nYou Lose, The Dealer Wins’)
m = m – b
fprintf(‘\nYou have %d left in your acount’,m)
fprintf(‘\n’)
end
end
%function Hit(~,~)
%HitGameButton = uicontrol(‘style’, ‘togglebutton’);
% set(HitGameButton, ‘position’, [30 30, 120, 20]);
% set(HitGameButton, ‘string’, ‘Hit’);
%set(HitGameButton, ‘callback’, {@Hit});
%global y
%y = ‘Y’
%end