1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
#include<iostream>
using namespace std;
const int size = 5;
void sort(int deck[],const int size);
void Flush(int deck[],double& flush,const int size);
void Straight(int deck[], double& straight,const int size);
int main()
{
int deck[52],i,n,w;
double flush=0,straight=0;
for(i=0;i<52;i++)
deck[i]=i;
//initialise with values 0-51 to deck. 0=ace,1=1,2=2......
for(w=0;w<1000000;w++)
//repeat deal 1000000 times
{
for(n=0;n<size;n++)
//deal the 5 cards
{
deck[n]=rand()%52;
}
Flush(deck,flush,size);
Straight(deck,straight,size);
// call the functions to count flushes and straights
}
flush=flush/10000;
straight=straight/10;
cout<<"Straight: "<<straight<<"%"<<endl<<"Flush: "<<flush<<"%"<<endl;
//print
return 0;
}
void sort (int deck[],const int size)//bubble sort
{
int temp,i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size-1-i;j++)
{
if (deck[j]<deck[j+1])
{
temp=deck[j];
deck[j]=deck[j+1];
deck[j+1]=temp;
}
}
}
}
void Flush (int deck[],double& flush,const int size)
{
int F_deck[5];//so not to change the array for next function
for(int i=0;i<size;i++)
{
F_deck[i]=(deck[i]/13);
//get suit
}
for(int j=0;j<size-1;j++)
{
if(F_deck[j]!=F_deck[j+1])
return;
//check if suits are equal
}
flush=flush+1;
}
void Straight(int deck[], double& straight,const int size)
{
for(int i=0;i<size;i++)
{
deck[i]=((deck[i]%13));
// give card value 0-12
}
sort(deck,size);
//puts in order
if((deck[0]-deck[1])!=1)
return;
if((deck[1]-deck[2])!=1)
return;
if((deck[2]-deck[3])!=1)
return;
if(((deck[3]-deck[4])==1)||((deck[3]-deck[4])==9)) //high low ace
{
straight++;
}
else
return;
}
|