Advertisement
Guest User

Full House With Jokers

a guest
May 17th, 2014
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1.  
  2. public class _04_FullHouseWithJokers {
  3.  
  4.     public static void main(String[] args) {
  5.         String[] faces = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
  6.         char[] suits = { '♣', '♦', '♠', '♥' };
  7.         char joker = '*';
  8.        
  9.         // declare these variables here to avoid multiple declarations later
  10.         int currentFoundIndex = 0;
  11.         String numberAsBinary = "";
  12.        
  13.         String[] cards = new String[5]; // contains current 5 cards
  14.         String[] tempCards = new String[5]; // use after overwriting with jokers
  15.        
  16.         int counter = 0;
  17.         for (int i = 0; i < faces.length; i++) { // first three cards
  18.             for (int q = 0; q < faces.length; q++) { // last two cards
  19.                 if (q == i) continue; // avoid repetition
  20.                 for (int suit = 0; suit < suits.length; suit++) {
  21.                     // suits for first three cards
  22.                     for (int suitTriple = 0; suitTriple < 3; suitTriple++) {
  23.                         cards[suitTriple] = faces[i] + suits[(suit+suitTriple)%suits.length];
  24.                     }
  25.                     // suits for last two cards
  26.                     for (int fourthSuit = 0; fourthSuit < suits.length; fourthSuit++) {
  27.                         for (int fifthSuit = fourthSuit + 1; fifthSuit < suits.length; fifthSuit++) {
  28.                             cards[3] = faces[q] + suits[fourthSuit];
  29.                             cards[4] = faces[q] + suits[fifthSuit];
  30.                             // find all possible placements for joker
  31.                             for (int j = 0; j < 32; j++) {
  32.                                 tempCards = cards.clone(); // save current hand
  33.                                 numberAsBinary = Integer.toBinaryString(j);
  34.                                 currentFoundIndex = numberAsBinary.indexOf('1');
  35.                                 while(currentFoundIndex != -1) {
  36.                                     cards[numberAsBinary.length() - 1 - currentFoundIndex] = Character.toString(joker);
  37.                                     currentFoundIndex = numberAsBinary.indexOf('1', currentFoundIndex + 1);
  38.                                 }
  39.                                
  40.                                 counter++;
  41.                                 printCards(cards);
  42.                                 cards = tempCards; // load initial hand
  43.                             }
  44.                         }
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.         System.out.println(counter + " full houses");
  50.     }
  51.    
  52.     public static void printCards(String[] cards) {
  53.         System.out.print('(');
  54.         for (int i = 0; i < cards.length; i++) {
  55.             if (i != 0) System.out.print(' '); // better formatting
  56.             System.out.print(cards[i]);
  57.         }
  58.         System.out.println(')');
  59.     }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement