MATLAB Answers

simulating a poker game

35 views (last 30 days)
Lewis on 12 Nov 2016
Commented: Lewis on 13 Nov 2016
Hi, I'm a beginner with matlab and decided to set a project for myself to simulate a poker game.
My problem is I don't know how to take the next step which is to qualify the hands. As a summary, this is what my code does so far:
1. creates cell array containing card values (I may later want to input specific hands at some point in the following format: AhKs etc., so I decided not to just use 1:52). also creates a secondary deck whose values will be removed as the hand progresses.
2. deals 2 holecards and removes them from the deck.
3. deals 3 flop cards, removes them from the deck.
4. deals 1 turn card, removes it from deck
5. deals 1 river card, removes it from deck.
My idea to continue is as follows:
1. based on the 7 cards drawn out, create an array for each possible 5-card combination. There should be 21
2. find some way ??? of arranging them from high to low. this is my problem.
3. check to see if there is a combination containing 5 of the same suit (second string value of each card, se we can test to see flushes)
4. check to see if a combo exists with 5 consecutive cards (for straights)
5. check to see duplicate values (first string value), to see pairs, 3 of a kind, 4 of a kind etc.
and I think these 5 extra steps will keep me busy for years, so I'll stop there.
Here is my code so far:
%----------DEFINE THE DECK
DECK = {'Ah' 'Kh' 'Qh' 'Jh' 'Th' '9h' '8h' '7h' '6h' '5h' '4h' '3h' '2h'...
'Ad' 'Kd' 'Qd' 'Jd' 'Td' '9d' '8d' '7d' '6d' '5d' '4d' '3d' '2d'...
'Ac' 'Kc' 'Qc' 'Jc' 'Tc' '9c' '8c' '7c' '6c' '5c' '4c' '3c' '2c'...
'As' 'Ks' 'Qs' 'Js' 'Ts' '9s' '8s' '7s' '6s' '5s' '4s' '3s' '2s'};
% create temporary deck to remove elements from as hand progresses
deck = DECK;
my_holecards = randsample(deck, 2);
hand = [my_holecards]
%find my_holecards in deck and remove them
index_card1 = find(ismember(deck, my_holecards(1)));
index_card2 = find(ismember(deck, my_holecards(2)));
% get my holecards values and suits
card1 = my_holecards{1};
card2 = my_holecards{2};
card1_val = card1(1);
card1_suit = card1(2);
card2_val = card2(1);
card2_suit = card2(2);
%----------DEAL FLOP
flop = randsample(deck, 3);
% find flop in deck and remove each card
% card3 = flop(1); card4 = flop(2); card5 = flop(3);
index_card3 = find(ismember(deck, flop(1)));
index_card4 = find(ismember(deck, flop(2)));
index_card5 = find(ismember(deck, flop(3)));
% get flop values and suits
card3 = flop{1};
card4 = flop{2};
card5 = flop{3};
card3_val = card3(1);
card3_suit = card3(2);
card4_val = card4(1);
card4_suit = card4(2);
card5_val = card5(1);
card5_suit = card5(2);
board = [flop];
hand = [my_holecards board];
%----------DEAL TURN
turn = randsample(deck, 1);
%find flop in deck and remove each card
index_card6 = find(ismember(deck,turn(1)));
card6 = turn{1};
card6_val = card6(1);
card6_suit = card6(2);
board = [flop, turn];
hand = [my_holecards board];
%----------DEAL RIVER
river = randsample(deck, 1);
%find flop in deck and remove each card
index_card7 = find(ismember(deck,river(1)));
card7 = river{1};
card7_val = card7(1);
card7_suit = card7(2);
board = [flop, turn, river];
hand = [my_holecards board];


Sign in to comment.

Accepted Answer

Daniel kiracofe
Daniel kiracofe on 13 Nov 2016
You've got a good start. Here's a few comments
1) Instead of using randsample() and then finding and removing elements from the deck, it might be much easier to simply use randperm(52) to generate a random order of the cards in the deck. You'll only need to do this once, then just grab the cards in order from the deck. e.g.
deck = DECK( randperm(52));
The deck is now shuffled, so your holes cards are just deck{1} and deck{2}. The flop is deck{3:5}, the turn is deck{6} and turn is deck{7} (assuming there is only one player and no burn cards).
Not only is this easier to program, it more closely simulates the actual game. i.e. the dealer shuffles once. dealer does not shuffle between flop and turn.
2) you said that your problem is the sorting. I think the reason why it is hard for you is that you have chosen to represent your data as a 2 character string. Although this is a human readable representation, it's not particularly easy for the computer to manipulate. You could come up with a custom sort routine that understands that 'Q' > 'J' and 'T' > '9', it would be easier to just represent the rank internally as a number between 1 and 13, and then write a small subroutine to translate the internal representation to an external representation. e.g.
if (card.rank == 13)
output = 'A';
else if (card.rank == 12)
output = 'K';
else if (card.rank == 11)
output = 'Q':
Then you can just use matlab's built-in sort routines, which already know how to sort integers just fine.
3) I alluded to this above, but it will also simplify your program if you replace the large number of individual variables with arrays of object variables. i.e. these lines here
card1_val = card1(1);
card1_suit = card1(2);
card2_val = card2(1);
card2_suit = card2(2);
you have 4 separate uniquely named variables here. it would be much better to have a card object, so you can talk about card.val and card.suit. Then card is a single object with 2 members, rather than 2 variables. Then instead of 2 separately named variables, have an array, so you have card(1).suit, card(1).val, card(2).suit, card(2).val. By having more structured data, the rest of the program should easier to write.
Hope this helps.

  1 Comment

Lewis on 13 Nov 2016
Hello, thanks for the detailed response - this gives me more than enough to work with!
I expected that the main problem would be with how I designed the deck, so thanks for giving me some direction on that. I have never built objects so it never crossed my mind but seems very reasonable.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by