Code:
#include <stdio.h>
#include <stdlib.h> /* include stdlib.h for rand functionality */
#include <time.h> /* include time.h for srand seed */
int main( void )
{
/* initialize variables */
int draw(); /* draw function prototype */
int hold; /* variable for bubble sorting hands */
int parray[5] = {0}; /* player's hand */
int carray[5] = {0}; /* computer's plan */
int pass; /* passes for sorting */
int i; /* counter for sorting */
/* seed random number generator */
srand((unsigned) time(0));
/* Draw hands*/
parray[0] = draw(); parray[1] = draw(), parray[2] = draw(); parray[3] = draw(); parray[4] = draw();
carray[0] = draw(); parray[1] = draw(), carray[2] = draw(); carray[3] = draw(); carray[4] = draw();
/* bubble sort player's hand*/
for( pass = 0; pass < 5; pass++)
{
/* loop to control number of comparisons per pass */
for ( i = ( pass + 1 ); i < 5; i++)
{
/* compare adjacent elements and swap them if first element is greater than second element */
if ( parray[ pass ] > parray[ i ] )
{
hold = parray[ pass ];
parray[ pass ] = parray[ i ];
parray[ i ] = hold;
}
}
}
/* bubble sort computer's hand*/
for( pass = 0; pass < 5; pass++)
{
/* loop to control number of comparisons per pass */
for ( i = ( pass + 1 ); i < 5; i++)
{
/* compare adjacent elements and swap them if first element is greater than second element */
if ( carray[ pass ] > carray[ i ] )
{
hold = carray[ pass ];
carray[ pass ] = carray[ i ];
carray[ i ] = hold;
}
}
}
/* assign array values to corresponding cards for easier human understanding */
/* print hands */
printf( "%d, %d, %d, %d, %d\t\t%d, %d, %d, %d, %d\n", parray[0], parray[1], parray[2], parray[3], parray[4], carray[0], carray[1], carray[2], carray[3], carray[4]);
printf( " Player \t \t \t Computer\n\n");
return 0; /* indicate program ended normally */
}
int draw ()
{
static int cards[ 52 ] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52};
int chosen = 0;
int card;
chosen = rand () % 52;
card = cards[ chosen ];
while ( cards[ chosen ] == 99 ) {
draw();
}
cards[ chosen ] = 99;
return card;
}