Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

There are 5 C++ files below, Card.h, Card.cpp, Deck.h, Deck.cpp, Main.cpp Put CArd.h, card.cpp,Deck.h, Deck.cpp under Main.cpp file i want everything under 1 single CPP

There are 5 C++ files below, Card.h, Card.cpp, Deck.h, Deck.cpp, Main.cpp

Put CArd.h, card.cpp,Deck.h, Deck.cpp under Main.cpp file

i want everything under 1 single CPP file,under 1 single C++ file, 4th time asking, please help

// Card.h

//Create a class Card

#ifndef CARD_H

#define CARD_H

#include

using namespace std;

class Card {

//Member variables

private:

char rank, suit;

//Member functions

public:

//Constructors

Card();

Card(char r, char s);

//Set card attributes

void setCard(char r, char s);

//Getter

int getValue();

void showCard();

};

#endif // !CARD_H

Card.cpp

//Implementation of card class

#include "Card.h"

//Constructors

Card::Card() {

rank = suit = ' ';

}

Card::Card(char r, char s) {

rank = r;

suit = s;

}

//Set card attributes

void Card::setCard(char r, char s) {

rank = r;

suit = s;

}

//Getter

int Card::getValue() {

if (rank == 'A') {

return 1;

}

else if (rank == '2') {

return 2;

}

else if (rank == '3') {

return 3;

}

else if (rank == '4') {

return 4;

}

else if (rank == '5') {

return 5;

}

else if (rank == '6') {

return 6;

}

else if (rank == '7') {

return 7;

}

else if (rank == '8') {

return 8;

}

else if (rank == '9') {

return 9;

}

else if (rank == 'K') {

return 10;

}

else if (rank == 'Q') {

return 10;

}

else {

return 10;

}

}

//Show a carrd representation

void Card::showCard() {

cout << rank << suit<<".";

}

Deck.h

//Create a deck of cards

#ifndef DECK_H

#define DECK_H

#include "Card.h"

class Deck {

//Member variables

private:

Card deck[52];

int cardsCnt;

//Member variables

public:

//Constructor

Deck();

//Create a fresh deck

void refreshDeck();

//Get a card from top

Card deal();

//Shuffle

void shuffle();

//Number of cards left in deck

int cardsLeft();

//Display deck

void displayDeck();

};

#endif // !DECK_H

Deck.cpp

//Implementation of deck class

#include "Deck.h"

//Constructor

Deck::Deck() {

char ranks[] = { 'A','1','2','3','4','5','6','7','8','9','J','Q','K' };

char suits[] = { 'S','H','D','C' };

int k = 0;

for (int i = 0; i < 4; i++) {

for (int j = 0; j < 13; j++)

{

deck[k++] = Card(ranks[j], suits[i]);

}

}

cardsCnt = 52;

}

//Create a fresh deck

void Deck::refreshDeck() {

char ranks[] = { 'A','1','2','3','4','5','6','7','8','9','J','Q','K' };

char suits[] = { 'S','H','D','C' };

int k = 0;

for (int i = 0; i < 4; i++) {

for (int j = 0; j < 13; j++)

{

deck[k++] = Card(ranks[j], suits[i]);

}

}

cardsCnt = 52;

}

//Get a card from top

Card Deck::deal() {

Card c=deck[cardsCnt - 1];

cardsCnt--;

return c;

}

//Shuffle

void Deck::shuffle() {

srand(0);

for (int i = 0; i

{

int r = i + (rand() % (52 - i));

Card temp = deck[i];

deck[i] = deck[r];

deck[r] = temp;

}

}

//Number of cards left in deck

int Deck::cardsLeft() {

return cardsCnt;

}

//Display deck

void Deck :: displayDeck() {

for (int i = 0; i < 52; i++) {

if (i % 13 == 0 && i != 0) {

cout << endl;

deck[i].showCard();

cout << " ";

}

else {

deck[i].showCard();

cout << " ";

}

}

}

Main.cpp

#include "Deck.h"

#include

//Create object of Deck class

Deck deck;

//Function prototypes

void playGame();

bool isPrime(int val);

void printStackReverse(stack s);

int main()

{

int ch;

//Loop until exit

while (true) {

//User choices

cout << "Welcome to Solitaire Prime! 1) New Deck 2) Display Deck 3) Shuffle Deck 4) Play Solitaire 5) Exit Enter choice: ";

cin >> ch;

//Switch according to choice

switch (ch) {

//Create a new deck

case 1:

deck.refreshDeck();

cout << " New deck created ";

break;

//Display deck

case 2:

cout << " Deck: ";

deck.displayDeck();

cout << endl;

break;

//Shuffle deck of cards

case 3:

deck.shuffle();

cout << " Shuffled deck created ";

break;

//Play game

case 4:

playGame();

break;

//End

case 5:

cout << " Thank you!!! ";

exit(0);

break;

default:

cout << " Incorrect choice ";

break;

}

cout << endl;

}

}

//Function simulate a play

void playGame() {

cout << deck.cardsLeft() << endl;

int piles = 0,sum=0;

stack hand;

cout << " Playing Solitaire game!! ";

//Play game until deck cards count reach 0

while (deck.cardsLeft() != 0) {

//Otherwise take a card check sum

Card c = deck.deal();

sum += c.getValue();

//If print then clear and increment piles count

if (isPrime(sum)) {

hand.push(c);

//Display stack reverse order

printStackReverse(hand);

hand = stack();

cout << " Prime: " << sum << endl;

piles++;

sum = 0;

}

//Otherwise add into stack

else {

hand.push(c);

}

}

if (sum!=0) {

printStackReverse(hand);

hand = stack();

cout << " Loser ";

}

else {

cout << " Winner in " << piles << " piles! ";

}

}

//Check passed value is prime or not

bool isPrime(int val) {

bool prime = true;

if (val == 0 || val == 1) {

prime = false;

}

else {

for (int i = 2; i <= val / 2; ++i) {

if (val % i == 0) {

prime = false;

break;

}

}

}

if (prime)

return true;

else

return false;

}

//Display stack in reverse order

void printStackReverse(stack s)

{

if (s.empty())

return;

Card x = s.top();

s.pop();

printStackReverse(s);

x.showCard();

cout << " ";

s.push(x);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions

Question

What are the various steps in the ter1nination interview?

Answered: 1 week ago

Question

Solve for x: 2(3x 1)2(x + 5) = 12

Answered: 1 week ago