Advertisement
enevlogiev

Straight Flush

Feb 9th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.HashMap;
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class Problem04_StraightFlush {
  8.     @SuppressWarnings("resource")
  9.     public static void main(String[] args) {
  10.         Scanner scn = new Scanner(System.in);
  11.         String[] input = scn.nextLine().split("\\W+");
  12.        
  13.         HashMap<String, ArrayList<String>> cards = new HashMap<>();
  14.         for (int i = 0; i < input.length; i++) {
  15.             String face = input[i].substring(0, input[i].length() - 1);
  16.             String suit = input[i].substring(input[i].length() - 1);
  17.             if (!cards.containsKey(suit)) {
  18.                 cards.put(suit, new ArrayList<String>());
  19.                 cards.get(suit).add(face);
  20.             }
  21.             else {
  22.                 cards.get(suit).add(face);
  23.             }
  24.         }
  25.         boolean found = false;
  26.         for (String suit : cards.keySet()) {
  27.             if (cards.get(suit).size() > 4) {
  28.                 for (String card : cards.get(suit)) {
  29.                     if (!card.equals("J") && !card.equals("Q") && !card.equals("K") && !card.equals("A")) {
  30.                         ArrayList<String> hand = getHand(card);
  31.                         if (cards.get(suit).containsAll(hand)) {
  32.                             System.out.print("[");
  33.                             for (int i = 0; i < hand.size(); i++) {
  34.                                 if (i == 4) {
  35.                                     System.out.print(hand.get(i) + suit + "]");
  36.                                 }
  37.                                 else {
  38.                                     System.out.print(hand.get(i) + suit + ", ");
  39.                                 }
  40.                             }
  41.                             System.out.println();
  42.                             found = true;
  43.                         }
  44.                     }                  
  45.                 }
  46.             }
  47.         }
  48.         if (!found) {
  49.             System.out.println("No Straight Flushes");
  50.         }
  51.     }
  52.    
  53.     public static ArrayList<String> getHand(String card) {
  54.         ArrayList<String> cards = new ArrayList<String>
  55.         (Arrays.asList("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"));
  56.         ArrayList<String> returnList = new ArrayList<String>();
  57.         int index = Integer.parseInt(card) - 2;
  58.         returnList = new ArrayList<>(cards.subList(index, index + 5));
  59.         return returnList;     
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement