Question
please help me with this coding program please and thanks write a c++ program that deals random sorted bridge hands using two different methods. Bridge
please help me with this coding program please and thanks
write a c++ program that deals random sorted "bridge hands" using two different methods.
Bridge is a card game that uses a standard deck of 52 cards. There are four players, and each player is dealt a hand consisting of 13 cards. Although not required, virtually every player sorts his/her hand according to the following rule: (1) Clubs come before diamonds, diamonds come before hearts, and hearts come before spades; (2) Within a given suit, the ranks are ordered in the usual way, with aces high; that is
deuce < three < ... < king < ace
The first method you will use to deal a random sorted hand mimics the way many players operate. That is, as each card is dealt, the player picks it up and inserts it into her hand so that it is in the proper position relative to the other cards.
The second method uses the fact that a sorted hand is a (random) 13-combination of the 52-card deck
this is the template for the assignment thanks for any and all help #include#include #include #include #include using namespace std; struct Card_Type { int rank; //integer between 0 and 12 representing the rank of the card: //0 = deuce, 1 = three, ..., 11 = king, 12 = ace 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]; //Don't use location 0. }; Card_Vector deal_hand_one(int number_of_cards); //Returns a "hand" containing the indicated number of cards. Card_Vector deal_hand_two(int number_of_cards); //Returns a "hand" containing the indicated 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); //Code needed here. } return result; } Card_Vector deal_hand_two(int number_of_cards) { Card_Vector result; bool deal_it[52]; //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. //Initialize result.card so that it contains the whole "deck" - //that is, all 52 cards, in order. //Code needed here. //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. //Code needed here. 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; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started