0 points

pulling 4 random cards from a deck


dashboard

Wed Nov 18 2020 05:02:50 GMT+0000 (UTC)

Posted by @mahmoud hussein

#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
	const int NUMBER_OF_CARDS = 52;
	int deck[NUMBER_OF_CARDS];
	string suits[] = { "Spades", "Hearts", "Diamonds","Clubs" };
	string ranks[] = { "Ace","2","3","4","5","6","7","8","9","10""Jack","King","Queen" };

	for (int  i = 0; i < NUMBER_OF_CARDS; i++)
	
		deck[i] = i;

		srand(time(0));

	for (size_t i = 0; i < NUMBER_OF_CARDS; i++)
		{
		int index = rand() % NUMBER_OF_CARDS;
		int temp = deck[i];
		deck[i] = deck[index];
		deck[index] = temp;
		}
	for (int i = 0; i < 4; i++)
	{
		string suit = suits[deck[i] / 13];
		string rank = ranks[deck[i] % 13];
		cout << "Card Number " << deck[i] << ": "
			<< rank << " of " << suit << endl;
	}
	return 0;

}
content_copy Copy

http://cpp.sh/