E. Final Program with Comments

%%Blackjack Game
clear
clc
%% Creating Deck of Cards
bet = 0;
finalMoney = 0;
deck = [];
wins = 0;
losses = 0;
playGame = input(‘Do you want to play Blackjack? 1 for yes, 0 for no: ‘); %asking to play
while playGame == 1 %starting playing loop
for k=1:52 %creating deck and assigning array values
if k<=4
deck(k)=1;
elseif k<=8
deck(k)=2;
elseif k<=12
deck(k)=3;
elseif k<=16
deck(k)=4;
elseif k<=20
deck(k)=5;
elseif k<=24
deck(k)=6;
elseif k<=28
deck(k)=7;
elseif k<=32
deck(k)=8;
elseif k<=36
deck(k)=9;
else k>36&&k<=52;
deck(k)=10;
end
end
%% Shuffle, Deal Cards, Initial Hand Value
deck = deck(randperm(length(deck))); %shuffling deck
deck;
bet = input(‘\nHow much would you like to bet? ‘); %asking for bet

playerHand = deck(1:2);
fprintf (‘\nYou have been dealt: %d & %d’, playerHand) %printing player hand
pause(1)
dealerHand1 = deck(3:4);
fprintf (‘\n The dealer has been dealt: %d & hole card ‘,dealerHand1(1)) %printing dealer hand
pause(1)
DealersHand= deck(3) + deck(4);
if (deck(3)==1) && (DealersHand <= 10) %determining 1 or 11 ace value for dealer with one ace
deck(3)=11;
elseif (deck(3)==1) && (DealersHand > 10)
deck(3)=1;
end
if (deck(4)==1) && (DealersHand <= 10) %determining 1 or 11 ace value for dealer with one ace
deck(4)=11;
elseif (deck(4)==1) && (DealersHand > 10)
deck(4)=1;
end
if (deck(3)==1) && (deck(4)==1) && (DealersHand <= 10) %determining 1 or 11 ace value for dealer with two aces
deck(3)=11;
elseif (deck(3)==1) && (deck(4)==1) && (DealersHand > 10)
deck(3)=1;
end
dealerBlackjack=0;
if deck(3)==1 && deck(4)==10 %checking for dealer blackjack
dealerBlackjack=1;
end
if deck(4)==1 && deck(3)==10 %checking for dealer blackjack
dealerBlackjack=1;
end

 

%% Player Hand Value
blackjack=0;
if deck(1)==1 && deck(2)==10 %checking for player blackjack
fprintf(‘\n The value of your hand is 21’)
fprintf(‘\n Congrats, you have a blackjack!’)
blackjack=1;
wins = wins + 1; %adding a player win
bet = bet+(1.5*bet); %giving the player money for winning
fprintf(‘\n You have won $%d’,bet); %printing how much money the player has won
end
if deck(2)==1 && deck(1)==10 %checking for player blackjack
fprintf(‘\n The value of your hand is 21’)
fprintf(‘\n Congrats, you have a blackjack!’)
blackjack=1;
wins = wins + 1;
bet = bet+(1.5*bet);
fprintf(‘\n You have won $%d’,bet);
end
if deck(1)==1 && (blackjack==0) %checking for an ace to ask for value (when player does not have a blackjack)
aceValue = input(‘\n Ace value equals 1 or 11? ‘); %asking for ace value
if aceValue==1
playerHandValue = 1 + deck(2);
fprintf(‘\n Your new hand value is %d’, playerHandValue) %printing new hand value
elseif aceValue==11
deck(1) = aceValue;
playerHandValue = 11 + deck(2);
fprintf(‘\n Your new hand value is %d’, playerHandValue)
end
end
if deck(2)==1 && (blackjack==0) %checking for an ace to ask for value (when player does not have a blackjack)
aceValue = input(‘\n Ace value equals 1 or 11? ‘);
if aceValue==1
playerHandValue = 1 + deck(1);
%fprintf(‘\n Your new hand value is %d’, playerHandValue)
elseif aceValue==11
deck(2) = aceValue;
playerHandValue = 11 + deck(1);
%fprintf(‘\n Your new hand value is %d’, playerHandValue)
end
end
if blackjack==0
playerHandValue = deck(1)+ deck(2);
fprintf(‘\n The value of your hand is %d’, playerHandValue) %printing hand value
end
if playerHandValue==9||playerHandValue==10 %checking values to double down
doubleBet = input(‘\n Double down? 1 for yes, 0 for no: ‘); %asking player to double down
if doubleBet==1
bet=2*bet;
end
end
if playerHandValue==11 && blackjack==0 %checking values to double down
doubleBet = input(‘\n Double down? 1 for yes, 0 for no: ‘); %asking player to double down
if doubleBet==1
bet=2*bet;
end
end
deck(1:4) = []; %deleting cards from the deck

