C++ 我如何找到21点游戏中平均玩家爆发率的正确概率?
我希望能够找到21点游戏中一个普通玩家的出局率。可在以下网站上找到正确的值: 为了实现这一点,我实现了一个card和一个deck类,负责处理卡片和洗牌。在主文件cards.cpp中,我使用参数threshold和iterations创建了一个单独的函数probabilities()。阈值将从10到20,这将决定玩家在突破之前必须达到的总目标。迭代将决定我想重复多少次,以确定玩家在达到阈值之前的崩溃频率。 以下是我的每个阈值的概率: 10: 0.6577 11: 0.849 12: 0.8942 13: 0.8955 14: 0.8777 15: 0.8531 16: 0.8714 17: 0.872 18: 0.8799 19: 0.8443 20: 0.8011 与在网站上找到的值相比,它们非常差。有人能指出我在概率函数中做错了什么吗 主文件:C++ 我如何找到21点游戏中平均玩家爆发率的正确概率?,c++,probability,blackjack,C++,Probability,Blackjack,我希望能够找到21点游戏中一个普通玩家的出局率。可在以下网站上找到正确的值: 为了实现这一点,我实现了一个card和一个deck类,负责处理卡片和洗牌。在主文件cards.cpp中,我使用参数threshold和iterations创建了一个单独的函数probabilities()。阈值将从10到20,这将决定玩家在突破之前必须达到的总目标。迭代将决定我想重复多少次,以确定玩家在达到阈值之前的崩溃频率。 以下是我的每个阈值的概率: 10: 0.6577 11: 0.849 12: 0.8942
#include "deck.h"
using namespace std;
int chooseRand(int a, int b){
srand(time(NULL));
int r = rand()%2;
if(r==0) return a;
else return b;
}
int probabilities(int threshold, int iterations){
Deck deck;
deck.shuffle();
Card currentCard;
int cardVal = 0;
int cardSum = 0;
int bust = 0;
int stand = 0;
for(int i = 0; i<iterations; i++){
for(int j = 0; j<iterations && cardSum<threshold; j++){
currentCard = deck.dealCard(); //
cardVal = currentCard.getVal();
if(deck.remainingCards() < 1) deck.shuffle();
if(cardSum < threshold){
if(cardVal != 1 && cardVal != 11 && cardVal != 12 && cardVal != 13) cardSum += cardVal;
else if(cardVal == 1) cardSum += chooseRand(1,11);
else if((cardVal == 11 || cardVal == 12 || cardVal == 13) && cardSum < threshold) cardSum += 10;
}
else break;
}
if(cardSum > threshold)bust++; cardSum = 0;
}
return (float)(bust);
}
int main(){
Deck deck;
Card currentCard;
int iterations = 10000;
for(int i = 10; i<=20; i++){
cout<<i<<": "<<(float)(probabilities(i, iterations)/10000.0)<<endl;
}
return 0;
}
#包括“deck.h”
使用名称空间std;
int选择器和(int a、int b){
srand(时间(空));
int r=rand()%2;
如果(r==0),则返回a;
否则返回b;
}
int概率(int阈值,int迭代){
甲板;
洗牌();
卡电流卡;
int-cardVal=0;
int cardSum=0;
int-bust=0;
国际标准=0;
对于(int i=0;iYou应该只调用一次srand
。这通常是main
中的第一件事。这是一种洗牌方法。j值不正确。j应该在0到51之间变化,就像IStack Overflow不是免费调试服务一样,您应该显示您尝试使用调试器或其他sim卡调试代码的情况pler方法,如debug print语句。您还可以分别测试代码的每一部分,以确定是哪一部分代码导致了问题,并做出正确的判断。这不会是您代码中出现错误的唯一一次,学习调试程序比让别人帮您查找错误更有帮助。
#ifndef DECK_H
#define DECK_H
#include "card.h"
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
const int NUM_CARDS_DECK = 52;
class Deck
{
public:
Deck();
Card dealCard();
void shuffle();
void printDeck() const;
int remainingCards();
private:
Card *deck; //Create a dynamically allocated array of cards
int currentCard;
int remainCardCount;
};
Deck::Deck(){
string faces[] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King"};
string suits[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
*/
int faces[] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
int suits[] = {1, 2, 3};
deck = new Card[NUM_CARDS_DECK];
currentCard = 0;
//Now fill up the card array with values
for(int i = 0; i<NUM_CARDS_DECK; i++){
deck[i] = Card(faces[i%13], suits[i/13]);
}
}
void Deck:: printDeck() const{
cout<<left;
for(int i = 0; i<NUM_CARDS_DECK; i++){
cout<<setw(19)<<deck[i].print();
if((i+1)%4 == 0){
cout<<endl;
}
}
}
void Deck::shuffle(){
currentCard = 0;
for(int i = 0; i<NUM_CARDS_DECK; i++){
int j = (rand() + time(0)) % NUM_CARDS_DECK;
Card temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
}
int Deck::remainingCards(){
return remainCardCount;
}
Card Deck::dealCard(){
remainCardCount = 52;
if(currentCard > NUM_CARDS_DECK){
shuffle();
remainCardCount = 0;
}
if(currentCard < NUM_CARDS_DECK){
remainCardCount = 51-currentCard;
return (deck[currentCard++]);
}
return(deck[0]);
}
#endif
#ifndef CARD_H
#define CARD_H
#include <string>
#include <iostream>
using namespace std;
class Card{
public:
Card(int cardNum, int cardSuit);
int getVal();//For blackjack
string print() const;
Card();
private:
int num;
int suit;
};
Card::Card(){}
Card::Card(int cardNum, int cardSuit){
num = cardNum;
suit = cardSuit;
}
int Card::getVal(){
return num;
}
string Card::print() const{
return (to_string(num) + "of" + to_string(suit));
}
#endif