Sie sind auf Seite 1von 24

%********************************************************************************

**%
% Blackjack for Matlab
%
%********************************************************************************
**%

clear all
disp(' ');
disp(' %%%%%%%%%%%%%%%%%%%%%%%%');
disp(' % %');
disp(' % %');
disp(' % ______________ %');
disp(' % | | %');
disp(' % | | %');
disp(' % | | %');
disp(' % | Matlab | %');
disp(' % | | %');
disp(' % | 21 | %');
disp(' % | | %');
disp(' % | Game | %');
disp(' % | | %');
disp(' % | | %');
disp(' % |______________| %');
disp(' % %');
disp(' % %');
disp(' %%%%%%%%%%%%%%%%%%%%%%%%');
disp(' ');
disp('Welcome to Matlab 21');
disp(' ');
disp('You are in luck, the computer is not very smart today!');
disp(' ');
name = input('Player Name: ','s');
disp(' ');
disp('The house stands on 17 and hits on 16. Blackjack pays double!');
disp(' ');
disp('Player cannot split cards or double down.');
%
play_again = 'y';
start = 10;
quit_count = 0;
games_total = 1;
%
% Initialize counter and input variables
%
% Enable continuous play
%
while play_again =='y' | play_again == 'Y'
ace = 0;
card1 = 0;
card2 = 0;
card3 = 0;
card4 = 0;
card5 = 0;
card6 = 0;
card7 = 0;
card8 = 0;
card9 = 0;
card10 = 0;
insurance = 'n';
bj = 0;
disp(' ');
disp(' ');
disp(['You have $',num2str(start),' in play money to start with, ',name,'.']);
disp(' ');
bet = [];
disp('Welcome to the table!');
disp(' ');
disp(' ');
%
% Take initial bet
%
while isempty(bet)
bet = input('How much do you want to bet? (whole dollar amounts only!) $');
if bet == 0
disp(' ');
disp('You must bet something!');
disp(' ');
bet = [];
elseif mod(bet,1) ~=0
disp(' ');
disp('Whole dollar amounts only please!');
disp(' ');
bet = [];
elseif bet > start
disp(' ');
disp('Not enough money to cover that bet');
disp(' ');
bet = [];
else
if bet == start
disp('Wow, big spender!');
end
disp(['Bet: $',num2str(bet)]);
end
end
disp(' ');

