Advertisement
Guest User

Untitled

a guest
May 29th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. import java.util.ArrayList;
  2. public class RandomPlayingCardGenerator
  3. {
  4. private static java.util.Scanner reader = new java.util.Scanner(System.in);
  5. private static java.util.Random rand = new java.util.Random();
  6. private static CardPile givenPile = new CardPile();
  7. public static void main(String[] args) throws InterruptedException
  8. {
  9. while (givenPile.pile.size() < 52)
  10. {
  11. int value = rand.nextInt(13) + 1, suit = rand.nextInt(4) + 1;
  12. Card c = new Card(value, suit);
  13. checkIfExists(c);
  14. }
  15. System.out.println("How many random playing cards do you wish to generate?");
  16. int numberOfCards = reader.nextInt();
  17. while (numberOfCards > 52)
  18. {
  19. System.out.println("You may only generate 52 cards or less. Please retry. How many random playing cards do you wish to generate?");
  20. numberOfCards = reader.nextInt();
  21. }
  22. for (int i = 0; i < numberOfCards; i++) givenPile.pile.get(i).print();
  23. }
  24. public static void checkIfExists(Card c)
  25. {
  26. int counter = 0;
  27. if (givenPile.pile.size() == 0) givenPile.pile.add(c);
  28. else
  29. {
  30. 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++;
  31. if (counter == givenPile.pile.size()) givenPile.pile.add(c);
  32. }
  33. }
  34. static class Card
  35. {
  36. int value;
  37. String suit;
  38.  
  39. public Card(int v, int s)
  40. {
  41. this.value = v;
  42. switch(s)
  43. {
  44. case 1 : this.suit = "Clubs";
  45. break;
  46. case 2 : this.suit = "Hearts";
  47. break;
  48. case 3 : this.suit = "Spades";
  49. break;
  50. case 4 : this.suit = "Diamonds";
  51. break;
  52. }
  53. }
  54. public void print()
  55. {
  56. String val = "";
  57. if (value >= 2 && value <= 10) val += ("" + value + "");
  58. else
  59. {
  60. switch(value)
  61. {
  62. case 1 : val = "Ace";
  63. break;
  64. case 11 : val = "Jack";
  65. break;
  66. case 12 : val = "Queen";
  67. break;
  68. case 13 : val = "King";
  69. break;
  70. }
  71. }
  72. System.out.println(val + " of " + suit);
  73. }
  74. }
  75. static class CardPile
  76. {
  77. ArrayList<Card> pile = new ArrayList<Card>();
  78. public void print(CardPile p)
  79. {
  80. for (int i = 0; i < p.pile.size(); i++) p.pile.get(i).print();
  81. }
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement