# Solitaire Prime – Prog 1 CECS 282-01 solution

\$30.00

Original Work ?

## Description

5/5 - (1 vote)

Solitaire is a card game that is played by one person. Solitaire is also known by the name “Patience”.

It is estimated that there are more than 1700 different versions of Solitaire… make that 1701 because we are inventing a new Solitaire game today.

Welcome to Solitaire Prime! This game uses one standard deck of cards. Here are the rules:

• Take the top card from the deck and place it face up on the table.
• The Sum is now the value of that card (Ace = 1, 2 = 2, … 10 = 10, Jack = 10, Queen = 10, King = 10)
• If the Sum is a prime number, discard that pile (hand), and start over at instruction #1
• If the Sum is not prime, take the next card from the top of the deck and place it on top of the card stack (pile or hand) on the table.
• The Sum is now the sum of all cards in the stack on the table.
• Go to instruction #3.

Continue to play the game, keeping track of how many piles you have created that are prime.

If the last card from the deck gives you a prime pile, then you win! Write the word “Winner” on the screen and show how many prime piles there were.

If the last card from the deck does not give you a prime pile, then you lose. Write the word “Loser” in the screen.

These 2 screen shots show possible winner and loser hands. When you acquire a Prime pile, print out the value (prime number) and start over on the next line.

You will have 2 classes:

• The Deck class which will create the deck of cards
• The Card class which creates cards

The main logic of the program will be in the main function. You will use the Card class and the Deck class to play the game.

Here the methods you will need to create. Feel free to add more if you need them.

public class Deck

public Deck( ) // constructor which creates a deck of 52 cards. Ace of Spades on top, followed by the rest of the spades in order, followed by Hearts, Diamonds and Clubs.

public void refreshDeck(); // reset the deck so it looks like a new deck.

public Card deal( ) // deal a card from the top of the deck.

public void shuffle( ) // shuffle the cards in the deck.

public int cardsLeft( ) // return the number of cards left in the deck

public void showDeck( ); // show all the cards in the deck: 13 columns and 4 rows.

public class Card

public Card( ) // create a “blank” card

public Card  ( char r, char s ) // constructor to create a card, setting the rank and suit

public void setCard( char r, char s) // set an existing blank card to a particular value

public int getValue( ) // return the point value of the card. Ace = 1, 2 thru 10, Jack = 10, Queen = 10, King = 10

public void showCard( ) // display the card using 2 fields… Ace of Spade:AS, Ten of Diamond:10D, Queen of Heart:QH, Three of Club:3C. (If you want to get fancy, you can use these symbols for the suit ♠, ♣, ♥, ♦)

In the main function, you will have a menu that looks like this:

Welcome to Solitaire Prime!

• New Deck
• Display Deck
• Shuffle Deck
• Play Solitaire Prime
• Exit

New Deck will create an unshuffled deck in the following order: Spades, Hearts, Diamonds, Clubs… Ace, 2, 3, …, 10, Jack, Queen, King

Display Deck will display all cards in a grid: 13 columns by 4 rows.

Shuffle Deck will randomly shuffle all cards in the deck.

Play Solitaire will play the game as described above.

Exit will exit the program.

You must create your own function to shuffle (cannot use the random_shuffle provided by C++) and you must create your own function called “isPrime” (cannot use any C++ library function). One is NOT a prime number.

Objectives:

• Understand how to create classes
• Learn how to use arrays in C++
• Learn how to use header files and .cpp file as separate files.
• Learn about preprocessor directives, #include libraries, and “using namespace std”
• Learn how to protect header files by using #ifndef, #define, #endif
• Difference between array of objects in C++ verses Java.
1. Java arrays of objects have object references -= not actual objects. No need for a default constructor – example –> array of cards: Card [ ] deck = new Card[52]; This array contains 52 Card references but no cards.
2. C++ arrays of objects have the actual objects. Need to have a default constructor. Example-> Card deck[52]; This array contains 52 cards – you need to create a default constructor, then you need to reset all 52 cards to actual values with a function setCard(‘A’, ‘S’) for the Ace of Spades.
• Introduce the rand() function for shuffling cards
• Get a random number within a range of numbers