clear
clc
suit = [2 3 4 5 6 7 8 9 10 10 10 10 11];
deck = [suit suit suit suit];
totalgames = 0;
wins = 0;
PlayerHand = [];
DealerHand = [];
state = 0;
startingmoney = 100
while (1)
  
switch state
    case 0 %% this case deals the cards to the dealer and player
        bet = input('Please input your Bet');
        
        suit = [2 3 4 5 6 7 8 9 10 10 10 10 11];
        deck = [suit suit suit suit];
        card = randi(length(deck),1);
        DealerHand = deck(card);
        deck(card) = [];
        card = randi(length(deck),1);
        PlayerHand = deck(card);
        deck(card) = [];
        card = randi(length(deck),1);
        DealerHand(end+1) = deck(card);
        deck(card) = [];
        card = randi(length(deck),1);
        PlayerHand(end+1) = deck(card);
        deck(card) = [];
        state = 1;
       
    case 1 %% this case sums the player's initial hand and shows the dealer's face up card
        phand = sum(PlayerHand);
        PlayerHand
        DealerHand
        dhand = DealerHand(end)
        dealersum = sum(DealerHand);
        state = 2;
       
    case 2
        if(length(PlayerHand) == 2)
         phand
        end
         if (dealersum ~= 21 && phand == 21)
             display('BlackJack')
             startingmoney = startingmoney + 1.5*bet
             state = 5;
         
         elseif (dealersum == 21 && phand ~= 21)
            totalgames = totalgames + 1;
            display('Dealer BlackJack, You Lose')
            startingmoney = startingmoney - bet
            playagain = input('Play Again Y/N')
            state = 5;
        
         elseif (dealersum == 21 && phand == 21)
             display('Push')
         
         elseif (phand == 10 || phand == 11)
             doubledown = input('Would you like to doubledown? Y/N')
             if (strcmp(doubledown, 'Y') == 1)
                 card = randi(length(deck),1);
                 PlayerHand(end+1) = deck(card);
                 deck(card) = [];
                 bet = bet*2;
                 state = 3;
             end
         elseif(phand ~= 11 || phand ~= 10)
             
                
            question = input('Stand or Hit')
         end
            if(strcmp(question,'Hit') == 1)
                state = 4;
            elseif(strcmp(question,'Stand') == 1)
                state =3;
            else
                display('Please Type "Hit or Stand" surrounded by single quotes')
                state = 2;
            end
% % % %        
    case 3
         if (dealersum < 17)
              card = randi(length(deck),1);
              DealerHand(end+1) = deck(card);
              deck(card) = [];
              dealersum = sum(DealerHand);
         end
         if(dealersum > 21)
             dealersum
             phand
             display('Dealer Bust')
            startingmoney = startingmoney + bet;
             state = 5;
         end
         if (dealersum < phand)
             dealersum
             phand
             display('You Win')
          startingmoney = startingmoney + bet;
             state = 5;
         end
         if (dealersum > phand && dealersum > 17 && dealersum <= 21)
             dealersum
             phand
             display('You Lose: Dealer Has Higher Number')
             startingmoney = startingmoney - bet;
             
             state = 5;
         end
         if( dealersum >= 17 && dealersum <=21 && dealersum == phand)
             display('Push')
             
             state = 5;
         end
    case 4
          card = randi(length(deck),1);
          PlayerHand(end+1) = deck(card);
          deck(card) = [];
          phand = sum(PlayerHand);
          phand
          if( phand > 21)
              display('You Lose')
              startingmoney = startingmoney - bet;
              state =5 ;
          end
        if (phand < 21)
            state = 2;
        end
    case 5
        playagain = input('Play Again Y/N');
        if(strcmp(playagain,'N') == 1)
            startingmoney
            break;
       
        elseif(strcmp(playagain,'Y') == 1)
            state = 0;
            startingmoney
        else
            startingmoney
            display('Please input Y or N with single quotes')
            state = 5;
        end
        
            
        
end
end

