Calculating poker hand probabilities
2 次查看(过去 30 天)
显示 更早的评论
What I have is a 5x10000 matrix y that is supposed to represent 5 card poker hands. The first character is the card and the second character is the suit. It looks like
AH QS ...
4C 3C ...
3D AD ...
2S 5D ...
TD AS ...
For example, if I want to find how many straights there are, I need to search each column for an A,2,3,4,5 or 2,3,4,5,6, or 3,4,5,6,7 and so on. I can't quite figure out to do that.
0 个评论
回答(1 个)
John D'Errico
2015-7-6
That is NOT a 5x10000 matrix, unless it is a cell array, each cell of which contains a two element character string.
Far simpler to convert the characters to numbers. So 'A' = 1, '2' = 2, ..., 'K' = 13. Then convert the suits to numbers too. So we would have a mapping like this:
'AC' --> [1 1]
'5H' --> [5 3]
It is far easier to work with pure numbers, and the mapping is trivial to do. I'd probably do it using a look up table. Or you could write a simple function that would work. Then use cellfun to apply it to each cell (assuming I am correct in how these cards are stored.) Trivial in any case.
Once this is done, I would tend to convert the array into a 5x2x10000 pure numeric array. Cells are more work than you need here.
Then do a sort. That will make the tests even more trivial. For example, the first hand you show
AH
4C
3D
2S
TD
converts to (I won't write the code to do the mapping for you, sorry):
Hand1 = [1 3
4 1
3 2
2 4
10 2];
Now, you would need to write code to search for a flush, for example, or one pair, two pair, 3 of a kind, etc.
How would you find if a hand contains ONE pair, and no more than that? What would it mean if this result
diff(sort(Hand1(:,1)))
ans =
1
1
1
6
contained exactly one zero? (Yes, that would represent exactly one pair.)
How about a straight? A straight must have all 5 cards in order, ignoring the suit. So then the result of the same computation above must have been a vector of 4 ones.
You should be able to see how to find out if you have a flush, a straight flush, 3 of a kind, etc.
This is your homework, so I won't write the code for you, but just start writing code.
2 个评论
John D'Errico
2015-7-6
Well, it is time for you to start learning MATLAB then. You said yourself something about a 5x10000 array. What do you think a 5x2x10000 array would be?
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!