matlab - How can i end an if statement and break out of all others? -


i making black jack program class, , have series of "if loops"my issue thatwhen yes, hit me card, proceeds next step like, when "no, holding", still asks me if hit again. how can make when no, end function completly? program below.

 players=input('welcome matjack! aces count 11, , face cards count 10. please enter number of players:')  if players==1;     card_1=randi(11,1);     card_2=randi(11,1);     fprintf ('face card %d! face down unknown!',card_1)     hit=input(' hit? y/n:','s')     if hit=='y';         card_3=randi(11,1);         cards=card_1+card_3;         player_1=card_1+card_2+card_3;         if player_1<21;             fprintf('value of face cards %d. face down still unknown!',cards);             hit=input(' hit again? y/n:','s')             if hit=='y';                 card_4=randi(11,1);                 cards=card_1+card_3+card_4;                 player_1=card_1+card_2+card_3+card_4;             end              if player_1>21 ;             fprintf ('player 1 broke! total card value %d',player_1)              end               if hit=='n';                  player_1=card_1+card_2+card_3;                  fprintf ('player 1 holds! total card value %d',player_1)              end         end         if player_1<21             fprintf('value of face cards %d. face down still unknown!',cards);                   hit=input(' hit again? y/n:','s')             if hit=='y';                 card_5=randi(11,1);                 cards=card_1+card_3+card_4+card_5;                 player_1=card_1+card_2+card_3+card_4+card_5;             end              if player_1>21 ;             fprintf ('player 1 broke! total card value %d',player_1)         end              if hit=='n';         player_1=card_1+card_2+card_3+card_4;         fprintf ('player 1 holds! total card value %d',player_1)         end          end         if player_1<21             fprintf('value of face cards %d. face down still unknown!',cards);                   hit=input(' hit again? y/n:','s')             if hit=='y';                 card_6=randi(11,1);                 cards=card_1+card_3+card_4+card_5+card_6;                 player_1=card_1+card_2+card_3+card_4+card_5+card_6;             end              if player_1>21 ;             fprintf ('player 1 broke! total card value %d',player_1)         end              if hit=='n';         player_1=card_1+card_2+card_3+card_4+card_5;         fprintf ('player 1 holds! total card value %d',player_1)         end         end     if player_1<21             fprintf('value of face cards %d. face down still unknown!',cards);                   hit=input(' hit again? y/n:','s')             if hit=='y';                 card_7=randi(11,1);                 cards=card_1+card_3+card_4+card_5+card_6+card_7;                 player_1=card_1+card_2+card_3+card_4+card_5+card_6+card_7;             end              if player_1>21 ;             fprintf ('player 1 broke! total card value %d',player_1)         end              if hit=='n';         player_1=card_1+card_2+card_3+card_4+card_5+card_6;         fprintf ('player 1 holds! total card value %d',player_1)              end     end     end      if hit=='n';         player_1=card_1+card_2;         fprintf ('player 1 holds! total card value %d',player_1)     end       end 

i worked on bit. functions might want add split, put in suits, , add in persistent player names. tried put in descriptive variable , function names.

function matjack global allcards decks allcards = []; clc  dealerhitonsoft17 = 1;  disp('welcome matjack! aces count 11, , face cards count 10.')  if dealerhitonsoft17     disp('dealer hits on soft 17') else     disp('dealer holds on soft 17') end  numberplayers = input('please enter number of players:'); decks = input('how many decks of cards want play with? ');  playername = cellfun(@(n) strjoin({'player ', num2str(n)}, ' '), num2cell((1:numberplayers)'), 'uni', 0);  dealercards = dealcards(2, []);  if blackjack('dealer', dealercards)     printhand('dealer''s', dealercards)     disp('you lose')     return end  printhand('dealer''s', dealercards(1))  playerhands = cellfun(@(n) dealcards(n, []), num2cell(2 * ones( numberplayers, 1)), 'uni', 0); isplaying = ones(size(playerhands));  = 1:numberplayers     isplaying(i) = ~blackjack(['player ', num2str(i)], playerhands{i}); end      while any(isplaying)         = 1:numberplayers             if isplaying(i)                 printhand(playername{i}, playerhands{i})                  if strcmpi(input([playername{i}, ' has ', num2str(sumhand(playerhands{i}, 0)),'. hit? y/n:'],'s'), 'y')                     playerhands{i} = dealcards(1, playerhands{i});                     printhand(playername{i}, playerhands{i})                     disp([playername{i}, ' has ', num2str(sumhand(playerhands{i}, 0)),'.'])                      if sumhand(playerhands{i}, 0) > 21                         disp([playername{i}, ' busts!'])                         isplaying(i) = 0;                     end                 else                     disp([playername{i}, ' holds @ ', num2str(sumhand(playerhands{i}, 0)),'.'])                     isplaying(i) = 0;                 end             end         end     end      printhand('dealer''s', dealercards)      while sumhand(dealercards, dealerhitonsoft17) < 17         dealercards = dealcards(1, dealercards);         printhand('dealer', dealercards)          if sumhand(dealercards, dealerhitonsoft17) > 21             disp('dealer busts!')         elseif sumhand(dealercards, dealerhitonsoft17) > 17             disp(['dealer holds @ ', num2str(sumhand(dealercards, dealerhitonsoft17)),'.'])         else             disp(['dealer has ', num2str(sumhand(dealercards, dealerhitonsoft17)),'.'])         end      end      cellfun(@(name, h) windrawlose(name, sumhand(h, 0), sumhand(dealercards, dealerhitonsoft17)), playername, playerhands);      end  function hand = dealcards(n, hand)     global allcards decks     possiblecards = randi(52 * decks, n, 1);      [c, ia, ~] = unique([allcards; possiblecards], 'stable');     ia = ia - length(allcards);     ia(ia < 1) = [];      n = length(allcards) + n - length(c);     allcards = c;     hand = [hand; possiblecards(ia)];      if n > 0         hand = dealcards(n, hand);     end end  function printhand(player, hand)     ord = horzcat('ace', cellfun(@num2str, num2cell(2:10), 'uni', 0), 'jack', 'queen', 'king');     hand = mod(hand - 1, 13) + 1;      if length(hand) == 1         disp([player, ' face card ', strjoin(ord(hand), ', '), '. face down unknown!'])     elseif length(hand) > 1         disp([player, ' cards ', strjoin(ord(hand), ', '), '.'])     end end  function s = sumhand(hand, dealersoft)     hand = mod(hand - 1, 13) + 1;     hand(hand >= 10) = 10;     hand(hand == 1 ) =  11;      s = sum(hand);      while ((s > 21) && any(hand == 11))         if ~(dealersoft && ((17 <= s) && (s <= 21)))             n = find(hand == 11, 1, 'first');             hand(n) = 1;             s = sum(hand);         end     end end  function bj = blackjack(player, hand)     if (length(hand) == 2) && (sumhand(hand, 0) == 21)         bj = 1;         disp([player, ' hit blackjack!'])     else         bj = 0;     end end  function windrawlose(name, player, dealer)     if ((player < dealer) && (dealer <= 21)) || (player > 21)         disp([name, ' loses.'])     elseif (player == dealer)         disp([name, ' draws.'])     else         disp([name, ' wins.'])     end end 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -