PLEASE HELP!!! The code works but I can not get it to draw any cards. I don\'t k
ID: 3789747 • Letter: P
Question
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 THANKS!!!!!
How do I figure that out. I've been looking up information and I don'treally know how to do it
#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
The code was wrong in two places - binary_search and deal_hand_one. Ive corrected that and now your code works fine. have a look at the output at the end. Since i dont have the requirements, I wasn't able to verify the output.
PROGRAM CODE:
/*
* cards.cpp
*
* Created on: 11-Feb-2017
* Author: kasturi
*/
#include <iostream>
#include <string>
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.length = 0;
result.card[result.length].rank = rand() % 13;
result.card[result.length].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 already 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[result.length].rank = c.rank;
result.card[result.length].suit = c.suit;
result.length++;
}
}
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(hand.card[mid].rank < card.rank)
lo = mid +1;
else if(hand.card[mid].rank == card.rank && hand.card[mid].suit == card.suit)
found = true;
else hi = mid -1;
}
if (found)
return mid;
else
return lo;
}
OUTPUT:
This program deals two random bridge hands (13 cards).
Each hand is output in increasing order, as defined in bridge.
Here is your first hand:
Three of Clubs
Four of Hearts
Four of Diamonds
Five of Hearts
Eight of Diamonds
Ace of Clubs
Ace of Diamonds
King of Spades
Eight of Hearts
Eight of Clubs
Ten of Spades
Eight of Hearts
Deuce of Clubs
Here is your second hand:
Three of Diamonds
Four of Hearts
Five of Spades
Six of Clubs
Seven of Clubs
Eight of Clubs
Nine of Clubs
Ten of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Clubs
Three of Diamonds
Would you like two more hands? no
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.