%% Blackjack Code
%Initial assignments of variables
computerhand = 0;
userhand = 0;
cardvalue = 0;
compcards = [];
i = 1;
k = 1;
choice = 2;
%Loads the card deck
load CardDeck;
%General introduction to the game complete with a small set of rules that
%the user needs to actually play the game and not lose 🙂
fprintf(“\nWelcome to Blackjack!”)
fprintf(“\nThe point of this game is to get as close to possible as 21 without going over!”)
fprintf(“\nYou will be competing with the computer, so best of luck!”)
fprintf(“\nIn this casino, Aces are 11”)
%Makes two lines of blank text because it looks better
for y = 1:2
fprintf(“\n”)
end
%Gets length of cards
length1 = linspace(1,52,52);
%Gets first random card
pos = randi(length(length1));
%Will keep loop running until the computervalue is greater than 14
while (computerhand < 17)
%Gets random card
pos = randi(length(length1));
if (pos == 1 || pos == 2 || pos == 3 || pos == 4)
%Updates computer value, updates the loop, and adds the card into
%the matrix to display at the end
cardvalue = 11;
imshow([BlueDeck{pos}]);
computerhand = computerhand + cardvalue;
compcards(i) = pos;
i = i + 1;
elseif (pos == 5 || pos == 6 || pos == 7 || pos == 8)
%Updates computer value, updates the loop, and adds the card into
%the matrix to display at the end
cardvalue = 2;
imshow([BlueDeck{pos}]);
computerhand = computerhand + cardvalue;
compcards(i) = pos;
i = i + 1;
elseif (pos == 9 || pos == 10 || pos == 11 || pos == 12)
%Updates computer value, updates the loop, and adds the card into
%the matrix to display at the end
cardvalue = 3;
imshow([BlueDeck{pos}]);
computerhand = computerhand + cardvalue;
compcards(i) = pos;
i = i + 1;
elseif (pos == 13 || pos == 14 || pos == 15 || pos == 16)
%Updates computer value, updates the loop, and adds the card into
%the matrix to display at the end
cardvalue = 4;
imshow([BlueDeck{pos}]);
computerhand = computerhand + cardvalue;
compcards(i) = pos;
i = i + 1;
elseif (pos == 17 || pos == 18 || pos == 19 || pos == 20)
%Updates computer value, updates the loop, and adds the card into
%the matrix to display at the end
cardvalue = 5;
imshow([BlueDeck{pos}]);
computerhand = computerhand + cardvalue;
compcards(i) = pos;
i = i + 1;
elseif (pos == 21 || pos == 22 || pos == 23 || pos == 24)
%Updates computer value, updates the loop, and adds the card into
%the matrix to display at the end
cardvalue = 6;
imshow([BlueDeck{pos}]);
computerhand = computerhand + cardvalue;
compcards(i) = pos;
i = i + 1;
elseif (pos == 25 || pos == 26 || pos == 27 || pos == 28)
%Updates computer value, updates the loop, and adds the card into
%the matrix to display at the end
cardvalue = 7;
imshow([BlueDeck{pos}]);
computerhand = computerhand + cardvalue;
compcards(i) = pos;
i = i + 1;
elseif (pos == 29 || pos == 30 || pos == 31 || pos == 32)
%Updates computer value, updates the loop, and adds the card into
%the matrix to display at the end
cardvalue = 8;
imshow([BlueDeck{pos}]);
computerhand = computerhand + cardvalue;
compcards(i) = pos;
i = i + 1;
elseif (pos == 33 || pos == 34 || pos == 35 || pos == 36)
%Updates computer value, updates the loop, and adds the card into
%the matrix to display at the end
cardvalue = 9;
imshow([BlueDeck{pos}]);
computerhand = computerhand + cardvalue;
compcards(i) = pos;
i = i + 1;
elseif (pos == 37 || pos == 38 || pos == 39 || pos == 40)
%Updates computer value, updates the loop, and adds the card into
%the matrix to display at the end
cardvalue = 10;
imshow([BlueDeck{pos}]);
computerhand = computerhand + cardvalue;
compcards(i) = pos;
i = i + 1;
elseif (pos == 41 || pos == 42 || pos == 43 || pos == 44)
%Updates computer value, updates the loop, and adds the card into
%the matrix to display at the end
cardvalue = 10;
imshow([BlueDeck{pos}]);
computerhand = computerhand + cardvalue;
compcards(i) = pos;
i = i + 1;
elseif (pos == 45 || pos == 46 || pos == 47 || pos == 48)
%Updates computer value, updates the loop, and adds the card into
%the matrix to display at the end
cardvalue = 10;
imshow([BlueDeck{pos}]);
computerhand = computerhand + cardvalue;
compcards(i) = pos;
i = i + 1;
elseif (pos == 49 || pos == 50 || pos == 51 || pos == 52)
%Updates computer value, updates the loop, and adds the card into
%the matrix to display at the end
cardvalue = 10;
imshow([BlueDeck{pos}]);
computerhand = computerhand + cardvalue;
compcards(i) = pos;
i = i + 1;
end
end
for j = 0:1
pos = randi(length(length1));
if (pos == 1 || pos == 2 || pos == 3 || pos == 4)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 11;
userhand = userhand + cardvalue;
usercards(k) = pos;
k = 2;
elseif (pos == 5 || pos == 6 || pos == 7 || pos == 8)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 2;
userhand = userhand + cardvalue;
usercards(k) = pos;
k = k + 1;
elseif (pos == 9 || pos == 10 || pos == 11 || pos == 12)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 3;
userhand = userhand + cardvalue;
usercards(k) = pos;
k = k + 1;
elseif (pos == 13 || pos == 14 || pos == 15 || pos == 16)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 4;
userhand = userhand + cardvalue;
usercards(k) = pos;
k = k + 1;
elseif (pos == 17 || pos == 18 || pos == 19 || pos == 20)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 5;
userhand = userhand + cardvalue;
usercards(k) = pos;
k = k + 1;
elseif (pos == 21 || pos == 22 || pos == 23 || pos == 24)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 6;
userhand = userhand + cardvalue;
usercards(k) = pos;
k = k + 1;
elseif (pos == 25 || pos == 26 || pos == 27 || pos == 28)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 7;
userhand = userhand + cardvalue;
usercards(k) = pos;
k = k + 1;
elseif (pos == 29 || pos == 30 || pos == 31 || pos == 32)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 8;
userhand = userhand + cardvalue;
usercards(k) = pos;
k = k + 1;
elseif (pos == 33 || pos == 34 || pos == 35 || pos == 36)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 9;
userhand = userhand + cardvalue;
usercards(k) = pos;
k = k + 1;
elseif (pos == 37 || pos == 38 || pos == 39 || pos == 40)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 10;
userhand = userhand + cardvalue;
usercards(k) = pos;
k = k + 1;
elseif (pos == 41 || pos == 42 || pos == 43 || pos == 44)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 10;
userhand = userhand + cardvalue;
usercards(k) = pos;
k = k + 1;
elseif (pos == 45 || pos == 46 || pos == 47 || pos == 48)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 10;
userhand = userhand + cardvalue;
usercards(k) = pos;
k = k + 1;
elseif (pos == 49 || pos == 50 || pos == 51 || pos == 52)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 10;
userhand = userhand + cardvalue;
usercards(k) = pos;
k = k + 1;
end
end
figure(‘Name’,’Initial Computer Cards with last hidden’,’Position’, [000, 800, 700, 700])
hidden = compcards(1);
imshow([BlueDeck{hidden}]);
%Displays the user’s initial point value
fprintf(“\nYour value is currently %i points”, userhand);
if (userhand >= 21)
choice = 1;
end
while (choice == 2)
%Asks if the user wants to hit or stay
check = input(“\nWould you like to hit or stay?(An empty line will count as stay): “,’s’);
pos = randi(length(length1));
if (check == “Stay” || check == “stay”)
%Exits the card drawing portion if the user selects stay
choice = 1;
elseif (check == “”)
%Exits the card drawing portion if the user inputs an empty line
choice = 1;
elseif (check == “Hit” || check == “hit”)
%Draws another card if the user
if (pos == 1 || pos == 2 || pos == 3 || pos == 4)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 11;
userhand = userhand + cardvalue;
usercards(k) = pos;
fprintf(“You drew an ace!”);
k = k + 1;
elseif (pos == 5 || pos == 6 || pos == 7 || pos == 8)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 2;
userhand = userhand + cardvalue;
usercards(k) = pos;
fprintf(“You drew a two!”);
k = k + 1;
elseif (pos == 9 || pos == 10 || pos == 11 || pos == 12)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 3;
userhand = userhand + cardvalue;
usercards(k) = pos;
fprintf(“You drew a three!”);
k = k + 1;
elseif (pos == 13 || pos == 14 || pos == 15 || pos == 16)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 4;
userhand = userhand + cardvalue;
usercards(k) = pos;
fprintf(“You drew a four!”);
k = k + 1;
elseif (pos == 17 || pos == 18 || pos == 19 || pos == 20)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 5;
userhand = userhand + cardvalue;
usercards(k) = pos;
fprintf(“You drew a five!”)
k = k + 1;
elseif (pos == 21 || pos == 22 || pos == 23 || pos == 24)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 6;
userhand = userhand + cardvalue;
usercards(k) = pos;
fprintf(“You drew a six!”);
k = k + 1;
elseif (pos == 25 || pos == 26 || pos == 27 || pos == 28)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 7;
userhand = userhand + cardvalue;
usercards(k) = pos;
fprintf(“You drew a seven!”);
k = k + 1;
elseif (pos == 29 || pos == 30 || pos == 31 || pos == 32)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 8;
userhand = userhand + cardvalue;
fprintf(“You drew an eight!”);
k = k + 1;
elseif (pos == 33 || pos == 34 || pos == 35 || pos == 36)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 9;
userhand = userhand + cardvalue;
usercards(k) = pos;
fprintf(“You drew a nine!”);
k = k + 1;
elseif (pos == 37 || pos == 38 || pos == 39 || pos == 40)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 10;
userhand = userhand + cardvalue;
usercards(k) = pos;
fprintf(“You drew a ten!”);
k = k + 1;
elseif (pos == 41 || pos == 42 || pos == 43 || pos == 44)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 10;
userhand = userhand + cardvalue;
usercards(k) = pos;
fprintf(“You drew a Jack!”);
k = k + 1;
elseif (pos == 45 || pos == 46 || pos == 47 || pos == 48)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 10;
userhand = userhand + cardvalue;
usercards(k) = pos;
fprintf(“You drew a Queen!”);
k = k + 1;
elseif (pos == 49 || pos == 50 || pos == 51 || pos == 52)
%Updates user value, tells them what card they drew, updates
%the loop, and adds the card into the matrix to display at the
%end
cardvalue = 10;
userhand = userhand + cardvalue;
usercards(k) = pos;
fprintf(“You drew a King!”);
k = k + 1;
end
figure(‘Name’, ‘User Hand’, ‘Position’, [300,300,500,500]);
imshow([BlueDeck{usercards}]);
else
%Handles if the user inputs anything other than specifie input
fprintf(“ERROR! I can’t recognize that input try again! Don’t try to break the game!”)
end
fprintf(“\nYour point value is currently %i points”, userhand);
if (userhand >= 21)
%Handles if the user goes over 21 while “hit”‘ing
choice = 1;
end
end
%Final statement that shows the user and the computer their scores
fprintf(“\n”)
fprintf(“\nThe computer has %i points”,computerhand)
fprintf(“\nThe user has %i points”,userhand)
if (userhand > 21)
%Win condition for if the user goes over 21 points
fprintf(“\nOver 21! You lose :(“)
elseif (userhand == 21 && computerhand ~= 21)
%Win condition for if the user gets 21 points
fprintf(“\nYou win!”);
elseif (userhand > computerhand && userhand <= 21)
%Win condition for if the user has more points than the
%computer
fprintf(“\nYou win!”);
elseif (computerhand > userhand && computerhand <= 21)
%Win condition for if the computer has more points than the
%user
fprintf(“\nComputer wins! Sorry :(“);
%Handles the tie condition
elseif (computerhand == userhand)
fprintf(“\nTie! Nobody wins :(“)
elseif (computerhand > userhand && computerhand > 21)
%Win condition for if the computer goes over 21 and the user
%doesn’t
fprintf(“\nComputer went over 21! User wins!”)
end
%Displays the user’s cards
figure(‘Name’, ‘User Hand’, ‘Position’, [000,300,700,700]);
imshow([BlueDeck{usercards}]);
%Displays the computer’s cards
figure(‘Name’, ‘Computer Hand’, ‘Position’, [000,000,700,700]);
imshow([BlueDeck{compcards}]);