Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- public class PokerHands {
- static ArrayList<String> arl = new ArrayList<String>();
- static final int CARDS_PER_HAND = 5;
- public ArrayList<String> createDeck() {
- String[] suits = { "Spades", "Diamonds", "Clubs", "Hearts" };
- String[] values = { "Ace", "2", "3", "4", "5", "6", "7", "8",
- "9", "10", "Jack", "Queen", "King" };
- for (String s: suits) {
- for (String v: values) {
- // add card to ArrayList
- arl.add(v + " of " + s);
- }
- }
- return arl;
- } //createDeck
- public String dealCard() {
- // remove top card
- String s = arl.get(0);
- arl.remove(s);
- return s;
- } //dealCard
- public void showUsage() {
- System.out.println("Usage: java PokerHands <n> "
- + "(where n is number of hands of poker)");
- } //showUsage
- public void dealPokerHands(int n) {
- if (n<1) {
- System.out.println("No poker hands were dealt.");
- } else {
- System.out.println("*** Dealing poker hands as follows: ***");
- for (int hands = 0; hands<n; hands++) {
- for (int i=0; i<CARDS_PER_HAND; i++) {
- // deal a card and display it
- System.out.print (dealCard());
- // separate each card with a comma
- if (i<CARDS_PER_HAND-1) System.out.print(", ");
- }
- if (hands<n-1) System.out.println("\n"); // blank line between hands
- else System.out.println();
- }
- }
- } //dealPokerHands
- public int getNumberOfHands(String s) {
- int n = 0;
- // ensure command line argument is an int
- try {
- n = Integer.parseInt(s);
- } catch (NumberFormatException e) {
- System.out.printf("Error! %s is not a whole number!\n", s);
- System.exit(0);
- }
- // can't have more than 10 hands of five cards each
- if (n>52/CARDS_PER_HAND) {
- System.out.println("Number too big! Not enough cards to go round.");
- System.exit(0);
- }
- return n;
- } //getNumberOfHands
- public static void main(String[] args) {
- // create instance of class
- PokerHands cards = new PokerHands();
- int n = 0;
- if (args.length==0) {
- cards.showUsage();
- System.exit(0);
- } else n = cards.getNumberOfHands(args[0]);
- arl = cards.createDeck();
- Collections.shuffle(arl);
- cards.dealPokerHands(n);
- } //main
- } //class PokerHands
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement