Wednesday, March 12, 2008

Card Shuffle routine in C#

This code example simply shows you how to take a deck of cards and return them to a console "shuffled". If you have any ideas that would make it more realistic, please post a comment. Thx

Here's the code:
//----------------------------------------

class Program
{
public static void Main()
{
int[] array = new int[52];
for( int i = 0; i < 52; i++ )
{
array[i] = i;
}
int[] newArray = new int[52];
bool[] used = new bool[52];

for( int j = 0; j < 52; j++ )
{
used[j] = false;
}
Random rnd = new Random();
int iCount = 0;
int iNum;
while( iCount < 52 )
{
iNum = rnd.Next( 0, 52 ); // between 0 and 51
if( used[iNum] == false )
{
newArray[iCount] = iNum;
used[iNum] = true;
iCount++;
}
}
// End of Shuffle Routine
// Load original array with shuffled array
array = newArray;
for( int n = 0; n < 52; n++ )
{
string cardvalue ="";
switch (array[n])
{
case 0: cardvalue = "AC"; break; case 1: cardvalue = "AH"; break; case 2: cardvalue = "AS"; break; case 3: cardvalue = "AD"; break;
case 4: cardvalue = "KC"; break; case 5: cardvalue = "KH"; break; case 6: cardvalue = "KS"; break; case 7: cardvalue = "KD"; break;
case 8: cardvalue = "QC"; break; case 9: cardvalue = "QH"; break; case 10: cardvalue = "QS"; break; case 11: cardvalue = "QD"; break;
case 12: cardvalue = "JC"; break; case 13: cardvalue = "JH"; break; case 14: cardvalue = "JS"; break; case 15: cardvalue = "JD"; break;
case 16: cardvalue = "10C"; break; case 17: cardvalue = "10H"; break; case 18: cardvalue = "10S"; break; case 19: cardvalue = "10D"; break;
case 20: cardvalue = "9C"; break; case 21: cardvalue = "9H"; break; case 22: cardvalue = "9S"; break; case 23: cardvalue = "9D"; break;
case 24: cardvalue = "8C"; break; case 25: cardvalue = "8H"; break; case 26: cardvalue = "8S"; break; case 27: cardvalue = "8D"; break;
case 28: cardvalue = "7C"; break; case 29: cardvalue = "7H"; break; case 30: cardvalue = "7S"; break; case 31: cardvalue = "7D"; break;
case 32: cardvalue = "6C"; break; case 33: cardvalue = "6H"; break; case 34: cardvalue = "6S"; break; case 35: cardvalue = "6D"; break;
case 36: cardvalue = "5C"; break; case 37: cardvalue = "5H"; break; case 38: cardvalue = "5S"; break; case 39: cardvalue = "5D"; break;
case 40: cardvalue = "4C"; break; case 41: cardvalue = "4H"; break; case 42: cardvalue = "4S"; break; case 43: cardvalue = "4D"; break;
case 44: cardvalue = "3C"; break; case 45: cardvalue = "3H"; break; case 46: cardvalue = "3S"; break; case 47: cardvalue = "3D"; break;
case 48: cardvalue = "2C"; break; case 49: cardvalue = "2H"; break; case 50: cardvalue = "2S"; break; case 51: cardvalue = "2D"; break;

default: cardvalue = "XX"; break;
}
Console.Write( "{0} ", cardvalue );
}
Console.ReadLine();
}

1 comment:

Anonymous said...

A pack of cards starts of shuffled or kind of shuffled in the first place even before it is shuffled for the first round of play. So as for your question to make it more realistic, the array should start off in the last order it was left in.