poker hand loop scripting help

14 Ansichten (letzte 30 Tage)
Jason Earls
Jason Earls am 30 Mär. 2021
Beantwortet: Image Analyst am 31 Mär. 2021
hey folks,
I have to create a random card function and count the number of pairs or three of a kind I have done that but my lack of knowlege for the next part is slowing me up.
"Write a script that can make N random hands of poker, count the number of hands that contain at least one pair and thereby estimate the possibility of at least one pair occurring"
I dont know how to code the make N random number of hands in this code.
I have done it by making while(ct <=15) this will produce 3 hands.
I also tried a prompt function but the issue i was having was the print function if for example I chose 3 hands I would have to manually program like something like this.
hand_1 = [card(1),card(2),card(3),card(4),card(5)]
hand_2 = [card(6),card(7),card(8),card(9),card(10)]
hand_3 = [card(11),card(12),card(13),card(14),card(15)]
but if i chose 4 hands it would give an error.
is there a simple way to create N number of hands and print them out instead of what I am doing. I would like to integrate it into my current code.
-------------------------------------------------------------------------
clear all
close all
clc
%-------------------------------
%prompt = 'What is the number of hands? ';
%Num_Hands = input(prompt);
ct = 1 ;
%---------------------------
%Cards_per_hand = 5; % there is 5 cards per hand
%Num_card_hand= Num_Hands*Cards_per_hand; % number of hands is how mnay cards used
while(ct <=15)
card(ct) = 52*rand() ; % Produce a number (real-valued) between 0 and 52
card(ct) = ceil(card(ct)) ; % Round up to nearest integer
% Now, as the above represents sampling with replacement (and we require
% sampling without replacement) we need to remove duplicate cards...
ct1 = 1 ;
while(ct1 < ct) %step through previous cards
if( (card(ct) == card(ct1) ))
ct1 = 6 ;% Leave the for loop by forcing ct1 to greater than 5
ct = ct-1 ; % This will cause for loop to step through this iteration again, overwriting duplicate card
end
ct1 = ct1+ 1 ;
end
ct = ct + 1 ;
end
hand_1 = [card(1),card(2),card(3),card(4),card(5)]
hand_2 = [card(6),card(7),card(8),card(9),card(10)]
hand_3 = [card(11),card(12),card(13),card(14),card(15)]
fprintf(1,'Hand 1: %d %d %d %d %d \n',hand_1)
fprintf(1,'Hand 2: %d %d %d %d %d \n',hand_2)
fprintf(1,'Hand 3: %d %d %d %d %d \n',hand_3);
num_pairs = 0; %init num pairs
num_three_kind = 0; % init num 3 of kind
for (pairs = 1:4) % create a loop to check against other 4 cards
for (card_num=pairs+1:5) %from 1st card to last
if (mod(card(pairs) - card(card_num) , 13) == 0) % check for multiple of 13 for pair
num_pairs=num_pairs+1 % add one to number of pairs if more than 1
if (pairs < 4) % check remaining cards for 3 of kind
for (card_num1 = card_num +1:5) % again 1 to last card
if (mod(card(pairs) - card(card_num1) , 13) == 0) % check for multple of 13
num_three_kind =num_three_kind+1 %add one to number of 3 of kind if more than 1
end
end
end
end
end
end
fprintf(1,'The number of pairs is: %d\n',num_pairs)
fprintf(1,'The number of 3 of kind is: %d\n',num_three_kind)

Antworten (1)

Image Analyst
Image Analyst am 31 Mär. 2021
For what it's worth (not that it will solve your problem, but it's fun), I'm attaching my Monte Carlo card dealing program that counts the number and percentage of all worthwhile poker hands. It only does it for one hand but deals millions of hands to get the percentages.
% Finds frequency of 5 card poker hands
% Reference
% https://en.wikipedia.org/wiki/Poker_probability#Frequency_of_5-card_poker_hands
% Theory says
% One pair 42.2569%
% Two pair 4.7539%
% Three of a kind 2.1128%
% Straight (excluding royal flush and straight flush) 0.3925%
% Flush (excluding royal and straight) 0.1956%
% Full house 0.1441%
% Four of a kind 0.0240%
% Straight Flush (excluding royal flush) 0.00139%
% Royal Flush = 0.000154%
Found 422411 "One Pair" in 1000000 hands. That is one in every 2 hands.
Percentage of "One Pair" = 42.241100%. Theory says 42.2569%
Found 47995 "Two Pairs" in 1000000 hands. That is one in every 21 hands.
Percentage of "Two Pairs" = 4.799500%. Theory says 4.7539%
Found 20894 "3 of a kind" in 1000000 hands. That is one in every 48 hands.
Percentage of "3 of a kind" = 2.089400%. Theory says 2.1128%
Found 3689 straights in 1000000 hands. That is one in every 271 hands.
Percentage of straights = 0.368900%. Theory says 0.3925%
Found 1870 Flushes (excluding straight and royal) in 1000000 hands. That is one in every 535 hands.
Percentage of Flushes = 0.187000%. Theory says 0.1956%
Found 1427 Full Houses in 1000000 hands. That is one in every 701 hands.
Percentage of Full Houses = 0.142700%. Theory says 0.1441%
Found 247 "4 of a kind" in 1000000 hands. That is one in every 4049 hands.
Percentage of "4 of a kind" = 0.024700%. Theory says 0.0240%
Found 11 straight flushes (excluding royal) in 1000000 hands. That is one in every 90909 hands.
Percentage of straight flushes = 0.001100%. Theory says 0.00139%.
Found 2 Royal Flushes in 1000000 hands. That is one in every 500000 hands.
Percentage of Royal Flushes = 0.000200%. Theory says 0.000154%

Kategorien

Mehr zu Card games finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by