Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below is the 5 small C++ files, files names are in Bold please put everything under one file Solitaire.cpp thanks Card.h #ifndef CARD_H #define CARD_H

Below is the 5 small C++ files, files names are in "Bold"

please put everything under one file "Solitaire.cpp"

thanks

Card.h

#ifndef CARD_H #define CARD_H #include using namespace std; class Card { private: char rank, suit; public: Card(); Card(char r, char s); void setCard(char r, char s); int getValue(); void showCard(); }; #endif

Card.cpp

#include "Card.h" Card::Card() { rank = suit = ' '; } Card::Card(char r, char s) { rank = r; suit = s; } void Card::setCard(char r, char s) { rank = r; suit = s; } 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; } } void Card::showCard() { cout << rank << suit << "."; }

Deck.h

#ifndef DECK_H #define DECK_H #include "Card.h" class Deck { private: Card deck[52]; int cardsCnt; public: Deck(); void refreshDeck(); Card deal(); void shuffle(); int cardsLeft(); void displayDeck(); }; #endif

Deck.cpp

#include "Deck.h" 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; } 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; } Card Deck::deal() { Card c = deck[cardsCnt - 1]; cardsCnt--; return c; } void Deck::shuffle() { srand(0); for (int i = 0; i < cardsCnt; i++) { int r = i + (rand() % (52 - i)); Card temp = deck[i]; deck[i] = deck[r]; deck[r] = temp; } } int Deck::cardsLeft() { return cardsCnt; } 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 << " "; } } }

Solitaire.cpp

#include "Deck.h" #include Deck deck;

void playGame(); bool isPrime(int val); void printStackReverse(stack s);

int main() { int A; while (true) { cout << "Welcome to Solitaire Prime! 1. A New Deck 2) Display the Deck 3) Shuffle the Deck 4) Play the Solitaire game 5) Exit the game Enter choice: "; cin >> A; switch (A) { case 1: deck.refreshDeck(); cout << " Now New deck is created "; break; case 2: cout << " Deck: "; deck.displayDeck(); cout << endl; break; case 3: deck.shuffle(); cout << " Shuffled deck created "; break; case 4: playGame(); break; case 5: cout << " Thank you for playing!!! "; exit(0); break; default: cout << " Incorrect choice, Please try again "; break; } cout << endl; } } void playGame() { cout << deck.cardsLeft() << endl; int piles = 0, sum = 0; stack hand; cout << " Playing The Solitaire game!! "; while (deck.cardsLeft() != 0) { Card c = deck.deal(); sum += c.getValue(); if (isPrime(sum)) { hand.push(c); printStackReverse(hand); hand = stack(); cout << " Prime: " << sum << endl; piles++; sum = 0; } else { hand.push(c); } } if (sum != 0) { printStackReverse(hand); hand = stack(); cout << " Loser "; } else { cout << " Winner in " << piles << " piles! "; } } 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; } 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

Step: 3

blur-text-image

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

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions

Question

Explain how the real balance effect works?

Answered: 1 week ago