Server IP : 172.16.15.8 / Your IP : 18.118.140.78 Web Server : Apache System : Linux zeus.vwu.edu 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Wed Nov 6 14:29:02 UTC 2024 x86_64 User : apache ( 48) PHP Version : 7.2.24 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON Directory (0705) : /home/jwmccreary/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
// Pirate's Gold - a simulated form of solitaire // // September 2008 // CS 311 // Justin McCreary // Professor: Dr. Ames // file name: piratesgold3.cpp // compile: g++ piratesgold3.cpp // run: ./a.out // Uses Card and Deck Classes #include <iostream> #include <iomanip> #include <cstdlib> // for random num generator #include <ctime> // for time to seed random num generator #include "cardclass.cpp" // Card, Deck types and classes using namespace std; // ----- Game Function Prototypes //--------------------------------------------------------------------------- void PlayPiratesGold (/* in */ DeckType deck); // ----- Main Function //--------------------------------------------------------------------------- int main() { DeckType deck; // A deck of cards for our game srand(time(0)); // "Turn on" the random number generator deck.Shuffle(); // Shuffle the deck PlayPiratesGold (deck); return 0; } //-------------------------------------------------------------------------- // PlayPiratesGold plays a modified version of Solitaire, called Pirate's Gold, in // which the deck is shuffled and eight cards are drawn from the deck and placed on the // "table" face up. The player then searches for two matching(face value) cards, at which // time two cards are drawn from the deck and placed on top of the matching cards. This process // is continued until either all of the cards have been used up, in which the game is won, or when // there are no longer any matching pairs, in which the game is lost. //-------------------------------------------------------------------------- // Accepts: the shuffled deck // Returns: Nothing //-------------------------------------------------------------------------- void PlayPiratesGold (DeckType deck) { // local type definition for a card hand typedef CardType HandType[8]; // local variables HandType hand; // One hand of cards on the table int round, //counter for progressing through the rounds j, //counter for progressing through the cards on the table in the hopes of finding a matching pair i, // counter for progressing through rounds, as well as for counting the first eight cards ii; // counter for diplaying the cards through each round bool matchfound; //true/false statement used for whether or not to print the cards //on the table const int NumCardsonTable = 8; //constant for the number of cards on table // Deal eight cards for (i = 0; i < NumCardsonTable; i++) { hand[i] = deck.DrawCard(); } cout << "Original Cards on Table\n-----------------------\n"; for (i = 0; i < NumCardsonTable/2; i++) { hand[i].Print(); cout << "\t"; } cout << "\n"; for (i = NumCardsonTable/2; i < NumCardsonTable; i++) { hand[i].Print(); cout << "\t"; } cout << "\n\n"; //Prints the first eight cards drawn from the deck cout << "Table After Each Round of Game\n------------------------------\n"; round = 1; matchfound = true; while(matchfound && !deck.IsEmpty()) { i = 0; matchfound = false; while( i < NumCardsonTable && !matchfound) { j = i + 1; while( j < NumCardsonTable && !matchfound) //the ii and j for loops go through each combin cards until match found { if( hand[i].EqualFace(hand[j]) ) { hand[i] = deck.DrawCard(); //draws a new card from the deck hand[j] = deck.DrawCard(); //draws a new card from the deck matchfound = true; //when a match is found, it prints the results of this round, //ends the round, //and then moves on to the next round cout << "\nRound " << round << "\n\n"; for (ii = 0; ii < NumCardsonTable/2; ii++) { hand[ii].Print(); cout << "\t"; } cout << "\n"; for (ii = NumCardsonTable/2; ii < NumCardsonTable; ii++) { hand[ii].Print(); cout << "\t"; } cout << "\n\n"; round = round + 1; } else { j++; //if a match is not found, no statement is printed,... //...and it moves on to next pair of cards in the combination } } i++; } } if(deck.IsEmpty()) { cout << "\nCongratulations! You won!\n"; //If all the cards have been drawn from the deck, //the game is won } else { cout << "\nSorry You lose!\n"; //If not, the game is lost } }