card = {};
suit = {};
title = {};
x = 0;
%
% Initial Deal will be x = 1 to 4; 5 thru 7 are computer's next three cards; 8-
10 are player's next three cards.
% Generate card values and suits based on a random number between 0 and 1...
There is no 2 or 3 of diamonds for
% sake of ease (1/.02 = 50 cards)
%
for x = 1:10
card{x} = rand;
if card{x} < .0193
card{x} = 2;
suit{x} = 'spades';
title{x} = 'two';
elseif card{x} < .0386
card{x} = 3;
suit{x} = 'spades';
title{x} = 'three';
elseif card{x} < .0579
card{x} = 4;
suit{x} = 'spades';
title{x} = 'four';
elseif card{x} < .0772
card{x} = 5;
suit{x} = 'spades';
title{x} = 'five';
elseif card{x} < .0965
card{x} = 6;
suit{x} = 'spades';
title{x} = 'six';
elseif card{x} < .1158
card{x} = 7;
suit{x} = 'spades';
title{x} = 'seven';
elseif card{x} < .1351
card{x} = 8;
suit{x} = 'spades';
title{x} = 'eight';
elseif card{x} < .1544
card{x} = 9;
suit{x} = 'spades';
title{x} = 'nine';
elseif card{x} < .1737
card{x} = 10;
suit{x} = 'spades';
title{x} = 'Ten';
elseif card{x} < .1930
card{x} = 10;
suit{x} = 'spades';
title{x} = 'Jack';
elseif card{x} < .2123
card{x} = 10;
suit{x} = 'spades';
title{x} = 'Queen';
elseif card{x} < .2316
card{x} = 10;
suit{x} = 'spades';
title{x} = 'King';
elseif card{x} < .2509
card{x} = 11;
suit{x} = 'spades';
title{x} = 'Ace';
elseif card{x} < .2702
card{x} = 2;
suit{x} = 'hearts';
title{x} = 'two';
elseif card{x} < .2895
card{x} = 3;
suit{x} = 'hearts';
title{x} = 'three';
elseif card{x} < .3088
card{x} = 4;
suit{x} = 'hearts';
title{x} = 'four';
elseif card{x} < .3281
card{x} = 5;
suit{x} = 'hearts';
title{x} = 'five';
elseif card{x} < .3474
card{x} = 6;
suit{x} = 'hearts';
title{x} = 'six';
elseif card{x} < .3667
card{x} = 7;
suit{x} = 'hearts';
title{x} = 'seven';
elseif card{x} < .3860
card{x} = 8;
suit{x} = 'hearts';
title{x} = 'eight';
elseif card{x} < .4053
card{x} = 9;
suit{x} = 'hearts';
title{x} = 'nine';
elseif card{x} < .4246
card{x} = 10;
suit{x} = 'hearts';
title{x} = 'Ten';
elseif card{x} < .4439
card{x} = 10;
suit{x} = 'hearts';
title{x} = 'Jack';
elseif card{x} < .4632
card{x} = 10;
suit{x} = 'hearts';
title{x} = 'Queen';
elseif card{x} < .4825
card{x} = 10;
suit{x} = 'hearts';
title{x} = 'King';
elseif card{x} < .5018
card{x} = 11;
suit{x} = 'hearts';
title{x} = 'Ace';
elseif card{x} < .5211
card{x} = 2;
suit{x} = 'clubs';
title{x} = 'two';
elseif card{x} < .5404
card{x} = 3;
suit{x} = 'clubs';
title{x} = 'three';
elseif card{x} < .5597
card{x} = 4;
suit{x} = 'clubs';
title{x} = 'four';
elseif card{x} < .5790
card{x} = 5;
suit{x} = 'clubs';
title{x} = 'five';
elseif card{x} < .5983
card{x} = 6;
suit{x} = 'clubs';
title{x} = 'six';
elseif card{x} < .6176
card{x} = 7;
suit{x} = 'clubs';
title{x} = 'seven';
elseif card{x} < .6369
card{x} = 8;
suit{x} = 'clubs';
title{x} = 'eight';
elseif card{x} < .6562
card{x} = 9;
suit{x} = 'clubs';
title{x} = 'nine';
elseif card{x} < .6755
card{x} = 10;
suit{x} = 'clubs';
title{x} = 'Ten';
elseif card{x} < .6948
card{x} = 10;
suit{x} = 'clubs';
title{x} = 'Jack';
elseif card{x} < .7141
card{x} = 10;
suit{x} = 'clubs';
title{x} = 'Queen';
elseif card{x} < .7334
card{x} = 10;
suit{x} = 'clubs';
title{x} = 'King';
elseif card{x} < .7527
card{x} = 11;
suit{x} = 'clubs';
title{x} = 'Ace';
elseif card{x} < .7720
card{x} = 3;
suit{x} = 'diamonds';
title{x} = 'three';
elseif card{x} < .7913
card{x} = 4;
suit{x} = 'diamonds';
title{x} = 'four';
elseif card{x} < .8106
card{x} = 5;
suit{x} = 'diamonds';
title{x} = 'five';
elseif card{x} < .8299
card{x} = 6;
suit{x} = 'diamonds';
title{x} = 'six';
elseif card{x} < .8492
card{x} = 7;
suit{x} = 'diamonds';
title{x} = 'seven';
elseif card{x} < .8685
card{x} = 8;
suit{x} = 'diamonds';
title{x} = 'eight';
elseif card{x} < .8878
card{x} = 9;
suit{x} = 'diamonds';
title{x} = 'nine';
elseif card{x} < .9071
card{x} = 10;
suit{x} = 'diamonds';
title{x} = 'Ten';
elseif card{x} < .9264
card{x} = 10;
suit{x} = 'diamonds';
title{x} = 'Jack';
elseif card{x} < .9457
card{x} = 10;
suit{x} = 'diamonds';
title{x} = 'Queen';
elseif card{x} < .9650
card{x} = 10;
suit{x} = 'diamonds';
title{x} = 'King';
elseif card{x} < .9843
card{x} = 11;
suit{x} = 'diamonds';
title{x} = 'Ace';
else % Since there's slightly less chance of getting
this card, let's make it a 2
card{x} = 2; % instead of the Ace of diamonds.
suit{x} = 'diamonds';
title{x} = 'Two'
end
end

bjstruct = struct('Card',card,'Suit',suit,'Title',title);

