How to restart the loop if players go bust
Show older comments
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)
Geoff Hayes
on 14 Jan 2015
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
Robert
on 14 Jan 2015
Geoff Hayes
on 14 Jan 2015
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?
Categories
Find more on Card games in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!