Problem 391. Poker Series 11: selectBestHand
The Poker Series consists of many short, well defined functions that when combined will lead to complex behavior. Our goal is to create a function that will take two hand matrices (defined below) and return the winning hand.
A hand matrix is 4x13 binary matrix showing the cards that are available for a poker player to use. This program will be expandable to use 5 card hands through 52 card hands! Suits of the cards are all equally ranked, so they only matter for determination of flushes (and straight flushes).
For each challenge, you should feel free to reuse your solutions from prior challenges in the series. To break this problem into smaller pieces, I am likely making architectural choices that are suboptimal for speed. This is being done as an exercise in coding. The larger goal of this project can likely be done in a much faster, but more obscure way.

Select the best hand using the definitions from earlier in this series.
out.hm{1} = cards used from the first hand matrix; out.hm{2} = cards used from the second hand matrix; out.winner = 1,2 or zero for a tie;
Standard poker ranks apply: Straight flush is best, high card is worst, with many gradations within each rank.
Solution Stats
Problem Comments

15 Comments
that took a really long time to do...
i think that it might be beneficial to return more information in the subfunctions such as the kicker(s) and the values of the cards themselves.
It's not that hard, using a modified hand card representation (sorting columns as A,K...2 and adding a repeated A for the straights) makes the comparison of two hands trivially simple (sortrows)
What i believe might have been beneficial would be to have the hand cards columns sorted by decreasing card value like its rows since all computations look much cleaner that way ...
scratch the above, it does need more work (and we need more test samples)
When deciding the best hand among two hands of the same rank (e.g. both players have a pair of 5's) what is more important, the suits of the hand or the rank of the kicker(s)? (i guess the latter but the test suite does not discriminate)
perhaps you could add these tests (most of the current solvers fail one or the other): TEST1: hm1=[ 0 0 0 0 0 0 0 0 0 1 0 1 0; 0 0 0 0 0 0 0 0 0 0 0 1 0; 0 0 0 0 0 0 0 0 0 1 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 1 0];
hm2=[ 0 0 0 0 0 0 0 0 0 0 0 0 0; 1 0 0 0 0 0 0 0 0 0 0 1 0; 1 0 0 0 0 0 0 0 0 0 0 1 0; 0 0 0 0 0 0 0 0 0 0 0 1 0];
out.hm{1} = hm1;
out.hm{2} = hm2;
out.winner = 2;
TEST2: hm1=[ 0 0 0 1 1 1 1 1 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0];
hm2=[ 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 1 1 1 1 1 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0];
out.hm{1} = hm1;
out.hm{2} = hm2;
out.winner = 0;
hm1=[1 0 0 0 0 0 0 0 0 0 0 0 1;1 0 0 0 0 0 0 0 0 0 0 0 0;1 0 0 0 0 0 0 0 0 0 0 0 0;1 0 0 0 0 0 0 0 0 0 0 0 0];
hm2=[1 0 0 0 0 0 0 0 0 0 0 0 0;1 0 0 0 0 0 0 0 0 0 0 0 0;1 0 0 0 0 0 0 0 0 0 0 0 0;1 0 0 0 0 0 0 0 0 0 0 0 1];
Is player 1 supposed to win in this case?
On a second thought.. shouldn't there be only one deck?
@albert: your example should be a tie (the card suits do not break ties; in fact they are not used at all for scoring). And I believe there is no limitation on the number of decks the hand cards come from...
Test case 7 is broken, the case is expecting matrices of type double in the output, but all other cases want matrices of type logical.
Test case 7 is confusing.
hm2 = [1 1 1 1 1 1 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0];
om2 = [0 1 1 1 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0];
1. Why the StraightFlush including Ace is not considered as the BestHand for hm2?
2. How could the winner be 2, since the first player has Ace in its BestHand?
Seconding both Athi's & Andrew's posts
Player 2 has an ace in his hand but chooses to play 2 3 4 5 6
That's backwards
also, the double thing is 100% true. why switch from logical to double
Many of the issues could have been easily solved with a better design choice (for instance, cases 5 and 9 are only an issue because the functions that identify both hands do not store the highest cards values). Storing the array usedCards is not useful, it would be best to store a number representing the hand, and the values of the highest cards or a number representing them.
For those asking why 23456 beats A2345: in poker, the rank of a straight is determined by the card at the high end. So 23456 beats A2345 because 6 beats 5. The ace becomes a low card.
smartest noncheating solution is scored 480? omegaLOL
Solution Comments
Show commentsProblem Recent Solvers35
Suggested Problems

Find state names that start with the letter N
1191 Solvers

3002 Solvers

340 Solvers

Is the paranthesis sequence balanced ?
160 Solvers

4366 Solvers
More from this Author51
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!