disp(' ');
disp(['You: ______________ ______________ ']);
disp([' | | | |']);
disp([' | | | |']);
disp([' | | | |']);
disp([' | ',title{1},' ',title{3},' ']);
disp([' | | | |']);
disp([' | of | | of |']);
disp([' | | | |']);
disp([' | ',suit{1},' ',suit{3},' ']);
disp([' | | | |']);
disp([' | | | |']);
disp([' |______________| |______________|']);
disp(' ');
disp(' ');
disp(' ');
disp(['Computer: ______________ ______________ ']);
if card{2} == 10 & card{4} == 11
disp([' | | | |']);
disp([' | | | |']);
disp([' | | | |']);
disp([' | ',title{2},' ',title{4},' ']);
disp([' | | | |']);
disp([' | of | | of |']);
disp([' | | | |']);
disp([' | ',suit{2},' ',suit{4},' ']);
disp([' | | | |']);
disp([' | | | |']);
disp([' |______________| |______________|']);
else
disp([' | | | |']);
disp([' | | |**************|']);
disp([' | | |^^^^^^^^^^^^^^|']);
disp([' | ',title{2}]);
disp([' | | |##############|']);
disp([' | of | |XXXXXXXXXXXXXX|']);
disp([' | | |##############|']);
disp([' | ',suit{2}]);
disp([' | | |**************|']);
disp([' | | |^^^^^^^^^^^^^^|']);
disp([' |______________| |______________|']);
end
disp(' ');
disp(' ');
%
% Automatic Victory/Defeat
%
if card{1} + card{3} == 21
if card{2} + card{4} < 21
disp(' ');
disp('You win!');
disp(' ');
disp('You won twice your bet!');
% Win double bet with blackjack
start = start + (2*bet);
else
disp(['Computer: ',title{2},' of ',suit{2},' ',title{4},'
of ',suit{4}]);
disp(' ');
disp('Tie - nobody wins');
disp(' ');
end
elseif card{2} + card{4} == 21 & card{4} == 11
disp(['Computer: ',title{2},' of ',suit{2},' ',title{4},' of
',suit{4}]);
disp(' ');
disp('House has Blackjack, you lose');
disp(' ');
start = start - bet;
else
%
% Insurance if Dealer has an ACE face up
%
if card{2} == 11
insurance = input('Do you want insurance? Y/N','s');
if insurance == 'Y' | insurance == 'y'
bet = bet*0.5;
bj = 1;
end
end
%
% First Player Move
%
disp(' ');
move = [];
move2 = [];
move3 = [];
%
while isempty(move)
move = input('(H)it,(S)tand, or (Q)uit? ','s');
%
% First Hit
%
if move == 'H' | move == 'h' | move == 'Hit'
disp(' ');
disp('Player hits');
disp(' ');
disp(['You: ______________ ______________
______________']);
disp([' | | | | |
|']);
disp([' | | | | |
|']);
disp([' | | | | |
|']);
disp([' | ',title{1},' ',title{3},'
',title{8},' ']);
disp([' | | | | |
|']);
disp([' | of | | of | |
of |']);
disp([' | | | | |
|']);
disp([' | ',suit{1},' ',suit{3},'
',suit{8},' ']);
disp([' | | | | |
|']);
disp([' | | | | |
|']);
disp([' |______________| |______________|
|______________|']);
disp(' ');
disp(' ');
if card{1} + card{3} + card{8} > 21 & title{1}(1) == 'A'
card{1} = 1;
card1 = 1;
end
if card{1} + card{3} + card{8} > 21 & title{3}(1) == 'A'
card{3} = 1;
card3 = 1;
end
if card{1} + card{3} + card{8} > 21 & title{8}(1) == 'A'
card{8} = 1;
card8 = 1;
end
if card{1} + card{3} + card{8} > 21
disp(' ');
disp('Busted!...Computer wins');
disp(' ');
start = start - bet;
else
while isempty(move2)
move2 = input('(H)it,(S)tand, or (Q)uit? ','s');
if move2 == 'H' | move2 == 'h' | move2 == 'Hit'
disp(' ');
disp('Player hits');
disp(' ');
disp(['You: ______________ ______________
______________ ______________']);
disp([' | | | |
| | | |']);
disp([' | | | |
| | | |']);
disp([' | | | |
| | | |']);
disp([' | ',title{1},'
',title{3},' ',title{8},' ',' ',title{9},' ']);
disp([' | | | |
| | | |']);
disp([' | of | | of |
| of | | of |']);
disp([' | | | |
| | | |']);
disp([' | ',suit{1},'
',suit{3},' ',suit{8},' ',' ',suit{9},' ']);
disp([' | | | |
| | | |']);
disp([' | | | |
| | | |']);
disp([' |______________| |______________|
|______________| |______________|']);
disp(' ');
disp(' ');
if card{1} + card{3} + card{8} + card{9} > 21 & title{1}(1)
== 'A' & card1 == 0
card{1} = 1;
card1 = 1;
end
if card{1} + card{3} + card{8} + card{9} > 21 & title{3}(1)
== 'A' & card3 == 0
card{3} = 1;
card3 = 1;
end
if card{1} + card{3} + card{8} + card{9} > 21 & title{8}(1)
== 'A' & card8 == 0
card{8} = 1;
card8 = 1;
end
if card{1} + card{3} + card{8} + card{9} > 21 & title{9}(1)
== 'A'
card{9} = 1;
card9 = 1;
end
if card{1} + card{3} + card{8} + card{9} > 21
disp(' ');
disp('Busted!...Computer wins');
disp(' ');
start = start - bet;
else
while isempty(move3)
move3 = input('(H)it,(S)tand, or (Q)uit? ','s');
if move3 == 'H' | move3 == 'h' | move3 == 'Hit'
disp(' ');
disp('Player hits');
disp(' ');
disp(['You: ______________
______________ ______________ ______________
______________']);
disp([' | | |
| | | | | | |']);
disp([' | | |
| | | | | | |']);
disp([' | | |
| | | | | | |']);
disp([' | ',title{1},'
',title{3},' ',title{8},' ',title{9},'
',title{10},' ']);
disp([' | | |
| | | | | | |']);
disp([' | of | | of
| | of | | of | | of |']);
disp([' | | |
| | | | |']);
disp([' | ',suit{1},'
',suit{3},' ',suit{8},' ',suit{9},'
',suit{10},' ']);
disp([' | | |
| | | | | | |']);
disp([' | | |
| | | | | | |']);
disp([' |______________|
|______________| |______________| |______________|
|______________|']);
if card{1} + card{3} + card{8} + card{9} + card{10}
> 21 & title{1}(1) == 'A' & card1 == 0
card{1} = 1;
card1 = 1;
end
if card{1} + card{3} + card{8} + card{9} + card{10}
> 21 & title{3}(1) == 'A' & card3 == 0
card{3} = 1;
card3 = 1;
end
if card{1} + card{3} + card{8} + card{9} + card{10}
> 21 & title{8}(1) == 'A' & card8 == 0
card{8} = 1;
card8 = 1;
end
if card{1} + card{3} + card{8} + card{9} + card{10}
> 21 & title{9}(1) == 'A' & card9 == 0
card{9} = 1;
card9 = 1;
end
if card{1} + card{3} + card{8} + card{9} + card{10}
> 21 & title{10}(1) == 'A'
card{10} = 1;
end
if card{1} + card{3} + card{8} + card{9} + card{10}
> 21
disp(' ');
disp('Busted!...Computer wins');
disp(' ');
start = start - bet;
else
disp(' ');
disp('Player has max number of cards. In this
game 5 cards is the limit.');
disp(' ');
if card{2} > 9
if card{4} > 9
disp(['Computer: ',title{2},' of
',suit{2}]);
disp(['Computer: ',title{4},' of
',suit{4}]);
else
disp(['Computer: ',title{2},' of
',suit{2}]);
disp(['Computer:
',num2str(card{4}),' of ',suit{4}]);
end
elseif card{4} > 9
disp(['Computer:
',num2str(card{2}),' of ',suit{2}]);
disp(['Computer: ',title{4},' of
',suit{4}]);
else
disp(['Computer:
',num2str(card{2}),' of ',suit{2}]);
disp(['Computer:
',num2str(card{4}),' of ',suit{4}]);
end
if card{2} + card{4} == 22
card{2} = 1;
card2 = 1;
end
if card{2} + card{4} > 16
if (card{1} + card{3} + card{8} + card{9} +
card{10}) > (card{2} + card{4})
disp(' ');
disp('You win!');
disp(' ');
start = start + bet;
elseif (card{1} + card{3} + card{8} + card{9}
+ card{10}) == (card{2} + card{4})
disp(' ');
disp('Draw');
disp(' ');
else
disp(' ');
disp('Computer wins...');
disp(' ');
start = start - bet;
end
else
disp(['Computer: ',title{5},' of
',suit{5}]);
if card{2} + card{4} + card{5} > 21 &
title{2}(1) == 'A'
card{2} = 1;
card2 = 1;
end
if card{2} + card{4} + card{5} > 21 &
title{4}(1) == 'A'
card{4} = 1;
card4 = 1;
end
if card{2} + card{4} + card{5} > 21 &
title{5}(1) == 'A'
card{5} = 1;
card5 = 1;
end
if card{2} + card{4} + card{5} > 16
if (card{1} + card{3} + card{8} + card{9}
+ card{10}) > (card{2} + card{4} + card{5}) | (card{2} + card{4} + card{5}) > 21
disp(' ');
if card{2} + card{4} + card{5} > 21
disp('Computer Busts!');
disp(' ');
end
disp('You win!');
disp(' ');
start = start + bet;
elseif (card{1} + card{3} + card{8} +
card{9} + card{10}) == (card{2} + card{4} + card{5})
disp(' ');
disp('Draw');
disp(' ');
else
disp(' ');
disp('Computer wins...');
disp(' ');
start = start - bet;
end
else
disp(['Computer: ',title{6},' of
',suit{6}]);
if card{2} + card{4} + card{5} + card{6} >
21 & title{2}(1) == 'A' & card2 == 0
card{2} = 1;
card2 = 1;
end
if card{2} + card{4} + card{5} + card{6} >
21 & title{4}(1) == 'A' & card4 == 0
card{4} = 1;
card4 = 1;
end
if card{2} + card{4} + card{5} + card{6} >
21 & title{5}(1) == 'A' & card5 == 0
card{5} = 1;
card5 = 1;
end
if card{2} + card{4} + card{5} + card{6} >
21 & title{6}(1) == 'A'
card{6} = 1;
card6 = 1;
end
if card{2}+ card{4} + card{5} + card{6} >
16
if (card{1} + card{3} + card{8} +
card{9} + card{10}) > (card{2} + card{4} + card{5} + card{6}) | (card{2} +
card{4} + card{5} + card{6}) > 21
disp(' ');
if card{2} + card{4} + card{5} +
card{6} > 21
disp('Computer Busts!');
disp(' ');
end
disp('You win!');
disp(' ');
start = start + bet;
elseif (card{1} + card{3} + card{8} +
card{9} + card{10}) == (card{2} + card{4} + card{5} + card{6})
disp(' ');
disp('Draw');
disp(' ');
else
disp(' ');
disp('Computer wins...');
disp(' ');
start = start - bet;
end
else
disp(['Computer: ',title{7},'
of ',suit{7}]);
if card{2} + card{4} + card{5} +
card{6} + card{7} > 21 & title{2}(1) == 'A' & card2 == 0
card{2} = 1;
card2 = 1;
end
if card{2} + card{4} + card{5} +
card{6} + card{7} > 21 & title{4}(1) == 'A' & card4 == 0
card{4} = 1;
card4 = 1;
end
if card{2} + card{4} + card{5} +
card{6} + card{7} > 21 & title{5}(1) == 'A' & card5 == 0
card{5} = 1;
card5 = 1;
end
if card{2} + card{4} + card{5} +
card{6} + card{7} > 21 & title{6}(1) == 'A' & card6 == 0
card{6} = 1;
card6 = 1;
end
if card{2} + card{4} + card{5} +
card{6} + card{7} > 21 & title{7}(1) == 'A'
card{7} = 1;
card7 = 1;
end
if card{2}+ card{4} + card{5} + card{6}
+ card{7} > 16
if (card{1} + card{3} + card{8} +
card{9} + card{10}) > (card{2} + card{4} + card{5} + card{6} + card{7}) |
(card{2} + card{4} + card{5} + card{6} + card{7}) > 21
disp(' ');
if card{2} + card{4} + card{5} +
card{6} + card{7} > 21
disp('Computer busts!');
disp(' ');
end
disp('You win!');
disp(' ');
start = start + bet;
elseif (card{1} + card{3} + card{8}
+ card{9} + card{10}) == (card{2} + card{4} + card{5} + card{6} + card{7})
disp(' ');
disp('Draw');
disp(' ');
else
disp(' ');
disp('Computer wins with a 5-card
trump...');
disp(' ');
start = start - bet;
end
end
end
end
end
end
elseif move3 == 'S' | move3 == 's' | move3 == 'Stand'
card{10} = 0;
disp(' ');
disp('Player stands');
disp(' ');
if card{2} > 9
if card{4} > 9
disp(['Computer: ',title{2},' of
',suit{2}]);
disp(['Computer: ',title{4},' of
',suit{4}]);
else
disp(['Computer: ',title{2},' of
',suit{2}]);
disp(['Computer:
',num2str(card{4}),' of ',suit{4}]);
end
else
if card{4} > 9
disp(['Computer:
',num2str(card{2}),' of ',suit{2}]);
disp(['Computer: ',title{4},' of
',suit{4}]);
else
disp(['Computer:
',num2str(card{2}),' of ',suit{2}]);
disp(['Computer:
',num2str(card{4}),' of ',suit{4}]);
end
end
if card{2} + card{4} == 22 % If both cards are
ACES
card{2} = 1;
card2 = 1;
end
if card{2} + card{4} > 16
if (card{1} + card{3} + card{8} + card{9} +
card{10}) > (card{2} + card{4})
disp(' ');
disp('You win!');
disp(' ');
start = start + bet;
elseif (card{1} + card{3} + card{8} + card{9} +
card{10}) == (card{2} + card{4})
disp(' ');
disp('Draw');
disp(' ');
else
disp(' ');
disp('Computer wins...');
disp(' ');
start = start - bet;
end
else
disp(['Computer: ',title{5},' of
',suit{5}]);
if card{2} + card{4} + card{5} > 21 &
title{2}(1) == 'A'
card{2} = 1;
card2 = 1;
end
if card{2} + card{4} + card{5} > 21 &
title{4}(1) == 'A'
card{4} = 1;
card4 = 1;
end
if card{2} + card{4} + card{5} > 21 &
title{5}(1) == 'A'
card{5} = 1;
card5 = 1;
end
if card{2} + card{4} + card{5} > 16
if (card{1} + card{3} + card{8} + card{9} +
card{10}) > (card{2} + card{4} + card{5}) | (card{2} + card{4} + card{5}) > 21
disp(' ');
if card{2} + card{4} + card{5} > 21
disp('Computer Busts!');
disp(' ');
end
disp('You win!');
disp(' ');
start = start + bet;
elseif (card{1} + card{3} + card{8} + card{9}
+ card{10}) == (card{2} + card{4} + card{5})
disp(' ');
disp('Draw');
disp(' ');
else
disp(' ');
disp('Computer wins...');
disp(' ');
start = start - bet;
end
else
disp(['Computer: ',title{6},' of
',suit{6}]);
if card{2} + card{4} + card{5} + card{6} > 21
& title{2}(1) == 'A' & card2 == 0
card{2} = 1;
card2 = 1;
end
if card{2} + card{4} + card{5} + card{6} > 21
& title{4}(1) == 'A' & card4 == 0
card{4} = 1;
card4 = 1;
end
if card{2} + card{4} + card{5} + card{6} > 21
& title{5}(1) == 'A' & card5 == 0
card{5} = 1;
card5 = 1;
end
if card{2} + card{4} + card{5} + card{6} > 21
& title{6}(1) == 'A'
card{6} = 1;
card6 = 1;
end
if card{2}+ card{4} + card{5} + card{6} > 16
if (card{1} + card{3} + card{8} + card{9}
+ card{10}) > (card{2} + card{4} + card{5} + card{6}) | (card{2} + card{4} +
card{5} + card{6}) > 21
disp(' ');
if card{2} + card{4} + card{5} +
card{6} > 21
disp('Computer Busts!');
disp(' ');
end
disp('You win!');
disp(' ');
start = start + bet;
elseif (card{1} + card{3} + card{8} +
card{9} + card{10}) == (card{2} + card{4} + card{5} + card{6})
disp(' ');
disp('Draw');
disp(' ');
else
disp(' ');
disp('Computer wins...');
disp(' ');
start = start - bet;
end
else
disp(['Computer: ',title{7},' of
',suit{7}]);
if card{2} + card{4} + card{5} + card{6} +
card{7} > 21 & title{2}(1) == 'A' & card2 == 0
card{2} = 1;
card2 = 1;
end
if card{2} + card{4} + card{5} + card{6} +
card{7} > 21 & title{4}(1) == 'A' & card4 == 0
card{4} = 1;
card4 = 1;
end
if card{2} + card{4} + card{5} + card{6} +
card{7} > 21 & title{5}(1) == 'A' & card5 == 0
card{5} = 1;
card5 = 1;
end
if card{2} + card{4} + card{5} + card{6} +
card{7} > 21 & title{6}(1) == 'A' & card6 == 0
card{6} = 1;
card6 = 1;
end
if card{2} + card{4} + card{5} + card{6} +
card{7} > 21 & title{7}(1) == 'A'
card{7} = 1;
card7 = 1;
end
if card{2}+ card{4} + card{5} + card{6} +
card{7} > 16
if (card{1} + card{3} + card{8} +
card{9} + card{10}) > (card{2} + card{4} + card{5} + card{6} + card{7}) |
(card{2} + card{4} + card{5} + card{6} + card{7}) > 21
disp(' ');
if card{2} + card{4} + card{5} +
card{6} + card{7} > 21
disp('Computer busts!');
disp(' ');
end
disp('You win!');
disp(' ');
start = start + bet;
elseif (card{1} + card{3} + card{8} +
card{9} + card{10}) == (card{2} + card{4} + card{5} + card{6} + card{7})
disp(' ');
disp('Draw');
disp(' ');
else
disp(' ');
disp('Computer wins with a 5-card
trump...');
disp(' ');
start = start - bet;
end
end
end
end
end
elseif move3 == 'Q' | move == 'q' | move == 'Quit'
disp(' ');
disp('Quitting this game...');
disp(' ');
quit_count = quit_count+1;
if quit_count > 1
disp(' ');
disp(['You lost your bid amount of
$',num2str(bet),' - You were warned!']);
disp(' ');
start = start - bet;
else
disp(' ');
disp('You did not lose your bet amount this
time.');
disp('However, from now on, if you QUIT, the
computer automatically wins the game');
disp(' ');
end
else
move2 = [];
disp(' ');
disp('Try again');
disp(' ');
end
end
end
elseif move2 == 'S' | move2 == 's' | move2 == 'Stand'
card{9} = 0;
card{10} = 0;
% Do computer's moves (same as STAND section below but with
CARD8 included)
disp(' ');
disp('Player stands');
disp(' ');
if card{2} > 9
if card{4} > 9
disp(['Computer: ',title{2},' of
',suit{2}]);
disp(['Computer: ',title{4},' of
',suit{4}]);
else
disp(['Computer: ',title{2},' of
',suit{2}]);
disp(['Computer: ',num2str(card{4}),' of
',suit{4}]);
end
else
if card{4} > 9
disp(['Computer: ',num2str(card{2}),' of
',suit{2}]);
disp(['Computer: ',title{4},' of
',suit{4}]);
else
disp(['Computer: ',num2str(card{2}),' of
',suit{2}]);
disp(['Computer: ',num2str(card{4}),' of
',suit{4}]);
end
end
if card{2} + card{4} == 22 % If both cards are ACES
card{2} = 1;
card2 = 1;
end
if card{2} + card{4} > 16
if (card{1} + card{3} + card{8} + card{9} + card{10}) >
(card{2} + card{4})
disp(' ');
disp('You win!');
disp(' ');
start = start + bet;
elseif (card{1} + card{3} + card{8} + card{9} + card{10})
== (card{2} + card{4})
disp(' ');
disp('Draw');
disp(' ');
else
disp(' ');
disp('Computer wins...');
disp(' ');
start = start - bet;
end
else
disp(['Computer: ',title{5},' of ',suit{5}]);
if card{2} + card{4} + card{5} > 21 & title{2}(1) == 'A'
card{2} = 1;
card2 = 1;
end
if card{2} + card{4} + card{5} > 21 & title{4}(1) == 'A'
card{4} = 1;
card4 = 1;
end
if card{2} + card{4} + card{5} > 21 & title{5}(1) == 'A'
card{5} = 1;
card5 = 1;
end
if card{2} + card{4} + card{5} > 16
if (card{1} + card{3} + card{8} + card{9} + card{10})
> (card{2} + card{4} + card{5}) | (card{2} + card{4} + card{5}) > 21
disp(' ');
if card{2} + card{4} + card{5} > 21
disp('Computer Busts!');
disp(' ');
end
disp('You win!');
disp(' ');
start = start + bet;
elseif (card{1} + card{3} + card{8} + card{9} +
card{10}) == (card{2} + card{4} + card{5})
disp(' ');
disp('Draw');
disp(' ');
else
disp(' ');
disp('Computer wins...');
disp(' ');
start = start - bet;
end
else
disp(['Computer: ',title{6},' of
',suit{6}]);
if card{2} + card{4} + card{5} + card{6} > 21 &
title{2}(1) == 'A' & card2 == 0
card{2} = 1;
card2 = 1;
end
if card{2} + card{4} + card{5} + card{6} > 21 &
title{4}(1) == 'A' & card4 == 0
card{4} = 1;
card4 = 1;
end
if card{2} + card{4} + card{5} + card{6} > 21 &
title{5}(1) == 'A' & card5 == 0
card{5} = 1;
card5 = 1;
end
if card{2} + card{4} + card{5} + card{6} > 21 &
title{6}(1) == 'A'
card{6} = 1;
card6 = 1;
end
if card{2}+ card{4} + card{5} + card{6} > 16
if (card{1} + card{3} + card{8} + card{9} +
card{10}) > (card{2} + card{4} + card{5} + card{6}) | (card{2} + card{4} +
card{5} + card{6}) > 21
disp(' ');
if card{2} + card{4} + card{5} + card{6} > 21
disp('Computer Busts!');
disp(' ');
end
disp('You win!');
disp(' ');
start = start + bet;
elseif (card{1} + card{3} + card{8} + card{9} +
card{10}) == (card{2} + card{4} + card{5} + card{6})
disp(' ');
disp('Draw');
disp(' ');
else
disp(' ');
disp('Computer wins...');
disp(' ');
start = start - bet;
end
else
disp(['Computer: ',title{7},' of
',suit{7}]);
if card{2} + card{4} + card{5} + card{6} + card{7}
> 21 & title{2}(1) == 'A' & card2 == 0
card{2} = 1;
card2 = 1;
end
if card{2} + card{4} + card{5} + card{6} + card{7}
> 21 & title{4}(1) == 'A' & card4 == 0
card{4} = 1;
card4 = 1;
end
if card{2} + card{4} + card{5} + card{6} + card{7}
> 21 & title{5}(1) == 'A' & card5 == 0
card{5} = 1;
card5 = 1;
end
if card{2} + card{4} + card{5} + card{6} + card{7}
> 21 & title{6}(1) == 'A' & card6 == 0
card{6} = 1;
card6 = 1;
end
if card{2} + card{4} + card{5} + card{6} + card{7}
> 21 & title{7}(1) == 'A'
card{7} = 1;
card7 = 1;
end
if card{2}+ card{4} + card{5} + card{6} + card{7} >
16
if (card{1} + card{3} + card{8} + card{9} +
card{10}) > (card{2} + card{4} + card{5} + card{6} + card{7}) | (card{2} +
card{4} + card{5} + card{6} + card{7}) > 21
disp(' ');
if card{2} + card{4} + card{5} + card{6} +
card{7} > 21
disp('Computer busts!');
disp(' ');
end
disp('You win!');
disp(' ');
start = start + bet;
elseif (card{1} + card{3} + card{8} + card{9} +
card{10}) == (card{2} + card{4} + card{5} + card{6} + card{7})
disp(' ');
disp('Draw');
disp(' ');
else
disp(' ');
disp('Computer wins with a 5-card trump...');
disp(' ');
start = start - bet;
end
end
end
end
end
elseif move2 == 'Q' | move == 'q' | move == 'Quit'
disp(' ');
disp('Quitting this game...');
disp(' ');
quit_count = quit_count+1;
if quit_count > 1
disp(' ');
disp(['You lost your bid amount of $',num2str(bet),' -
You were warned!']);
disp(' ');
start = start - bet;
else
disp(' ');
disp('You did not lose your bet amount this time.');
disp('However, from now on, if you QUIT, the computer
automatically wins the game');
disp(' ');
end
else
move2 = [];
disp(' ');
disp('Try again');
disp(' ');
end
end
end
% Stand
elseif move == 'S' | move == 's' | move == 'Stand'
card{8} = 0;
card{9} = 0;
card{10} = 0;
disp(' ');
disp('Player stands');
disp(' ');
if card{2} > 9
if card{4} > 9
disp(['Computer: ',title{2},' of ',suit{2}]);
disp(['Computer: ',title{4},' of ',suit{4}]);
else
disp(['Computer: ',title{2},' of ',suit{2}]);
disp(['Computer: ',num2str(card{4}),' of ',suit{4}]);
end
else
if card{4} > 9
disp(['Computer: ',num2str(card{2}),' of ',suit{2}]);
disp(['Computer: ',title{4},' of ',suit{4}]);
else
disp(['Computer: ',num2str(card{2}),' of ',suit{2}]);
disp(['Computer: ',num2str(card{4}),' of ',suit{4}]);
end
end
if card{2} + card{4} == 22 % If both cards are ACES
card{2} = 1;
card2 = 1;
end
if card{2} + card{4} > 16
if (card{1} + card{3} + card{8} + card{9} + card{10}) > (card{2} +
card{4})
disp(' ');
disp('You win!');
disp(' ');
start = start + bet;
elseif (card{1} + card{3} + card{8} + card{9} + card{10}) ==
(card{2} + card{4})
disp(' ');
disp('Draw');
disp(' ');
else
if card{2} == 11 & card{4} == 10
disp('Computer has blackjack');
disp(' ');
if bj == 1
disp('You lose 1/2 of your original bet');
else
disp('You lose your bet');
end
end
disp(' ');
disp('Computer wins...');
disp(' ');
start = start - bet;
end
else
disp(['Computer: ',title{5},' of ',suit{5}]);
if card{2} + card{4} + card{5} > 21 & title{2}(1) == 'A'
card{2} = 1;
card2 = 1;
end
if card{2} + card{4} + card{5} > 21 & title{4}(1) == 'A'
card{4} = 1;
card4 = 1;
end
if card{2} + card{4} + card{5} > 21 & title{5}(1) == 'A'
card{5} = 1;
card5 = 1;
end
if card{2} + card{4} + card{5} > 16
if (card{1} + card{3} + card{8} + card{9} + card{10}) >
(card{2} + card{4} + card{5}) | (card{2} + card{4} + card{5}) > 21
disp(' ');
if card{2} + card{4} + card{5} > 21
disp('Computer Busts!');
disp(' ');
end
disp('You win!');
disp(' ');
start = start + bet;
elseif (card{1} + card{3} + card{8} + card{9} + card{10}) ==
(card{2} + card{4} + card{5})
disp(' ');
disp('Draw');
disp(' ');
else
disp(' ');
disp('Computer wins...');
disp(' ');
start = start - bet;
end
else
disp(['Computer: ',title{6},' of ',suit{6}]);
if card{2} + card{4} + card{5} + card{6} > 21 & title{2}(1) ==
'A' & card2 == 0
card{2} = 1;
card2 = 1;
end
if card{2} + card{4} + card{5} + card{6} > 21 & title{4}(1) ==
'A' & card4 == 0
card{4} = 1;
card4 = 1;
end
if card{2} + card{4} + card{5} + card{6} > 21 & title{5}(1) ==
'A' & card5 == 0
card{5} = 1;
card5 = 1;
end
if card{2} + card{4} + card{5} + card{6} > 21 & title{6}(1) ==
'A'
card{6} = 1;
card6 = 1;
end
if card{2}+ card{4} + card{5} + card{6} > 16
if (card{1} + card{3} + card{8} + card{9} + card{10}) >
(card{2} + card{4} + card{5} + card{6}) | (card{2} + card{4} + card{5} + card{6})
> 21
disp(' ');
if card{2} + card{4} + card{5} + card{6} > 21
disp('Computer Busts!');
disp(' ');
end
disp('You win!');
disp(' ');
start = start + bet;
elseif (card{1} + card{3} + card{8} + card{9} + card{10}) ==
(card{2} + card{4} + card{5} + card{6})
disp(' ');
disp('Draw');
disp(' ');
else
disp(' ');
disp('Computer wins...');
disp(' ');
start = start - bet;
end
else
disp(['Computer: ',title{7},' of ',suit{7}]);
if card{2} + card{4} + card{5} + card{6} + card{7} > 21 &
title{2}(1) == 'A' & card2 == 0
card{2} = 1;
card2 = 1;
end
if card{2} + card{4} + card{5} + card{6} + card{7} > 21 &
title{4}(1) == 'A' & card4 == 0
card{4} = 1;
card4 = 1;
end
if card{2} + card{4} + card{5} + card{6} + card{7} > 21 &
title{5}(1) == 'A' & card5 == 0
card{5} = 1;
card5 = 1;
end
if card{2} + card{4} + card{5} + card{6} + card{7} > 21 &
title{6}(1) == 'A' & card6 == 0
card{6} = 1;
card6 = 1;
end
if card{2} + card{4} + card{5} + card{6} + card{7} > 21 &
title{7}(1) == 'A'
card{7} = 1;
card7 = 1;
end
if card{2}+ card{4} + card{5} + card{6} + card{7} > 16
if (card{1} + card{3} + card{8} + card{9} + card{10}) >
(card{2} + card{4} + card{5} + card{6} + card{7}) | (card{2} + card{4} + card{5}
+ card{6} + card{7}) > 21
disp(' ');
if card{2} + card{4} + card{5} + card{6} + card{7} >
21
disp('Computer busts!');
disp(' ');
end
disp('You win!');
disp(' ');
start = start + bet;
elseif (card{1} + card{3} + card{8} + card{9} + card{10})
== (card{2} + card{4} + card{5} + card{6} + card{7})
disp(' ');
disp('Draw');
disp(' ');
else
disp(' ');
disp('Computer wins with a 5-card trump...');
disp(' ');
start = start - bet;
end
end
end
end
end

% Quit
elseif move == 'Q' | move == 'q' | move == 'Quit'
disp(' ');
disp('Quitting this game...');
disp(' ');
quit_count = quit_count+1;
if quit_count > 1
disp(' ');
disp(['You lost your bid amount of $',num2str(bet),' - You were
warned!']);
disp(' ');
start = start - bet;
else
disp(' ');
disp('You did not lose your bet amount this time.');
disp('However, from now on, if you QUIT, the computer
automatically wins the game');
disp(' ');
end
else
move = [];
disp(' ');
disp('Try again');
disp(' ');
end
end
end
if start < 1
disp(' ');
if start > 0
disp('You do not have enough money to bet anymore...');
end
disp('You are out of play money...sorry!');
disp(' ');
disp(['You played ',num2str(games_total),' games and walked away
broke...']);
disp('**** GAME OVER ****');
for i=1:1000000
% pause
end
else
response = [];
while isempty(response)
play_again=input('Play again? (y/n): ','s');
if play_again == 'n' | play_again == 'N'
disp(' ');
disp(['Thanks for playing! You played ',num2str(games_total),'
games, and you finished with $',num2str(start),'!']);
response = 'N';
elseif play_again == 'y' | play_again == 'Y'
disp(' ');
response = 'Y';
games_total = games_total + 1;
else
disp('Invalid Entry');
disp(' ');
response = [];
end
end
end
end
for i=1:1000000
% pause
end