/* ************************************************************************** * Program name : 025_Cardgame (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/06/11 - first version * ************************************************************************** * Description : (a) Reset all counter or coins to zero * * (b) Show the rule of this play * * (c) Ask user to input no. of round * * (d) Draw the Cards * * (e) Ask user selection * * (f) Detect win or loss * * (g) Calculate coins * * (h) End of game * * (i) Try again ? * ************************************************************************** */ #include #include #include int main() { // part 1 : declaration int Coins, // Your coins Round, // No. of round per section Count, // Count no. of round Card_1, // First Card Card_2, // Second Card Card_3, // Third Card Temp_card, // keep the Card no / use while // compare Card_1 & Card_2 Bit, // Amount per round Gain, // Gain per round Loss; // Loss per round char Again, // Run this program again ? Choice, // Your selection Status; // keep status of win / loss // part 2 : main program body do { // part 2a : Reset all counter or coins to zero Coins = 100; Round = Count = Card_1 = Card_2 = Card_3 = Bit = Gain = Loss = 0; Again = 'Y'; srand(time(NULL)); // part 2b : How to play cout << "\n Card game\n" << "\nAfter the computer takes 2 cards (within 1 to 13)" << "\nPlease select your choice : inside , outside or bounder" << "\nIf you choice is correct , your coins will be increased." << "\nHowever it is not easy to WIN."; // part 2c : Ask user to input no. of round do { cout << "\n\nInput the max. number of round per game : "; cin >> Round; if (Round <= 0) cout << "\nThe max. number of round must greater than 0 (ZERO)."; } while (Round <= 0); for (Count=1; Count<=Round; Count++) { // part 2d : Draw the Cards Card_1 = (rand() % 13)+1; Card_2 = (rand() % 13)+1; Card_3 = (rand() % 13)+1; if (Card_1 > Card_2) { Temp_card = Card_1; Card_1 = Card_2; Card_2 = Temp_card; } // part 2e : Ask user selection cout << "\n*******************************************************" << "\nThis is round " << Count << " out of " << Round << "\nYou have " << Coins << " coins now." << "\nCard 1 is : " << Card_1 << "\nCard 2 is : " << Card_2 << "\n"; do { cout << "\nHow many coins you bit on this round : "; cin >> Bit; if (Bit<=0 || Bit>Coins) cout << "Can not be less than 1 or more than no. of your coins."; } while (Bit<=0 || Bit>Coins); do { cout << "\nEnter your choice : [I]nside, [O]utside or [B]ounder : "; cin >> Choice; } while (Choice != 'I' && Choice != 'i' && Choice != 'B' && Choice != 'b' && Choice != 'O' && Choice != 'o'); // part 2f : Detect win or loss switch (Choice) { case 'I' : case 'i' : if (Card_3 > Card_1 && Card_3 < Card_2) Status = 'W'; else if ((Card_3 = Card_1) || (Card_3 = Card_2)) Status = 'D'; else Status = 'L'; break; case 'O' : case 'o' : if (Card_3 < Card_1 || Card_3 > Card_2) Status = 'W'; else if ((Card_3 = Card_1) || (Card_3 = Card_2)) Status = 'D'; else Status = 'L'; break; case 'B' : case 'b' : if ((Card_3 = Card_1) || (Card_3 = Card_2)) Status = 'T'; else Status = 'L'; break; } // End of switch control structure // part 2g : Calculate coins switch (Status) { case 'W' : Gain = Bit; Coins = Coins+Gain; cout << "\nYes! The 3rd card is " << Card_3 << " You win " << Gain << " coins"; break; case 'T' : Gain = Bit*3; Coins = Coins+Gain; cout << "\nExcellent! The 3rd card is " << Card_3 << " You win " << Gain << " coins"; break; case 'D' : Loss = Bit*2; Coins = Coins-Loss; cout << "\nToo Bad! The 3rd card is " << Card_3 << " You loss " << Loss << " coins"; break; case 'L' : Loss = Bit; Coins = Coins-Loss; cout << "\nSorry! The 3rd card is " << Card_3 << " You loss " << Loss << " coins"; break; } // End of switch control structure if (Coins < 0) { Coins = 0 ; break; // exit from the for loop } } // End of for loop // part 2h : End of game cout << "\n*******************************************************" << "\n Final result : You have " << Coins << " coin(s)" << "\n*******************************************************" << "\n"; // part 2i : try another game ? cout << "\aTry another game (Y/N) : "; cin >> Again; cout << "\n"; } while (Again=='Y' || Again=='y'); cout << "\n" << endl; system("PAUSE"); return 0; }