/* Shuffle cards*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int card[2][52];
void CreateCards()
{
int i;
for(i=0;i<52;++i)
{
card[0][i]=1+i%13;//store the value of cards
card[1][i]=(int)(i/13);//store the type of cards
}
}
void ShowCards()
{ int i;
int value,type;
for(i=0;i<52;++i)
{
switch(card[0][i])
{
case 1: card[0][i]=65; break;//transform"1" to "A"
case 11:card[0][i]=74; break;//transform"11"to "J"
case 12:card[0][i]=81; break;
case 13:card[0][i]=75; break;
default:card[0][i]=card[0][i];
}
switch(card[1][i])
{
case 0: card[1][i]=83; break;//transform"type1"to"spades"
case 1: card[1][i]=72; break;//transform"type2"to"hearts"
case 2: card[1][i]=67; break;//transform"type3"to"clubs"
case 3: card[1][i]=68; break;//transform"type4"to"diamands"
}
value=card[0][i];
type =card[1][i];
if(value > 13)
printf("%c%c ",value,type);
else
printf("%d%c ",value,type);
}
puts("\n");
}
void ShuffleCards(int times)//times is the times of shuffle
{
int i,Pos1,Pos2,tempv=0,tempt=0;
//Shuffle method:switch cards in Pos1 and Pos2.
for(i=0;i<=times;++i)
{ Pos1 = (int)rand()%52;
Pos2 = (int)rand()%52;
tempv = card[0][Pos1];
tempt = card[1][Pos1];
card[0][Pos1] = card[0][Pos2];
card[1][Pos1] = card[1][Pos2];
card[0][Pos2] = tempv;
card[1][Pos2] = tempt;
}
}
int main()
{ int times;
srand(time(NULL));
printf("Unshuffled cards:\n");
CreateCards();
ShowCards();
printf("Please input the shuffle times(to make an efficient shuffle,try times>=50):");
scanf("%i",×);
puts("shuffling...");
ShuffleCards(times);
ShowCards();
return 0;
}