Advertisement
Fleshian

Jokers

May 22nd, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. import java.util.HashSet;
  2. import java.util.Set;
  3.  
  4.  
  5. public class _03FullHouse {
  6.  
  7.     public static void main(String[] args) {
  8.        
  9.         char[] suits = {'\u2660','\u2665','\u2666','\u2663'};
  10.         char[] cards = {'2','3','4','5','6','7','8','9','T','J','Q','K','A',};
  11.         String[] allCards= new String[52];
  12.        
  13.         //add all cards in array
  14.         int indexCounter = 0;
  15.         for (int i = 0; i < cards.length; i++) {
  16.             for (int j = 0; j < suits.length; j++) {
  17.                 allCards[indexCounter] = Character.toString(cards[i]) + Character.toString(suits[j]);
  18.                 indexCounter++;
  19.             }
  20.         }
  21.        
  22.         int fullHouseCounter = 0;
  23.         for (int i1 = 0; i1 < 48; i1++)
  24.             for (int i2 = i1 + 1; i2 < 49; i2++)
  25.                 for (int i3 = i2 + 1; i3 < 50; i3++)
  26.                     for (int i4 = i3 + 1; i4 < 51; i4++)
  27.                         for (int i5 = i4 + 1; i5 < 52; i5++) {
  28.                             char[] handsCardsArr = {
  29.                                     allCards[i1].charAt(0),
  30.                                     allCards[i2].charAt(0),                                
  31.                                     allCards[i3].charAt(0),                                
  32.                                     allCards[i4].charAt(0),                                
  33.                                     allCards[i5].charAt(0)                                                                     
  34.                             };
  35.                            
  36.                             //  the HashSet counts distinct values
  37.                             //  the logic is that all full houses have only 2 different card values
  38.                             Set<Character> fullHouseSet  = new HashSet<>();
  39.                             for (char c : handsCardsArr) {
  40.                                 fullHouseSet.add(c);
  41.                             }
  42.                            
  43.                             if (fullHouseSet.size() == 2) {
  44.                                
  45.                                 // four of a kind hands have 2 distinct cards as well so
  46.                                 // we check if the hand is four of a kind
  47.                                 char firstChar = handsCardsArr[0];
  48.                                 char lastChar = handsCardsArr[4];
  49.                                 int diffCounter1 = 0;
  50.                                 int diffCounter2 = 0;
  51.                                
  52.                                 for (int i = 0; i < handsCardsArr.length; i++) {
  53.                                      if(handsCardsArr[i] == firstChar) {
  54.                                          diffCounter1++;
  55.                                      }
  56.                                      else if (handsCardsArr[i] == lastChar) {
  57.                                          diffCounter2++;
  58.                                      }
  59.                                 }
  60.                                 boolean notFourOfAkind = diffCounter1 != 4 && diffCounter2 !=4;
  61.                                
  62.                                 if (notFourOfAkind) {
  63.                                     System.out.println(allCards[i1] + allCards[i2] +
  64.                                             allCards[i3] + allCards[i4] + allCards[i5]);
  65.                                     fullHouseCounter++;
  66.                                 }
  67.                            
  68.                             }
  69.                         }
  70.         System.out.println(fullHouseCounter);
  71.     }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement