Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class RandomPlayingCardGenerator
- {
- private static java.util.Scanner reader = new java.util.Scanner(System.in);
- private static java.util.Random rand = new java.util.Random();
- private static CardPile givenPile = new CardPile();
- public static void main(String[] args) throws InterruptedException
- {
- while (givenPile.pile.size() < 52)
- {
- int value = rand.nextInt(13) + 1, suit = rand.nextInt(4) + 1;
- Card c = new Card(value, suit);
- checkIfExists(c);
- }
- System.out.println("How many random playing cards do you wish to generate?");
- int numberOfCards = reader.nextInt();
- while (numberOfCards > 52)
- {
- System.out.println("You may only generate 52 cards or less. Please retry. How many random playing cards do you wish to generate?");
- numberOfCards = reader.nextInt();
- }
- for (int i = 0; i < numberOfCards; i++) givenPile.pile.get(i).print();
- }
- public static void checkIfExists(Card c)
- {
- int counter = 0;
- if (givenPile.pile.size() == 0) givenPile.pile.add(c);
- else
- {
- for (int i = 0; i < givenPile.pile.size(); i++) if (!(givenPile.pile.get(i).value == c.value && givenPile.pile.get(i).suit.equals(c.suit))) counter++;
- if (counter == givenPile.pile.size()) givenPile.pile.add(c);
- }
- }
- static class Card
- {
- int value;
- String suit;
- public Card(int v, int s)
- {
- this.value = v;
- switch(s)
- {
- case 1 : this.suit = "Clubs";
- break;
- case 2 : this.suit = "Hearts";
- break;
- case 3 : this.suit = "Spades";
- break;
- case 4 : this.suit = "Diamonds";
- break;
- }
- }
- public void print()
- {
- String val = "";
- if (value >= 2 && value <= 10) val += ("" + value + "");
- else
- {
- switch(value)
- {
- case 1 : val = "Ace";
- break;
- case 11 : val = "Jack";
- break;
- case 12 : val = "Queen";
- break;
- case 13 : val = "King";
- break;
- }
- }
- System.out.println(val + " of " + suit);
- }
- }
- static class CardPile
- {
- ArrayList<Card> pile = new ArrayList<Card>();
- public void print(CardPile p)
- {
- for (int i = 0; i < p.pile.size(); i++) p.pile.get(i).print();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement