create a while loop to play poker until a full house in matlab

main.m
% Initialize variables
deck = 1:52;
suits = ["Hearts", "Diamonds", "Spades", "Clubs"];
values = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"];
playerHand = zeros(1,5);

% While loop to play poker until a full house is obtained
while true
    % Shuffle deck
    deck = deck(randperm(length(deck)));
    
    % Deal player hand
    playerHand = deck(1:5);
    
    % Determine if player hand is a full house
    if isFullHouse(playerHand, values)
        disp("Congratulations! You have a full house!");
        break;
    end
end

function result = isFullHouse(hand, values)
    % Convert hand to values
    handValues = zeros(1,5);
    for i = 1:length(hand)
        handValues(i) = mod(hand(i)-1,13)+1;
    end
    
    % Count number of cards with each value
    valueCounts = zeros(1,13);
    for i = 1:length(handValues)
        valueCounts(handValues(i)) = valueCounts(handValues(i)) + 1;
    end
    
    % Check for full house
    result = any(valueCounts == 3) & any(valueCounts == 2);
end
1031 chars
38 lines

This code initializes variables for a deck of cards, suits, and values. It then sets up a while loop to repeatedly shuffle and deal a player's hand, and check if the hand is a full house using the isFullHouse function. If the hand is a full house, the loop breaks and a congratulatory message is displayed. If not, the loop continues until a full house is obtained. The isFullHouse function converts a hand to its corresponding values, counts the number of cards with each value, and checks if the hand contains three cards of one value and two cards of another value (a full house).

gistlibby LogSnag