Need help with program

This is my assignment.
Write a program to simulate a deck of 52 playing cards.

Represent your deck as a 2D Array where the value in
the cell is the position of the card in the deck.

Represent the names for the suits and faces as
an array of Strings.

Write an algorithm to shuffle the deck.

Display the deck. For Example:
1 Jack of Spades
2 Deuce of Diamonds
3 Three of Diamonds
4 King of Spades
5 Eight of Clubs
6 King of Diamonds
7 Six of Clubs
8 Six of Spades
9 Eight of Hearts

Deal two hands of five cards each. For Example:

****** HAND 1 ******
Jack of Spades
Three of Diamonds
Eight of Clubs
Six of Clubs
Eight of Hearts
...

Below is what I have so far. All i need help with is dealing two hands and displaying it. How would I be able to do that?

#include <iostream>
#include <ctime>
#include <string>
using namespace std;

void shuffle(int[][2]);
int main()
{

int j,deck[52][2];

string suit[4]={"hearts","diamonds","spades","clubs"};
string card[13]={"Ace","Deuce","Three","Four","Five","Six",
"Seven","Eight","Nine","Ten","Jack","Queen","King"};
srand(time(0));
shuffle(deck);
cout<<"deck-after shuffled:\n";
for(j=0;j<52;j++)
cout<<card[deck[j][1]]<<" of "<<suit[deck[j][0]]<<endl;
cout<<endl;
system("pause");
return 0;
}


void shuffle(int deck[][2])
{bool cards[4][13];
int i,j,num,type;
for(i=0;i<4;i++)
for(j=0;j<13;j++)
cards[i][j]=false;

for(j=0;j<52;j++)
{do
{
num=rand()%13;
type=rand()%4;
}while(cards[type][num]);
deck[j][0]=type;
deck[j][1]=num;
cards[type][num]=true;
}

return;
}


Thanks
You've created a shuffled deck, but you're missing the following instruction:
Represent your deck as a 2D Array where the value in the cell is the position of the card in the deck.

You're also missing an ability to remove cards from the deck. There are more elegant ways to deal with this problem, but they involve structs and vectors and it's not clear that you've covered those.

Given your code, I would do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
  int cards_in_deck = 52;

string draw_card (int deck[][2]) 
  { string result; 
     cards_in_deck--;  // Make sure this doesn't go negative
     result = card[deck[cards_in_deck][0]]
     result += " of "; 
    result = suit[deck[cards_in_deck][1]];
    cards_in_deck--;
    return result;
}
  // Draw 5 cards
  cout << "Player 1 draws: " << endl;
  for (int i=0; i<5; i++)
    cout << draw_card(deck) << endl;


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Edit: The above code was not meant to meet the stated requirement of using a 2D array to represent the position of the card in the deck.
Last edited on
Represent your deck as a 2D Array where the value in the cell is the position of the card in the deck.

I although what you both did makes sense, I don't think either of you have followed this instruction. You both have the first index of the array representing the position in the deck, the value of the array is the card's suit or value. The assignment is asking for this:
int deck[13][4];
where deck[x][y]==z means that the card with value x (ace, 2, 3, ... king) and suit y (club, spade, diamond, heart) is in position z (1...52)

Jaishun, you should check with your professor to see if this is what he/she really means because it makes very little sense to do it this way.
Last edited on
Topic archived. No new replies allowed.