create a function that finds a 5 of a kind in poker in matlab

Here's a function that finds 5 of a kind in a hand of poker:

main.m
function five_of_a_kind = check_five_of_a_kind(hand)
% This function takes in a hand of poker represented as a vector of numbers
% and returns true if the hand has five of a kind, false otherwise.

% First, we sort the hand in ascending order
hand = sort(hand);

% We loop through each element in the hand, looking for five consecutive
% elements that are equal
for i = 1:length(hand)-4
    if hand(i)==hand(i+1) && hand(i)==hand(i+2) && hand(i)==hand(i+3) && hand(i)==hand(i+4)
        five_of_a_kind = true;
        return;
    end
end

% If we reach this point, we didn't find five of a kind
five_of_a_kind = false;
end
623 chars
20 lines

To use this function, you can call it with a hand represented as a vector of numbers, like so:

main.m
hand = [2 2 2 2 2]; % five 2's
has_five_of_a_kind = check_five_of_a_kind(hand) % returns true

hand = [10 10 10 10 11]; % four 10's and one 11
has_five_of_a_kind = check_five_of_a_kind(hand) % returns false
207 chars
6 lines

gistlibby LogSnag