Code:
/***********************************************************
*
* Program: Poker Game
* Created: 09/12/02 03:38:AM
* Author: Karan Sandhu
* Requested:
* Comments: A small and simple poker game.
*
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_SUITS 4
#define NUM_FACES 13
#define HAND_SIZE 5
#define NUM_HAND 2
#define POINT_SIZE 2
void shuffle( int deck[][ NUM_FACES ] );
void deal( int deck[][ NUM_FACES ], int hand[][HAND_SIZE], int hand_size );
void showhand( const int hand[][HAND_SIZE], int hand_size,
const char *suits[], const char *faces[] );
void getPair( int hand[][HAND_SIZE], int hand_size, const char *suits[], int point[] );
void getPairFace( int hand[][HAND_SIZE], int hand_size, const char *face[], int point[] );
int getSuit( int Card );
int getFace( int Card );
void winner (int point[]);
int main()
{
const char *suit[ NUM_SUITS ] =
{ "Hearts", "Diamonds", "Clubs", "Spades" };
const char *face[ NUM_FACES ] =
{ "Ace", "Deuce", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King" };
int deck[ NUM_SUITS ][ NUM_FACES ] = { { 0 } };
int hand[NUM_HAND][HAND_SIZE];
int point[POINT_SIZE] = {0};
srand( time( NULL ) );
printf( "--------PKœR--------\n" );
printf("©---------ª ©---------ª\n"
"| | | |\n"
"| | | |\n"
"| | | |\n"
"| | | |\n"
"| | Presented | |\n"
"À---------Ù By À---------Ù\n");
printf(" Datanjector \n");
printf("©---------ª ©---------ª\n"
"| | | |\n"
"| | | |\n"
"| | | |\n"
"| | | |\n"
"| | | |\n"
"À---------Ù À---------Ù\n");
printf ("Press <return> to continue with the game.....");
getchar();
system("cls");
shuffle( deck );
deal( deck, hand, HAND_SIZE );
showhand( hand, HAND_SIZE, suit, face );
getPair( hand, HAND_SIZE, suit, point );
getPairFace(hand, HAND_SIZE, face, point );
puts(" ");
winner ( point );
system("pause");
return 0;
}
void shuffle( int wDeck[][ NUM_FACES ] )
{
int row, column, card;
for(card = 1; card <= NUM_FACES*NUM_SUITS; card++) {
do {
row = rand( ) % NUM_SUITS;
column = rand( ) % NUM_FACES;
} while(wDeck[ row ][ column ] != 0);
wDeck[ row ][ column ] = card;
}
}
void deal( int wDeck[][ NUM_FACES ], int hand[][HAND_SIZE], int hand_size )
{
int card, row, column, hand_num;
for (hand_num =0; hand_num <2; hand_num++ )
{
for(card = 0; card < hand_size; card++) {
do {
row = rand( ) % NUM_SUITS;
column = rand( ) % NUM_FACES;
} while(wDeck[ row ][ column ] == 0);
hand[hand_num][card] = wDeck[ row ][ column ];
wDeck[ row ][ column ] = 0; /* this card has been dealt */
}
}
}
void showhand( const int hand[][HAND_SIZE], int hand_size,
const char *suits[], const char *faces[] )
{
int card, suit, face, hand_num;
for ( hand_num =0; hand_num < 2; hand_num++ )
{
for(card = 0 ; card < hand_size ; card++) {
suit = getSuit( hand[hand_num][card] );
face = getFace( hand[hand_num][card] );
printf( "%5s of %-8s%c",
faces[ face ], suits[ suit ], '\n' );
} printf ("\n");
}
}
void getPair( int hand[][HAND_SIZE], int hand_size,
const char *suits[], int point[] )
{
int cnt_main, hand_num;
int s1;
int store[2][5] = {0};
for ( hand_num =0; hand_num < 2; hand_num++ )
{
for(cnt_main =0; cnt_main < hand_size; cnt_main++)
{
s1 = getSuit( hand[hand_num][cnt_main] );
++store[hand_num][s1];
if ( store[hand_num][s1] == 2 )
{
printf ("You have a pair of %s\n", suits[s1]);
++point[hand_num];
}
else if ( store[hand_num][s1] == 4 )
{
printf( "And another pairs of %s\n", suits[s1] );
point[hand_num] += 2;
}
else if ( store[hand_num][s1] == 5)
{
printf( "Congatulation you got a flush" );
point[hand_num] += 5;
}
}
printf ("\n");
}
}
void getPairFace ( int hand[][HAND_SIZE], int hand_size,
const char *face[], int point[] )
{
int cnt_main, hand_num;
int s1;
int store[2][13] = {0};
for ( hand_num =0; hand_num < 2; hand_num++ )
{
for ( cnt_main = 0; cnt_main < hand_size; cnt_main++ )
{
s1 = getFace ( hand[hand_num][cnt_main] );
++store[hand_num][s1];
if ( store[hand_num][s1] == 3 )
{
printf ("You have three %s\n", face[s1]);
point[hand_num] += 3;
}
else if ( store[hand_num][s1] == 4 )
{
printf ("You have four %s\n", face[s1]);
point[hand_num] += 4;
}
else if ( store[hand_num][s1] == 5 )
{
printf( "You have a Straight\n" );
point[hand_num] += 10;
}
}
}
}
int getSuit( int Card ) {
return ( Card-1 )/NUM_FACES;
}
int getFace( int Card ) {
return ( Card-1 )%NUM_FACES;
}
void winner (int point[])
{
if ( point[0] > point[1] )
printf("\nHand 1 wins!!\n");
else if ( point[0] < point[1] )
printf("\nHand 2 wins!!\n");
else
printf ("\nIts a Draw!!\n");
}