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
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
void pause_215(bool have_newline)
{
if (have_newline) {
// Ignore the newline after the user's previous input.
cin.ignore(256, '\n');
}
// Prompt for the user to press ENTER, then wait for a newline.
cout << endl << "Press ENTER to continue." << endl;
cin.ignore(256, '\n');
}
// This function takes the user's poker cards, displays the match of the cards
// Parameters: the array and the size
// Returns: user_match_num, which is the highest number that matches in the user's cards
int user_compare_cards(int match_array[], int size)
{
int i;
int j;
int match = 0;
int user_match_num = 0;
// two for loops so that it can check to see which cards match
for (i = 0; i < size; i++)
{
for (j = i + 1; j < size; j++)
{
if (match_array[i] == match_array[j])
{
// assign user_match_num to card that matches for later use
user_match_num = match_array[i];
// Match is simply used as a system to decide the higher/better matches of the cards
match += 1;
}
}
}
cout << " " << endl;
if (match == 1)
{
cout << "Pair!" << endl;
}
else if (match == 2)
{
cout << "Pair of two sets!" << endl;
}
else if (match == 3)
{
cout << "Three of a kind!" << endl;
}
else if (match == 4)
{
cout << "Full House!" << endl;
}
else if (match == 6)
{
cout << "Four of a kind!" << endl;
}
else
{
cout << "no cards matched" << endl;
}
return user_match_num;
}
// This function is similar to the one above because it must once again find the match for the user, but this time it must return it
// Parameters: the array and the size
// Returns: user_match, the match number for the user
int user_compare_cards_return_match(int match_array[], int size)
{
int i;
int j;
int user_match = 0;
int user_match_num = 0;
for (i = 0; i < size; i++)
{
for (j = i + 1; j < size; j++)
{
if (match_array[i] == match_array[j])
{
user_match_num = match_array[i];
user_match += 1;
}
}
}
return user_match;
}
// This function takes the computer's cards and displays its matches
// Parameters: the array and the size
// Returns: computer_match_num, which is the highest number that matches in the computer's cards
int computer_compare_cards(int match_array[], int size)
{
int i;
int j;
int match = 0;
int computer_match_num = 0;
for (i = 0; i < size; i++)
{
for (j = i + 1; j < size; j++)
{
if (match_array[i] == match_array[j])
{
computer_match_num = match_array[i];
match += 1;
}
}
}
cout << " " << endl;
if (match == 1)
{
cout << "Pair!" << endl;
}
else if (match == 2)
{
cout << "Pair of two sets!" << endl;
}
else if (match == 3)
{
cout << "Three of a kind!" << endl;
}
else if (match == 4)
{
cout << "Full House!" << endl;
}
else if (match == 6)
{
cout << "Four of a kind!" << endl;
}
else
{
cout << "no cards matched" << endl;
}
return computer_match_num;
}
// This function is similar to the one above because it must once again find the match for the computer, but this time it must return it
// Parameters: the array and the size
// Returns: computer_match, the match number for the computer
int computer_compare_cards_return_match(int match_array[], int size)
{
int i;
int j;
int computer_match = 0;
int computer_match_num = 0;
for (i = 0; i < size; i++)
{
for (j = i + 1; j < size; j++)
{
if (match_array[i] == match_array[j])
{
computer_match_num = match_array[i];
computer_match += 1;
}
}
}
return computer_match;
}
// This function takes the match numbers from the computer and the user and the highest cards that matched from each, so that it can decide
// and output to the user whether they or the computer won.
// Parameters: user_match - the match number for the user's cards
// computer_match - the match number for the computer's cards
// user_match_num - the highest number that matches in the user's cards
// computer_match_num - the highest number that matches in the computer's cards
// Returns: nothing
int compare_user_computer_cards(int user_match, int computer_match, int user_match_num, int computer_match_num)
{
if (user_match > computer_match)
{
cout << "Congratulations, you won the game!" << endl;
}
else
{
if (computer_match > user_match)
cout << "Sorry, you lost the game!" << endl;
}
if (user_match == computer_match)
{
if (user_match_num > computer_match_num)
{
cout << "Congratulations, you won the game!" << endl;
}
else
{
if (computer_match_num < user_match_num)
{
cout << "Sorry, you lost the game!" << endl;
}
}
}
return 0;
}
int main()
{
srand(time(0));
string user_input;
int user_card;
while (user_input != "Q" || user_input != "q")
{
// Generate computer numbers
int computer_card1 = rand() % 13 + 1;
int computer_card2 = rand() % 13 + 1;
int computer_card3 = rand() % 13 + 1;
int computer_card4 = rand() % 13 + 1;
int computer_card5 = rand() % 13 + 1;
// Make sure all 5 are not the same
if (computer_card5 == computer_card4 && computer_card4 == computer_card3 && computer_card3 == computer_card2 && computer_card2 == computer_card1)
{
computer_card5 = (rand() % 13 + 1) != computer_card1;
}
// Generate user numbers
int user_card1 = rand() % 13 + 1;
int user_card2 = rand() % 13 + 1;
int user_card3 = rand() % 13 + 1;
int user_card4 = rand() % 13 + 1;
// Make sure all 5 are not the same
if (user_card4 == user_card3 && user_card3 == user_card3 == user_card2 && user_card2 == user_card1)
{
user_card4 = (rand() % 13 + 1) != user_card1;
}
// Get card from user
cout << "Let us start to play the game (Press Q to quit)" << endl;
cout << "Enter your card number (in the range of [1, 13]): ";
cin >> user_card;
if (user_card < 0 || user_card > 13)
{
cout << "Invalid range!" << endl;
}
if (cin.fail())
{
cin.clear();
cin >> user_input;
if (user_input == "Q" || user_input == "q")
{
cout << "Thank you for playing this game!" << endl;
cout << " " << endl;
pause_215(true);
return 0;
}
}
// Display user and computer's cards
cout << " " << endl;
cout << "Your five cards are" << " " << user_card << " " << user_card1 << " " << user_card2 << " " << user_card3 << " " << user_card4 << endl;
cout << " " << endl;
cout << "My five cards are" << " " << computer_card1 << " " << computer_card2 << " " << computer_card3 << " " << computer_card4 << " " << computer_card5 << endl;
cout << " " << endl;
// Initialize all variables
//int i;
//int j;
int user_match_num = 0;
int computer_match_num = 0;
int user_match = 0;
int computer_match = 0;
int size = 5;
// Create array for user
int user_match_array[] = { user_card, user_card1, user_card2, user_card3, user_card4 };
// Take and display the user's highest matched card
int user_ans = user_compare_cards(user_match_array, size);
cout << "The ranking of your poker cards is: " << user_ans << endl;
user_compare_cards_return_match(user_match_array, size);
// Create array for computer
int computer_match_array[] = { computer_card1, computer_card2, computer_card3, computer_card4, computer_card5 };
// Take and display the computer's highest matched card
int computer_ans = computer_compare_cards(computer_match_array, size);
cout << "The ranking of my poker cards is: " << computer_ans << endl;
computer_compare_cards_return_match(computer_match_array, size);
cout << " " << endl;
// Compare user and computer's matches and display who won
compare_user_computer_cards(user_match, computer_match, user_match_num, computer_match_num);
cout << " " << endl;
}
// End program
pause_215(true);
return 0;
}
|