RandomPokerHands.java


Below is the syntax highlighted version of RandomPokerHands.java.


/******************************************************************************
  * name: Donna Gabai
  * netID: dgabai
  * precept: P99
  * 
  * Description: 
  * Read integer N from command-line.
  * print N five-card poker hands (one per line).
  * 
  * Dependencies: none
  * Citation: initial code from introcs Deck.java
  * 
  */

public class RandomPokerHands {
    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);  // Number of Hands to deal
        int GAME = 5;        // game is five card stud so deal 5 cards
        
        String[] suit = { "C", "D", "H", "S" };
        String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "T", "J",
            "Q", "K", "A" };
        
        // avoid hardwired constants
        int SUITS = suit.length;
        int RANKS = rank.length;
        int DECK_SIZE = SUITS * RANKS;
        
        // initialize deck
        String[] deck = new String[DECK_SIZE];
        for (int i = 0; i < RANKS; i++) {
            for (int j = 0; j < SUITS; j++) {
                deck[SUITS*i + j] = rank[i] + suit[j];
            }
        }
        
        // shuffle deck, deal a hand
        // repeat N = Number of Hands times
        for (int hands = 0; hands < N; hands++) {
            
            // shuffle
            for (int i = 0; i < DECK_SIZE; i++) {
                int r = i + (int) (Math.random() * (DECK_SIZE-i));
                String t = deck[r];
                deck[r] = deck[i];
                deck[i] = t;
            }
            
            // print one five-card hand
            for (int i = 0; i < GAME; i++) {
                System.out.print(deck[i] + " ");
            }
            System.out.println();
        }
    }
}