Answer to Question #125018 in C++ for Shafaqat

Question #125018
In the game of contract bridge, each of four players is dealt 13 cards, thus exhausting the entire deck. Modify the CARDARAY program given in Chapter 7 Arrays and Strings (by Robert Lafore) after shuffling the deck, it deals four hands of 13 cards each. Each of the four players’ hands should then be displayed.
1
Expert's answer
2020-07-07T09:54:44-0400
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
enum Suit { clubs, diamonds, hearts, spades };
const int jack = 11;
const int queen = 12;
const int king = 13;
const int ace = 14;


class card
{
private:
	int number;
	Suit suit;
public:
	card() { }
	void set(int n, Suit s) 
	{
		suit = s; number = n;
	}
	void display();
};


void card::display()
{
	if (number >= 2 && number <= 10)
		cout << number;
	else
	{
		switch (number)
		{
			case jack: cout << 'J'; break;
			case queen: cout << 'Q'; break;
			case king: cout << 'K'; break;
			case ace: cout << 'A'; break;
		}
	}
	switch (suit)
	{
		case clubs: cout << char(5); break;
		case diamonds: cout << char(4); break;
		case hearts: cout << char(3); break;
		case spades: cout << char(6); break;
	}
}
void Hand_Display(card*);


int main()
{
	card* deck = new card[52];
	for (size_t i = 0; i < 52; i++)
	{
		int num = (i % 13) + 2;
		Suit su = Suit(i / 13);
		deck[i].set(num, su);
	}


	srand(time(NULL)); 
	for (size_t i = 0; i < 52; i++)
	{
		int k = rand() % 52;
		card temp = deck[i];
		deck[i] = deck[k];
		deck[k] = temp;
	}
		
	card hand1[13];
	card hand2[13];
	card hand3[13];
	card hand4[13];


	for (size_t i = 0; i < 52; i++)
	{
		if (i < 13)
			hand1[i] = deck[i];
		if (12 < i && i < 26)
			hand2[i - 13] = deck[i];
		if (25 < i && i < 39)
			hand3[i - 26] = deck[i];
		if (38 < i && i < 52)
			hand4[i - 39] = deck[i];
	}
	delete[] deck;
	cout << "\nHand1:\n";
	Hand_Display(hand1);
	cout << "\nHand2:\n";
	Hand_Display(hand2);
	cout << "\nHand3:\n";
	Hand_Display(hand3);
	cout << "\nHand4:\n";
	Hand_Display(hand4);
	return 0;
}
void Hand_Display(card* hand)
{
	for (size_t i = 0; i < 13; i++)
	{
		hand[i].display();
		cout << " ";
	}
	cout << "\n";
}
//for showing suits in console You might need to switch on "dotted font style"

//in console properties

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS