These are my instructions for my program. Create an object class names DeckOfCar
ID: 3655466 • Letter: T
Question
These are my instructions for my program.
Create an object class names DeckOfCards that models a randomized deck of playing cards. Individual "cards" will be represented by an integer array as follows:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 . . . 51
A 2 3 4 5 6 7 8 9 10 J Q K A 2 3 4 K
The DeckOfCards class should include the following private data members:
- 52 element integer array to represent the shuffled deck of cards
- an integer index to maintain the "next card" to be dealt from the deck
- a single RandomVariable object to provide pseudo-random values for the shuffling method
The class must also provide the following methods:
- a default constructor that will first initialize the values of the card deck array elements 0 through 51, then shuffle the card deck
- a member function dealCard() that will provide the "next" card value from the shuffled deck. If no cards remain, then the deck should be reshuffled (and reset)
- A member function shuffle() that takes no argument and "shuffles" (randomizes) the card deck using the Knuth Shuffle algorithm
For i=n-1 TO 1 REPEAT the following two lines:
j=a random integer from the interval 0<=j<= i
exchange a[i] and a[j]
Write a main() driver function:
- instantiate a DeckOfCards object
- Create an array that will represent a "hand" of dealt cards
- Write a user-defined function (not part of any class) that will take an array of cards representing a single "hand" and the number of cards in the array as arguments and display the card values of the hand on the terminal in the following format:
A 2 4 K 10
- Using the DeckOfCards object, deal 13 seperate 4-card "hands" and display them on the termina, each on a seperate line
Here is my code: (can someone look over it and fix what i did wrong because i keep getting error messages and I don't know how to fix it)
#include<iostream>
using namespace std;
void display(int arr[],int count);
class DeckOfCards
{
public:
DeckOfCards();
void shuffle();
int dealCard();
private:
int deck[52];
int nextCard;
unsigned int pseudoRand();
};
unsigned int DeckOfCards::pseudoRand()
{
unsigned int x;
x = 22695477 * x + 1;
x = xR;
return x;
}
DeckOfCards::DeckOfCards()
{
for(int i=0;i<52;i++)
{
deck[i]=pseudoRand()R;
}
shuffle();
nextCard=0;
}
void DeckOfCards::shuffle()
{
int temp, j;
for(int i=51;i>0;i--)
{
j=pseudoRand();
temp=deck[i];
deck[i]=deck[j];
deck[j]=temp;
}
return;
}
int DeckOfCards::dealCard()
{
nextCard++;
if(nextCard<=52)
return deck[nextCard-1];
else
return -1;
}
int main()
{
DeckOfCards deck;
int hand[4];
for(int i=0;i<13;i++)
{
for(int j=0;j<4;j++)
{
hand[j]=deck.dealCard();
}
display(hand,4);
}
cout<<endl;
return 0;
}
void display(int arr[],int count)
{
cout<<endl;
for(int i=0;i<count;i++)
{
switch(arr[i])
{
case 0:
cout<<"A ";
break;
case 10:
cout<<"J ";
break;
case 11:
cout<<"Q ";
break;
case 12:
cout<<"K ";
break;
case -1:
cout<<"[no card] ";
break;
default:
cout<<arr[i]<<" ";
}
}
return;
}
Explanation / Answer
//forgot to mod arr[] by 13 in display //b/c it's not a real random fxn, it'll always be the same output per run (good for testing //purposes //added setnum() https://www.dropbox.com/s/yln6cx1qrrd97gi/intArrayDeck.txt
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.