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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#include <iostream>
#include <ctime>
using namespace std;
void dealHand(char R[], char S[], int n);
void inputHand(char R[], char S[], int n);
bool straightFlush(char R[], char s[], int n);
bool flush(char R[], char s[], int n);
bool fullHouse(char R[], char s[], int n);
bool threeKind(char R[], char s[], int n);
bool straight(char R[], char s[], int n);
bool fourKind(char R[], char s[], int n);
bool twoPair(char R[], char s[], int n);
bool onepair(char R[], char s[], int n);
int main(void){
const int N = 5;
char Rank[N], Suit[N];
int i;
// Call srand() just once, at the beginning of main()
srand((unsigned int)time(NULL));
// inputHand prompts the user for a hand
// CAREFUL: enter rank in descending order
// The ranks, i.e. T or 9, sent back in Rank
// The suits, i.e. C or H, sent back in Suit
cout << "User inputs a hand:" << endl; // LINE 22
inputHand(Rank, Suit, N); // LINE 23
//cout << "Call dealHand():" << endl; // LINE 25
//dealHand(Rank, Suit, N); // LINE 26
// dealHand generates a random hand of N cards
// Each hand is from a fresh deck of 52 playing cards
// The ranks, i.e. T or 9, sent back in Rank
// The suits, i.e. C or H, sent back in Suit
cout << endl;
for (i = 0; i < N; i++)
cout << Rank[i] << Suit[i] << endl;
cout << endl;
if (straightFlush(Rank, Suit, N) == true)
cout << "Straight Flush!" << endl;
else if (flush(Rank, Suit, N))
cout << "Flush!" << endl;
else if (straight(Rank, Suit, N))
cout << "Straight!" << endl;
else if (twoPair(Rank, Suit, N))
cout << "Two Pair!" << endl;
return 0;
}
void dealHand(char R[], char S[], int n){
int i, j, cnum, cards[52], temp[52];
string ranks = "23456789TJQKA";
string suits = "CDHS";
if (n > 52){
cout << "Error...cannot deal " << n
<< " cards from a 52-card deck\n";
return;
}
for (i = 0; i < 52; i++)
cards[i] = i;
for (i = 0; i<500; i++)
swap(cards[rand() % 52], cards[rand() % 52]);
for (i = 0; i < n; i++){
temp[i] = cards[51 - i];
}
for (i = 0; i < n - 1; i++)
for (j = i + 1; j < n; j++)
if (((temp[i] % 13) < (temp[j] % 13))
|| (((temp[i] % 13) == (temp[j] % 13)) && ((temp[i] / 13) < (temp[j] / 13))))
swap(temp[i], temp[j]);
for (i = 0; i < n; i++){
cnum = temp[i];
R[i] = ranks[cnum % 13];
S[i] = suits[cnum / 13];
}
}
void inputHand(char R[], char S[], int n){
int i;
cout << "Input a hand of " << n << " cards in rank order" << endl;
for (i = 0; i < n; i++)
{
cout << "input rank[" << i << "] ";
cin >> R[i]; R[i] = toupper(R[i]);
cout << "input suit[" << i << "] ";
cin >> S[i]; S[i] = toupper(S[i]);
}
}
bool flush(char R[], char s[], int n){
if (s[0] == s[1] == s[0] == s[2] == s[0] == s[3] == s[0] == s[4]){
return true;
}
else{
return false;
}
}
bool straight(char R[], char s[], int n){
int arrayi[5];
for (int j = 0; j < 5; j++){
if (R[j] == 'T'){
arrayi[j] = 10;
}
else if (R[j] == 'J'){
arrayi[j] = 11;
}
else if (R[j] == 'Q'){
arrayi[j] = 12;
}
else if (R[j] == 'K'){
arrayi[j] = 13;
}
else if (R[j] == 'A'){
arrayi[j] = 14;
}
else
arrayi[j] = R[j];
}
if ((arrayi[0] == (arrayi[1] - 1)) && (arrayi[0] == (arrayi[2] - 2)) && (arrayi[0] == (arrayi[3] - 3)) && (arrayi[0] == (arrayi[4] - 4))){
return true;
}
else
return false;
}
bool fourKind(char R[], char s[], int n){
if (((R[0] == R[1]) && (R[0] == R[2]) && (R[0] == R[3]) || (R[1] == R[2]) && (R[1] == R[3]) && (R[1] == R[4]))){
return true;
}
else
return false;
}
bool threeKind(char R[], char s[], int n){
for (int i = 2; i < 5; i++){
if (R[i] == R[i - 1] && R[i] == R[i - 2])
return true;
}
return false;
}
bool twoPair(char R[], char s[], int n){
int pairs = 0;
for (int i = 0; i <= 3; i++){
if (R[i] == R[i + 1])
pairs++;
}
if (pairs == 2)
return true;
else
return false;
}
bool straightFlush(char R[], char s[], int n){
if (straight(R, s, n) && flush(R, s, n))
return true;
else
return false;
}
bool onepair(char R[], char s[], int n){
if (((R[0] == R[1]) || (R[1] == R[2]) || (R[2] == R[3]) || (R[3] == R[4])))
return true;
else
return false;
}
|