simulating a poker game
Afficher commentaires plus anciens
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;
%----------DEAL HOLECARDS
my_holecards = randsample(deck, 2);
hand = [my_holecards]
%find my_holecards in deck and remove them
index_card1 = find(ismember(deck, my_holecards(1)));
deck(index_card1)=[];
index_card2 = find(ismember(deck, my_holecards(2)));
deck(index_card2)=[];
deck;
% 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)));
deck(index_card3)=[];
index_card4 = find(ismember(deck, flop(2)));
deck(index_card4)=[];
index_card5 = find(ismember(deck, flop(3)));
deck(index_card5)=[];
% 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)));
deck(index_card6)=[];
deck;
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)));
deck(index_card7)=[];
deck;
card7 = river{1};
card7_val = card7(1);
card7_suit = card7(2);
board = [flop, turn, river];
hand = [my_holecards board];
Réponse acceptée
Plus de réponses (1)
Sahil
le 18 Sep 2022
Modifié(e) : Walter Roberson
le 18 Sep 2022
%----------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;
%----------DEAL HOLECARDS
my_holecards = randsample(deck, 2);
hand = [my_holecards]
%find my_holecards in deck and remove them
index_card1 = find(ismember(deck, my_holecards(1)));
deck(index_card1)=[];
index_card2 = find(ismember(deck, my_holecards(2)));
deck(index_card2)=[];
deck;
% 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)));
deck(index_card3)=[];
index_card4 = find(ismember(deck, flop(2)));
deck(index_card4)=[];
index_card5 = find(ismember(deck, flop(3)));
deck(index_card5)=[];
% 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)));
deck(index_card6)=[];
deck;
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)));
deck(index_card7)=[];
deck;
card7 = river{1};
card7_val = card7(1);
card7_suit = card7(2);
board = [flop, turn, river];
hand = [my_holecards board];
board
hand
Catégories
En savoir plus sur Card games dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!