2013-07-02 09:27:44Morris

[UVA] 462 - Bridge Hand Evaluator


 Bridge Hand Evaluator 

In the card game ``Bridge'', players must assess the strength of their hands prior to bidding against one another. Most players use a point-count scheme which employs the following rules:

  1. Each ace counts 4 points. Each king counts 3 points. Each queen counts 2 points. Each jack counts one point.
  2. Subtract a point for any king of a suit in which the hand holds no other cards.
  3. Subtract a point for any queen in a suit in which the hand holds only zero or one other cards.
  4. Subtract a point for any jack in a suit in which the hand holds only zero, one, or two other cards.
  5. Add a point for each suit in which the hand contains exactly two cards.
  6. Add two points for each suit in which the hand contains exactly one card.
  7. Add two points for each suit in which the hand contains no cards.

A suit is stopped if it contains an ace, or if it contains a king and at least one other card, or if it contains a queen and at least two other cards.

During the opening assessment, the three most common possibilities are:

  • If the hand evaluates to fewer than 14 points, then the player must pass.
  • One may open bidding in a suit if the hand evaluates to 14 or more points. Bidding is always opened in one of the suits with the most cards.
  • One may open bidding in ``no trump'' if the hand evaluates to 16 or more points ignoring rules 5, 6, and 7 and if all four suits are stopped. A no trump bid is always preferred over a suit bid when both are possible.

Input

Input to your program consists of a series of lines, each line containing 13 cards. Each card consists of two characters. The first represents the rank of the card: `A', `2', `3', `4', `5', `6', `7', `8', `9', `T', `J', `Q', `K'. The second represents the suit of the card: `S', `H', `D', `C', standing for ``spades'', ``hearts'', ``diamonds'', and ``clubs'', respectively.

Output

For each line of the input, print one line containing the recommended bid, either ``PASS'', the first ``BID suit'', where suit is ``S'', ``H'', ``D'', or ``C'' (sorted in this natural way if two or more are possible), or ``BID NO-TRUMP''.

For the first example below, the evaluation starts with 6 points for the two kings, 4 for the ace, 6 for the three queens, and one for the jack. To this tally of 17 points, we add 1 point for having only two cards in spades, and subtract 1 for having a queen in spaces with only one other card in spades. The resulting 17 points is enough to justify opening in a suit.

The evaluation for no-trump is 16 points, since we cannot count the one point for having only two spades. We cannot open in no-trump, however, because the hearts suit is not stopped.

Hence we must open bidding in a suit. The two longest suits are clubs and diamonds, with four cards each, so the possible outputs are ``BID C'', or ``BID D''.

Sample Input

KS QS TH 8H 4H AC QC TC 5C KD QD JD 8D
AC 3C 4C AS 7S 4S AD TD 7D 5D AH 7H 5H

Sample Output

BID D
BID NO-TRUMP

根據規則計算手牌的點數, 並且進行招標。
如果點數不夠, 喊出 PASS,
如果點數夠, 而且可以喊出 NO-TRUMP 的話, 先喊這個,
否則喊最多牌的那門花色

#include <stdio.h>
int trans(char c) {
switch(c) {
case 'T':return 10;
case 'J':return 11;
case 'Q':return 12;
case 'K':return 13;
case 'A':return 1;
}
return c-'0';
}
int main() {
char s[5];
while(true) {
int card[4][15] = {};
int S, H, D, C;
int i, j, k;
S = H = D = C = 0;
int pt = 0, pt2 = 0;
for(i = 0; i < 13; i++) {
if(scanf("%s", &s) != 1)
return 0;
int x = trans(s[0]);
if(s[1] == 'S')card[0][x] = 1, S++;
if(s[1] == 'H')card[1][x] = 1, H++;
if(s[1] == 'D')card[2][x] = 1, D++;
if(s[1] == 'C')card[3][x] = 1, C++;
if(x == 1) pt +=4;
else if(x == 11) pt += 1;
else if(x == 12) pt += 2;
else if(x == 13) pt += 3;
}
if(card[0][13] && S-1 == 0) pt--;
if(card[1][13] && H-1 == 0) pt--;
if(card[2][13] && D-1 == 0) pt--;
if(card[3][13] && C-1 == 0) pt--;
if(card[0][12] && S-1 < 2) pt--;
if(card[1][12] && H-1 < 2) pt--;
if(card[2][12] && D-1 < 2) pt--;
if(card[3][12] && C-1 < 2) pt--;
if(card[0][11] && S-1 < 3) pt--;
if(card[1][11] && H-1 < 3) pt--;
if(card[2][11] && D-1 < 3) pt--;
if(card[3][11] && C-1 < 3) pt--;
if(S == 2) pt2++;
if(H == 2) pt2++;
if(D == 2) pt2++;
if(C == 2) pt2++;
if(S == 0 || S == 1) pt2+=2;
if(H == 0 || H == 1) pt2+=2;
if(D == 0 || D == 1) pt2+=2;
if(C == 0 || C == 1) pt2+=2;
int stop[4] = {};
for(i = 0; i < 4; i++) {
int x = 0;
for(j = 1; j <= 13; j++) x+=card[i][j];
if(card[i][1])
stop[i] = 1;
else if(card[i][13] && x > 1)
stop[i] = 1;
else if(card[i][12] && x > 2)
stop[i] = 1;
}
if(pt >= 16 && stop[0] && stop[1] && stop[2] && stop[3])
puts("BID NO-TRUMP");
else if(pt+pt2 < 14)
puts("PASS");
else {
if(S >= H && S >= D && S >= C)
puts("BID S");
else if(H >= D && H >= C)
puts("BID H");
else if(D >= C)
puts("BID D");
else
puts("BID C");
}

}
return 0;
}