intelektual alternatives

ALGORITMA POKER WITH C-FREE

Posted on: 8 Juni 2011


#include
#include

#define NUM_RANKS 13
#define NUM_SUITS 4
#define NUM_CARDS 5
#define TRUE 1
#define FALSE 0

typedef int bool;
int num_in_rank[NUM_RANKS];
int num_in_suit[NUM_SUITS];
bool straight , flush , four , three;
int pairs ;
void read_cards(void);
void analyze_hand(void);
void print_result(void);
main()
{
for (;;){
read_cards();
analyze_hand();
print_result();
}
}
void read_cards(void)
{
bool card_exists [NUM_RANKS] [NUM_SUITS];
char ch, rank_ch , suit_ch;
int rank, suit;
bool bad_card;
int cards_read=0;

for (rank=0; rank < NUM_RANKS; rank++ ){
num_in_rank[rank]=0;
for (suit = 0; suit <NUM_SUITS; suit++)
card_exists[rank][suit]= FALSE;
}
for (suit = 0; suit <NUM_SUITS; suit++)
num_in_suit[suit]=0;
while (cards_read < NUM_CARDS){
bad_card = FALSE;
printf( ” masukkan kartu : ” );
rank_ch = getchar();
switch (rank_ch) {
case ‘0’ : exit(EXIT_SUCCESS);
case ‘2’ : rank = 0 ; break;
case ‘3’ : rank = 1 ; break;
case ‘4’ : rank = 2 ; break;
case ‘5’ : rank = 3 ; break;
case ‘6’ : rank = 4 ; break;
case ‘7’ : rank = 5 ; break;
case ‘8’ : rank = 6 ; break;
case ‘9’ : rank = 7 ; break;
case ‘t’ : case ‘T’ : rank = 8 ; break;
case ‘j’ : case ‘J’ : rank = 9 ; break;
case ‘q’ : case ‘Q’ : rank = 10 ; break;
case ‘k’ : case ‘K’ : rank = 11; break;
case ‘a’ : case ‘A’ : rank = 12; break;
default : bad_card = TRUE;
}
suit_ch =getchar();
switch (suit_ch){
case ‘c’ : case ‘C’ : suit = 0 ; break;
case ‘d’ : case ‘D’ : suit = 1 ; break;
case ‘h’ : case ‘H’ : suit = 2 ; break;
case ‘s’ : case ‘S’ : suit = 3 ; break;
default : bad_card = TRUE;
}while ((ch = getchar()) !=’\n’)
if (ch !=’ ‘) bad_card = TRUE;
if (bad_card)
printf(“bad card; ignored.\n”);
else if (card_exists[rank] [suit])
printf(“duplicate card; ignored.\n”);
else{
num_in_rank[rank]++;
num_in_suit[suit]++;
card_exists[rank][suit]=TRUE;
cards_read++;
}
}
}
void analyze_hand(void)
{
int num_consec=0;
int rank,suit;

straight = FALSE;
flush = FALSE;
four = FALSE;
three = FALSE;
pairs = 0;

for (suit = 0 ; suit <NUM_SUITS; suit++)
if (num_in_suit[suit] == NUM_CARDS)
flush = TRUE;

rank = 0;
while (num_in_rank[rank]==0) rank++ ;
for (;rank<NUM_RANKS && num_in_rank[rank];rank++)
num_consec++;
if (num_consec == NUM_CARDS)
{
straight = TRUE;
return;
}

for (rank = 0; rank < NUM_RANKS;rank++)
{
if (num_in_rank[rank] == 4 ) four = TRUE ;
if (num_in_rank[rank] == 3 ) three = TRUE ;
if (num_in_rank[rank] == 2 ) pairs++ ;
}
}

void print_result(void)
{
if ( straight && flush ) printf(“straight flush \n\n”);
else if (four) printf(“four of a kind \n\n”);
else if (three && pairs == 1) printf(“full house\n\n”);
else if (flush) printf(“flush\n\n”);
else if (straight) printf(“straight\n\n”);
else if (three) printf(“three of a kind\n\n”);
else if (pairs == 2) printf(“two pairs\n\n”);
else if (four) printf(“pair\n\n”);
else printf(“haigh cards\n\n”);
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: