gpt4 book ai didi

C++ 需要帮助遍历卡片、定位对子和同花顺

转载 作者:行者123 更新时间:2023-11-28 08:19:03 25 4
gpt4 key购买 nike

下面的程序假设正在寻找“对子”和“同花”。它迭代 10 次试验,包括 10,000 手牌,每手牌由 5 张牌组成。结果应该(当然现在不是)由 10 行组成,反射(reflect)每个试验的独特结果。我卡住了...提前致谢。

#include "card.h"
#include "deck.h"
#include "game1.h"
#include <iostream>
#include <time.h>
#include <stdlib.h>

using namespace std;


int main() {

int pair = 0;
int flush = 0;
int h; //Hands
int c; //Cards
int t; //Trials

const int MAXTRIALS = 10;
const int MAXHANDS = 10000;
const int MAXCARDS = 5;
const int MAXSHUFFLE = 100;

Deck myDeck;
Card myCards[MAXCARDS];
myDeck.shuffle(MAXSHUFFLE); //How often would you shuffle?

srand((unsigned)time(NULL)); //Randon initilizer

for (t = 0 ; t < MAXTRIALS; ++t) //Outermost loop for the Trials
{

for (h = 0; h < MAXHANDS; ++h) //InnerLoop for Hands
{

myCards[0] = myDeck.getCard();
for (c = 1; c < MAXCARDS; ++c) //InnerMost Loop for Cards
{
myCards[c] = myDeck.getCard();
if (myCards[c].getValue() == myCards[0].getValue())
{
pair++;
}

if (myCards[c].getSuit() == myCards[0].getSuit())
{
flush++;
}

myDeck.addCard(myCards[c]);
c++;

}
myDeck.shuffle(MAXSHUFFLE);
h++;
}

cout << "pairs: " << pair << "\tflushes: " << flush << endl;

}
cin.get();
}

enter image description here

最佳答案

如果我理解你的问题,“结果应该......由 10 行组成,反射(reflect)每次试验的独特结果”,问题很简单,你没有重置 flush 每次试验之间的计数器变量。像下面这样的“试用”for 循环开始的地方应该可以解决问题:

for (t = 0 ; t < MAXTRIALS; ++t) 
{
pair = 0;
flush = 0;

// the remainder as is...

关于C++ 需要帮助遍历卡片、定位对子和同花顺,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6619530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com