First, you need to create a Windows Console Application. Under File->New, select Win32 Console Application. Give it a name over on the right and click OK. In the next window, select "An empty project", click "Finish" then "OK" in next window. Then go to File->New again, and under Files tab, select C++ Source File. Copy and paste the following revised code into the .cpp file and run.
/************************************************** ********************
Filename: poker.cpp
Chapter: 4 Classes
Compiler: Borland C++ Version 5.01 Summer 1998
C++ for C Programmers, Edition 3 By Ira Pohl
************************************************** ******************/
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
enum suit { clubs, diamonds, hearts, spades };
class pips {
public:
void assign(int n) { p = n % 13 + 1; }
int getpip() { return p; }
void print() { cout << p; }
private:
int p;
};
class card {
public:
suit s;
pips p;
void assign(int n)
{ cd = n; s = static_cast<suit>(n/13); p.assign(n); }
void pr_card();
private:
int cd; //a cd is from 0 to 51
};
class deck {
public:
void init_deck();
void shuffle();
void deal(int, int, card*);
void pr_deck();
private:
card d[52];
};
void deck::init_deck()
{
for (int i = 0; i < 52; ++i)
d[i].assign(i);
}
void deck::shuffle()
{
for (int i = 0; i < 52; ++i) {
int k = i + (rand() % (52 - i));
card t = d[i]; //swap cards
d[i] = d[k];
d[k] = t;
}
}
void deck::deal(int n, int pos, card* hand)
{
for (int i = pos; i < pos + n; ++i)
hand[i - pos] = d[i];
}
int main()
{
card one_hand[9]; //max hand is 9 cards
deck dk;
int i, j, k, fcnt = 0, sval[4];
int ndeal, nc, nhand;
do {
cout << "\nEnter no. cards in a hand (5-9):";
cin >> nc;
} while (nc < 5 || nc > 9);
nhand = 52 / nc;
cout << "\nEnter no. of hands to deal: ";
cin >> ndeal;
srand(time(NULL)); //seed rand() from time()
dk.init_deck();
for (k = 0; k < ndeal; k += nhand) {
if ((nhand + k) > ndeal)
nhand = ndeal - k;
dk.shuffle();
for (i = 0; i < nc * nhand; i += nc) {
for (j = 0; j < 4; ++j) //zero suit counts
sval[j] = 0;
dk.deal(nc, i, one_hand); //deal next hand
for (j = 0; j < nc; ++j)
sval[one_hand[j].s]++; //increment suit count
for (j = 0; j < 4; ++j)
if (sval[j] >= 5) //5 or more is flush
fcnt++;
}
}
cout << "\n\nIn " << ndeal << " ";
cout << nc << "-card hands there were ";
cout << fcnt << " flushes\n ";
int look; cin >> look;
return 0;
}
|