Thursday, April 18, 2013

How to Shuffle Cards in Card Game using C++ in OOP

By Unknown   Posted at  4:56 AM   C++ Programming No comments

#include<iostream>
#include<conio.h>
#include<ctime>
#include<stdlib.h>
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{
int number;
Suit suit;
public:
void set(int n,Suit s){
number=n;
suit=s;}
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;
} }
/*#############################*/
int main(){

card deck[52];
int j;
cout<<endl;
    for(j=0;j<52;j++){
    int num=(j%13)+2;
    Suit su=Suit(j/13);
    deck[j].set(num,su);       }
    cout<<"\n Ordered deck:\n";
          for(j=0;j<52;j++){
          deck[j].display();
                   cout<<"     ";
                   if(!((j+1)%13))
                   cout<<endl;
                   }
             srand(time(0));
                   for(j=0;j<52;j++){
                                    
                   int k =rand()%52;
                   card temp=deck[j];
                   deck[j]=deck[k];
                   deck[k]=temp;}
                cout<<"\n  shuffled  deck:\n";
            for(j=0;j<52;j++)
            {
            deck[j].display();
            cout<<"       ";
            if(!((j+1)%13))
            cout<<endl; }
            system("pause");
            getch();
            }

About the Author

Nulla sagittis convallis arcu. Sed sed nunc. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.
View all posts by: BT9

0 comments:

Back to top ↑
Connect with Us

    What's Trending in Videoism

    Total Pageviews

    Copy of any Post is Not Allowed. All Rights Reserved with Matee ur Rehman (Author). Powered by Blogger.

    Advertisements

    Follow us on FaceBook

What they says

© 2013 GC Study Timez. WP Daily Quranand Hadith Converted by Matee Gojra
Blogger templates. Proudly Powered by Blogger.