Advertisement
gdog2u

DeckCreator

Jul 31st, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1.         public static ArrayList<Card> createDeck(){
  2.             ArrayList<Card> deck = new ArrayList<>();
  3.             int i = 0;  //Used to limit the number of the cards below 14 (i.e. Ace)
  4.             int j = 0;  //Used to keep the total number of cards below 52
  5.             int k = 0;  //Temporary variable used to increment J, and make sure there are only 4 of each card face
  6.             int l = 0;  //Used to assign the suit of the card
  7.             for(;i < 14;i++){
  8.                 for(;j<(k+4) && j < 52;j++){
  9.                     deck.add(new Card((l+1),(int)(Math.floor(j/4)+2))); //Adds a new Card to the array with the suit, then the number of the card.
  10.                     System.out.println((j+1) + ":" + deck.get(i).getName() + ":" + deck.get(i).getSuit()); //<1>
  11.                     l++;
  12.                 }
  13.                 l = 0;
  14.                 k = j;
  15.             }
  16.             return deck;
  17.         }
  18.     }
  19.    
  20.     public static void main(String[] args){
  21.         ArrayList<Card> deck = new ArrayList<>();
  22.         deck = Card.createDeck();
  23.         for(int i = 0; i < 52;i++){
  24.             System.out.println(deck.get(i).getName() + ":" + deck.get(i).getSuit()); //<2>
  25.         }
  26.     }
  27.  
  28. /**
  29. *   Sample output from <1>:
  30. *       1:2:Hearts
  31. *       2:2:Diamonds
  32. *       3:2:Clubs
  33. *       etc.
  34. **/
  35.  
  36. /**
  37. *   Sample output from <2>:
  38. *       Ace:Spades
  39. *       Ace:Spades
  40. *       Ace:Spades
  41. *       etc.
  42. **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement