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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
#include <fstream>
#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>
#include <cctype>
#include <cmath>
const short NUMBER_OF_CARDS = 5;
using namespace std;
//convert string array containing card values (2-10, Q, J, K, A) into numerical values (2-14)
void convert_array(string value_str[], short value[]);
//convert single string element (2-10, Q, J, K, A) into numerical value (2-14)
short convert_value(string value);
//returns true if the cards are all of the same suit
//returns false otherwise
bool same_suit(string suit[]);
//returns true if the cards consist of a straight combination
//returns false otherwise
bool straight(short value[]);
//input is an array of card values
//function outputs array sepcifying how many times same value cards occured along with
//the size of the array (i.e. the number of times cards of different values were encountered)
void occurences(short value[], short num_of_combos[], short& size);
//tests for multiple occurences of cards
//"four" becomes true if there's four of a kind in set of cards -- "four" is false otherwise
//"three" becomes true if there's three of a kind in set of cards -- "three" is false otherwise
//"pair" becomes true if there's two of a kind in set of cards -- "pair" is false otherwise
//"two_pair" becomes true if there are two pairs in a set of cards -- "two_pair" is false otherwise
void check_cards( bool four, bool three, bool pair, bool two_pair, short num_of_combos, short size);
int main()
{
char choice = 'n';
do
{
string card[NUMBER_OF_CARDS];
short value[NUMBER_OF_CARDS];
string value_str[NUMBER_OF_CARDS];
string suit[NUMBER_OF_CARDS];
for(short i = 0; i < NUMBER_OF_CARDS; i++)
{
cout << "Enter a card in suit/value format: ";
getline(cin, card[i]);
suit[i] = card[i].substr(0, 1);
value_str[i] = card[i].substr(1, card[i].length() - 1);
}
convert_array(value_str, value);
short num_of_combos[NUMBER_OF_CARDS], size(0);
occurences(value, num_of_combos, size);
bool pair(false), two_pair(false), three(false), four(false);
//TESTING
void check_cards(bool four, bool three, bool pair, bool two_pair, short num_of_combos, short size);
bool is_straight = straight(value);
bool is_same_suit = same_suit(suit);
if(is_same_suit && is_straight)
{
cout << "Straight Flush\n\n";
}
else if(is_straight)
{
cout << "Straight\n\n";
}
else if(four)
{
cout << "Four of a kind\n\n";
}
else if(three && pair)
{
cout << "Full House\n\n";
}
else if(is_same_suit)
{
cout << "Flush\n\n";
}
else if(two_pair)
{
cout << "Two pairs\n\n";
}
else if(pair)
{
cout << "Pair\n\n";
}
else//nothing
{
cout << "Nothing\n\n";
}
cout << "Do you wish to run this program again? (y/n): ";
cin >> choice;
choice = tolower(choice);
while((choice != 'y') && (choice != 'n'))
{
cout << "Incorrect input. Do you wish to run this program again? (y/n): ";
cin >> choice;
}
}while(choice == 'y');
cout << "End of the program.\nEnter key to continue\n\n";
system("pause");
return 0;
}
void convert_array(string value_str[], short value[])
{
for(short i = 0; i < NUMBER_OF_CARDS; i++)
{
value[i] = convert_value(value_str[i]);
}
return;
}
short convert_value(string value)
{
if(value == "2")
{
return 2;
}
else if(value == "3")
{
return 3;
}
else if(value == "4")
{
return 4;
}
else if(value == "5")
{
return 5;
}
else if(value == "6")
{
return 6;
}
else if(value == "7")
{
return 7;
}
else if(value == "8")
{
return 8;
}
else if(value == "9")
{
return 9;
}
else if(value == "10")
{
return 10;
}
else if((value == "q") || (value == "Q"))
{
return 11;
}
else if((value == "j") || (value == "J"))
{
return 12;
}
else if((value == "k") || (value == "K"))
{
return 13;
}
else//((value == "a") || (value == "A"))
{
return 14;
}
}
bool same_suit(string suit[])
{
string test = suit[0];
for(short i = 1; i < NUMBER_OF_CARDS; i++)
{
if(test != suit[i])
{
return false;
}
}
return true;
}
bool straight(short value[])
{
short min = value[0];
for(short i = 1; i < NUMBER_OF_CARDS; i++)
{
if(value[i] < min)
{
min = value[i];
}
}
short next = min + 1;
short i = 0;
while(i < NUMBER_OF_CARDS)
{
if(value[i] == next)
{
next++;
i=0;
}
else
{
i++;
}
}
//The while loop above will set "next" equal to 5 more than "min" if there is a straight
if(next == (min+5))
{
return true;
}
else
{
return false;
}
}
void occurences(short value[], short num_of_combos[], short& size)
{
short k = 0;
short count;
bool encountered[] = {false, false, false, false, false};
for(short i = 0; i < NUMBER_OF_CARDS; i++)
{
if(!encountered[i])
{
size++;
encountered[i] = true;
count = 1;
for(short j = 1; j < NUMBER_OF_CARDS; j++)
{
if((!encountered[j]) && (value[i] == value[j]))
{
encountered[j] = true;
count++;
}
}
num_of_combos[k] = count;
k++;
}
}
return;
}
void check_cards(bool four, bool three, bool pair, bool two_pair, short num_of_combos[], short size)
{
short pairs(0), threes(0), fours(0);
for(short i = 0; i < size; i++)
{
if(num_of_combos[i] == 2)
{
pairs++;
}
if(num_of_combos[i] == 3)
{
three = true;
}
if(num_of_combos[i] == 4)
{
four = true;
}
}
if(pairs == 2)
{
two_pair = true;
}
else if(pairs == 1)
{
pair = true;
}
/*
//TESTING
cout << "pair: " << pair << endl;
cout << "two_pairs: " << two_pair << endl;
cout << "four: " << four << endl;
cout << "three: " << three << endl;
*/
return;
}
|