Advertisement
Fleshian

Full House Counter

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