UVA 462 – Bridge Hand Evaluator

Just an implementation problem. Pretty lengthy though.

import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 * 
 * @author Sanchit M. Bhatnagar
 * @see http://uhunt.felix-halim.net/id/74004
 * 
 */
public class P462 {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);

    while (sc.hasNextLine()) {
      String line = sc.nextLine();
      if (line == null)
        break;
      if (line.equals(""))
        continue;

      StringTokenizer st = new StringTokenizer(line);

      int sum = 0;
      Card[] hand = new Card[13];
      // Step 1
      for (int i = 0; i < 13; i++) {
        hand[i] = new Card(st.nextToken());
        sum += gameCardValue(hand[i]);
      }

      // Step 2
      for (int i = 0; i < 13; i++) {
        if (hand[i].isFaceCard() && hand[i].card.charAt(0) == 'K') {
          char suite = hand[i].card.charAt(1);
          boolean found = false;
          for (int j = 0; j < 13; j++) {
            if (j == i)
              continue;
            if (hand[j].card.charAt(1) == suite) {
              found = true;
              break;
            }
          }
          if (!found)
            sum--;
        }
      }

      // Step 3
      for (int i = 0; i < 13; i++) {
        if (hand[i].isFaceCard() && hand[i].card.charAt(0) == 'Q') {
          char suite = hand[i].card.charAt(1);
          int count = 0;
          for (int j = 0; j < 13; j++) {
            if (j == i)
              continue;
            if (hand[j].card.charAt(1) == suite) {
              count++;
            }
          }
          if (count < 2)
            sum--;
        }
      }

      // Step 4
      for (int i = 0; i < 13; i++) {
        if (hand[i].isFaceCard() && hand[i].card.charAt(0) == 'J') {
          char suite = hand[i].card.charAt(1);
          int count = 0;
          for (int j = 0; j < 13; j++) {
            if (j == i)
              continue;
            if (hand[j].card.charAt(1) == suite) {
              count++;
            }
          }
          if (count < 3)
            sum--;
        }
      }

      // Step 5, 6, 7
      int sum567 = 0;
      int[] suiteCount = new int[4];
      for (int i = 0; i < 13; i++) {
        char suite = hand[i].card.charAt(1);
        switch (suite) {
          case 'S':
            suiteCount[0]++;
            break;
          case 'H':
            suiteCount[1]++;
            break;
          case 'D':
            suiteCount[2]++;
            break;
          case 'C':
            suiteCount[3]++;
            break;
        }
      }
      for (int i = 0; i < 4; i++) {
        if (suiteCount[i] == 2) {
          sum567++;
        } else if (suiteCount[i] == 1) {
          sum567 += 2;
        } else if (suiteCount[i] == 0) {
          sum567 += 2;
        }
      }

      // IsStopped
      boolean[] isStopped = new boolean[4];
      boolean[][] specialCards = new boolean[4][3];
      for (int i = 0; i < 13; i++) {
        char suite = hand[i].card.charAt(1);
        switch (suite) {
          case 'S':
            if (hand[i].card.charAt(0) == 'A') {
              specialCards[0][0] = true;
              isStopped[0] = true;
            } else if (hand[i].card.charAt(0) == 'K') {
              specialCards[0][1] = true;
              if (suiteCount[0] > 1)
                isStopped[0] = true;
            } else if (hand[i].card.charAt(0) == 'Q') {
              if (suiteCount[0] > 2)
                isStopped[0] = true;
              specialCards[0][2] = true;
            }
            break;
          case 'H':
            if (hand[i].card.charAt(0) == 'A') {
              specialCards[1][0] = true;
              isStopped[1] = true;
            } else if (hand[i].card.charAt(0) == 'K') {
              specialCards[1][1] = true;
              if (suiteCount[1] > 1)
                isStopped[1] = true;
            } else if (hand[i].card.charAt(0) == 'Q') {
              if (suiteCount[1] > 2)
                isStopped[1] = true;
              specialCards[1][2] = true;
            }
            break;
          case 'D':
            if (hand[i].card.charAt(0) == 'A') {
              specialCards[2][0] = true;
              isStopped[2] = true;
            } else if (hand[i].card.charAt(0) == 'K') {
              specialCards[2][1] = true;
              if (suiteCount[2] > 1)
                isStopped[2] = true;
            } else if (hand[i].card.charAt(0) == 'Q') {
              specialCards[2][2] = true;
              if (suiteCount[2] > 2)
                isStopped[2] = true;
            }
            break;
          case 'C':
            if (hand[i].card.charAt(0) == 'A') {
              specialCards[3][0] = true;
              isStopped[3] = true;
            } else if (hand[i].card.charAt(0) == 'K') {
              specialCards[3][1] = true;
              if (suiteCount[3] > 1)
                isStopped[3] = true;
            } else if (hand[i].card.charAt(0) == 'Q') {
              specialCards[3][2] = true;
              if (suiteCount[3] > 2)
                isStopped[3] = true;
            }
            break;
        }
      }

      if (sum + sum567 < 14) {
        out.println("PASS");
      } else if (sum + sum567 > 13) {
        if (sum > 15 && isStopped[0] && isStopped[1] && isStopped[2] && isStopped[3]) {
          out.println("BID NO-TRUMP");
        } else {
          int max = -1;
          int maxIndex = -1;
          for (int i = 0; i < 4; i++) {
            if (suiteCount[i] > max) {
              max = suiteCount[i];
              maxIndex = i;
            }
          }
          out.print("BID ");
          switch (maxIndex) {
            case 0:
              out.println("S");
              break;
            case 1:
              out.println("H");
              break;
            case 2:
              out.println("D");
              break;
            case 3:
              out.println("C");
              break;
          }
        }
      }

    }

    out.close();
    sc.close();
  }

  private static int gameCardValue(Card c) {
    if (c.getCardValue() == 1)
      return 4;
    else if (c.getCardValue() == 13)
      return 3;
    else if (c.getCardValue() == 12)
      return 2;
    else if (c.getCardValue() == 11)
      return 1;
    else
      return 0;
  }

  private static class Card {
    String card;

    public Card(String card) {
      int test1 = getSuitIdx(card.charAt(1));
      if (test1 == -1)
        return;
      int test2 = getNumIdx(card.charAt(0));
      if (test2 == -1)
        return;
      this.card = card;
    }

    public boolean isFaceCard() {
      return getSuitIdx(card.charAt(1)) != -1;
    }

    public int getCardValue() {
      return getNumIdx(card.charAt(0));
    }

    private int getSuitIdx(char suit) {
      switch (suit) {
        case 'H':
          return 0;
        case 'S':
          return 1;
        case 'D':
          return 2;
        case 'C':
          return 3;
      }
      return -1;
    }

    private int getNumIdx(char num) {
      switch (num) {
        case 'A':
          return 1;
        case 'T':
          return 10;
        case 'J':
          return 11;
        case 'Q':
          return 12;
        case 'K':
          return 13;
        default:
          try {
            int val = Integer.parseInt(num + "");
            if (val > 1 && val < 10)
              return val;
            return -1;
          } catch (Exception e) {
            return -1;
          }
      }
    }
  }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.