Program not calculating properly

조회 수: 2 (최근 30일)
TheSaint
TheSaint 2024년 2월 26일
답변: Image Analyst 2024년 2월 26일
function PokerGame()
clc
clear
RunTotal = 100000;
CardsPerHand = 5;
NoPair = 0;
OnePair = 0;
TwoPairs = 0;
ThreeofKind =0;
FullHouse = 0;
FourofKind = 0;
FiveofKind = 0;
for i = 1:RunTotal
Cards = sort(randperm(52, CardsPerHand));
Deck = zeros(1, 52);
Deck(Cards) =1;
Deck = reshape(Deck, [13,4]);
Val= sum(Deck, 2);
%Here, I will check for no pairs
if sum(Val ==2) == 0&& sum(Val== 3) == 0
NoPair = NoPair + 1;
end
%Here, I will check for one pairs
if sum(Val == 2) == 1 && sum(Val ==3) ==0
OnePair = OnePair + 1;
%Here, I will check for two pairs
if sum(Val == 2) ==2
TwoPairs = TwoPairs + 1;
%Here, I will check for 3 of a kind
end
if any(Val ==3) && sum(Val ==2) ==0
ThreeofKind = ThreeofKind + 1;
end
%Here, I will check for Full houses
if sum(Val ==2) ==1 && sum(Val ==3)==1
FullHouse = FullHouse + 1;
end
%Here, I will check for four of a kind
if any(Val ==4)
FourofKind = FourofKind + 1;
end
%Here, I will check for five of a kind
if any(Val ==5)
FiveofKind = FiveofKind + 1;
end
end
end
%Now I will print the results
fprintf('The percentage chance of getting a no pair in a game of poker is %0.1f%%. \n', 100*NoPair/RunTotal);
fprintf('The percentage chance of getting a one pair in a game of poker is %0.1f%%. \n', 100*OnePair/RunTotal);
fprintf('The percentage chance of getting a two pair in a game of poker is %0.1f%%. \n', 100*TwoPairs/RunTotal);
fprintf('The percentage chance of getting a three of a kind in a game of poker is %0.1f%%. \n', 100*ThreeofKind/RunTotal);
fprintf('The percentage chance of getting a full house in a game of poker is %0.1f%%. \n', 100*FullHouse/RunTotal);
fprintf('The percentage chance of getting a four of a kind in a game of poker is %0.1f%%. \n', 100*FourofKind/RunTotal);
fprintf('The percentage chance of getting a three of a kind in a game of poker is %0.1f%%. \n', 100*ThreeofKind/RunTotal);
end
This is my code for a poker game. It successfully prints the first two odds for No Pair and One Pair, but for the rest, it just says 0. Can anyone help me figure out as to why it only prints the first two properly?

답변 (2개)

