/*
* Pointers 712 : Five Hand Card
*
* Created on: Feb 18, 2012
* Author: aeriqusyairi
*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//main functions
void shuffle( int wDeck[][ 13 ] );
void deal( const int wDeck[][ 13 ], const char *wface[], const char *wSuit[], char *wfSuit[], char *wfFace[] );
//operation functions
void pair( const char *wfSuit[], const char *wfFace[]);
void twoPair( const char *wfSuit[], const char *wfFace[] );
void threeKind( const char *wfSuit[], const char *wfFace[] );
void fourKind( const char *wfSuit[], const char *wfFace[] );
void flush( const char *wfSuit[], const char *wfFace[] );
void straight( const char *wfSuit[], const char *wfFace[], const char *wFace[] );
//sub-operation functions
void result( const int compare, const int compareTo, const char *wfFace[], const char *wfSuit[], const char statement[] );
int main(void){
//initilize suit array
const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
//initialize face array
const char *face[ 13 ] = { "Ace", "Deuce", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King" };
//initialize suit and face array for storing five-hand card
char *fSuit[ 5 ] ;// "Hearts", "Hearts", "Hearts", "Hearts", "Hearts" };
char *fFace[ 5 ] ;// "Seven", "Ace", "Ace", "Four", "Five" };
//initialize deck array
int deck[ 4 ][ 13 ] = { 0 };
//seed random number generator
srand( time( 0 ) );
shuffle( deck );//shuffle the deck
deal( deck, face, suit, fSuit, fFace );//deal five-hand card
pair( fSuit, fFace );//compute for pair
twoPair( fSuit, fFace );//compute for two-pair
threeKind( fSuit, fFace );//compute for three of a kind
fourKind( fSuit, fFace );//compute for four of the kind
flush( fSuit, fFace );//compute for flush
straight( fSuit, fFace, face );//compute for straight
system("pause");
return 0;
}
void shuffle( int wDeck[][ 13 ] ){
int row, column, card;
for(card = 1; card <= 52; card++){
do{
row = rand() % 4;
column = rand() % 13;
}while(wDeck[ row ][ column ] != 0);
wDeck[ row ][ column ] = card;
}
}
void deal( const int wDeck[][ 13 ], const char *wFace[], const char *wSuit[], char *wfSuit[], char *wfFace[] ){
int card, row, column, i;
//deal 5-card poker hand
for(card = 1, i = 0; card <= 5; card++, i++){
for(row = 0; row < 4; row++){
for(column = 0; column < 13; column++){
if(wDeck[ row ][ column ] == card){
wfSuit[ i ] = wSuit[ row ];
wfFace[ i ] = wFace[ column ];
}
}
}
}
//display 5-card poker hand
printf("Your five-card poker hand...\n\n");
for(i = 0; i < 5; i++){
printf("%10s of %-8s\n", wfFace[ i ], wfSuit[ i ]);
}
}
void pair(const char *wfSuit[], const char *wfFace[]){
int card, i, j = 0, k;
int pair[ 5 ] = { 0 };
//compute whether the five-hand card contains a pair
printf("\nPair...\n");
for(card = 0; card < 5; card++){
for(i = card + 1; i < 5; i++){
if(wfFace[ card ] == wfFace[ i ]){
j++;
pair[ card ] = i;
}
}
}
//display result
if(j == 1){
for(card = 0; card < 5; card++){
if(pair[ card ] != 0){
k = pair[ card ];
printf("\n%10s of %-8s and %5s of %-8s\n", wfFace[ card ], wfSuit[ card ], wfFace[ k ], wfSuit[ k ] );
}
}
}else{
printf("\n Your five-hand card doesn't contains any pair.\n");
}
}
void twoPair( const char *wfSuit[], const char *wfFace[] ){
int card, i, j = 0, k;
int pair[ 5 ] = { 0 };
//compute whether the five-hand card contains two pair
printf("\nTwo-pair...\n");
for(card = 0; card < 5; card++){
for(i = card + 1; i < 5; i++){
if(wfFace[ card ] == wfFace[ i ]){
j++;
pair[ card ] = i;
}
}
}
//display result
if(j == 2){
for(card = 0; card < 5; card++){
if(pair[ card ] != 0){
k = pair[ card ];
printf("\n%10s of %-8s and %5s of %-8s\n", wfFace[ card ], wfSuit[ card ], wfFace[ k ], wfSuit[ k ] );
}
}
}else{
printf("\n Your five-hand card doesn't contains two pair.\n");
}
}
void threeKind( const char *wfSuit[], const char *wfFace[] ){
int card, i, j = 0, k, x;
int pair[ 3 ] = { 0 };
//compute whether the five-hand card contains three of a kind
printf("\nThree of a kind...\n");
for(card = 0; card < 3; card++){
for(i = card + 1; i < 5; i++){
if(wfFace[ card ] == wfFace[ i ]){
for(x = i + 1; x < 5; x++){
if(wfFace[ i ] == wfFace[ x ]){
j++;
pair[ 0 ] = card;
pair[ 1 ] = i;
pair[ 2 ] = x;
}
}
}
}
}
//display result
if(j == 1){
for(card = 0; card < 3; card++){
k = pair[ card ];
printf("\n%10s of %-8s\n", wfFace[ k ], wfSuit[ k ] );
}
}else{
printf("\n Your five-hand card doesn't contains three of a kind.\n");
}
}
void fourKind( const char *wfSuit[], const char *wfFace[] ){
int card, i, indi = 0, pair[ 5 ] = { 0 };
//compute whether your five-hand card contains four-of-a-kind
printf("\nFour of a kind...\n");
for(card = 0; card < 2; card++){
for(i = card + 1; i < 5; i++){
if(wfFace[ card ] == wfFace[ i ]){
indi++;
if(indi == 1)
pair[ i - 1 ] = 1;
pair[ i ] = 1;
}
}
if(indi == 3)
break;
else if(indi > 3)
break;
else{
indi = 0;
for(i = 0; i < 5; i++)
pair[ i ] = 0;
}
}
//display result
if(indi == 3){
for(i = 0; i < 5; i++){
if( pair[ i ] == 1)
printf("\n%5s of %-9s\n", wfFace[ i ], wfSuit[ i ] );
}
printf("\n");
}else{
printf("\n Your five-hand card doesn't contains four-of-a-kind.\n");
}
}
void flush ( const char *wfSuit[], const char *wfFace[] ){
int i, j = 0;
//compute whether your five-hand card contains a flush
printf("\nFlush...\n");
for(i = 1; i < 5; i++){
if(wfSuit[ 0 ] == wfSuit[ i ] )
j++;
}
//display result
result( j, 0, wfFace, wfSuit, "\n Not flush!\n" );
}
void straight( const char *wfSuit[], const char *wfFace[], const char *wFace[] ){
int i, j, k, pass, count, hold, check = 1;
int faceValue[ 5 ] = { 0 };
printf("\nStraight...\n");
//locate face value and store in an array
for(i = 0 ; i < 5; i++){
for(j = 0 ; j < 13; j++){
if(wfFace[ i ] == wFace[ j ]){
faceValue[ i ] = j;
}
}
}
//sort face value in ascending order using bubble sort
for(pass = 0; pass < 4; pass++){
for(count = 0; count < 4; count++){
if(faceValue[ count ] > faceValue[ count + 1 ]){
//swap
hold = faceValue[ count ];
faceValue[ count ] = faceValue[ count + 1 ];
faceValue[ count + 1 ] = hold;
}
}
}
//check if the hand contains a straight
for(i = 0; i < 4; i++){
if(faceValue[ i ] + 1 != faceValue[ i + 1 ]){
check = 0;
}
}
//display result
result( check, 1, wfFace, wfSuit, "\n Not a straight hand.\n" );
printf("\n");
}
//sub-operation functions
void result( const int compare, const int compareTo, const char *wfFace[], const char *wfSuit[], const char statement[] ){
int i;
if(compare == compareTo){
for(i = 0; i < 5; i++){
printf("\n %5s of %-8s\n", wfFace[ i ], wfSuit[ i ] );
}
}else{
printf("%s", statement );
}
}
My C Repository
C How to Program
Sweet thanks to my broski Yusuke Fiz for this Deitel book.
You light up my life dude.
Just another personal public repository.
Friday, March 16, 2012
2) Card Shuffling and Dealing: Basic Five-card Poker Hand
Deals a five-card poker hand and determine if the hand contains a pair, two pair, three of a kind, four of a kind, flush, and straight.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment