The program below is suppose to be looking for "Pair's" and "Flush's". It iterates through 10 Trials consisting of 10,000 hands, each hand consisting of 5 cards. The result should (of course it doesn't right now) consist of 10 rows reflecting unique results for each trial. I am stuck...thanks in advance.
#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;
con开发者_如何学Cst 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();
}
If I understand your question, "the result should ... consist of 10 rows reflecting unique results for each trial", the problem is simply that you don't reset the pair
and flush
counter variables between each trial. Something like the following where the 'trial' for
loop starts should do the trick:
for (t = 0 ; t < MAXTRIALS; ++t)
{
pair = 0;
flush = 0;
// the remainder as is...
With a lot of guessing what exactly should happen...
1) Is it made sure, that myDeck.getCard()
does not draw the same card twice? Or does it not matter for your task?
2) What is myDeck.addCard(myCards[c])
exactly doing?
3) Why do you increment the loop counter a second time? c++
If this is made sure, you are only comparing against the first card. If you want to compare a complete hand, your code should look something like this:
// first draw the complete hand
for(int card = 0; card < MAX_CARDS; ++card)
{
myCards[card] = myDeck.getCard();
}
// now that we have the full hand, compare each card against each other card
for(int start = 0; start < MAXCARDS-1; ++start)
{
for(int compare = start+1; compare < MAXCARDS; ++compare)
{
if (myCards[start].getValue() == myCards[compare].getValue())
{
pair++
}
// do similar for flushs
}
}
I didn't test this code, but this should give you a start.
This would count every pair, even if there are two pairs in one hand. It would need additional code to break out of the loops, if a pair was found.
btw: looks like homework to me...
The c++
and h++
are a little bit suspicious (did you really mean to only touch every other item)? But without more information as to what you are observing, it would be hard to give a definitive answer.
Also, some minor stylistic recommendations regarding your code:
- I recommend putting off the declaration of "h", "c", and "t" to the first point at which they are needed, so I would declare them in the for-loop (e.g. "for (int h = 0; h < ... ; h++)").
- It is more idiomatic to use
static_cast
in C++ code (i.e.srand(static_cast(time(NULL)))
), than it is to use a C-style cast, however both forms are correct.
精彩评论