How to restart the loop if players go bust

This is my code to select 2 inital cards, and create a hand, one for dealer and one for player.
dealer count = 0
loop for dealer's inital 2 cards to be drawn
y='y';
n='n';
dhand = 0;
imshow(b1) %inital dealers first card which is faced down
title('dealer''s card')
for idx = randperm(52) for each card in the deck, which has been randomly shuffled
figure
dealer_count = dealer_count + 1;
imshow(deckcan{idx}{1}) %show the card
title('dealer''s card') %title for dealers card
dcardvalue = deckcan{idx}{2} %if the card selected is an Ace lets player choose whether 1 or 11
if dealer_count == 1 break
end
end
dhand = dhand + dcardvalue
phand = 0;
player_count = 0;%loops for players inital 2 cards to be drawn
for idx = randperm(length(deckcan))
figure
imshow(deckcan{idx}{1}) %shows players card
title('player''s card')
pcardvalue1 = deckcan{idx}{2} %shows value of card selected
acesolvertest2(pcardvalue1);
player_count = player_count + 1;
if player_count >=1 break
end
end
player_count = 0;%loops for players inital 2 cards to be drawn
for idx = randperm(length(deckcan))
figure
imshow(deckcan{idx}{1})%shows players card
title('player''s card')
pcardvalue2 = deckcan{idx}{2}%shows value of card selected
acesolvertest2(pcardvalue2);
player_count = player_count + 1;
if player_count >=1 break
end
end% signifies end of drawing of two inital cards
phand = pcardvalue1 + pcardvalue2 % adds up both loops card values to create playerhand aka phand
prompt = 'Would player like more cards press y then enter to hit and n then enter to stand'%loop for player to increase hand
for idx = randperm(52);
response = input('Press y or n');
if response == y;
figure
imshow(deckcan{idx}{1})
title('player''s card')
pcardvalue = deckcan{idx}{2}
acesolvertest2(pcardvalue);
phand = phand + pcardvalue % adds the selected cards value to
disp('you hit') % signifies you have drawn a card
if phand > 21 % if players hand exceeds that of 21 you go bust
disp('you went bust, press n for dealers turn')
end
else response == n;
disp('you stand, dealers turn')
break
end
end
What im struggling with is when the player's hand, phand exceeds 21 and they go bust, how to restart the entire loop

Answers (1)

Robert - you could wrap the majority of your code in a while loop so that once the player goes bust (or I suppose wins) you could ask him/her if he/she wishes to continue. If so, then you continue, else you exit the loop. Something like
% loop until user decides to exit
while true
% play game - so this would be your code
% game is over - does the player wish to continue
result = input('Do you wish to play again? (Y/N)','s');
if ~strcmpi(result,'Y')
break;
end
end
So long as the user enters Y (or y), the game will continue.

2 Comments

Hi Geoff, I tried implementing your suggestion and the game resets, but i don't think it works how you suggested, due to my poor coding skills, not your advice.
while true % Thinking about swapping this for while bust is true
round_count = 0;
player_score = [] dealer_score = [] %add round count = 0
while round_count < 3
y='y'; n='n'; dhand = 0;
imshow(b1)%loop for dealer's inital 2 cards to be drawn
title('dealer''s card')
dealer_count = 0;
for idx = randperm(52)
figure
dealer_count = dealer_count + 1;
imshow(deckcan{idx}{1})
title('dealer''s card')
dcardvalue = deckcan{idx}{2}%if the card selected is an Ace lets player choose whether 1 or 11
if dealer_count == 1 break
end
end
dhand = dhand + dcardvalue
phand = 0;
player_count = 0;%loops for players inital 2 cards to be drawn
for idx = randperm(length(deckcan))
figure
imshow(deckcan{idx}{1})%shows players card
title('player''s card')
pcardvalue1 = deckcan{idx}{2}%shows value of card selected
player_count = player_count + 1;
if player_count >=1 break
end
end
player_count = 0;%loops for players inital 2 cards to be drawn
for idx = randperm(length(deckcan))
figure
imshow(deckcan{idx}{1})%shows players card
title('player''s card')
pcardvalue2 = deckcan{idx}{2}%shows value of card selected
player_count = player_count + 1;
if player_count >=1 break
end
end% signifies end of drawing of two inital cards
phand = pcardvalue1 + pcardvalue2% adds up both loops card values to create playerhand aka phand
prompt = 'Would player like more cards press y then enter to hit and n then enter to stand'%loop for player to increase hand
for idx = randperm(52);
response = input('Press y or n');
if response == y;
figure
imshow(deckcan{idx}{1})
title('player''s card')
pcardvalue = deckcan{idx}{2}
phand = phand + pcardvalue
disp('you hit')
if phand > 21
bust = false
disp('you went bust,')
'does the player wish to continue'
result = input('Do you wish to play again? (yes/no)','s');
if ~strcmpi(result,'yes')
break;
end
end
else response == n;
disp('you stand, dealers turn')
break
This is basically what i've got. I also tried adding a while bust is true rather than while true. However what is occuring when i press n the game restarts, what i was hoping is when i press n, the players turn ends, so they "stand" and the program moves onto the next loop for the dealer which is the second half of the program. Im stuck in how to stop it resetting whilst pressing n, i tried a yes/no as opposed to y and n but still no luck
Robert - it would be easier to understand what you code is doing if it were attached as an m file rather than just pasting it into your above comment. Please use the paperclip button to attach the file. If you ingnore the code that I suggested you add, does the program worm well for one round i.e. until the user goes bust?

Sign in to comment.

Categories

Find more on Card games in Help Center and File Exchange

Products

Asked:

on 13 Jan 2015

Commented:

on 14 Jan 2015

Community Treasure Hunt

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

Start Hunting!