Image Analyst
Image Analyst 2024년 2월 26일
Looks like you just took my code that I gave you in your other question and made a few changes to make it not work. I suggest you undo those changes and it will work again. I've validated it against the known percentages so I know it works.
% Finds frequency of 5 card poker hands.
% By Image Analyst
% 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%
function MonteCarloCardDealing()
% Simple Monte Carlo card dealing to determine percentage of 4-of-a-kinds, straights, royal flushes, etc.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
% Define parameters.
numberOfHands = 1000000 % i.e. # persons playing cards.
numCardsPerHand = 5 % # cards each person holds
% Initialize counts for the different types of hands.
onePairs = 0;
twoPairs = 0;
fourOfAKind = 0;
threeOfAKind = 0;
straights = 0;
fullHouses = 0;
royalFlushes = 0;
straightFlushes = 0;
flushes = 0;
% Monte Carlo simulation:
for h = 1 : numberOfHands
% Get the cards. Each card is numbered from 1 - 52
% 2 is card 1, and Ace is card 13 (for ease of testing for Royal Flushes).
cards = sort(randperm(52, numCardsPerHand));
% Create a full deck map
% Columns represent suits, and rows are the values.
fullDeck = zeros(1, 52);
fullDeck(cards) = 1;
fullDeck = reshape(fullDeck, [13, 4]);
% values = mod(cards, 13);
% suits = floor(cards/4) + 1;
% Find the number of each value by summing horizontally.
values = sum(fullDeck, 2);
% Check for one pair : for example
% 8 of spades, 8 of clubs.
% But have to exclude the full house hand because that is counted separately.
if sum(values == 2) == 1 && sum(values == 3) == 0
onePairs = onePairs + 1;
end
% Check for two pairs : for example
% 8 of spades, 8 of clubs, and 10 of hearts and 10 of diamonds.
if sum(values == 2) == 2
twoPairs = twoPairs + 1;
end
% Check for three of a kind, for example :
% 8 of spades, 8 of clubs, and 8 of diamonds.
% But have to exclude the full house hand because that is counted separately.
if any(values == 3) && sum(values == 2) == 0
threeOfAKind = threeOfAKind + 1;
end
% Check for straight : consecutive numbers but not all in one suit (which would be a straight flush or royal flush).
% 3 of hearts, 4 of spades, 5 of clubs, 6 of diamonds, and 7 of hearts.
if ~isempty(strfind(values', [1,1,1,1,1])) % They're in numerical order
if ~any(sum(fullDeck, 1) == 5)
% If it's not a straight, i.e. all 5 cards are not in the same suit.
straights = straights + 1;
end
end
% Check for full house : two of one value and 3 of another value.
% 8 of spades, 8 of clubs, and 10 of hearts, 10 of clubs, and 10 of diamonds.
if sum(values == 2) == 1 && sum(values == 3) == 1
fullHouses = fullHouses + 1;
end
% Check for four of a kind, for example :
% 7 of spades, 7 of clubs, 7 of hearts, and 7 of diamonds.
if any(values == 4)
fourOfAKind = fourOfAKind + 1;
end
% Check for flushes: where all 5 cards are one suit.
if any(sum(fullDeck, 1) == 5)
% Check for a 5-card straight flush. Must be in the same suit.
% and be consecutive values, like 7, 8, 9, 10, Jack.
% We can count by doing a 2D convolution with the proper kernel.
kernel = [1;1;1;1;1];
cardCounts = conv2(fullDeck, kernel, 'valid'); % Count cards in 5 consecutive numbers.
% First check for Royal flush: 10, Jack, Queen, King, Ace
% If this happens, in the last row of cardCounts there will be a 5.
royalFlush = any(cardCounts(end, :) == 5);
% Next, see if there are any runs of 5 anywhere
straightFlush = any(cardCounts(:) == 5);
if royalFlush
royalFlushes = royalFlushes + 1;
elseif straightFlush
% For example, cards = [3,4,5,6,7] in any single suit would be a straight flush.
straightFlushes = straightFlushes + 1;
else
% It's a flush but not a straight flush or royal flush.
% It could be any 5 random, unordered cards of the same suit.
% For example, cards = [3,4,5,6,10] in any single suit would be a flush.
% Count flushes that are not royal flushes or straight flushes.
flushes = flushes + 1;
end
end
if mod(h, 10000) == 0
fprintf('Done dealing and inspecting hand #%d of %d = %5.1f%%\n', h, numberOfHands, 100 * h / numberOfHands);
end
end
% Print results to command window.
fprintf('\nFound %d "One Pair" in %d hands. That is one in every %d hands.', onePairs, numberOfHands,round(numberOfHands/onePairs));
fprintf('\nPercentage of "One Pair" = %f%%. Theory says 42.2569%%\n', 100*onePairs/numberOfHands);
fprintf('\nFound %d "Two Pairs" in %d hands. That is one in every %d hands.', twoPairs, numberOfHands,round(numberOfHands/twoPairs));
fprintf('\nPercentage of "Two Pairs" = %f%%. Theory says 4.7539%%\n', 100*twoPairs/numberOfHands);
fprintf('\nFound %d "3 of a kind" in %d hands. That is one in every %d hands.', threeOfAKind, numberOfHands, round(numberOfHands/threeOfAKind));
fprintf('\nPercentage of "3 of a kind" = %f%%. Theory says 2.1128%%\n', 100*threeOfAKind/numberOfHands);
fprintf('\nFound %d straights in %d hands. That is one in every %d hands.', straights, numberOfHands, round(numberOfHands/straights));
fprintf('\nPercentage of straights = %f%%. Theory says 0.3925%%\n', 100*straights/numberOfHands);
fprintf('\nFound %d Flushes (excluding straight and royal) in %d hands. That is one in every %d hands.', flushes, numberOfHands, round(numberOfHands/flushes));
fprintf('\nPercentage of Flushes = %f%%. Theory says 0.1956%%\n', 100*flushes/numberOfHands);
fprintf('\nFound %d Full Houses in %d hands. That is one in every %d hands.', fullHouses, numberOfHands, round(numberOfHands/fullHouses));
fprintf('\nPercentage of Full Houses = %f%%. Theory says 0.1441%%\n', 100*fullHouses/numberOfHands);
fprintf('\nFound %d "4 of a kind" in %d hands. That is one in every %d hands.', fourOfAKind, numberOfHands, round(numberOfHands/fourOfAKind));
fprintf('\nPercentage of "4 of a kind" = %f%%. Theory says 0.0240%%\n', 100*fourOfAKind/numberOfHands);
fprintf('\nFound %d straight flushes (excluding royal) in %d hands. That is one in every %d hands.', straightFlushes, numberOfHands, round(numberOfHands/straightFlushes));
fprintf('\nPercentage of straight flushes = %f%%. Theory says 0.00139%%.\n', 100*straightFlushes/numberOfHands);
fprintf('\nFound %d Royal Flushes in %d hands. That is one in every %d hands.', royalFlushes, numberOfHands, round(numberOfHands/royalFlushes));
fprintf('\nPercentage of Royal Flushes = %f%%. Theory says 0.000154%%\n', 100*royalFlushes/numberOfHands);

Sai Teja G
Sai Teja G 2024년 2월 26일
Hi,
It looks like there are some logical issues in your code that are preventing the correct calculation of poker hand probabilities, can you refer to the below refactored code.
function PokerGame()
clc
clear
RunTotal = 100000;
CardsPerHand = 5;
NoPair = 0;
OnePair = 0;
TwoPairs = 0;
ThreeofKind = 0;
FullHouse = 0;
FourofKind = 0;
for i = 1:RunTotal
Cards = sort(randperm(52, CardsPerHand));
Deck = zeros(1, 52);
Deck(Cards) = 1;
Deck = reshape(Deck, [13, 4]);
Val = sum(Deck, 2);
% Check for No Pair
if sum(Val == 1) == CardsPerHand
NoPair = NoPair + 1;
end
% Check for One Pair
if sum(Val == 2) == 1 && sum(Val >= 3) == 0
OnePair = OnePair + 1;
end
% Check for Two Pairs
if sum(Val == 2) == 2
TwoPairs = TwoPairs + 1;
end
% Check for Three of a Kind
if sum(Val == 3) == 1 && sum(Val ~= 2) == CardsPerHand - 3
ThreeofKind = ThreeofKind + 1;
end
% Check for Full House
if sum(Val == 3) == 1 && sum(Val == 2) == 1
FullHouse = FullHouse + 1;
end
% Check for Four of a Kind
if sum(Val == 4) == 1
FourofKind = FourofKind + 1;
end
end
% Print the results
fprintf('The percentage chance of getting a no pair in a game of poker is %0.1f%%. \n', 100 * NoPair / RunTotal);
fprintf('The percentage chance of getting a one pair in a game of poker is %0.1f%%. \n', 100 * OnePair / RunTotal);
fprintf('The percentage chance of getting a two pair in a game of poker is %0.1f%%. \n', 100 * TwoPairs / RunTotal);
fprintf('The percentage chance of getting a three of a kind in a game of poker is %0.1f%%. \n', 100 * ThreeofKind / RunTotal);
fprintf('The percentage chance of getting a full house in a game of poker is %0.1f%%. \n', 100 * FullHouse / RunTotal);
fprintf('The percentage chance of getting a four of a kind in a game of poker is %0.1f%%. \n', 100 * FourofKind / RunTotal);
end

카테고리

Help CenterFile Exchange에서 Card games에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by