Server IP : 172.16.15.8 / Your IP : 18.119.192.2 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/rnlink/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
// Pirate's Gold - a simple card game // // August 2008 // R Link // 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 PlayPirateGame (/* 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 PlayPirateGame (deck); return 0; } //-------------------------------------------------------------------------- // PlayPirateGame plays a game known as Pirate's Gold, laying out eight // cards out of the full deck and looking to see if there are any // matching pairs of face value. If there are, the two cards are // replaced with two new cards drawn from the deck. If no pairs are // present and the deck is not empty, then the player loses the game. // Otherwise, the game is played until the deck is empty and the player // wins. //-------------------------------------------------------------------------- // Accepts: the shuffled deck // Returns: nothing //-------------------------------------------------------------------------- void PlayPirateGame(DeckType deck) { // local type definition for a card hand (or, in this game, a layout // of eight cards) typedef CardType HandType[DECKSIZE]; // loval variable HandType layout; // the array that will hold the eight cards // being played int i, // counter for progressing through the layout cards u; // counter for comparing the layout cards // Output headings cout << "\n\n\nThe Game of Pirate's Gold\n\n"; cout << "Original Cards on Table\n"; cout << "-------------------------\n\n"; // Draw the initial eight cards and display them. for (i = 0; i < 8; i++) { layout[i] = deck.DrawCard(); cout << i+1 << ". "; layout[i].Print(); if( i == 3 || i == 8) cout << "\n"; else cout << "\t"; } cout << "\n----------------------------------------------------------------------------\n"; // Play the game // Compare the cards in the layout for face value matches for( i = 0; i < 8; i++) // look at the first card { for( u = i+1; u < 8; u++) // compare it to the second card { //---------------------------------------------------------------------------------- // Because of the reassignment of the loop control variable in the following 'if' // statements, it is necessary to put in an 'if' statement specifically to ensure // that the first card (in place [0]) is compared to the others. Therefore, this // is the function of the following 'if' statement. //---------------------------------------------------------------------------------- if( layout[0].EqualFace(layout[u])) { cout << "\nMatch found! Card 1 is the same as card " << u+1; cout << ". So now we'll replace those cards! \nHere's our new"; cout << " eight-card-layout!\n\n"; layout[0] = deck.DrawCard(); // If a match is found with the first layout[u] = deck.DrawCard(); // card, the two cards are replaced. for (i = 0; i < 8; i++) // Then the new layout is printed to { // the screen cout << i+1 << ". "; layout[i].Print(); if( i == 3 || i == 8) cout << "\n"; else cout << "\t"; } cout << "\n\n"; if(deck.IsEmpty() == true) // If the deck has been depleated, the game { break; } // is ended and the player has one. i=0; // If not, 'i' is reset to 0 so as to go } // through the 'for' statement again and // continue comparing. It is because of // this statement, and 'i++' of the 'for' // loop, that the [0] place must be // compared seperately from the following. else if( layout[i].EqualFace(layout[u])) // Compares the other cards to each other. { cout << "\nMatch found! Card " << i+1 << " is the same as card " << u+1; cout << ". So now we'll replace those cards! \nHere's our "; cout << "new eight-card-layout!\n\n"; layout[i] = deck.DrawCard(); // If a match is found, the cards are layout[u] = deck.DrawCard(); // replaced... for (i = 0; i < 8; i++) // ...and the new layout printed. { cout << i+1 << ". "; layout[i].Print(); if( i == 3 || i == 8) cout << "\n"; else cout << "\t"; } cout << "\n\n"; if(deck.IsEmpty() == true) // If the deck is, then, empty, the game { break; } // is done, and the loop is broken. i=0; // If not, 'i' is reset to 0 so as to go // through the 'for' statement again. } } } if(deck.IsEmpty() == false) // If, at the end of the game, the deck cout << "\n I'm sorry, you lose. \n\n"; // is not empty, the player loses. else // Otherwise he/she wins. cout << "You win!\n\n"; }