Posted: Thu Jan 03, 2002 11:12 am
Some thing seems wrong.
There are same cards in the 13 cards of a hand. How could I calc the point?
There are same cards in the 13 cards of a hand. How could I calc the point?
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main() {
char hand[13][4]; /* 13 cards - #/suit */
int score, no_trump_score;
int S_stopped, H_stopped, D_stopped, C_stopped; /* Flags for stopped suits */
int S_num, H_num, D_num, C_num; /* Number of each suit */
char bid[15]; /* Pass, bid suit, bid no trump */
int first_time = 1; /* Flag for first time through loop */
int i;
while( !feof(stdin) ){
/* Print \n if not first time through loop */
if( first_time == 0 ) {
printf("\n");
}
first_time = 0;
/* Initialization */
score = no_trump_score = S_stopped = H_stopped = D_stopped = C_stopped = S_num = H_num = D_num = C_num = 0;
/* Get input, Count cards in each suit as they are input */
for( i = 0; i < 13; i++ ) {
scanf("%s", hand[i]);
switch( hand[i][1] ) {
case 'S': S_num++;
break;
case 'H': H_num++;
break;
case 'D': D_num++;
break;
case 'C': C_num++;
break;
}
}
/* Apply rules 1-4 to score and no_trump_score, also finds stopped suits */
for( i = 0; i < 13; i++ ) {
/* Case: ACE */
if( hand[i][0] == 'A' ) {
/* Rule 1: Add 4 points */
score += 4;
no_trump_score += 4;
/* Stop suit */
switch( hand[i][1] ) {
case 'S': S_stopped = 1;
break;
case 'H': H_stopped = 1;
break;
case 'D': D_stopped = 1;
break;
case 'C': C_stopped = 1;
break;
}
}
/* Case: KING */
if( hand[i][0] == 'K' ) {
/* Rule 1: Add 3 points */
score += 3;
no_trump_score += 3;
/* Rule 2: Subtract 1 point if no other cards in this king's suit */
switch( hand[i][1] ) {
case 'S': if( S_num == 1 )
score--;
no_trump_score--;
break;
case 'H': if( H_num == 1 )
score--;
no_trump_score--;
break;
case 'D': if( D_num == 1 )
score--;
no_trump_score--;
break;
case 'C': if( C_num == 1 )
score--;
no_trump_score--;
break;
}
/* Stop suit if one other card of suit exists */
switch( hand[i][1] ) {
case 'S': if( S_num > 1 )
S_stopped = 1;
break;
case 'H': if( H_num > 1 )
H_stopped = 1;
break;
case 'D': if( D_num > 1 )
D_stopped = 1;
break;
case 'C': if( C_num > 1 )
C_stopped = 1;
break;
}
}
/* Case: QUEEN */
if( hand[i][0] == 'Q' ) {
/* Rule 1: Add 2 points */
score += 2;
no_trump_score += 2;
/* Rule 3: Subtract 1 point if less than 2 other cards in this queen's suit */
switch( hand[i][1] ) {
case 'S': if( S_num < 3 )
score--;
no_trump_score--;
break;
case 'H': if( H_num < 3 )
score--;
no_trump_score--;
break;
case 'D': if( D_num < 3 )
score--;
no_trump_score--;
break;
case 'C': if( C_num < 3 )
score--;
no_trump_score--;
break;
}
/* Stop suit if two other cards of suit exists */
switch( hand[i][1] ) {
case 'S': if( S_num > 2 )
S_stopped = 1;
break;
case 'H': if( H_num > 2 )
H_stopped = 1;
break;
case 'D': if( D_num > 2 )
D_stopped = 1;
break;
case 'C': if( C_num > 2 )
C_stopped = 1;
break;
}
}
/* Case: JACK */
if( hand[i][0] == 'J' ) {
/* Rule 1: Add 1 point */
score += 1;
no_trump_score += 1;
/* Rule 4: Subtract 1 point if less than 3 other cards in this jack's suit */
switch( hand[i][1] ) {
case 'S': if( S_num < 4 )
score--;
no_trump_score--;
break;
case 'H': if( H_num < 4 )
score--;
no_trump_score--;
break;
case 'D': if( D_num < 4 )
score--;
no_trump_score--;
break;
case 'C': if( C_num < 4 )
score--;
no_trump_score--;
break;
}
}
}
/* Apply rules 5-7 to score */
if( S_num == 2 )
score++;
else if( S_num == 1 || S_num == 0 )
score += 2;
if( H_num == 2 )
score++;
else if( H_num == 1 || H_num == 0 )
score += 2;
if( D_num == 2 )
score++;
else if( D_num == 1 || D_num == 0 )
score += 2;
if( C_num == 2 )
score++;
else if( C_num == 1 || C_num == 0 )
score += 2;
/* Find recommended bid */
/* Case: PASS */
if( score < 14 ) {
strcpy( bid, "PASS" );
}
/* Case: BID NO-TRUMP */
else if( no_trump_score >= 16 && S_stopped == 1 && H_stopped == 1 && D_stopped == 1 && C_stopped == 1 ) {
strcpy( bid, "BID NO-TRUMP" );
}
/* Case: BID SUIT */
else {
/* Find suit with highest num */
if( S_num >= H_num && S_num >= D_num && S_num >= C_num ) {
strcpy( bid, "BID S" );
}
else if( H_num > S_num && H_num >= D_num && H_num >= C_num ) {
strcpy( bid, "BID H" );
}
else if( D_num > S_num && D_num > H_num && D_num >= C_num ) {
strcpy( bid, "BID D" );
}
else {
strcpy( bid, "BID C" );
}
}
/* Print result for this hand */
printf( "%s", bid );
}
}
Code: Select all
KS QS AS 2S 3S 4S 5S 6S 7S 8S JS TS 8H
KS QS AC AS 3S 4C 5C 6C 7S 8S JS TS 8H
KD QD AD 2D 3S 4S 5S 6S 7S 8D JS TS 8H
KS QS AS 2D 3D 4C 5H 6H 7H 8S JH TH 8H
KS QS AS AD KD QD AH KH QH AC JC KC 2C
outputs :
BID S
BID S
BID S
PASS
BID NO-TRUMP
Code: Select all
case 'S':
if( S_num == 1 )
score--;
no_trump_score--;
case ...