Question

C++ programming: Card game Can same one help me out with a example finsih and working...

C++ programming: Card game

Can same one help me out with a example finsih and working programm ?

Program the basis for a card game (as a class card game):
● A card is represented by its suit/symbol (clubs, spades, hearts, diamonds) and a value (seven, eight, nine, ten, jack, queen, king, ace).
● A deck of cards consists of a pile of 32 cards that are completely connected to four players are distributed.
● Implement the following menu:
=== Card Game Menu ===
1 shuffle cards
Deal 2 cards
View 3 cards of a player
0 EXIT
Selection (0-3): _
● Explanation of the individual menu items:
1. shuffle cards: To do this, implement a shuffle method that uses the
Shuffles the cards. At the end, enter the control message "Cards ready
mixed!" on the screen.
Deal two cards: To do this, implement a method of dealing
the cards are completely distributed to the four players. At the end, deal the
Control message "Cards dealt!" on the screen.
3. view a player's cards: Implement another menu for this purpose,
with the help of which it is possible to select one of the four players and to
display maps on the screen. The output should only be made
if the cards have been distributed before (i.e. only if the last
menu item 2 and not menu item 1 was selected).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The following is the code for the given problem explained along with comments :-

(screenshots of an example run of the program and the screenshots of the code are also included. If this answer helped you, kindly up vote it. In case of any query regarding this solution, feel free to ask through the comments)

*************************************************************************************************************

#include <iostream>
#include <stdlib.h> // srand, rand
#include <time.h>
#include<algorithm>
#include <random> // std::default_random_engine
using namespace std;

pair<int,char> deck[32];
// cards will be stored as an array of a pair of an integer and character
//the integer values will be 1,7,8,9,10,11,12,13
//1 represents ace,11 represents jack,12 represents queen and 13 represents king
// and the characters will be as follows:-
//s is for spades
//c for clubs
//h for hearts
//d for diamonds

int flag = 0; // to check whether cards have been dealt or not

// the following arrays of pairs keep the cards of the 4 players

pair<int,char> player1[8];
pair<int,char> player2[8];
pair<int,char> player3[8];
pair<int,char> player4[8];

void menu()
{
cout<<"press 1 to shuffle the cards\n";
cout<<"press 2 to deal the cards\n";
cout<<"press 3 to view a player's cards\n";
cout<<"press 0 to exit\n";
}
void shuffleCards()
{

// To obtain a time-based seed
unsigned seed = 0;

// Shuffling the array
shuffle(deck, deck + 32,default_random_engine(seed));
flag = 0;
cout<<"Cards ready mixed!\n";

}

void dealCards()
{ int j =0; // to track the index of the deck
// player 1:-
for(int i =0;i<8;i++)
{
player1[i].first = deck[j].first;
player1[i].second = deck[j].second;
j++;
}
// player 2:-
for(int i =0;i<8;i++)
{
player2[i].first = deck[j].first;
player2[i].second = deck[j].second;
j++;
}
// player 3:-
for(int i =0;i<8;i++)
{
player3[i].first = deck[j].first;
player3[i].second = deck[j].second;
j++;
}
// player 4:-
for(int i =0;i<8;i++)
{
player4[i].first = deck[j].first;
player4[i].second = deck[j].second;
j++;
}
flag = 1; //cards have been dealt
cout<<"Cards dealt!\n";
}
void displayCards()
{
bool display = true;
if(flag == 0)
{
cout<<"cards not dealt yet\n";
return;
}
while(display == true){
cout<<"choose player 1,2,3 or 4\n or choose 0 to stop\n";
int player;
cin>>player;
if(player!=0)
cout<<"value suit\n";
switch(player)
{

case 1:{
{
for(int i = 0;i<8;i++)
{
cout<<player1[i].first<<" "<<player1[i].second<<endl;
}
}
}
break;
case 2:{
for(int i = 0;i<8;i++)
{
cout<<player2[i].first<<" "<<player2[i].second<<endl;
}
}
break;
case 3:{
for(int i = 0;i<8;i++)
{
cout<<player3[i].first<<" "<<player3[i].second<<endl;
}
}
break;
case 4:{
for(int i = 0;i<8;i++)
{
cout<<player4[i].first<<" "<<player4[i].second<<endl;
}
}
break;
case 0:
display = false;
break;
default:
cout<<"invalid player\n";
}
}
}
int main()
{
int choice; // choice for user to select from the menu;
bool play = true; // boolean to control for how long to run the program

//initialize the placement of cards to get an initial unshuffled deck:-

int j = 0; // index for the deck

//spades placed first
for(int i=7;i<=14;i++)
{ if(i!=14)
deck[j].first = (i);
else
deck[j].first = 1;
deck[j].second = 's';
j++;
}
//clubs
for(int i=7;i<=14;i++)
{ if(i!=14)
deck[j].first = (i);
else
deck[j].first = 1;
deck[j].second = 'c';
j++;
}
//hearts
for(int i=7;i<=14;i++)
{ if(i!=14)
deck[j].first = (i);
else
deck[j].first = 1;
deck[j].second = 'h';
j++;
}
//diamonds
for(int i=7;i<=14;i++)
{ if(i!=14)
deck[j].first = (i);
else
deck[j].first = 1;
deck[j].second = 'd';
j++;
}

while(play)
{
menu();
cin>>choice;
switch(choice)
{
case 1:
shuffleCards();
break;
case 2:
dealCards();
break;
case 3:
displayCards();
break;
case 0:{
cout<<"exit game\n";
play = false;
}
break;
default:
cout<<"invalid choice, please enter again\n";
}

}

}

