Advertisement
Booster

All Full House variations

Jan 23rd, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. public class FullHouse {
  2.    
  3.     /**
  4.      *
  5.      * @param card1 - first card
  6.      * @param card2 - second card
  7.      * @return - if card's last index (suit) is equal or not
  8.      */
  9.     static boolean checkSuitIfEqual(String card1, String card2) {
  10.         if (card1.charAt(card1.length() - 1)
  11.                 == card2.charAt(card2.length() - 1)) {
  12.             return true;
  13.         }
  14.         return false;
  15.     }
  16.     /**
  17.      *
  18.      * @param card1 - first card
  19.      * @param card2 - second card
  20.      * @return - if card's first index (face) is equal or not
  21.      */
  22.     static boolean checkFaceIfEqual(String card1, String card2) {
  23.         if (card1.charAt(0) == card2.charAt(0)) {
  24.             return true;
  25.         }
  26.         return false;
  27.     }
  28.    
  29.  
  30.     public static void main(String[] args) {
  31.  
  32.         String[] faces = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
  33.                 "Q", "K", "A" };
  34.         char[] suits = { '\u2663', '\u2666', '\u2660', '\u2665' };
  35.         int counter = 0;
  36.    
  37.         // First create array with 52cards
  38.  
  39.         String[] cards = new String[52];
  40.         byte index = 0;
  41.         for (int i = 0; i < faces.length; i++) {
  42.             for (int j = 0; j < suits.length; j++) {
  43.                 cards[index] = faces[i] + suits[j];
  44.                 index++;
  45.             }
  46.         }
  47.         //First 3 cards from the same face
  48.         for (int i = 0; i < cards.length; i++) {
  49.             for (int j = i + 1; j < cards.length; j++) {
  50.                 //Check if face is equal and suit different
  51.                 if (checkFaceIfEqual(cards[i], cards[j])
  52.                         && !checkSuitIfEqual(cards[i], cards[j])) {
  53.                     for (int k = j + 1; k < cards.length; k++) {
  54.                         if (checkFaceIfEqual(cards[j], cards[k])
  55.                                 && !checkSuitIfEqual(cards[i], cards[k])
  56.                                 && !checkSuitIfEqual(cards[j], cards[k])) {
  57.                             //As we have 3 cards from the same face and different suit
  58.                             //start to iterate the other pair of cards
  59.                             for (int l = 0; l < cards.length; l++) {
  60.                                 for (int m = l + 1; m < cards.length; m++) {
  61.                                     //Last 2 cards must be same face but different suits
  62.                                     //and different face from previous three cards
  63.                                     if (checkFaceIfEqual(cards[l], cards[m])
  64.                                             && !checkFaceIfEqual(cards[l], cards[k])
  65.                                             && !checkSuitIfEqual(cards[l], cards[m])) {
  66.                                         System.out.printf(
  67.                                                 "(%1$s %2$s %3$s %4$s %5$s)\n",
  68.                                                 cards[i], cards[j], cards[k],
  69.                                                 cards[l], cards[m]);
  70.                                         counter++;
  71.                                     }
  72.                                 }
  73.  
  74.                             }
  75.  
  76.                         }
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.         System.out.println(counter + " full houses");
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement