Monday, August 01, 2005

#include
#include
#include
#include
#include
#include

typedef enum { FALSE, TRUE } boolean;
typedef enum { NO_PATTERN, PAIR, FLUSH, STRAIGHT, PAIR_FLUSH, STRAIGHT_FLUSH, TRIO } rank;
char *rank_rep[] = { "No Pattern", "Pair", "Flush", "Straight", "Pair Flush", "Straight Flush", "Trio"};
typedef enum { FLOWER, SPADE, HEART, DIAMOND, JOKER } suit;
#define SUIT_TYPES 5
/* char suit_ascii[] = { 5, 6, 3, 4 }; */

char suit_ascii[] = { 'f', 's', 'h', 'd' };

/* card value representation */
char *card_rep[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king"};

enum picked { AVAILABLE, TAKEN };
#define MAX_CARDS 54
int cards[MAX_CARDS];
#define CARD_ON_HAND 3
int c_1[CARD_ON_HAND]; /* value from 0 to 53 */
int c_2[CARD_ON_HAND];


int m_1; /* money */
int m_2;
int b1; /* bet */
int b2;
rank r_1; /* rank */
rank r_2;

#define CARD_GROUP 13
void card_initialize()
{
int i;
for( i = 0; i < MAX_CARDS; i++ )
{
cards[i] = AVAILABLE;
}
}


int numbersuit_to_card(int n, suit s)
{
/*
n - 1
note: C is zero based. Ace is zero, 2 is 1, 3 is 2, ...
*/
return (s * CARD_GROUP) + (n-1);
}

char *card_visualize(int card_index)
{
char *cvs; /* card visualize */
int card_number;
int card_suit;

/* 10 is ample enough for visualization of card */
cvs = (char *) malloc(50);



if( card_index == 52 card_index == 53 )
{
strcpy(cvs,"joker");
return cvs;
}
else
{
card_suit = card_index / CARD_GROUP;
card_number = card_index % CARD_GROUP;
sprintf(cvs, "%s %c", card_rep[card_number], suit_ascii[card_suit]);
return cvs;
}
}
int card_to_number(int c)
{
return c % CARD_GROUP;
}

int card_to_suit(int n)
{
return n / CARD_GROUP;
}

void card_visualize_first2()
{
printf("\n\nVisualize first 2 cards");
printf("\nPlayer 1: %s %s", card_visualize(c_1[0]), card_visualize(c_1[1]));
printf("\nPlayer 2: %s %s", card_visualize(c_2[0]), card_visualize(c_2[1]));
}


void card_visualize_last_card()
{
printf("\n\nVisualize unknown/surprise cards");
printf("\nPlayer 1: %s ", card_visualize(c_1[2]));
printf("\nPlayer 2: %s ", card_visualize(c_2[2]));
}

int card_pick()
{

int cp;
do
{
cp = random(MAX_CARDS);
}
while(! (cards[ cp ] == AVAILABLE ));
cards[ cp ] = TAKEN;
return cp;
}
void player_showscore()
{
printf("\n\nCurrent money");
printf( "\t\nPlayer 1 : %d", m_1 );
printf( "\t\nPlayer 2 : %d", m_2 );
}
void player_promptbet()
{
printf("\n\nBet");
printf("\nPlayer 1 : ");
scanf("%d", &b1);
printf("Player 2 : ");
scanf("%d", &b2);
}

void player_shownewscore()
{
printf("\n\nNew money");
printf( "\t\nPlayer 1 : %d", m_1 );
printf( "\t\nPlayer 2 : %d", m_2 );
}
void card_visualize_all()
{
printf("\n\nVisualize sorted cards");
printf("\nPlayer 1: %s %s %s", card_visualize(c_1[0]), card_visualize(c_1[1]), card_visualize(c_1[2]) );
printf("\nPlayer 2: %s %s %s", card_visualize(c_2[0]), card_visualize(c_2[1]), card_visualize(c_2[2]) );
}

int compare(const void *a, const void *b)
{
/* since card index is selection from index 1 to 54(52 + 2(joker)) */
/* we must convert the card index to card_number */

int a_n;
int b_n;
a_n = *((int *)a) % CARD_GROUP;
b_n = *((int *)b) % CARD_GROUP;
return ( a_n - b_n );
}

void card_on_hand_detect_rank()
{

int paircardnumber_checker_1[CARD_GROUP];
int paircardnumber_checker_2[CARD_GROUP];

int flush_checker_1[SUIT_TYPES];
int flush_checker_2[SUIT_TYPES];

boolean p1_is_flush, p1_is_straight, p1_is_pair;
boolean p2_is_flush, p2_is_straight, p2_is_pair;



int i;

p1_is_flush = 0;
p1_is_straight = 0;
p1_is_pair = 0;
p2_is_flush = 0;
p2_is_straight = 0;
p2_is_pair = 0;
for (i = 0; i < CARD_GROUP; i++)
{
paircardnumber_checker_1[i] = FALSE;
paircardnumber_checker_2[i] = FALSE;
}


for (i = 0; i < 6; i++)
{
flush_checker_1[i] = 0;
flush_checker_2[i] = 0;
}


r_1 = 0;
/* cards are already sorted, so we can immediately check straightness */
if (card_to_number(c_1[0]) + 1 == card_to_number(c_1[1]) &&
card_to_number(c_1[1]) + 1 == card_to_number(c_1[2]))
{
p1_is_straight = TRUE;
r_1 = STRAIGHT;
}

if (card_to_number(c_2[0]) + 1 == card_to_number(c_2[1]) &&
card_to_number(c_2[1]) + 1 == card_to_number(c_2[2]))
{
p2_is_straight = TRUE;
r_2 = STRAIGHT;
}


/* pair checker... */
paircardnumber_checker_1[ card_to_number(c_1[0]) ]++;
paircardnumber_checker_1[ card_to_number(c_1[1]) ]++;
paircardnumber_checker_1[ card_to_number(c_1[2]) ]++;
paircardnumber_checker_2[ card_to_number(c_2[0]) ]++;
paircardnumber_checker_2[ card_to_number(c_2[1]) ]++;
paircardnumber_checker_2[ card_to_number(c_2[2]) ]++;

for(i = 0; i < CARD_GROUP; i++)
{
if (paircardnumber_checker_1[i] == 2)
{
p1_is_pair = TRUE;
r_1 = PAIR;
break;
}
else if (paircardnumber_checker_1[i] == 3)
{
r_1 = TRIO;
break;
}
}
for(i = 0; i < CARD_GROUP; i++)
{
if (paircardnumber_checker_2[i] == 2)
{
p2_is_pair = TRUE;
r_2 = PAIR;
break;
}
else if (paircardnumber_checker_2[i] == 3)
{
r_2 = TRIO;
break;
}
}

/* ...pair checker */

/* flush checker... */


flush_checker_1[ card_to_suit(c_1[0]) ]++;
flush_checker_1[ card_to_suit(c_1[1]) ]++;
flush_checker_1[ card_to_suit(c_1[2]) ]++;
flush_checker_2[ card_to_suit(c_2[0]) ]++;
flush_checker_2[ card_to_suit(c_2[1]) ]++;
flush_checker_2[ card_to_suit(c_2[2]) ]++;


for (i = 0; i < SUIT_TYPES; i++ )
{
if (flush_checker_1[ i ] == 3)
{
r_1 = FLUSH;
p1_is_flush = TRUE;
break;
}
}
for (i = 0; i < SUIT_TYPES; i++ )
{
if (flush_checker_2[ i ] == 3)
{
r_2 = FLUSH;
p2_is_flush = TRUE;
break;
}
}


/* ...flush checker */

if (p1_is_pair && p1_is_flush)
{
r_1 = PAIR_FLUSH;
}
if (p1_is_straight && p1_is_flush)
{
r_1 = STRAIGHT_FLUSH;
}


if (p2_is_pair && p2_is_flush)
{
r_2 = PAIR_FLUSH;
}
if (p2_is_straight && p2_is_flush)
{
r_2 = STRAIGHT_FLUSH;
}



}

void showrank()
{
printf("\n\nPlayer 1 rank: %d %s",r_1, rank_rep[r_1]);
printf("\nPlayer 2 rank: %d %s",r_1, rank_rep[r_1]);
}
void showchecker()
{
printf("\n\nRank: %d, combination type: %s", r_1, rank_rep[r_1]);
}

suit char_to_suit(char s)
{
switch(s)
{
case 'f':
return FLOWER;
case 's':
return SPADE;
case 'd':
return DIAMOND;
case 'h':
return HEART;
}
}

void card_distribute()
{
char ans;
char ch;
int c1n, c2n, c3n;
char c1s, c2s, c3s;
suit s1, s2, s3;

m_1 = 50;
m_2 = 50;



printf("r)andom or c)hecker ? ");
ch = toupper(getchar());

if (ch == 'R')
{
do
{


card_initialize();



printf("\n\nPoker--------------------\n\n");


/* player 1 first 2 cards */
c_1[0] = numbersuit_to_card(1, SPADE); /*card_pick();*/
c_1[1] = numbersuit_to_card(2, SPADE); /* card_pick(); */

/* player 2 first 2 cards */
c_2[0] = card_pick();
c_2[1] = card_pick();
card_visualize_first2();



player_showscore();

c_1[2] = numbersuit_to_card(3, SPADE); /* card_pick(); */
c_2[2] = card_pick();

player_promptbet();
card_visualize_last_card();


qsort (c_2, 3, sizeof(int), compare);

player_shownewscore();

card_on_hand_detect_rank();
card_visualize_all();



printf("\n\nPlay Again? ");
/* flush the enter character from bet input */
/* or alternative use getchar() instead of flush */
fflush(stdin);
ans = toupper(getchar());

}
while(! ( ans == 'N' ) );
}
else
{
do
{
printf("\n\nEnter cards: ");

scanf("%d %c %d %c %d %c", &c1n, &c1s, &c2n, &c2s, &c3n, &c3s);

s1 = char_to_suit(c1s);
s2 = char_to_suit(c2s);
s3 = char_to_suit(c3s);
c_1[0] = numbersuit_to_card(c1n, s1); /* card_pick(); */
c_1[1] = numbersuit_to_card(c2n, s2); /* card_pick(); */
c_1[2] = numbersuit_to_card(c3n, s3); /* card_pick(); */
qsort (c_1, 3, sizeof(int), compare);
card_on_hand_detect_rank();
showchecker();
printf("\nAgain Y/N? ");
fflush(stdin);
ans = toupper(getchar());
}
while (! (ans == 'N') );
}
}




void main()
{
clrscr();
card_initialize();
card_distribute();


}

0 Comments:

Post a Comment

<< Home