DO NOT WRITE IT OUT ON PAPER I CAN NOT READ IT PLEASE TYPE O
DO NOT WRITE IT OUT ON PAPER! I CAN NOT READ IT! PLEASE TYPE OUT THE ENTIRE CODE THANKS!
I need help again I had someone help me from this site but it is not working. please can somone see what is wrong with it and try to fix it please. and do not write it on paper i cant read it please type the entire code out. something is either missing or not typed right and i can not find it. thanks for any and all help.
Here are the instructions for this program.
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.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
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);
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.
//Initialize result.card so that it contains the whole \"deck\" -
//that is, all 52 cards, in order.
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;
}
Solution
Second method:
enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES };
enum Rank { ACE=1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE,
TEN, JACK, QUEEN, KING };
struct Card
{
Rank rank;
Suit suit;
Card (Suit s, Rank r);
};
int index = 0;
for (Suit suit = CLUBS; suit <= SPADES; suit = Suit(suit+1)) {
for (Rank rank = ACE; rank <= KING; rank = Rank(rank+1)) {
deck[index].suit = suit;
deck[index].rank = rank;
index++;
}
}
suit = Suit(suit+1);
rank = Rank(rank+1);
switch (suit) {
case CLUBS: return \"Clubs\";
case DIAMONDS: return \"Diamonds\";
case HEARTS: return \"Hearts\";
case SPADES: return \"Spades\";
default: return \"Not a valid suit\";
}
struct Deck {
pvector<Card> cards;
Deck (int n);
};
Deck::Deck (int size)
{
pvector<Card> temp (size);
cards = temp;
}
Deck::Deck ()
{
pvector<Card> temp (52);
cards = temp;
int i = 0;
for (Suit suit = CLUBS; suit <= SPADES; suit = Suit(suit+1)) {
for (Rank rank = ACE; rank <= KING; rank = Rank(rank+1)) {
cards[i].suit = suit;
cards[i].rank = rank;
i++;
}
}
}
void Deck::print () const {
for (int i = 0; i < cards.length(); i++) {
cards[i].print ();
}
}
int Card::find (const Deck& deck) const {
for (int i = 0; i < deck.cards.length(); i++) {
if (equals (deck.cards[i], *this)) return i;
}
return -1;
}
struct Deck;
// that way we can refer to it in the definition of Card
struct Card
{
int suit, rank;
Card ();
Card (int s, int r);
void print () const;
bool isGreater (const Card& c2) const;
int find (const Deck& deck) const;
};
// and then later we provide the definition of Deck
struct Deck {
pvector<Card> cards;
Deck ();
Deck (int n);
void print () const;
int find (const Card& card) const;
};
for (int i=0; i<cards.length(); i++) {
// choose a random number between i and cards.length()
// swap the ith card and the randomly-chosen card
}
for (int i=0; i<cards.length(); i++) {
// find the lowest card at or to the right of i
// swap the ith card and the lowest card
}
Deck Deck::subdeck (int low, int high) const {
Deck sub (high-low+1);
for (int i = 0; i<sub.cards.length(); i++) {
sub.cards[i] = cards[low+i];
}
return sub;
}
Deck deck; // create a standard 52-card deck
deck.shuffle (); // shuffle it
Then, to deal out several hands, we can use subdeck:
Deck hand1 = deck.subdeck (0, 4);
Deck hand2 = deck.subdeck (5, 9);
Deck pack = deck.subdeck (10, 51);
Deck merge (const Deck& d1, const Deck& d2) {
// create a new deck big enough for all the cards
Deck result (d1.cards.length() + d2.cards.length());
// use the index i to keep track of where we are in
// the first deck, and the index j for the second deck
int i = 0;
int j = 0;
// the index k traverses the result deck
for (int k = 0; k<result.cards.length(); k++) {
// if d1 is empty, d2 wins; if d2 is empty, d1 wins;
// otherwise, compare the two cards
// add the winner to the new deck
}
return result;
}
Deck Deck::mergeSort (Deck deck) const {
// if the deck is 0 or 1 cards, return it
// find the midpoint of the deck
// divide the deck into two subdecks
// sort the subdecks using mergesort
// merge the two halves and return the result
}





