how to write this quetion in C++?

Part 1
Create a standard deck of cards using an array. (1-dimensional or 2-dimensional, up to you). The deck should have 52 cards. The deck should have 13 ranks (1 to King) for each of the 4 suits (Spades, Hearts, Clubs and Diamonds).
After that, add in code that will be able to draw a card randomly from the deck.

Part 2
Next implement a 3-card guessing game that will do the following:
1.Draw ONE card each for LEFT, MID and RIGHT.
2.Indicate (or show) the user the cards drawn and their original position.
3.Clearly point out the card in MID.
4.Flip the cards around and shuffle them. (don’t show the player which card is in which position)
5.Let the player guess where the original MID card is.
6.If the player guess correct, declare the player as a winner. If the player guess wrongly, show where the correct card is and declare the player as loser.
7.Ask the player if he/she wish to play again.
Please note that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.
#include<iostream>
#include<cassert>
#include<fstream>
#include<string>
#include<iomanip>

using namespace std;


const unsigned int DECK_SIZE=52;
const unsigned int MAX_RANK=13;
const unsigned int MAX_SUIT=4;

const string ranks[MAX_RANK]=
{
"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"

};

const string suits[MAX_SUIT]=
{
"Spades","Hearts","Clubs","Diamonds"
};

struct Card
{
string ranks;
string suits;
};
Card deck[DECK_SIZE];
void initializeDeck();

void initializeDeck()
{
int i,j;
int k=0;


for(i=0; i<MAX_RANK; i++)
{
for(j=0; j<MAX_SUIT;j++ )
{
deck[k].ranks=ranks[i];
deck[k].suits=suits[j];
k++;
}
}

}


int main()
{

initializeDeck();


for(int i=0; i<52; i++)
{
int j=(rand()%52);
Card temp=deck[i];
deck[i]=deck[j];
deck[j]=temp;
}


for(int i=0; i<52; i++)
{
cout<<setw(10)<<deck[i].ranks;
cout<<setw(8)<<deck[i].suits;

if (i%4==3)
{
cout<< endl;
}

Card deck[3];

void initializeDeck();

initializeDeck();

int x,y;
int k=0;


for(x=0; x<3; x++)
{
for(y=0; y<4;y++ )
{
deck[k].ranks=ranks[x];
deck[k].suits=suits[y];
k++;
}
}

}




return 0;
}
Last edited on
Search the forum for "shuffle deck" and you'll find several similar threads. Some highlights:
- Think about how you want to represent a card in the code. A single number from 1 to 52? From 0-51? A class with individual "suit" and "rank" members?
- Think about how you want to display a card to the user.
- For this assignment, I see that you only need to display the card. You don't need to do anything with the suit or rank. Even so, give some thought to how you'd get a card's suit and rank and how you'd compare them. Who knows, maybe the next assignment will draw two cards and ask which is "higher."

Once you have a plan for a card, you might write code to create a single card and display a string for it (e.g., "king of hearts" or "2 of diamonds" or just "KH" or "2D". I'd consider testing code for the suit and rank too, but that isn't necessary for this assignment.

Once you have a single card working, you can worry about the deck. One important thing: notice that a deck of 52 cards is nearly identical to the short stack of 3 cards. Each one is just a collection of cards in a specific order. For this reason, I'd think of both as a "hand" of cards. The deck is just a very big hand.

A hand is a vector<card>.
You can use std::shuffle() to shuffle the deck.
To deal a card, take the last card from the hand (hand.back()) and then shrink the hand by one. (hand.pop_back())

Once you have the details for an individual card and the algorithms for shuffling and dealing, the main program is pretty straightforward.
A lot of progress in an hour from being 'clueless' (Nov 10, 2018 at 12:50pm -> Nov 10, 2018 at 2:02pm).

Not to mention finding time to post it elsewhere...
https://www.codeproject.com/Questions/1266275/answer.aspx
https://www.reddit.com/r/learnprogramming/comments/9vtdug/how_to_write_programming_in_c/

Disturbingly similar to this as well.
http://www.cplusplus.com/forum/beginner/243906/

Google + copy/paste + nagging is NOT how you learn to be a programmer.
Topic archived. No new replies allowed.