Problem 386. Poker Series 10: bestHand
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.

Find the best hand using the definitions from earlier in this series.
out.code is:
 Straight Flush
 Quads
 Full House
 Flush
 Straight
 Three of a kind
 Two pair
 Pair
 High Card
 No valid Hand
out.cardsUsed is the same as is returned from the functions defined earlier for each type of hand.
This hand matrix:
0 1 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 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0
represents a three of a kind, so the return code is 6.
Solution Stats
Problem Comments

10 Comments
I believe the example is a Straight instead?
agreed
Test 6 and 7 are the same (no actual problems, just saying).
I removed the extra (duplicate) test case that Albert mentioned.
How annoying of this one, is truly beyond my imagination...
@Bainhome, recently I read a discussion on Mathworks MATLAB Answers [https://www.mathworks.com/matlabcentral/answers/27340whatdoyouthinkofcodynewserviceformatlabcentral]. According to the community member, Jan; "Cody players learn to use MATLAB inefficiently", hence, he would prefer the top ten Answers or FileExchange contributors to the top ten Cody contributors for a serious project. Clearly, problems such as Doug Hull's poker series and Grant's Scrabble series prove otherwise. So, even though, "all the subfunction were already solved in previous problem" as noted by you, bringing them together in an efficient manner might still be required. Infact, this is actually the case with the top scores. In summary, I think this particular problem is great for Cody players. :)
@J. S. Kowontan, I just read those information about the Jan's opinions on code MATLAB more effciently. and for this besthand problem, I think I missed something critical, THX for the heads up, later I will improved my lazy and horrible code. However, I only partially agree with Jan. size is no doubt a controverial topic, but people's learning skill, basic programming concept can be built either in cody's way or answer's way, or any other way, top ten is bit of drama queen stuff, but when we talked about who is the most suitable programme project charger, I can guarantee those guys like alfonso, Tim, you, yurenchu, Liupeng, LY Cao and my friend binbin are both qualified. as matter of fact and I know that for sure: lots of them already have their own project, and they seem doing just fine. My opinion is: people's mind can be shaped by "real world", envolved with demand.
Hum, Kowontan. I believe this series proves actually the opposite. Many of the functions in this series should have been used as a constructor of others. For instance the function isHighCard should have been used at the functions isQuads, isThreeKind and isTwopair. Moreover, this variable usedCards is completely useless since now at this problem we are replacing it with a number! This should have been done at the very beginning, since this number will define which hand wins. This choice of variables and sequence of problems unfortunately demonstrates poor design and programming choices. It's overall a good series, but not to teach people the right way to code.
And, bainhome, unfortunately, LY Cao used a hack to solve this problem, so it is not a very good example although he seems to be a good programmer. And probably everyone you mentioned already used hacking at Cody (some I know for sure, like Alfonso). Hacking is cool (and a great tool for learning or testing things), but it is not really a good programming practice.
When we want to teach someone, we don't show them a poor design, and ask them to do something a little better, we show them one of the best solutions/designs, and challenge them to do better. At the very least, if they can't, they will know one of the best ways to do it (knowledge they will use in real life).
Solution Comments
Show commentsProblem Recent Solvers42
Suggested Problems

Find state names that start with the letter N
1172 Solvers

How long is the longest prime diagonal?
391 Solvers

744 Solvers

12517 Solvers

162 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!