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 sub-optimal 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.
--------
A straight flush is 5 cards of the same suit (row) and five continuous ranks (columns). The Ace (first column) can also make a straight flush when combined with the last four columns. The columns represent A, 2, 3, ... K.
This hand matrix:
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
represents a straight flush, so the return value from the function is TRUE.
This hand matrix does not:
0 1 1 0 1 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 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
so the return value should be FALSE.
This hand matrix does represent a straight flush
1 0 0 0 0 0 0 0 0 1 1 1 1
0 0 0 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 0 0 0 1 0 0 0 0 0 0 0
Remember, hand matrices can contain any number of 1's from 0 to 52.
0 1 1 0 1 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 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0
Would be FALSE for this function.
A second output argument should come from this function. It is a usedCards Matrix. It is of the same form as the hand Matrix, but it only shows the cards used to make the Straight Flush. If more than one straight flush can be made, return the higher ranking one (the one with the highest top card. Ace being the highest). If different suits are possible for the same straight flush, return the one higher up in the matrix.
Solution Stats
Problem Comments
8 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers121
Suggested Problems
-
3808 Solvers
-
Set some matrix elements to zero
621 Solvers
-
192 Solvers
-
Determine the number of odd integers in a vector
820 Solvers
-
446 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!
define "higher up", is row 1 higher or lower than row 4?
Don't test with with 2 or more straight flushes with different rankings.
@Jean,
Huh? Is that a suggestion to add a test?
Yes, as for example test 8 in problem 333 : Poker Series 02: isQuads.
It is better to modify the last test case as hm = [0 0 0 0 0 0 0 0 1 1 0 1 1;
1 1 0 0 0 0 0 0 0 1 1 1 1;
1 0 0 0 0 0 0 0 0 1 1 1 1;
0 0 0 0 0 0 0 0 0 0 0 0 0];
y_correct.flag = true;
y_correct.usedCards = logical([0 0 0 0 0 0 0 0 0 0 0 0 0;
1 0 0 0 0 0 0 0 0 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]), where I have interchanged the 2nd and 3rd rows of hm. The reason to make this change is that some solutions which simply check whether the row sum equals 5 are indeed incorrect.
Very late here since the last comment, but if you care to update, my code passed all the tests but it ran into an error when dealing with a normal flush. May want to put a test in there for five 1's in a single row but not consecutive
So, just to clarify, because in normal poker, 9 10 J Q K beats A 2 3 4 5, at least I think and an online simulator proved that to be correct...
in your case, A high beats K high? I might need to analyze the expected answers a bit but you did say in your problem statement that A high trumps, so I just wanted to check
If you are starting the Poker Series, consider including a score for the hand in your code. Problems 1-9 will allow you to identify the hand types (flush, straight, quads, etc.) in problem 10, but in problem 11 you will need a way to break ties between two straights, two full houses, two flushes, etc. For this problem, for example, the score could simply be the high card of the straight flush.