Can anyone help? I need to make a deck of cards shuffle them and deal them out i
ID: 3790415 • Letter: C
Question
Can anyone help? I need to make a deck of cards shuffle them and deal them out in a c++ program. Please also type it out I have a hard time reading when it's written on paper. Please and thanks for any and all help!!!!!
PLEASE HELP!!! The code works but I can not get it to draw any cards. I don't know what I am doing wrong. PLEASE just someone help me. There is a bolded part in the code is that what is wrong please someone just help me fix it!!!! PLEASE AND THANK YOU!!!
Ok I know I need binary search in here too just not sure how and where can someone please help with that too!!
#include
#include
#include
#include
#include
using namespace std;
struct Card_Type
{
int rank;
int suit; /*integer between 0 and 3 representing the suit of the card:
0 = clubs, 1 = diamonds, 2 = hearts, 3 = spades*/
};
struct Card_Vector
{
int length;
Card_Type card[53];
};
Card_Vector deal_hand_one(int number_of_cards);
Card_Vector deal_hand_two(int number_of_cards);
int binary_search(Card_Type card, Card_Vector &hand);
//Performs a binary search for card in hand.
//Returns the position of card in hand if found;
//otherwise, returns the position into which card should be inserted.
//Note that hand is passed by reference.
int main()
{
Card_Vector bridge_hand;
string array_of_suits[4] = {"Clubs","Diamonds","Hearts","Spades"};
string array_of_ranks[13] = {"Deuce","Three","Four","Five","Six","Seven","Eight",
"Nine","Ten","Jack","Queen","King","Ace"};
string resp = "Yes";
int r,s;
cout << "This program deals two random bridge hands (13 cards)." << endl;
cout << "Each hand is output in increasing order, as defined in bridge." << endl;
cout << endl;
while (resp == "Yes")
{
cout << "Here is your first hand:" << endl;
bridge_hand = deal_hand_one(13);
for (int i = 1; i <= 13; i++)
{
r = bridge_hand.card[i].rank;
s = bridge_hand.card[i].suit;
cout << array_of_ranks[r] << " of " << array_of_suits[s] << endl;
};
cout << endl << endl;
cout << "Here is your second hand:" << endl;
bridge_hand = deal_hand_two(13);
for (int i = 1; i <= 13; i++)
{
r = bridge_hand.card[i].rank;
s = bridge_hand.card[i].suit;
cout << array_of_ranks[r] << " of " << array_of_suits[s] << endl;
};
cout << endl << endl;
cout << "Would you like two more hands? ";
cin >> resp;
cout << endl;
}
system("pause");
return 0;
}
Card_Vector deal_hand_one(int number_of_cards)
{
Card_Vector result;
Card_Type c;
int p;
srand(time(0)); //Seed the random number generator.
//Deal the first card and put it in the hand.
result.card[1].rank = rand() % 13;
result.card[1].suit = rand() % 4;
result.length = 1;
//Deal the rest of the hand.
while (result.length < number_of_cards)
{
//Deal a card.
c.rank = rand() % 13;
c.suit = rand() % 4;
//Use binary_search to check whether that card has aready been dealt.
//If it has, ignore it.
//If it has not, insert it into the hand.
p = binary_search(c,result);
if(p > 1) {
result.card[2].rank = c.rank;
result.card[2].suit = c.suit;
result.length = 2;
}
}
return result;
}
Card_Vector deal_hand_two(int number_of_cards)
{
Card_Vector result;
result.length = 0;
bool deal_it[53]; //deal_it[i] is true iff result.card[i] should be dealt.
int temp; //Used for swapping.
int t; //A random integer in the appropriate range.
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 4; j++)
{
result.card[i].rank = i;
result.card[j].suit = j;
result.length = result.length + 1;
}
}
//Initialize deal_it to all false.
for (int i = 1; i <= 52; i++)
deal_it[i] = false;
srand(time(0)); //Seed the random number generator.
//Make deal_it represent a random (number_of_cards)-combination of {1,2,...,52}.
//That is, deal_it[i] should be true for exactly number_of_cards values of i.
int n = 52; //Number of indices to select from.
int k = number_of_cards; //Number of indices that still need to be selected.
while (k > 0)
{
t = 1 + ((rand() % n)); //Random integer between 1 and n.
if (t <= k)
//Select the index 53 - n.
{
deal_it[53 - n] = true;
k = k - 1;
}
n = n - 1;
}
//Use deal_it to make result.card[1], result.card[2], ..., result.card[number_of_cards]
//a random (number_of_cards)-permutation of the deck.
result.card[number_of_cards].rank = rand() % 13;
result.card[number_of_cards].suit = rand() % 4;
result.length = number_of_cards;
return result;
}
int binary_search(Card_Type card, Card_Vector &hand)
{
int lo = 1; //Assumption: If card is in hand, it is between
int hi = hand.length; //hand.card[lo] and hand.card[hi].
int mid; //Average of lo and hi.
bool found = false; //Indicates whether card has been found.
bool temp = true; // Place holder variable. Should be deleted when coding begins.
while ((lo <= hi) && (!found))
{
mid = (lo + hi)/2;
if (temp)
found = true;
else if (temp)
hi = mid - 1;
else
lo = mid + 1;
}
if (found)
return mid;
else
return lo;
}
Explanation / Answer
#include
#include
#include
#include
#include
using namespace std;
struct Card_Type
{
int rank;
int suit; /*integer between 0 and 3 representing the suit of the card:
0 = clubs, 1 = diamonds, 2 = hearts, 3 = spades*/
};
struct Card_Vector
{
int length;
Card_Type card[53];
};
Card_Vector deal_hand_one(int number_of_cards);
Card_Vector deal_hand_two(int number_of_cards);
int binary_search(Card_Type card, Card_Vector &hand);
//Performs a binary search for card in hand.
//Returns the position of card in hand if found;
//otherwise, returns the position into which card should be inserted.
//Note that hand is passed by reference.
int main()
{
Card_Vector bridge_hand;
string array_of_suits[4] = {"Clubs","Diamonds","Hearts","Spades"};
string array_of_ranks[13] = {"Deuce","Three","Four","Five","Six","Seven","Eight",
"Nine","Ten","Jack","Queen","King","Ace"};
string resp = "Yes";
int r,s;
cout << "This program deals two random bridge hands (13 cards)." << endl;
cout << "Each hand is output in increasing order, as defined in bridge." << endl;
cout << endl;
while (resp == "Yes")
{
cout << "Here is your first hand:" << endl;
bridge_hand = deal_hand_one(13);
for (int i = 1; i <= 13; i++)
{
r = bridge_hand.card[i].rank;
s = bridge_hand.card[i].suit;
cout << array_of_ranks[r] << " of " << array_of_suits[s] << endl;
};
cout << endl << endl;
cout << "Here is your second hand:" << endl;
bridge_hand = deal_hand_two(13);
for (int i = 1; i <= 13; i++)
{
r = bridge_hand.card[i].rank;
s = bridge_hand.card[i].suit;
cout << array_of_ranks[r] << " of " << array_of_suits[s] << endl;
};
cout << endl << endl;
cout << "Would you like two more hands? ";
cin >> resp;
cout << endl;
}
system("pause");
return 0;
}
Card_Vector deal_hand_one(int number_of_cards)
{
Card_Vector result;
Card_Type c;
int p;
srand(time(0)); //Seed the random number generator.
//Deal the first card and put it in the hand.
result.card[1].rank = rand() % 13;
result.card[1].suit = rand() % 4;
result.length = 1;
//Deal the rest of the hand.
while (result.length < number_of_cards)
{
//Deal a card.
c.rank = rand() % 13;
c.suit = rand() % 4;
//Use binary_search to check whether that card has aready been dealt.
//If it has, ignore it.
//If it has not, insert it into the hand.
p = binary_search(c,result);
if(p > 1) {
result.card[2].rank = c.rank;
result.card[2].suit = c.suit;
result.length = 2;
}
}
return result;
}
Card_Vector deal_hand_two(int number_of_cards)
{
Card_Vector result;
result.length = 0;
bool deal_it[53]; //deal_it[i] is true iff result.card[i] should be dealt.
int temp; //Used for swapping.
int t; //A random integer in the appropriate range.
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 4; j++)
{
result.card[i].rank = i;
result.card[j].suit = j;
result.length = result.length + 1;
}
}
//Initialize deal_it to all false.
for (int i = 1; i <= 52; i++)
deal_it[i] = false;
srand(time(0)); //Seed the random number generator.
//Make deal_it represent a random (number_of_cards)-combination of {1,2,...,52}.
//That is, deal_it[i] should be true for exactly number_of_cards values of i.
int n = 52; //Number of indices to select from.
int k = number_of_cards; //Number of indices that still need to be selected.
while (k > 0)
{
t = 1 + ((rand() % n)); //Random integer between 1 and n.
if (t <= k)
//Select the index 53 - n.
{
deal_it[53 - n] = true;
k = k - 1;
}
n = n - 1;
}
//Use deal_it to make result.card[1], result.card[2], ..., result.card[number_of_cards]
//a random (number_of_cards)-permutation of the deck.
result.card[number_of_cards].rank = rand() % 13;
result.card[number_of_cards].suit = rand() % 4;
result.length = number_of_cards;
return result;
}
int binary_search(Card_Type card, Card_Vector &hand)
{
int lo = 1; //Assumption: If card is in hand, it is between
int hi = hand.length; //hand.card[lo] and hand.card[hi].
int mid; //Average of lo and hi.
bool found = false; //Indicates whether card has been found.
bool temp = true; // Place holder variable. Should be deleted when coding begins.
while ((lo <= hi) && (!found))
{
mid = (lo + hi)/2;
if (temp)
found = true;
else if (temp)
hi = mid - 1;
else
lo = mid + 1;
}
if (found)
return mid;
else
return lo;
}