***************************************************************************************************************************

screen shots of example finish output :-

***************************************************************************************************************

the screen shots of the code are:-

Add a comment
Know the answer?
Add Answer to:
C++ programming: Card game Can same one help me out with a example finsih and working...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program 4: C++ The Game of War The game of war is a card game played by children and budding comp...

    Program 4: C++ The Game of War The game of war is a card game played by children and budding computer scientists. From Wikipedia: The objective of the game is to win all cards [Source: Wikipedia]. There are different interpretations on how to play The Game of War, so we will specify our SMU rules below: 1) 52 cards are shuffled and split evenly amongst two players (26 each) a. The 26 cards are placed into a “to play” pile...

  • C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification...

    C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification file) Card.cpp (class implementation file) DeckOfCards.h (class specification file) DeckOfCards.cpp (class implementation file) 200_assign6.cpp (application program) NU eelLS Seven UT Diamonds Nine of Hearts Six of Diamonds For your sixth programming assignment you will be writing a program to shuffle and deal a deck of cards. The program should consist of class Card, class DeckOfCards and an application program. Class Card should provide: a....

  • War—A Card game Playing cards are used in many computer games, including versions of such classic...

    War—A Card game Playing cards are used in many computer games, including versions of such classics as solitaire, hearts, and poker. War:    Deal two Cards—one for the computer and one for the player—and determine the higher card, then display a message indicating whether the cards are equal, the computer won, or the player won. (Playing cards are considered equal when they have the same value, no matter what their suit is.) For this game, assume the Ace (value 1) is...

  • CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing...

    CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties an integer for the rank (1 (corresponding to Ace) ,2,3, 13 (correspond ing to King) and suit (Spades, Hearts, Diamonds, or Clubs). Make the suit an enumerated data type. Include getters and setters and a method that tests if a card is valid. Write a class named Deck whose instances are full...

  • 2 A Game of UNO You are to develop an interactive game of UNO between a...

    2 A Game of UNO You are to develop an interactive game of UNO between a number of players. The gameplay for UNO is described at https://www.unorules.com/. Your program should operate as follows. 2.1 Setup 1. UNO cards are represented as variables of the following type: typedef struct card_s { char suit[7]; int value; char action[15]; struct card_s *pt; } card; You are allowed to add attributes to this definition, but not to remove any. You can represent colors by...

  • NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET...

    NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET CODE TO RUN!! THANKS Extend the DeckofCards and the Card class in the book to implement a card game application such as BlackJack, Texas poker or others. Your game should support multiple players (up to 5 for BlackJack). You must build your game based on the Cards and DeckofCards class from the book. You need to implement the logic of the game. You can...

  • I've created a Card class and I'm asked to implement a class called DeckOfCards that stores...

    I've created a Card class and I'm asked to implement a class called DeckOfCards that stores 52 objects of the Card class. It says to include methods to shuffle the deck, deal a card, and report the number of cards left in the deck, and a toString to show the contents of the deck. The shuffle methods should assume a full deck. I also need to create a separate driver class that first outputs the populated deck to prove it...

  • Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack...

    Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack class, which implements a BlackJack game. Please do not use any java applet on the coding. Hint: Use a test class to test above classes. Pulic class Card {    private final String face; // face of card ("Ace", "Deuce", ...)    private final String suit; // suit of card ("Hearts", "Diamonds", ...)    // two-argument constructor initializes card's face and suit    public...

  • Please to indent and follow structure!!!!! Assignment 3 - The card game: War Due Date: June...

    Please to indent and follow structure!!!!! Assignment 3 - The card game: War Due Date: June 9th, 2018 @ 23:55 Percentage overall grade: 5% Penalties: No late assignments allowed Maximum Marks: 10 Pedagogical Goal: Refresher of Python and hands-on experience with algorithm coding, input validation, exceptions, file reading, Queues, and data structures with encapsulation. The card game War is a card game that is played with a deck of 52 cards. The goal is to be the first player to...

  • CARD GAME (LINKING AND SORTING) DATA STRUCTURES TOPIC(S) Topic Primary Linked lists Sorting Searching Iterators As...

    CARD GAME (LINKING AND SORTING) DATA STRUCTURES TOPIC(S) Topic Primary Linked lists Sorting Searching Iterators As needed Recursion OBJECTIVES Understand linked lists and sorting concepts and the syntax specific to implementing those concepts in Java. PROJECT The final objective of this project is to create a multi-player card game, but you will create your classes in the order given, as specified. (Analogy: you must have a solid foundation before you can build a house). In order to allow creative freedom,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT