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
functionfive_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 orderhand = sort(hand);
% We loop through each element in the hand, looking for five consecutive% elements that are equalfori = 1:length(hand)-4if 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;
endend% If we reach this point, we didn't find five of a kindfive_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 = [22222]; % five 2'shas_five_of_a_kind = check_five_of_a_kind(hand) % returns truehand = [1010101011]; % four 10's and one 11has_five_of_a_kind = check_five_of_a_kind(hand) % returns false