Advertisement
brilliant_moves

PokerHands.java

Sep 26th, 2017
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.22 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3.  
  4. public class PokerHands {
  5.  
  6.     static ArrayList<String> arl = new ArrayList<String>();
  7.     static final int CARDS_PER_HAND = 5;
  8.  
  9.     public ArrayList<String> createDeck() {
  10.         String[] suits = { "Spades", "Diamonds", "Clubs", "Hearts" };
  11.         String[] values = { "Ace", "2", "3", "4", "5", "6", "7", "8",
  12.                  "9", "10", "Jack", "Queen", "King" };
  13.         for (String s: suits) {
  14.             for (String v: values) {
  15.                 // add card to ArrayList
  16.                 arl.add(v + " of " + s);
  17.             }
  18.         }
  19.         return arl;
  20.     } //createDeck
  21.  
  22.     public String dealCard() {
  23.         // remove top card
  24.         String s = arl.get(0);
  25.         arl.remove(s);
  26.         return s;
  27.     } //dealCard
  28.  
  29.     public void showUsage() {
  30.         System.out.println("Usage: java PokerHands <n> "
  31.          + "(where n is number of hands of poker)");
  32.     } //showUsage
  33.  
  34.     public void dealPokerHands(int n) {
  35.         if (n<1) {
  36.             System.out.println("No poker hands were dealt.");
  37.         } else {
  38.             System.out.println("*** Dealing poker hands as follows: ***");
  39.             for (int hands = 0; hands<n; hands++) {
  40.                 for (int i=0; i<CARDS_PER_HAND; i++) {
  41.                     // deal a card and display it
  42.                     System.out.print (dealCard());
  43.                     // separate each card with a comma
  44.                     if (i<CARDS_PER_HAND-1) System.out.print(", ");
  45.                 }
  46.                 if (hands<n-1) System.out.println("\n"); // blank line between hands
  47.                 else System.out.println();
  48.             }
  49.         }
  50.     } //dealPokerHands
  51.  
  52.     public int getNumberOfHands(String s) {
  53.         int n = 0;
  54.         // ensure command line argument is an int
  55.         try {
  56.             n = Integer.parseInt(s);
  57.         } catch (NumberFormatException e) {
  58.             System.out.printf("Error! %s is not a whole number!\n", s);
  59.             System.exit(0);
  60.         }
  61.         // can't have more than 10 hands of five cards each
  62.         if (n>52/CARDS_PER_HAND) {
  63.             System.out.println("Number too big! Not enough cards to go round.");
  64.             System.exit(0);
  65.         }
  66.         return n;
  67.     } //getNumberOfHands
  68.  
  69.     public static void main(String[] args) {
  70.         // create instance of class
  71.         PokerHands cards = new PokerHands();
  72.         int n = 0;
  73.         if (args.length==0) {
  74.             cards.showUsage();
  75.             System.exit(0);
  76.         } else n = cards.getNumberOfHands(args[0]);
  77.  
  78.         arl = cards.createDeck();
  79.         Collections.shuffle(arl);
  80.         cards.dealPokerHands(n);
  81.     } //main
  82.  
  83. } //class PokerHands
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement