//****************************************************************** // IMPLEMENTATION FILE (player.cpp) // This file implements the Player class member functions //****************************************************************** #include "player.h" // Private members of class // CardDeck deck; Deck of cards // CardPile onTable; Cards face up on the table // CardPile discardPile; Cards on discard pile // // void TryRemove( CardPile&, CardPile& ); // void MoveFour( CardPile&, CardPile& ); // void MoveTwo( CardPile&, CardPile& ); //****************************************************************** void Player::PlayGame( /* in */ int numberOfShuffles, /* out */ bool& won ) // Places each card in the deck face up on the table // and applies rules for moving // Precondition: // numberOfShuffles is assigned // Postcondition: // After deck has been shuffled numberOfShuffles times, // all cards have been moved one at a time from deck to onTable // && Cards have (possibly) been moved from onTable to discardPile // according to the rules of the game // && won == true, if the game was won // == false, otherwise { CardType tempCard; // Temporary card deck.Shuffle(numberOfShuffles); while (deck.Length() > 0) { deck.RemoveTop(tempCard); onTable.InsertTop(tempCard); TryRemove(); } won = (onTable.Length() == 0); deck.Recreate(onTable, discardPile); } //****************************************************************** void Player::TryRemove() // If the first (top) four cards in onTable are the same suit, // they are moved to discardPile. If the first card and the fourth // card are the same suit, the second and third card are moved from // onTable to discardPile. This process continues until no further // moves can be made // Precondition: // Length of onTable > 0 // Postcondition: // Cards have (possibly) been moved from onTable to discardPile // according to the above rules { bool moveMade = true; // True if a move has been made while (onTable.Length() >= 4 && moveMade) if (onTable.CardAt(1).suit == onTable.CardAt(4).suit) // A move will be made if (onTable.CardAt(1).suit == onTable.CardAt(2).suit && onTable.CardAt(1).suit == onTable.CardAt(3).suit) MoveFour(); // Four alike else MoveTwo(); // 1st and 4th alike else moveMade = false; } //****************************************************************** void Player::MoveFour() // Moves the first four cards from onTable to discardPile // Precondition: // Length of onTable >= 4 // Postcondition: // The first four cards have been removed from onTable and // placed at the front of discardPile { CardType tempCard; // Temporary card int count; // Loop counter for (count = 1; count <= 4; count++) { onTable.RemoveTop(tempCard); discardPile.InsertTop(tempCard); } } //****************************************************************** void Player::MoveTwo() // Moves the second and third cards from onTable to discardPile // Precondition: // Length of onTable >= 4 // Postcondition: // The second and third cards have been removed from onTable and // placed at the front of discardPile // && The first card in onTable at entry is still the first card // in onTable { CardType tempCard; // Temporary card CardType firstCard; // First card in onTable // Remove and save first card onTable.RemoveTop(firstCard); // Move second card onTable.RemoveTop(tempCard); discardPile.InsertTop(tempCard); // Move third card onTable.RemoveTop(tempCard); discardPile.InsertTop(tempCard); // Restore first card onTable.InsertTop(firstCard); }