%% Hit or Stay
if blackjack~=1
pause(1)
hitStay = input(‘\n Hit or Stay, press 1 for hit and 0 for stay: ‘); %asking the player to hit or stay
stillPlaying=1;
dealerPlaying=1;
userBust=1;
while stillPlaying==1

if hitStay==1 %goes through loop if player hits
pause(1)
fprintf(‘\n You have been dealt %d’, deck(1)); %prints dealt card value
if deck(1)==1 && (blackjack==0) %checking for an ace to ask for value (when player does not have a blackjack)
aceValue = input(‘\n Ace value equals 1 or 11? ‘); %asking for ace value
if aceValue==1;
deck(1) = aceValue;
else
deck(1) = aceValue;
end
end
playerHandValue = playerHandValue + deck(1); %adding dealt card to previous hand
pause(1)
fprintf(‘\n Your new hand value is %d’, playerHandValue) %printing new hand value
deck(1)=[];
if playerHandValue>= 22 %checks for hand values at or above 22
fprintf(‘\n BUST!!!!!!!’) %prints “BUST” if player’s value is at or above 22
hitStay=0;
userBust=0;
losses = losses + 1; %adding player loss
bet=-1*bet; %taking the player’s money for losing
fprintf(‘\n You have lost $%d’,bet); %printing how much money the player has lost
else
pause(1)
hitStay = input(‘\n Hit or Stay, press 1 for hit and 0 for stay: ‘); %asks the player to hit or stay
end

if (playerHandValue==21) && (dealerBlackjack==0)
%dealer does not have blackjack he loses
fprintf(‘\n The dealer does not have a Blackjack … you win!’)
pause(1)
stillPlaying=0;
wins = wins + 1;
bet = 2*bet;
fprintf(‘\n You have won $%d’,bet);
elseif (playerHandValue==21) && (dealerBlackjack==1)
%dealer has blackjack, you lose
fprintf(‘\n The dealer has a Blackjack … you lose!’)
pause(1)
stillPlaying=0;
losses = losses + 1;
bet=-1*bet;
fprintf(‘\n You have lost $%d’,bet);

end

elseif (hitStay==0) && (userBust~=0)
if dealerBlackjack==1 %checks for dealer blackjack
fprintf(‘\n The dealer has a blackjack… you lose!’) %prints player loss
pause(1)
losses = losses + 1;
bet=-1*bet;
fprintf(‘\n You have lost $%d’,bet);

else
if DealersHand>playerHandValue %checks if the dealer’s hand value is higher than the player’s
pause(1)
fprintf(‘\n The dealers hand value is %d … you lose!’, DealersHand) %prints player loss (dealer’s hand is higher)
pause(1)
dealerPlaying=0;
losses = losses + 1;
bet=-1*bet;
fprintf(‘\n You have lost $%d’,bet);
end
while DealersHand<=playerHandValue %checks if the dealer’s hand value is less than or equal to the player’s
pause(1)
fprintf(‘\n The dealer has been dealt %d’,deck(1)) %adds card to dealer’s hand
pause(1)
if (deck(1)==1) && (DealersHand + deck(1) <= 10) %checks for an ace to assign an 11 (if hand value is less than or equal to 10)
deck(1)=11;
elseif (deck(1)==1) && (DealersHand + deck(1) > 10) %checks for an ace to assign a 1 (if hand value is greater than 10)
deck(1)=1;
end
DealersHand = DealersHand + deck(1); %adds dealt card to dealer’s initial hand
deck(1)=[]; %deletes dealt card from deck
if (DealersHand>playerHandValue) && (DealersHand<=21) %checks if dealer’s hand is higher than player’s and less than or equal to 21
pause(1)
fprintf(‘\n The dealers hand value is %d … you lose!’, DealersHand) %prints player loss (dealer’s hand is higher)
dealerPlaying=0;
losses = losses + 1;
bet=-1*bet;
fprintf(‘\n You have lost $%d’,bet);
elseif DealersHand>21 %checks if dealer’s hand is higher 21
pause(1)
fprintf(‘\n The dealer busted … you win!’) %prints that the dealer has busted and player wins
pause(1)
dealerPlaying=0;
wins = wins + 1;
bet=2*bet;
fprintf(‘\n You have won $%d’,bet);
end
end

end
if ((hitStay==0)&&(dealerPlaying==0))||(userBust==0)
stillPlaying=0;
end
end
end
end
finalMoney=finalMoney+bet; %adds player money together
pause(1)
fprintf(‘\n You have %d win(s) and %d loss(es)’,wins,losses) %prints player total wins and losses
pause(1)
fprintf(‘\n You have earned a total of $%d’,finalMoney) %prints player’s total money
pause(1)
playGame = input(‘\n Do you want to play Blackjack? 1 for yes, 0 for no: ‘); %asks player if to play again
if playGame == 1 %if player chooses to play again deck reshuffles
deck = deck(randperm(length(deck)));
end
end