Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
function value = evaluateHand(hand)
	possibilites = {@isNothing, @isPair, @isTwoPairs, @isTrips, ...
    @isStraight, @isFlush, @isFullHouse, @isQuads, @isStraightFlush};

    for poss = possibilites
        [isValid, name] = poss{1}(hand);
        if isValid
            value = name;
            break
        end
    end
end

function list = getNumbers(hand)
    list = sort([hand.value]);
end

function list = getSuits(hand)
    list = sort([hand.suit]);
end

function [bool, name] = isNothing(hand)
    copy = hand;
    hand = getNumbers(hand);
    bool = length(unique(hand)) == 5 && ~isStraight(copy) && ~isFlush(copy);
    name = 'High Card';
end

function [bool, name] = isPair(hand)
    bool = length(unique(getNumbers(hand))) == 4;
    name = 'Pair';
end

function [bool, name] = isTwoPairs(hand)
    hand = getNumbers(hand);
    bool = length(unique(hand)) == 3 ...
        && sum(mode(hand) == hand) == 2;
    name = 'Two Pairs';
end

function [bool, name] = isTrips(hand)
    hand = getNumbers(hand);
    bool = length(unique(hand)) == 3 ...
        && sum(mode(hand) == hand) == 3;
    name = 'Trips';
    end

function [bool, name] = isStraight(hand)
    copy = hand;
    hand = getNumbers(hand);
    bool = false;
    if length(unique(hand)) == 5
        bool = hand(5) - hand(1) == 4;
        if sum(hand == 14) == 1
            hand(hand == 14) = 1;
            hand = sort(hand);
            bool = bool || hand(5) - hand(1) == 4;
        end
        if bool && isFlush(copy)
            name = 'Straight Flush';
            return
        end

    end
    name = 'Straight';
end

function [bool, name] = isFlush(hand)
    bool = length(unique(getSuits(hand))) == 1;
    name = 'Flush';
end

function [bool, name] = isFullHouse(hand)
    hand = getNumbers(hand);
    bool = length(unique(hand)) == 2 ...
        && sum(mode(hand) == hand) == 3;
    name = 'Full House';
end

function [bool, name] = isQuads(hand)
    hand = getNumbers(hand);
    bool = sum(mode(hand) == hand) == 4;
    name = 'Quads';
end

function [bool, name] = isStraightFlush(hand)
    bool = isStraight(hand) && isFlush(hand);
    name = 'Straight Flush';
end

 

de)

Code Block
function data = main(num)
    data = strings(1, num);
    for i = 1 : num
        hand = randomHand(5);
        value = getValue(hand);
        data(i) = string(value);
    end
    names = ["High Card", "Pair", "Two Pairs", "Trips", "Straight", "Flush", "Full House", "Quads", "Straight Flush"];
    for name = names
        num = sum(data == name);
        fprintf('%s occurred %i times. That is %.3f%%\n', name, num, 100 * num/length(data)); 
    end
end

...