Advertisement
makispaiktis

CardsGame - CardsPack.java

Mar 16th, 2020 (edited)
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3.  
  4. public class CardsPack {
  5.  
  6.     // Variables
  7.     ArrayList <Card> cards;
  8.    
  9.     // Constructor
  10.     CardsPack(){
  11.         cards = new ArrayList <Card> ();
  12.         // 1. Add  the "heart" cards
  13.         for(int i=0; i<10; i++){
  14.             Card card = new Card(Integer.toString(i+1), "heart");
  15.             cards.add(card);
  16.         }
  17.         cards.add(new Card("J", "heart"));
  18.         cards.add(new Card("Q", "heart"));
  19.         cards.add(new Card("K", "heart"));
  20.         // 2. Add  the club" cards
  21.         for(int i=0; i<10; i++){
  22.             Card card = new Card(Integer.toString(i+1), "club");
  23.             cards.add(card);
  24.         }
  25.         cards.add(new Card("J", "club"));
  26.         cards.add(new Card("Q", "club"));
  27.         cards.add(new Card("K", "club"));
  28.         // 3. Add  the "diamond" cards
  29.         for(int i=0; i<10; i++){
  30.             Card card = new Card(Integer.toString(i+1), "diamond");
  31.             cards.add(card);
  32.         }
  33.         cards.add(new Card("J", "diamond"));
  34.         cards.add(new Card("Q", "diamond"));
  35.         cards.add(new Card("K", "diamond"));
  36.         // 4. Add  the "spade" cards
  37.         for(int i=0; i<10; i++){
  38.             Card card = new Card(Integer.toString(i+1), "spade");
  39.             cards.add(card);
  40.         }
  41.         cards.add(new Card("J", "spade"));
  42.         cards.add(new Card("Q", "spade"));
  43.         cards.add(new Card("K", "spade"));
  44.        
  45.     }
  46.    
  47.     // Methods
  48.     public void showStatus(){
  49.         for(int i=0; i<cards.size(); i++){
  50.             System.out.println("Card " + (i+1) + ": " + cards.get(i).value + " of " + cards.get(i).symbol + "s");
  51.         }
  52.     }
  53.    
  54.     public void display(){
  55.         for(int i=0; i<cards.size(); i++){
  56.             cards.get(i).display();
  57.             System.out.print(" ");
  58.         }
  59.     }
  60.    
  61.     public void shuffle(){
  62.         Collections.shuffle(cards);
  63.         Collections.shuffle(cards);
  64.     }
  65.    
  66.    
  67.     // ********************************************************************************************************
  68.     // ********************************************************************************************************
  69.     // USEFUL METHODS WHILE PLAYING
  70.     public Card returnFirstCard(){
  71.         return cards.get(0);
  72.     }
  73.    
  74.     public void removeFirstCard(){
  75.         cards.remove(cards.get(0));
  76.     }
  77.    
  78.     public ArrayList <Card> returnFirstNCards(int N){
  79.         ArrayList <Card> returnedList = new ArrayList <Card> ();
  80.         for(int i=0; i<N; i++){
  81.             returnedList.add(cards.get(i));
  82.         }
  83.         return returnedList;
  84.     }
  85.    
  86.     public void removeFirstNCards(int N){
  87.         for(int i=0; i<N; i++){
  88.             cards.remove(cards.get(i));
  89.         }
  90.     }
  91.    
  92.    
  93.     // ********************************************************************************************************
  94.     // ********************************************************************************************************
  95.    
  96.    
  97.     // MAIN FUNCTION
  98.     public static void main(String[] args) {
  99.         System.out.println("******** Not a shuffled cards pack ********");
  100.         CardsPack trapoula = new CardsPack();
  101.         trapoula.showStatus();
  102.         trapoula.display();
  103.         System.out.println();
  104.         System.out.println();
  105.         System.out.println();
  106.         System.out.println();
  107.         System.out.println("******** A shuffled cards pack ********");
  108.         trapoula.shuffle();
  109.         trapoula.showStatus();
  110.     }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement